Installing Nginx on Ubuntu Minimal
To begin, update your system’s package list to ensure you have access to the latest versions:
sudo apt update
Install Nginx using the apt package manager:
sudo apt install nginx
After installation, Nginx will start automatically. Verify its status with:
sudo systemctl status nginx
You should see an output indicating the service is active (running).
Starting and Enabling Nginx
To start Nginx manually (if it didn’t start automatically):
sudo systemctl start nginx
Enable Nginx to launch at boot for persistent availability:
sudo systemctl enable nginx
Configuring the Firewall (UFW)
If you’ve enabled UFW (Uncomplicated Firewall), allow HTTP (port 80) and HTTPS (port 443) traffic to permit web access:
sudo ufw allow 'Nginx Full'
Reload UFW to apply the changes:
sudo ufw reload
Check the firewall status to confirm the rules are in place:
sudo ufw status
Verifying Nginx Installation
Open a browser and navigate to your server’s IP address (e.g., http://your_server_ip). You should see Nginx’s default welcome page. Alternatively, use curl to check from the command line:
curl -v http://your_server_ip
Creating a Virtual Host (Custom Configuration)
For a production website, you’ll need a custom virtual host. First, create a directory for your website files (replace your_domain with your actual domain or subdomain):
sudo mkdir -p /var/www/your_domain/html
Set appropriate ownership and permissions for the directory (replace $USER with your username):
sudo chown -R $USER:$USER /var/www/your_domain/html
sudo chmod -R 755 /var/www/your_domain
Create a test HTML file to verify the configuration:
nano /var/www/your_domain/html/index.html
Add simple content to confirm the setup:
<html>
<head>
<title>Welcome to Your Domain!</title>
</head>
<body>
<h1>Success!</h1>
<p>Your Nginx virtual host is configured correctly.</p>
</body>
</html>
Save and exit the editor (Ctrl+O, Enter, Ctrl+X).
Next, create a virtual host configuration file in the sites-available directory:
sudo nano /etc/nginx/sites-available/your_domain
Add the following configuration (adjust server_name to your domain):
server {
listen 80;
server_name your_domain.com www.your_domain.com;
root /var/www/your_domain/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
error_log ${NGINX_LOG_DIR}/your_domain_error.log;
access_log ${NGINX_LOG_DIR}/your_domain_access.log;
}
Save and exit the editor.
Enable the virtual host by creating a symbolic link from sites-available to sites-enabled:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
Testing and Reloading Nginx
Before applying changes, test your Nginx configuration for syntax errors:
sudo nginx -t
If the test passes (output shows “syntax is ok” and “test is successful”), reload Nginx to apply the new configuration:
sudo systemctl reload nginx
Optional: Configuring SSL with Let’s Encrypt
To secure your site with HTTPS, install Certbot (a tool for obtaining free SSL certificates):
sudo apt install certbot python3-certbot-nginx
Obtain a certificate for your domain (replace your_domain.com with your actual domain):
sudo certbot --nginx -d your_domain.com -d www.your_domain.com
Follow the on-screen prompts to complete the process. Certbot will automatically configure Nginx to use the SSL certificate.
Basic Nginx Commands
Manage your Nginx service with these essential commands:
sudo systemctl start nginxsudo systemctl stop nginxsudo systemctl restart nginx (stops and starts the service)sudo systemctl reload nginx (applies configuration changes without downtime)sudo systemctl status nginx (shows if Nginx is running)nginx -v (basic version info) or nginx -V (detailed version + modules)Troubleshooting Common Issues
sudo systemctl stop apache2) or change Nginx’s listening port in the configuration file.www-data or your username) and have proper permissions (chmod 755 for directories, chmod 644 for files).sudo nginx -t to identify syntax issues. Fix errors in the configuration file and reload Nginx./var/log/nginx/error.log) and access log (/var/log/nginx/access.log) for detailed information about issues. Use tail -f /var/log/nginx/error.log to view logs in real-time.By following these steps, you’ll have a fully functional Nginx server on Ubuntu Minimal, ready to host websites or act as a reverse proxy.