Nginx is one of the most popular web servers in the world which serves worlds some high traffic and largest sites on the Internet. It is free, open-source, high-performance HTTP and reverse proxy server.In this tutorial we will learn how to install and configure nginx on ubuntu 16.04 .
Before we begin
- You should have a regular, non-root user with sudo privileges
- Make sure you don’t have Apache or any other service running on port 80
Step 1: Install Nginx
Nginx packages are available in Ubuntu default software repositories.So installation is simple and straight forward.
First,update our local package index in order to get latest packages
sudo apt-get update
Now install nginx as below
sudo apt-get install nginx
This will install Nginx and any required dependencies like nginx-core,nginx-common to your server.
Once the installation is completed Nginx will be automatically started.
2.Check Nginx installation
You can make sure that Nginx service is running with the following command:
sudo systemctl status nginx
Will get output like
● 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 Sat 2020-07-11 20:53:40 IST; 9min ago Main PID: 5016 (nginx) CGroup: /system.slice/nginx.service ├─5016 nginx: master process /usr/sbin/nginx -g daemon on; master_process on ├─5017 nginx: worker process └─5018 nginx: worker process Jul 11 20:53:40 gitserver systemd: Starting A high performance web server and a reverse proxy server... Jul 11 20:53:40 gitserver systemd: Started A high performance web server and a reverse proxy server. Jul 11 20:53:55 gitserver systemd: Started A high performance web server and a reverse proxy server.
You can also use command sudo service nginx status to check the nginx status
As you can see above, the service appears to have started successfully. By default nginx will run on port 80.However, the best way to test this is to actually request a page from Nginx.
When correctly installed Nginx’s default file will appear in /var/www/html as index.nginx-debian.html.
You can access the default Nginx landing page to confirm that the software is running properly. You can access this through your server’s domain name or IP address.
Since it is installed on our local machine just type http://localhost (If you hava domain name or ip type instead of localhost).You can see output like below
Manage the Nginx service with systemctl
You can manage the Nginx service by using below commands
To stop the Nginx service, run:
sudo systemctl stop nginx
To start the Nginx service, type:
sudo systemctl start nginx
To check Nginx configuration is correct
It must give below output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
if it gives any error we need to check the configuration files and fix the issue
Restart the Nginx service:
sudo systemctl restart nginx
Reload the Nginx service after you have made some configuration changes:
sudo systemctl reload nginx
Enable the Nginx service to start at boot
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
- 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
myexample.comthen your configuration file should be named
- Nginx log files (
error.log) are located in the
- You can set your domain document root directory to any location you want. The most common locations for webroot include:
You have successfully installed Nginx on your Ubuntu 16.04 server. You’re now ready to start deploying your applications and use Nginx as a web or proxy server