Nginx (pronounced “engine-x”) is a powerful, high-performance web server, reverse proxy, and modern load balancer. Known for its stability, rich feature set, and low resource consumption, it powers a massive portion of the modern web.
This guide will walk you through the process of installing, configuring, and managing an Nginx web server on a Linux machine (specifically focusing on Ubuntu/Debian distributions, which are the most common).
Prerequisites
Before you begin, ensure you have:
- A Linux Server: An Ubuntu or Debian-based server (local or cloud-based like AWS, DigitalOcean, or Linode).
- Root/Sudo Access: You need administrative privileges to install packages and modify system configurations.
- A Registered Domain Name (Optional but recommended): Pointing to your server’s public IP address.
Step 1: Update Your System
It is always a best practice to update your system’s package index before installing new software to ensure you get the latest versions and security patches.
Run the following commands in your terminal:
sudo apt update
sudo apt upgrade -y
Step 2: Install Nginx
Nginx is available by default in the official Ubuntu/Debian repositories, making the installation process straightforward.
sudo apt install nginx -y
Once the installation is complete, the Nginx service will usually start automatically.
Step 3: Configure the Firewall
If you are using ufw (Uncomplicated Firewall) on your server, you need to allow HTTP and HTTPS traffic so the outside world can access your web server. Nginx registers itself with ufw upon installation.
List the available applications configurations in ufw:
sudo ufw app list
You should see something like:
Nginx HTTP(port 80)Nginx HTTPS(port 443)Nginx Full(ports 80 & 443)
Allow traffic on both HTTP and HTTPS by enabling Nginx Full:
sudo ufw allow 'Nginx Full'
Verify the status of your firewall:
sudo ufw status
Step 4: Verify the Web Server
To check if Nginx is running properly, check its systemd status:
sudo systemctl status nginx
(Look for the active (running) status in green).
You can also test it by opening your favorite web browser and navigating to your server’s IP address:
http://your_server_ip
You should see the default “Welcome to nginx!” landing page.
Step 5: Understanding Nginx Files and Directories
Before configuring custom websites, it’s important to understand where Nginx stores its data and configuration files:
/var/www/html: The default web root directory where your website files (like index.html) go./etc/nginx/nginx.conf: The main, global configuration file for Nginx./etc/nginx/sites-available/: The directory where per-site “server blocks” (virtual hosts) are stored. Nginx will not use these configurations unless they are linked to thesites-enableddirectory./etc/nginx/sites-enabled/: The directory that stores symlinks pointing to active server blocks insites-available./var/log/nginx/access.log: Logs every request to your web server./var/log/nginx/error.log: Logs errors generated by Nginx.
Step 6: Set Up Server Blocks (Virtual Hosts)
By default, Nginx has one server block configured to handle global traffic. If you want to host multiple websites or a specific domain (e.g., example.com), you should create customized server blocks.
1. Create a Directory for Your Site
Create a directory structure for your domain inside /var/www/.
sudo mkdir -p /var/www/example.com/html
Assign ownership of the directory to the current system user:
sudo chown -R $USER:$USER /var/www/example.com/html
Set the correct permissions:
sudo chmod -R 755 /var/www/example.com
2. Create a Sample Page
Create an index.html file to test:
nano /var/www/example.com/html/index.html
Add some basic HTML:
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The example.com server block is working!</h1>
</body>
</html>
Save and exit the file (Ctrl+O, Enter, Ctrl+X).
3. Create the Server Block Configuration
Now, create a new server block file in sites-available:
sudo nano /etc/nginx/sites-available/example.com
Paste the following configuration template (make sure to replace example.com with your actual domain):
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
}
4. Enable the Server Block
To enable the file, create a symbolic link (symlink) from it to the sites-enabled directory:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
5. Test and Restart Nginx
Whenever you change Nginx configurations, rigorously test them for syntax errors:
sudo nginx -t
If everything is correct, it will say syntax is ok and test is successful.
Restart Nginx to apply all the changes:
sudo systemctl restart nginx
Now, navigating to http://example.com in your browser will display your custom HTML page.
Managing Nginx Processes
Here are some essential systemctl commands to manage your Nginx service on a day-to-day basis:
- Start Nginx:
sudo systemctl start nginx - Stop Nginx:
sudo systemctl stop nginx - Restart Nginx (closes connections and restarts):
sudo systemctl restart nginx - Reload Nginx (applies config changes without dropping connections):
sudo systemctl reload nginx - Disable starting at boot:
sudo systemctl disable nginx - Enable starting at boot:
sudo systemctl enable nginx
Conclusion
You have successfully installed Nginx, configured the firewall, learned the basic file structure, and deployed a custom server block for your domain. From here, you can explore adding SSL certificates using Let’s Encrypt (Certbot) for HTTPS, or configure Nginx as a reverse proxy for Node.js, Python, or PHP applications!