Setup Node.js with Apache Proxy on Ubuntu 18.04 for production. This guide shows you how to install Node.js and configure it with Apache for production. You will also install and configure free Let’s Encrypt SSL and secure your installation.
This setup is tested on Google Cloud Platform which is running Ubuntu 18.04. This guide will also helps you set this up on AWS, DigitalOcean, or any other cloud or VPS or Dedicated Linux based servers.
Prerequisites
If you plan to use Google Cloud for example you can follow this or you just need SSH access to your server with sudo
privileges.
- A running Compute Engine, see the Setting up Compute Engine Instance with Ubuntu 18.04
- Initial Ubuntu Server Set up..
- Setup Cloud DNS, see the Setting up Google Cloud DNS for your domain.
Step 1: Install Node.js
You can install the latest LTS release of Node.js
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt install -y nodejs
Once the installation is complete you can check the node.js
version and npm
version using the following commands
node -v
npm -v
Some packages requires compiling from source so you need to install the build-essential
package.
sudo apt install build-essential
Step 2: Create a Node.js Application
Now you can create a demo Node.js app
cd ~/
sudo nano server.js
Insert the following code into the file
const http = require('http');
const hostname = 'localhost';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Welcome to Node.js!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Save the file and exit.
Step 3: Install Process Manager
sudo npm install pm2@latest -g
Now you can start your app using the process manager
pm2 start server.js
Now your Node.js application is running in the background
Step 4: Install Apache
sudo apt install apache2
Remove default Apache configuration.
sudo a2dissite 000-default
Step 5: Enable Apache modules
Now you need to enable Apache proxy module and rewrite modules.
sudo a2enmod proxy proxy_http rewrite headers expires
Step 6: Configure Apache Proxy
Once the above mentioned steps are done you can configure Apache to serve Node.js application.
Create a new virtual host configuration for Node.js.
sudo nano /etc/apache2/sites-available/domain.conf
Add the below configurations to the file.
<VirtualHost *:80>
ServerName domain.com
ServerAlias www.domain.com
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>
Hit CTRL + X
followed by Y
and Enter
to save and close the file.
Enable the newly created configuration.
sudo a2ensite domain.conf
Now you can restart Apache.
sudo service apache2 restart
Step 7: Test the setup
Now you can visit your domain name in browser, you should view the output of your server.js
(Welcome to Node.js!).
Step 8: Install SSL Certificate
sudo add-apt-repository ppa:certbot/certbot
sudo apt update
sudo apt install python-certbot-apache
Now you have installed Certbot for Apache which automates the installation of Let’sEncrypt certificate.
You just need to execute the following command to install your certificate.
sudo certbot --apache -m your-email -d domain.com -d www.domain.com
When prompted you can choose to redirect all HTTP requests to HTTPS.
Once done when you visit your site you will be redirected with HTTPS.
Prepare yourself for a role working as an Information Technology Professional with Linux operating system
Conclusion
Now you have learned to install Node.js, Apache and configure it to run on production.
Very helpful. Thank you!