Complete Guide on How to Install Nginx on Ubuntu 18.04 – Nginx pronounced “engine x” is a free, open source, high performance HTTP, and reverse proxy server that is responsible for handling the loads of some of the largest sites on the Internet and can also function as an IMAP / POP3 proxy. The source code for nginx was written by a Russian national named Igor Sysoev in 2002 and released to the public in 2004.
Nginx can be used as a standalone web server, and as a reverse proxy for Apache and other web servers.
In a computer network, Reverse Proxy or reverse proxy is a type of proxy server that fetches resources on behalf of clients from one or more servers. These resources are then returned to the client, looking as if they came from the proxy server itself.
Compared to Apache, Nginx can handle a large number of concurrent connections and has a smaller memory footprint per connection.
This guide will outline the step by step required for how to install Nginx on Ubuntu 18.04.
Requirements
Before starting with the tutorial, make sure you are logged in as a privileged user sudo
and you don’t have Apache or any other web server running on that port 80
or 443
.
How to Install Nginx
Nginx packages are readily available in the default Ubuntu repositories. Installing Nginx is fairly straightforward.
I’ll start by updating the packages list and then installing Nginx:
$ sudo apt update
$ sudo apt install nginx
After the installation is complete, the Nginx service will start automatically. You can check the service status with the following command:
$ sudo systemctl status nginx
The output will look like this:
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 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
Firewall configuration
Assuming you are using UFW
to manage your firewall you must open the HTTP port (80
) and HTTPS (443
). You can do this by enabling the ‘Nginx Full’ profile which includes rules for both ports:
$ sudo ufw allow 'Nginx Full'
To verify Type Status:
$ sudo ufw status
The output will look something like this:
Output
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)
Installation Test
You can test your new Nginx installation by opening http:// IP_Anda
in your best browser, and you will see the default Nginx landing page as shown in the image below:
Manage Nginx Services
You can manage the Nginx service in the same way as any other systemd service.
To Stop Nginx Services, run:
$ sudo systemctl stop nginx
To Start again, type:
$ sudo systemctl start nginx
To restart the Nginx service:
$ sudo systemctl restart nginx
Load Reload Nginx services after you’ve made a few configuration changes:
$ sudo systemctl reload nginx
By default the Nginx service will start at boot. If you want to disable the Nginx service to start on boot type the following command:
$ sudo systemctl disable nginx
And to reactivate it:
$ sudo systemctl enable nginx
Nginx Configuration File Structure and Best Practices
- All Nginx configuration files are located in a directory
/etc/nginx
. - The main Nginx configuration file is
/etc/nginx/nginx.conf
. - To make Nginx configurations easier to maintain, it is recommended to create a separate configuration file for each domain. You can have as many server block files as you need.
- Files Nginx server block stored in the directory
/etc/nginx/sites-available
. The configuration files found in this directory are not used by Nginx unless they are linked by a directory/etc/nginx/sites-enabled
. - To activate server blocks, you need to create a symlink (pointer) from the configuration file site in the site’s available directory to the directory the site supports.
- It is recommended to 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
. - Directory
/etc/nginx/snippets
contains configuration snippets that can be included in the server block file. If you are using a repeatable configuration segment, then you can break the segment into chunks and attach the chunk file to the server block. - Nginx log files (
access.log
anderror.log
) is located in the directory/var/log/nginx
. It is recommended to have different access and error log files for each server block. - You can set your domain’s document root directory to any location you want. The most common locations for webroots 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 are now ready to start deploying your application and use Nginx as a web server or proxy server. A secure SSL certificate is a must-have feature for all websites today, to secure your website with Free Let’s Encrypt SSL certificate, You can follow this guide about Securing Nginx with Let’s Encrypt on Ubuntu 18.04.
Read: How to Configure Ubuntu Server as a Router (18.04.1 LTS)
And that’s a complete guide on how to install Nginx on an Ubuntu 18.04 server, I hope this article is useful for you and good luck. 🙂