Hello world version of Node.js
source code hosted on GitHub
When you run it, you should see something like this,
This Node.js application simply listens on the specified IP address and port, and returns “Hello World” with a 200 HTTP status code. This means that the application is only reachable from servers on the same private network, such as our web server. Let’s test the application through the private IP address. Open another a terminal session and connect to your web server. Because the web server is on the same private network, it should be able to reach the private IP address of the application using curl. Be sured to change the PRIVATE_IP_ADDRESS to your private IP address and PORT_THAT_Node_JS_APP_LISTEN if you changed to something else.
curl to your Node.js application via private IP address
This should return Hello World! Once this is good. Then, we may proceed and install PM2. Now, we will install PM2, which is a process manager for Node.js application that is running, and configured to listen on the proper IP address and port your specify earlier. And, we will be using npm to install our PM2.
installing PM2 using npm
Once this is installed, then we may use PM2 to start a Node.js as simple as this, also be sured to replaced YOUR_NODE_JS_SERVER_NAME_FILE with the name of the Node.js server file you named.
start Node.js application with PM2
Then, you should be able to see that PM2 is now running. You may use the following command to check if the Node.js application is running or not via PM2.
check if Node.js application is running or not with PM2
Now, you should be able to see your application is running in that output from the command above.
Normally, PM2 will restart automatically if the application crashes or is killed (thanks to PM2), but additonal step need to get the application running again if the web server boot or reboot itself. Hence, be sured to run the following command, so that your PM2 will restart your node.js application say one day you reboot your web server.
run the command so that one day when you reboot your web server, the PM2 will automatically restart your Node.js application
Once this is done, then we may proceed and set up a reverse proxy server so that we can access our Node.js application via the domain name that you have. In order for this to work, you need to have Apache2 installed in your web server and it can be achieved like this,
installing Apache2 on a web server via a command line
Then, you may use the following command to see if it is running,
checking if Apache2 is running
It should print something like apache2 is running. If it is not already running, use the following command to start the Apache2,
start the Apache2 server
Now, we need to go to Apache2 folder and insert the following configuration for the Apache2 server to work with PM2. Do the following command to get to Apache2 folder and modify the following file,
about to make changes to the config file for Apache2, so that we may access Node.js application via a domain name
add the following configuration in the 000-default.conf file
source code hosted on GitHub
Now, we need to restart the Apache2 server to take effect.
restart your Apache2 server
Assume that your Node.js application is running using PM2, and your application and Apache2 configuration are correct, you should now be able to access your application via the reverse proxy of the web server. Try it out by accessing your web server with the domain name that you have (the one you put in the Apache2 config file).
Hopefully this guide has given you the confidence to do configuration setting with running Node.js application using PM2, and access the application via a reverse proxy of the web server with Apache2. If you would like to learn more and do this in Nginx way, please go here. As our tutorial is greatly inspired from it. I am sure that with this guide, you will now be more comfortable with doing the server side configuration with your Node.js application and Apache2 server on Ubuntu 14.04. I hope that this post has helped you and thank you for reading!
I’ll try to keep this list current and up to date. If you know of a great resource you’d like to share or notice a broken link, please let us know.