Express is one of the best backend frameworks for building REST APIs. But how to deploy the created backend to a cloud server for making it live?
Yes, here we are discussing the steps to deploy an express backend in the cloud server.
We will connect a domain name to access our REST APIs (https://api.mynewblog33.com) and set up an SSL certificate to secure it.
I assume that you have already created an express backend to deploy and pushed it to GitHub or Bitbucket. Otherwise please go through my previous article that you can see below.
How To Build A Simple REST API With Node/Express
So, the express backend is ready and now, we need a cloud server to deploy this app.
I also assume that you already have a cloud server with you. Otherwise, please refer to the article below to create your own cloud server in Vultr.
How To Set Up Your Own Cloud Server On Vultr (VC2)
You can choose any OS in the cloud server, but I prefer Ubuntu. All the commands in this article work fine in Ubuntu.
So, after setting up a cloud server, you will get a Terminal.
We can use this terminal to configure our server for running an Express app.
1. Configure the cloud server
To run our Express app in an Ubuntu server, we must install some tools and configure well.
1.1 Install Nodejs and npm
Because Express is a Node.js framework, it requires Node.js and npm to be installed in our server.
The below commands install the latest version of Nodejs.
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - sudo apt-get install -y nodejs
1.2 Install pm2
While developing an Express backend, we used Nodemon, a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.
But it in our server, instead of Nodemon, we use pm2 that allows our application to run in the background.
Install pm2 with the below command.
npm install pm2 -g
Run one more that that helps to start pm2 running apps even after our server gets restarted.
pm2 startup ubuntu
1.3 Setup Firewall
We need to enable the firewall. The following command enables the firewall.
sudo ufw enable
Now, configure the firewall to allow HTTP, HTTPS, and SSH access.
sudo ufw allow http sudo ufw allow https sudo ufw allow ssh
Note: For the Lightsail server, you need to enable, SSH, HTTP, and HTTPS from the Networking tab in the Lightsail dashboard.
1.4 Setup NGINX
NGINX is a free, open-source, high-performance HTTP server and reverse proxy. So, Install NGINX to configure a reverse proxy for our application.
sudo apt-get install nginx
1.5 Create an NGINX configuration
Now, we need to edit the default configuration of NGINX and add ours. So, using nano editor, open the default configuration file.
sudo nano /etc/nginx/sites-available/default
You will get the configuration opened in the nano editor. Remove all the lines using Backspace and add the below configuration to it.
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; } }
Use the below keyboard shortcuts to save the file in the nano editor.
Nano editor tips:-
1. Ctrl + O (Write out)
2. Enter
3. Ctrl + x (exit)
Create MyBlog.conf file inside the sites-available directory which describes the configuration of the app we are going to deploy.
sudo nano /etc/nginx/sites-available/MyBlog.conf
Add the configuration details as below. You have to change the server_name with your domain. This configuration directs the api.mynewblog33.com to the pm2 app running in our server with the port 3001.
server { root /var/www/html; index index.html index.htm index.nginx-debian.html; server_name api.mynewblog33.com; location / { proxy_pass http://localhost:3001; 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; } }
Use the below keyboard shortcuts to save the file in nano editor.
Nano editor tips:-
1. Ctrl + O (Write out)
2. Enter
3. Ctrl + x (exit)
Enable the configurations we made.
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled sudo ln -s /etc/nginx/sites-available/MyBlog.conf /etc/nginx/sites-enabled/
1.6 Checking Your Configuration File
Whenever you make changes to your Nginx configuration file, it’s important to check whether you’ve left behind any syntax errors. This can be done by running the following command.
sudo nginx -t
If there are errors in your configuration file, the command’s output will tell you exactly where in the file the error was found.
Conversely, if there are no syntax errors in any of your NGINX config files, you will see output similar to the following.
Outputnginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
1.7 Restart NGINX server
If the test is successful, restart NGINX using the below command.
sudo service nginx restart
2. Clone and Start the Express project
We already created an Express backend and pushed it to a GitHub repository. Clone the repository to our server.
Right now, I am cloning the simple express backend that I have already created.
The below command will make a clone of this app in our server.
git clone https://github.com/techomoro/SimpleExpressBackend.git
Enter the project and install dependencies using the below commands.
cd SimpleExpressBackend npm i
Now start the express app using pm2.
pm2 start index.js
Note:- We can name the PM2 process to be run. For this, we need to define a script in package.json.
"scripts": { "start": "node index.js" },
Now, execute the below command.
pm2 start npm --name "Simple Express Backend" -- start
3. Buy a Domain and change DNS
So our Express backend is ready. Now we can connect a domain name to access the REST APIs.
We can buy a domain from any Domain registrar service. Some of them are listed below.
Here I am choosing Godaddy to purchase the domain.
3.1 Search for a domain
Log on to https://www.godaddy.com/ and search for a domain. If it’s available add it to cart.
3.2 Choose the bundle options
Godaddy will force you to buy domain privacy protection, email address, and hosting with the domain. Here I prefer skipping those bundle options.
3.3 Complete the purchase
Complete the purchase by entering the billing information and payment details. You can purchase the domain for a minimum of 1 year to 5 years. Here I am buying it for 1 year.
3.4 Change the DNS
Now from the top menu, select My Products. You can see the purchased domain there.
Click on the DNS to see the DNS records.
Add an A record with the name api and value Server IP address.
4. Setup SSL certificate with Let’s Encrypt
An SSL(Secure Sockets Layer) certificate, creates a secure link between your website and a visitor’s browser. So that the data sending from the user’s browser to our server get encrypted. This won’t allow intruders to read the data. All the browsers nowadays show a warning if the site doesn’t have an SSL certificate.
So we are going to set up an SSL certificate for our API URL. Let’sEncrypt provides an easy way to set up a free SSL certificate.
So follow the steps to set up an SSL certificate for the blog we are going to create.
4.1. Add the repository
Add the Certbot repository to our system first.
sudo add-apt-repository ppa:certbot/certbot
4.2. Update system packages
The below command updates the package lists for upgrades for packages that need upgrading and new packages that have just come to the repositories.
sudo apt-get update
4.3. Install Certbot
Now install Certbot from the new repository.
sudo apt-get install python3-certbot-nginx
Set up an SSL certificate with the below command.
sudo certbot --nginx -d api.mynewblog33.com
Note: Replace the domain name with yours.
4.4 Setup auto-renew for SSL certificate
Let’s Encrypt certificates only last for 90 days. But a single command can auto-renew the certificate.
sudo certbot renew --dry-run
4.5 Restart NGINX server
All setup will reflect in our server after restarting NGINX.
sudo service nginx restart
5. Access the REST API
Now we can access the APIs that we have created with the below URL.
https://api.mynewblog33.com
Let’s see an example.
Summary
So we have discussed the steps to successfully deploy an Express backend in the cloud server. We used a pre-built express to deploy in the Vultr VC2 server and connected a domain name.