Read in 4 minutes
How To Install Nginx on Ubuntu 18.04
Nginx pronounced “engine x” is a free, open-source, high-performance HTTP and reverse proxy server responsible for handling the load of some of the largest sites on the Internet. This tutorial will outline the steps to install and manage Nginx on your Ubuntu 18.04 machine.
Before starting with the tutorial, make sure you are logged in as a user with sudo privileges and you don’t have Apache or any other service running on port 80 or 443.
Nginx packages are available in Ubuntu default software repositories. The installation is pretty straightforward, just type the following commands:
sudo apt update sudo apt install nginx
Once the installation is completed, check the service status and the Nginx version wit the following commands:
sudo systemctl status nginx
● 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
sudo nginx -v
nginx version: nginx/1.14.0 (Ubuntu)
Configure the firewall
If you are running a firewall you also need to open ports
sudo ufw allow 'Nginx Full'
You can verify the change with:
sudo ufw status
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
http://YOUR_IP in your browser of choice, and you should be able to see the default Nginx landing page as shown below:
Manage the Nginx service with systemctl
You can manage the Nginx service as any other systemd unit. To stop the Nginx service, run:
sudo systemctl stop nginx
To start it again, type:
sudo systemctl start nginx
To restart the Nginx service:
sudo systemctl restart nginx
To reload the Nginx service after you made some configuration changes:
sudo systemctl reload nginx
If you want to disable the Nginx service to start at boot:
sudo systemctl disable nginx
And to re-enable it again:
sudo systemctl enable nginx
Nginx Configuration File’s Structure and Best Practices
- All Nginx configuration files are located in the
- The main Nginx configuration file is
- To make Nginx configuration 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.
- Nginx server block files are stored in
/etc/nginx/sites-availabledirectory. The configuration files found in this directory are not used by Nginx unless they are linked to the
- To activate a server block you need to create a symlink (a pointer) from the configuration file sites in a
sites-availabledirectory to the
- It is a good idea to follow a standard naming convention, for example if your domain name is
mydomain.comthen you the configuration file should be named
/etc/nginx/snippetsdirectory contains configuration snippets that can be included in the server block files. If you use repeatable configuration segments then you can refactor those segments into snippets and include the snippet file to the server blocks.
- Nginx log files (
error.log) are located in the
/var/log/nginx/directory. It is recommended to have a different
errorlog files for each server block.
- You can set your domain document root directory to any location you want. The most common locations for webroot include:
Congratulations, you have successfully installed Nginx on your Ubuntu 18.04 server. You’re now ready to start deploying your applications and use Nginx as a web or proxy server. A secure certificate is a ‘must-have’ feature for all websites nowadays, to secure your website with a free Let’s Encrypt SSL certificate, you can follow the instructions from this article.
If you intend to host multiple domains on your server, you can check this tutorial and learn how to create Ningx server blocks.