How to Deploy Node.js/Express.js Application on Ubuntu Server
Introduction:
Node.js is an open supply JavaScript runtime environment for building server-side and networking applications in a simple way. This platform runs on Linux, OS X, FreeBSD, and Windows.
We will focus on running the node.js application as a service rather than running it at the command line. In this way, it can automatically restart on reboot or failure, and can safely be used in a production environment.
When you have completed your node.js application or you are done with enough code to make it public then deployment of the Node.js application starts. So in this guide, we are going to deploy a basic node.js Application on Ubuntu 20.04.
Let’s Set it up with 5 Simple and Quick Steps.
- Connect with Server using SSH
- Installation of Node.js
- Pull/Fetch code or Create a new application
- Install PM2 & Manage Application with It
- Setup Nginx as Reverse proxy Server
Step 1: Connect with remote Server using SSH
Everything starts by connecting with a remote server. I use PuTTY to connect to the server using ssh.
use the given command
ssh <user>@<ip-address>
# e.g. ssh root@123.45.67.890
Once you have entered the above-mentioned command it will ask for a password. Enter the Password and we are ready to start the show.
Step 2: Installation of Node.js (if not already installed)
Make sure to have the latest version of Node.js, you can check it with the following command:
node — version
Make sure to have the node version as 12.0+
If it's not already installed then let’s install it with APT
, from the default repository.
Ubuntu 20.04, By default, contains a version of node.js in the default repository.
We only need to install it. By using APT
, for that, refresh your local package index
sudo apt update
Now install Node.js
sudo apt install nodejs
Now install npm (Node Package Manager)
with APT
command
sudo apt install npm
Node.js
and npm
is successful installed, you can now verify it by checking the version of node.js node -v
Step 2: Pull/Fetch code or Create a new application
Navigate to the home directory
Now, You can either
- Pull/Fetch/Checkout Code
Use the git commands to checkout/fetch the latest code from your repository and install the dependencies.
2. Upload the Code files directly to the Server
Using Filezilla or any other FTP solution, upload the code files directly to the server and install dependencies if required.
3. Create a new Node.js/Express.js Application
Create a new Directory (if required) and initialize a new Node.js/Express.js Project
mkdir nodeApplication
cd nodeApplication
npm init
Answer the questions and add Express
to the project
npm install express --save
Edit the file and add the code given below
nano app.js
#To Open/Edit File
#Add Folowing code
const express = require(‘express’)
const app = express()
const port = 3000app.get(‘/’, (req, res) => res.send(‘Hello World!’))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
Ensure that the port you have chosen is not already in use.
Exit the file after saving it.
For testing your application, makeapp.js
executable :
chmod +x ./app.js
Now Run the application.
node app.js
Test the Application by Opening a new Terminal and run the following command:
curl http://localhost::3000
If you get the below output, your application is working properly and is listing to the correct port.
Hello World
Step 3: Install PM2 & Manage Application with It
PM2 is a process manager for Node.js applications. It provides an easy way to manage and demonize applications (run them in the background as a service).
Run the command given below to install PM2.
sudo npm install -g pm2
The -g option tells npm to install the module globally in this way it is available system-wide.
Execute the app.js
command in the background by using the following command:
pm2 start app.js
Step 4: Setup Nginx as a reverse proxy server
As our application is working fine at localhost now we need to setup Nginx as a reverse proxy server so that it is accessible for the users.
Run the command to edit the following file:
sudo nano /etc/nginx/sites-available/default
Replace the blow content of existing block content with-in the server block. You would need to update the highlighted area to point to the correct port number if the application is configured to listen to a different port.
server {
…
location / {
proxy_pass http: //localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
…
}
Exit the file after saving it.
To verify there is no syntax error run the command given below:
sudo nginx -t
Restart Nginx.
sudo systemctl restart nginx
You should now be able to access your application via the Nginx reverse proxy by accessing your server’s URL.
Appreciating the efforts of Naiha Shahid.