How to Install Nginx on Ubuntu 16.04
Prerequisites
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.
Install Nginx
Nginx packages are available in Ubuntu default software repositories. The installation is a pretty straightforward, simply run the following commands:
sudo apt update
sudo apt install nginx
Once the installation is completed, check the status of the Nginx service by typing:
sudo systemctl status nginx
The output should show you that the Nginx service is active and running:
● 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-01-05 15:44:04 UTC; 1min 59s ago
Main PID: 1291 (nginx)
CGroup: /system.slice/nginx.service
├─1291 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
└─1293 nginx: worker process
To check the Nginx version type:
sudo nginx -v
nginx version: nginx/1.10.3 (Ubuntu)
Adjust the Firewall
Assuming you are using UFW
to manage your firewall, you’ll need to open HTTP (80
) and HTTPS (443
) ports.
You can do that by enabling the ‘Nginx Full’ profile which includes rules for both ports:
sudo ufw allow 'Nginx Full'
To verify the firewall status type:
sudo ufw status
The output will look something like below:
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 verify that Nginx works as expected open http://YOUR_IP
in your browser of choice, and you will be presented with the default Nginx welcome page as shown below:
The Nginx packages from the Ubuntu repositories are often outdated. To install the latest Nginx version, use the official Nginx PPA repository.
Follow the steps below to install the latest version of Nginx on Ubuntu 16.04:
First install the
software-properties-common
package:sudo apt install software-properties-common
Add the Nginx PPA repository using the following command:
sudo add-apt-repository ppa:nginx/stable
Update the packages list and install Nginx:
sudo apt update
sudo apt install nginx
After the installation is complete, check the Nginx version with:
sudo nginx -v
The output will look something like this:
nginx version: nginx/1.12.2
Manage the Nginx service with systemctl
You can manage the Nginx service in the same way as any other systemd unit.To stop the Nginx service, run:
sudo systemctl stop nginx
To start the Nginx service, type:
sudo systemctl start nginx
sudo systemctl restart nginx
Reload the Nginx service after you have made some configuration changes:
sudo systemctl reload nginx
Disable the Nginx service to start at boot:
sudo systemctl disable nginx
Re-enable the Nginx service to start at boot again:
sudo systemctl enable nginx
Nginx Configuration File’s Structure and Best Practices
- All Nginx configuration files are located in the
/etc/nginx/
directory. - The main Nginx configuration file is
/etc/nginx/nginx.conf
. - 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-available
directory. The configuration files found in this directory are not used by Nginx unless they are linked to the/etc/nginx/sites-enabled
directory. - To activate a server block you need to create a symlink (a pointer) from the configuration file sites in a
sites-available
directory to thesites-enabled
directory. - It is a good idea to follow a standard naming convention, 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 use repeatable configuration segments then you can refactor those segments into snippets and include the snippet file to the server blocks. - Nginx log files (
access.log
anderror.log
) are located in the/var/log/nginx/
directory. It is recommended to have a differentaccess
anderror
log 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:
/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’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 check the following guide:
Comments
Post a Comment