Programming

Deploy Express Web App on AlmaLinux 8

In this article, we’ll explain how to deploy Express Web App on AlmaLinux 8 with Nginx and Pm2.

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

Let’s get started.

Deploy Express Web App on AlmaLinux 8

Prerequisites:

  • A AlmaLinux installed dedicated server or KVM VPS.
  • A root user access or normal user with administrative privileges.

1. Keep the server up to date

# dnf update -y

2. Install NodeJS

To see a list of available streams, use following command:

# dnf module list nodejs

We’re installing Node.JS 14 using following command:

# dnf module install nodejs:14

3. Install Pm2

# npm i -g pm2

4. Setup Project

Create a directory where we store all the project files.

# mkdir express-website
# cd express-website
# npm init -y

Install Express.js

# npm i express

Create your project’s main file using following command:

# vi index.js

Paste the following into your editor.

const express = require("express"); // Acquire the express package and assign it to a variable called "express"
const app = express(); // Calls the method "express()" and assigns it's output to "app". "express()" will create an express app for you.

app.get("/", (req, res) => { // Creates sort of a listener for when there are "GET" requests to the "/" (root) path. Takes in req (request) and res (response)
    res.send("Hello world!"); // For the response, send a string "Hello World!"
});

app.listen(3000, () => { // Tells the app to start on port 3000. This function below is run when
    console.log("Server listening on port 3000!"); // Say in the console "Server listening on port 3000!"
})

Save and exit the file.

Run your app using following command:

# node index.js

If it works, it reports “Server listening on port 3000!“. Type CTRL + C to exit.

5. Daemonize the App

Daemonize the app with pm2.

# pm2 start index.js

To verify it has daemonized, run following command:

# pm2 list.

6. Install and Configure Nginx

Install Nginx using following command:

# dnf install nginx -y

Create your server block in Nginx.

# vi /etc/nginx/conf.d/express.conf

Paste the following in your editor:


server {
    listen 80; # Listen on port 80
    listen [::]:80; # Listen on port 80 for ipv6

    server_name _;

    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}

Save and exit the file.

Test and restart Nginx

# nginx -t

# systemctl restart nginx

Add ports in firewalld

# firewall-cmd –permanent –add-service=http

# firewall-cmd –permanent –add-service=https

# firewall-cmd –permanent –add-port=3000/tcp

Reload the firewalld

# firewall-cmd –reload

7. Configure SELinux

# setsebool -P httpd_can_network_relay on

# setsebool -P httpd_can_network_connect on

Once all the steps done successfully, navigate to your browser and access ExpressJS using your server IP.

The installation and configuration process has been done successfully.

In this article, we have seen how to deploy Express Web App on AlmaLinux 8 with Nginx and Pm2.

Troubleshoot

If you get default Ngnix page even if you configure Nginx server blocks, open /etc/nginx/nginx.conf file and comment server{} block. Now restart Nginx service.

Related Articles