Nginx is a widely used web server known for its efficiency and event-driven architecture, which allows it to handle multiple requests within a single thread. It’s also popular for its use as a reverse proxy. This guide will walk you through the installation and basic management of Nginx on Ubuntu 20.04.
Installation
To install Nginx from Ubuntu's default repositories, follow these steps:
Update Package Lists:
sudo apt update
Upgrade Installed Packages:
sudo apt upgrade
Install Nginx:
sudo apt install nginx
Firewall Configuration
After installation, Nginx registers itself as a service with ufw
(Uncomplicated Firewall). If ufw
is not configured to allow Nginx, access will be blocked.
Check Available Application Profiles:
sudo ufw app list
You should see output similar to:
Nginx Full: Opens/closes port 80 and port 443
Nginx HTTP: Opens/closes only port 80
Nginx HTTPS: Opens/closes only port 443
OpenSSH: Opens/closes only port 22
Allow Nginx HTTP:
sudo ufw allow 'Nginx HTTP'
Check ufw
Status:
sudo ufw status
The output should look like this:
This should show Nginx HTTP (and optionally HTTPS) as allowed.
Checking Web Server Status
To ensure that Nginx is running, use the following command:
systemctl status nginx
The output should look like this:
Alternatively, you can verify by visiting your server’s IP address or hostname in a web browser.
Managing Nginx
Here are some essential commands for managing Nginx:
Stop Nginx:
sudo systemctl stop nginx
Start Nginx:
sudo systemctl start nginx
Restart Nginx:
sudo systemctl restart nginx
Reload Nginx (apply configuration changes without dropping connections):
sudo systemctl reload nginx
Disable Nginx from starting at boot:
sudo systemctl disable nginx
Enable Nginx to start at boot:
sudo systemctl enable nginx
Nginx Files and Directories
- Web Content Directory:
/var/www/html
(default Nginx page)
- Configuration Directory:
/etc/nginx
- Main Configuration File:
/etc/nginx/nginx.conf
- Server Blocks Configuration:
/etc/nginx/sites-available/
(configuration files)
/etc/nginx/sites-enabled/
(symlinks to enabled configurations)
- Log Files:
/var/log/nginx/access.log
(access requests)
/var/log/nginx/error.log
(error logs)
Setting Up Server Blocks
Server Blocks in Nginx are akin to Virtual Hosts in Apache, allowing you to host multiple domains on a single server.
Create Directory for Your Domain:
sudo mkdir -p /var/www/your_domain_name/html
Assign Ownership:
sudo chown -R $USER:$USER /var/www/your_domain_name/html
Set Permissions:
sudo chmod -R 755 /var/www/your_domain_name
Create an Index File:
nano /var/www/your_domain_name/html/index.html
Add the following content:
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Success! Your_domain_name server block is working!</h1>
</body>
</html>
Create a Server Block Configuration:
sudo nano /etc/nginx/sites-available/your_domain_name
Add the following configuration:
server {
listen 80;
listen [::]:80;
root /var/www/your_domain_name/html;
index index.html index.htm;
server_name your_domain_name www.your_domain_name;
location / {
try_files $uri $uri/ =404;
}
}
Enable the Server Block:
sudo ln -s /etc/nginx/sites-available/your_domain_name /etc/nginx/sites-enabled/
Visit your domain via browser to verify it’s working:
Check Nginx Configuration and Reload:
sudo nginx -t
sudo systemctl reload nginx
Verify Your Domain:
Visit your domain in a web browser to ensure it’s working correctly.
List All Server Blocks:
nginx -T | grep "server_name "
Recommendations
To avoid hash bucket memory issues when adding multiple server names, consider adjusting the server_names_hash_bucket_size
parameter in the Nginx configuration:
Edit Nginx Configuration:
sudo nano /etc/nginx/nginx.conf
Modify or Add the server_names_hash_bucket_size
Line:
http {
server_names_hash_bucket_size 64;
...
}
Save and Exit:
Restart Nginx to apply changes:
sudo systemctl restart nginx
This revised guide provides a comprehensive overview of setting up and managing Nginx on Ubuntu 20.04.