Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Install and Configure Nginx on Ubuntu 16.04
Nginx is a high-performance web server and reverse proxy server that powers many of the world's busiest websites. It is known for its stability, rich feature set, simple configuration, and low resource consumption. This guide covers installing and configuring Nginx on Ubuntu 16.04.
Prerequisites
Before starting, ensure you have:
Ubuntu 16.04 installed on your machine
A non-root user with sudo privileges
Internet connection for package installation
Installing Nginx
Nginx is available in Ubuntu's default repository, making installation straightforward using the apt package manager.
Update Package Repository
First, update your system's package index to ensure you get the latest available packages:
sudo apt-get update
Install Nginx Package
Install Nginx using the following command:
sudo apt-get install nginx
The system will download and install Nginx along with its dependencies. When prompted, press Y and then Enter to confirm the installation.
Configuring the Firewall
Ubuntu 16.04 comes with UFW (Uncomplicated Firewall) which needs to be configured to allow HTTP traffic to reach Nginx.
Check Available Applications
View the list of applications that UFW recognizes:
sudo ufw app list
Available applications: Nginx Full Nginx HTTP Nginx HTTPS OpenSSH
Allow Nginx HTTP Traffic
Enable HTTP access through the firewall:
sudo ufw allow 'Nginx HTTP'
Verify the firewall status:
sudo ufw status
Verifying Nginx Installation
Check if Nginx is running properly using the systemctl command:
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 2016-08-14 16:44:20 IST; 8min ago
Main PID: 4983 (nginx)
CGroup: /system.slice/nginx.service
??4983 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
??4984 nginx: worker process
If the service shows as active (running), Nginx has been installed successfully. You can also test by opening your web browser and navigating to http://your-server-ip. You should see the default Nginx welcome page.
Managing Nginx Service
Nginx can be controlled using standard systemctl commands:
| Action | Command | Description |
|---|---|---|
| Stop | sudo systemctl stop nginx |
Stops the web server |
| Start | sudo systemctl start nginx |
Starts the web server |
| Restart | sudo systemctl restart nginx |
Stops and starts the service |
| Reload | sudo systemctl reload nginx |
Reloads configuration without dropping connections |
| Enable | sudo systemctl enable nginx |
Auto-starts Nginx at boot |
| Disable | sudo systemctl disable nginx |
Prevents auto-start at boot |
Important Nginx Files and Directories
Content Directory
/var/www/html− Default document root where web files are served from
Configuration Files
/etc/nginx− Main Nginx configuration directory/etc/nginx/nginx.conf− Primary configuration file for global settings/etc/nginx/sites-available/− Directory for per-site server block configurations/etc/nginx/sites-enabled/− Directory for enabled site configurations (symlinks from sites-available)/etc/nginx/snippets/− Reusable configuration fragments
Log Files
/var/log/nginx/access.log− Records all requests to the web server/var/log/nginx/error.log− Records Nginx errors and diagnostic information
Conclusion
You have successfully installed and configured Nginx on Ubuntu 16.04. The web server is now running and ready to serve content. From here, you can customize server blocks, configure SSL, or set up reverse proxy functionality depending on your specific needs.
