How to install Nginx on Ubuntu 18.04

s2 0

Nginx, pronounced “engine x”, is a free, open source, high performance HTTP and reverse proxy server that is responsible for handling the load of some of the largest websites on the Internet.

Nginx can be used as a standalone web server and as a reverse proxy for Apache and other web servers.

Compared to Apache, Nginx can handle a much larger number of simultaneous connections and has a lower memory footprint per connection.

This tutorial describes the steps required to install Nginx on an Ubuntu 18.04 computer.

requirements

Before starting the tutorial, make sure that you are logged in as a user with sudo privileges and that no Apache or other web server is running on port 80 or 443.

Install Nginx

Nginx packages are available in the standard Ubuntu repositories. Installation is pretty straightforward.

We’ll start by updating the list of packages and then installing Nginx:

sudo apt updatesudo apt install nginx

Once the installation is complete, the Nginx service will start automatically. You can check the status of the service with the following command:

sudo systemctl status nginx

The output looks something like this:

● 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 Sun 2018-04-29 06:43:26 UTC; 8s ago
     Docs: man:nginx(8)
  Process: 3091 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 3080 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 3095 (nginx)
    Tasks: 2 (limit: 507)
   CGroup: /system.slice/nginx.service
           ├─3095 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─3097 nginx: worker process

Configure the firewall

Assuming you use UFW
To manage your firewall you need to use HTTP (80) and HTTPS (443) Ports. You can do this by enabling the “Nginx Full” profile, which contains rules for both ports:

sudo ufw allow 'Nginx Full'

To check the status type:

sudo ufw status

The output looks something like this:

Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)

Test the installation

To test your new Nginx installation, open https://YOUR_IP in your browser of your choice, and you will get the default Nginx landing page as shown in the image below:

Manage the Nginx service

You can manage the Nginx service like any other systemd service.

To stop the Nginx service, do the following:

sudo systemctl stop nginx

To start it again, type:

sudo systemctl start nginx

To restart the Nginx service:

sudo systemctl restart nginx

After making some configuration changes, reload the Nginx service:

sudo systemctl reload nginx

By default, the Nginx service starts at boot. If you want to disable the Nginx service to start at boot:

sudo systemctl disable nginx

And to activate it again:

sudo systemctl enable nginx

Structure and best practices of the Nginx configuration file

  • All Nginx configuration files are in the /etc/nginx Directory.
  • The main configuration file of Nginx is /etc/nginx/nginx.conf.
  • To simplify the maintenance of the Nginx configuration, it is recommended to create a separate configuration file for each domain. You can have as many server block files as you need.
  • Nginx server block files are stored in /etc/nginx/sites-available Directory. The configuration files found in this directory will not be used by Nginx unless they are associated with the /etc/nginx/sites-enabled Directory.
  • To activate a server block you need to get a symlink (pointer) from the configuration file sites in a. create sites-available Directory for sites-enabled Directory.
  • It is recommended that you follow standard naming conventions, for example if your domain name. is mydomain.com then your configuration file should be named /etc/nginx/sites-available/mydomain.com.conf
  • the /etc/nginx/snippets -Directory contains configuration snippets that can be included in the server block files. If you are using repeatable configuration segments, you can convert these segments into snippets and include the snippet file in the server blocks.
  • Nginx log files (access.log and error.log) are located in the /var/log/nginx Directory. It is recommended to have another one access and error Log files for each server block.
  • You can set your domain document root directory to any location. The most common locations for Webroot include:
    • /home/<user_name>/<site_name>
    • /var/www/<site_name>
    • /var/www/html/<site_name>
    • /opt/<site_name>

Conclusion

Congratulations, you have successfully installed Nginx on your Ubuntu 18.04 server. You can now start deploying your applications and using Nginx as a web or proxy server. A secure certificate is a must for all websites these days. To secure your website with a free Let’s Encrypt SSL certificate, you can follow this guide on how to secure Nginx with Let’s Encrypt on Ubuntu 18.04.

If you intend to host multiple domains on your server, you can read this tutorial and learn how to create Nginx server blocks.

This post is part of the how-installed-lemp-stack-on-ubuntu-18-04 Series.
Further articles in this series:


How to install Nginx on Ubuntu 18.04

How to set up Nginx server blocks on Ubuntu 18.04

Secure Nginx with Let’s Encrypt on Ubuntu 18.04

How to install MySQL on Ubuntu 18.04

How to install PHP on Ubuntu 18.04

This post is part of the how-installed-lemp-stack-on-ubuntu-18-04 Series.
Further articles in this series:


How to install Nginx on Ubuntu 18.04

How to set up Nginx server blocks on Ubuntu 18.04

Secure Nginx with Let’s Encrypt on Ubuntu 18.04

How to install MySQL on Ubuntu 18.04

How to install PHP on Ubuntu 18.04

How to install Nginx on Ubuntu 18.04

How to set up Nginx server blocks on Ubuntu 18.04

Secure Nginx with Let’s Encrypt on Ubuntu 18.04

How to install MySQL on Ubuntu 18.04

How to install PHP on Ubuntu 18.04