This guide covers an easy to follow, step-by-step guide on how to install Nginx on Ubuntu 18.04 LTE, 16.04, and 14.04. While writing this tutorial I used Ubuntu 18.04 LTE though these resources in the tutorial are useful in installing Nginx on Ubuntu 16.04 and 14.04 as well.
Introduction
Nginx is one of the most popular and powerful web servers in the world used by some of the best, largest and high-traffic websites. Its asynchronous architecture makes it the most powerful, most popular and best-performing web servers available.
I personally prefer Nginx web server for my all personal and client’s website. It is more powerful, and resource-friendly than apache.
If you are looking to install Ubuntu (Linux) with Nginx, MySQL and PHP (LEMP Stack Installation) consider reading:
How to Install LEMP Stack – Ubuntu (Linux), NGINX, MySQL, PHP
For Ubuntu Ubuntu (Linux), Apache, MySQL and PHP (LAMP Stack) Installation guide read:
How To Install LAMP Stack – Ubuntu (Linux), Apache, MySQL, PHP
Prerequisites
Let’s first update your server by running the following command:
sudo apt-get update && sudo apt-get upgrade
How To Install Nginx on Ubuntu: Step-by-step
Step 1 – Install Nginx on Ubuntu
Once you are done updating your server, install Nginx on Ubuntu from its default repository using the apt packaging system by running the following command:
sudo apt-get install nginx
Step 2 – Configure the Firewall
Before testing the Nginx installation on Ubuntu, let’s first adjust the firewall to allow access to the service. Nginx by default registers itself as a service with ufw.
You can check the status by running the following command:
Output Available applications: Nginx Full Nginx HTTP Nginx HTTPS OpenSSH
Nginx Full: It means the port 80 is open (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted) are open for http
protocol. So that anyone can access the web server by entering the IP address in the web browser.
Nginx HTTP: This only opens port 80 (normal, unencrypted web traffic)
Nginx HTTPS: This opens port 443 (TLS/SSL encrypted)
It is important that we enable all the required profiles for HTTP and HTTPS.
You can see the status by running the following command:
sudo ufw status
You should see the allowed HTTP and HTTPS traffic. You can see the output below:
Status: active To Action From -- ------ ---- Nginx Full ALLOW Anywhere Nginx HTTP ALLOW Anywhere Nginx HTTPS ALLOW Anywhere Nginx Full (v6) ALLOW Anywhere (v6) Nginx HTTP (v6) ALLOW Anywhere (v6) Nginx HTTPS (v6) ALLOW Anywhere (v6)
Step 3 – Checking Your Server
Now you can check the status of your Nginx web server by typing the following command in the terminal:
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: en Active: active (running) since Thu 2019-01-24 13:12:02 IST; 5 days ago Process: 22119 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 Process: 22125 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (cod Process: 22122 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process Main PID: 22127 (nginx) CGroup: /system.slice/nginx.service ├─22127 nginx: master process /usr/sbin/nginx -g daemon on; master_pr └─22128 nginx: worker process Warning: Journal has been rotated since unit was started. Log output is incomple lines 1-12/12 (END)
We can see that Nginx is started successfully.
So now you need to put the IP address of your server in the web browser to see the Nginx welcome page.
In case you don’t know the IP address of your server, run the following command to display the IP address:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
Now when you enter the IP address of your server in the browser, you get to see the Welcome page by Nginx.
If you see the above page successfully by running the IP address in the browser, mean that Nginx is configured correctly.
Step 4 – Managing Nginx and its Processes
Below are some of the very useful command that you will need while managing Nginx on your server.
To stop your web server:
sudo systemctl stop nginx
or
sudo service nginx stop
or
sudo /etc/init.d/nginx stop
To start the web server:
sudo systemctl start nginx
or
sudo service nginx start
or
sudo /etc/init.d/nginx start
To restart the web server:
sudo systemctl restart nginx
or
sudo service nginx restart
or
sudo /etc/init.d/nginx restart
To reload the Nginx without dropping the connection you can run the reload command:
sudo systemctl reload nginx
or
sudo service nginx reload
or
sudo /etc/init.d/nginx reload
In any case, if you need to disable your web server, you might want to run the disable command:
sudo systemctl disable nginx
To enable web server:
sudo systemctl enable nginx
That’s it!
You are done installing and configuring Nginx web server. For more information regarding Nginx documentation, check official documentation by Nginx.
If you need to install a WordPress installation using Ubuntu, Nginx and MySQL read WordPress Installation guide on LEMP with Ubuntu.
I hope you will find this guide on how to install Nginx on Ubuntu 18.04, 16.04 and 16.04 useful.
If you have any other web application to install, you can put it in the comment section or mail me on my email address. I will personally get in touch with you to help you setup your application.