Install Nginx on Ubuntu

Nginx is one of the most popular web servers in the world together with Apache. Nginx is more resource friendly than Apache and can be used as the reverse proxy server. In this tutorial, you will learn how to install Nginx on Ubuntu 18.04.

Prerequisites

Before you start to install Nginx on Ubuntu 18.04. You must have the non-root user account on your server with sudo privileges.

1. Install NGINX

To install Nginx first update local package index to access most recent package listing by typing

sudo apt update

Now install Nginx by typing

sudo apt install nginx

You can check the status of Nginx by typing

systemctl status nginx

You can also restart Nginx server by typing

sudo systemctl restart nginx

The output should be

Output
    ● nginx.service - A high performance web server and a reverse proxy server
    Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
    Active: active (running) since Fri 2018-10-23 09:09:23 UTC; 1 days ago
    Docs: man:nginx(8)
    Main PID: 2369 (nginx)
    Tasks: 2 (limit: 1153)
    CGroup: /system.slice/nginx.service
           ├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─2380 nginx: worker process

2. Setting up Firewall

By assuming **ufw** is running on your server. Check if the firewall is active on your server by typing.

sudo ufw status

If the status is not active then activate it by using the following command.

sudo ufw enable

The system may notify you that current connections will destroy after activating the firewall. Allow incoming connection on port 80, run following command.

sudo ufw allow 80/tcp

Allow incoming connection on port 443 run following command.

sudo ufw allow 443/tcp

Confirm changes by checking the current status of the firewall by

sudo ufw status numbered
Output:
    Status: active

     To                         Action      From
     --                         ------      ----
    [ 1] 443/tcp                    ALLOW IN    Anywhere
    [ 2] 80/tcp                     ALLOW IN    Anywhere
    [ 3] 443/tcp (v6)               ALLOW IN    Anywhere (v6)
    [ 4] 80/tcp (v6)                ALLOW IN    Anywhere (v6)

3. Set up Nginx configuration file

Create a directory inside var/www/html named example.com (you can use your domain name)

sudo mkdir -p /var/www/html/example.com

Now you should remove the default configuration file provided. To delete default Nginx configuration file type following

sudo rm -f /etc/nginx/sites-enabled/default

Configuration files for websites are stored inside /etc/nginx/sites-available directory so you need to create configuration file inside this directory named example.com.conf (you can use your domain name). Then enter following code inside that file by replacing example.com with your domain name

/etc/nginx/sites-available/example.com.conf
    server {
        listen         80 default_server;
        listen         [::]:80 default_server;
        server_name    example.com www.example.com;
        root           /var/www/html/example.com;
        index          index.php;

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

        location ~* \.php$ {
          fastcgi_pass unix:/run/php/php7.2-fpm.sock;
          include         fastcgi_params;
          fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
          fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }
    }

Create a link of above configuration file inside /etc/nginx/sites-enabled/ directory by typing

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Ensure your domain reaching at your server by configuring DNS records of your domain

Now you need to restart PHP and reload Nginx configuration file as you have made changes in Nginx configuration directory. Type following command to restart PHP and reload Nginx

sudo systemctl restart php7.2-fpm   
    sudo nginx -s reload

You can check the status of Nginx by typing following

sudo nginx -t

Create an index.php file inside /var/www/html/example.com directory and enter following code inside the file

/var/www/html/example.com
<html>
    <head>
        <title>Index Page</title>
    </head>
    <body>
        <h1>Success!</h1>
    </body>
</html>

Now visit http://example.com. Replacing example with your domain name where you can see output like Success!

Conclusion

You have successfully learned how to install Nginx on Ubuntu 18.04. If you have any queries regarding this then don’t forget to comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *
You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>