Install Nginx and Apache on the same server on Ubuntu 18.04

If you already read my previous blog about https://agustox21.wordpress.com/2020/05/05/install-cms-moodle-on-ubuntu-18-04-server-with-apache2-mariadb-and-php-support/ , i have been  installed the Apache server there and running CMS Joomla on top of Apache web server.

Now i am challenging again my self again to install another web server Nginx on top that server too.

I have been read a lot articles around that and it has been proven that Ubuntu 18.04 have capable to have more than one web server.  So i tried to install Nginx side by side with Apache Web Server. Because Apache Web Server already used Port 80, for the Nginx it will use port 2100.

Here are the following step i did it :

1.Apt-get update

Before installing new software, it is strongly recommended to update your local software database. Updating helps to make sure you’re installing the latest and best-patched software available.

sudo apt-get update

2. Install Nginx as Web Server

Enter the following to install Nginx on Ubuntu:

sudo apt-get install nginx

3. Adjusting the Firewall

nginx1
As you can see, there are three profiles available for Nginx:

  • Nginx Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
  • Nginx HTTP: This profile opens only port 80 (normal, unencrypted web traffic)
  • Nginx HTTPS: This profile opens only port 443 (TLS/SSL encrypted traffic)

It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured.

You can enable this by typing:

sudo ufw allow 'Nginx HTTP'

But if you Firewall Inactive, you don’t have to set up the firewall.

4. Define Server Blocks for Port 2100

By default, the main Nginx configuration file is located at /etc/nginx/nginx.conf.  Server block configuration files are located at /etc/nginx/sites-available.  So we have to create another folder directory inside the site-available folder.  Here the syntax :

sudo mkdir /etc/nginx/sites-available/html_2100

nginx2

Paste in the following configuration block, which is similar to the default, but updated for our new directory and domain name:

server {
        listen 2100;
        listen [::]:2100;

        root /var/www/html_2100/html
        index index.html index.htm index.nginx-debian.html; 

        server_name www.example.com;

        location / {
                try_files $uri $uri/ =404;
        }
}

5. Create a new sample web page

Create the directory for html_2100 as follows, using the -p flag to create any necessary parent directories:

sudo mkdir -p /var/www/html_2100/html

Next, assign ownership of the directory with the $USER environment variable:

sudo chown -R $USER:$USER /var/www/html_2100/html

The permissions of your web roots should be correct if you haven’t modified your umask value, but you can make sure by typing:

sudo chmod -R 755 /var/www/html_2100

Next, create a sample index.html page using nano or your favorite editor:

sudo nano /var/www/html_2100/html/index.html

Copy paste the following html text below to index.html and the save it.

nginx3

Then let’s enable the file by creating a link from it to the sites-enabled directory, which Nginx reads from during startup:

sudo ln -s /etc/nginx/sites-available/html_2100 /etc/nginx/sites-enabled/

All the configuration are done, and now we can testing 🙂

nginx4

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.