DEV Community

Ahaiwe Emmanuel
Ahaiwe Emmanuel

Posted on • Updated on

Serving Multiple Applications with Nginx

Imagine this scenario, you just got your dream DevOps job. You are very excited. In no time, you get your first task to host a javascript, python and PHP application on a single Linux centos server. At first, you are confused about how you'll serve multiple applications from a single server. You do a little digging and then you strike gold. Nginx!

"NGINX is open-source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It started out as a web server designed for maximum performance and stability. In addition to its HTTP server capabilities, NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers."


  • Linux centos server


Log in to your server using ssh

ssh root@server_ip_address

You'll be asked to input your password.

Installing Nginx on Centos
Nginx packages can be gotten from EPEL repositories:

sudo yum install epel-release

Install Nginx:

sudo yum install nginx

Output should look like:

Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
Userid     : "Fedora EPEL (7) <>"
Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
Package    : epel-release-7-9.noarch (@extras)
From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Is this ok [y/N]:

Press y and enter

Upon completion, start Nginx and check it's status using:

sudo systemctl enable nginx
sudo systemctl start nginx
sudo systemctl status nginx

If all goes well, you should see this:

● nginx.service - The nginx HTTP and reverse proxy server
  Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
  Active: active (running) since Mon 2018-03-12 16:12:48 UTC; 2s ago
  Process: 1677 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 1675 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 1673 ExecStartPre=/usr/bin/rm -f /run/ (code=exited, status=0/SUCCESS)
Main PID: 1680 (nginx)
  CGroup: /system.slice/nginx.service
          ├─1680 nginx: master process /usr/sbin/nginx
          └─1681 nginx: worker process

Open HTTP(80) and HTTPS(433) if you are behind a firewall with these commands:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

Open http://YOUR_IP in your browser of choice, you should see an Nginx homepage displayed. Congratulation!!!

Step 2

Create a New User Account with sudo Privileges
For security reasons, it is advisable not to use the root user to run commands but to create a new user with sudo privileges:

useradd username
  • Replace username with your username with Sudo privileges.

Set the User Password

passwd username

You should get the output:

Changing password for user username.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

Add User to the sudo Group

usermod -aG wheel username

Switch to Newly Created User

su - username

Then list \root directory content:

sudo ls -l /root

You'll see a banner message the first time you sudo from this account:

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for username:

Serve Multiple Applications

Log in to the Server Using Your username with Root Privileges

Our directory structure would look like this:

│   └── public_html
│__ └── public_html

Let's create a directory where your javascript and python application would reside:

sudo mkdir -p /var/www/
sudo mkdir -p /var/www/

To avoid any permission issues, let us change ownership to our username with sudo privileges:

sudo chown -R username: /var/www/
sudo chown -R username: /var/www/

sudo vi /var/www/

Copy and paste the following lines:

\<!DOCTYPE html>
\<html lang="en" dir="ltr">
    \<meta charset="utf-8">
    \<title>Welcome to</title>
    \<h1>Success! home page!</h1>

Create a Server Block
Server blocks are stored in the /etc/nginx/conf.d directory and must end with .conf

sudo vi /etc/nginx/conf.d/

Copy and paste the following lines into the file, remember to replace the server name with your value:

server {
    listen 80;
    listen [::]:80;
    root /var/www/;
    index index.html;
    access_log /var/log/nginx/;
    error_log /var/log/nginx/;
    location / {
        try_files $uri $uri/ =404;

Save the file and check that configuration is right:

sudo nginx -t

Restart Nginx:

sudo systemctl restart nginx

Then visit your server name:

NB: This must be a configured DNS to your server else just use the server IP address.

Final Step

Configure Nginx for Node JS Application

Upload node project to /var/www/, build the application and start node application on a designated port.

Edit the application .conf file to look like this:
sudo vi /etc/nginx/conf.d/

server {
    listen 80;
    listen [::]:80;
    root /var/www/;
    access_log /var/log/nginx/;
    error_log /var/log/nginx/;
    location / {
        proxy_pass http://internal_server_ip:node_app_PORT;
        try_files $uri $uri/ =404;

NB: You can get the internal_server_ip by running hostname -I on the terminal. See for more.

Restart Nginx:

sudo systemctl restart nginx

And visit your server name or IP to verify that your application is served.

Congratulation! Your javascript app is successfully served!
You can follow a similar process to serve your python application in the created directory.

This post is inspired by

Discussion (0)