Set up a Node.js app for a website with Apache on Ubuntu 16.04
Execution of Node.js scripts as a service
Although open source JavaScript Node.js scripts can be run from the command line using screen, running the scripts as a service using the process manager PM2 gives the scripts a more robust set of behaviors. When run as a service this way, the scripts will automatically restart if the server is rebooted or the script crashes.
PM2 is a process manager for Node.js, with a wide range of features you can use to control and manage your Node.js scripts. Visit the official PM2 website for more information on using PM2.
Requirements
- A Cloud Server running Linux (Ubuntu 16.04).
- A working domain name which links to the server.
- A functional Apache web server installed and running.
Thanks to free starting credit, you can test the IONOS cloud server for 1 month free of charge (or until the credit is used up) and experience the perfect combination of performance and security!
Install Node.js
Update your server’s packages and install curl with the following commands:
Download the Node.js personal package archive (PPA). This contains a more recent Node.js version than the Ubuntu repositories:
Run the nodesource_setup.sh command to add the PPA to your server’s package cache:
This script will update the server automatically. There is no need to run apt-get update a second time.
Afterwards, install Node.js:
This will automatically install npm as well.
Finally, install the build-essential package for npm:
Create a sample Node.js application
For this example, we will begin by creating a separate directory in your website’s document root for housing Node.js applications:
Create the file hello.js in this directory:
Add the following example content to the file:
Save and exit the file and make the file executable:
Install PM2
Use npm to install PM2 with the command:
Start the hello.js example script that you’ve just created with the command:
As root add PM2 to the startup scripts, so that it will automatically restart if the server is rebooted:
- Automatic backup & easy recovery
- Intuitive scheduling and management
- AI-based threat protection
Configure Apache
To access the Node.js script from the web, install the Apache modules proxy and proxy_http with the commands:
Once the installation is complete, restart Apache for the changes to take effect:
Next, you will need to adjust the Apache proxy configurations. The following directives need to be inserted into the VirtualHost command block in the site’s main Apache configuration file.
By common convention, this Apache configuration file is usually /etc/apache2/sites-available/example.com.conf on Ubuntu.
The location and filename of a site’s Apache configuration file can vary.
Edit this file with your editor of choice, for example with the command:
Scroll through the file until you find the VirtualHost command block, which will look like:
Add the following to VirtualHost command block:
Be sure to put these lines outside any Directory command blocks. For example:
Save and exit the file, then restart Apache for the changes to take effect:
After restarting Apache, you can test the application by viewing it in a browser. You should see the following message from the test file you created earlier: