This guide will walk you through the process of installing and configuring the Nginx web server on an Ubuntu or Debian system. By the end of this tutorial, you will have a basic understanding of how to set up Nginx, create server blocks (virtual hosts), manage sites, configure firewall rules, and test your setup.
Installing Nginx
To install Nginx on Ubuntu or Debian, follow these steps:
- Update the package index on your server:
- Install Nginx using the following command:
- Once the installation is complete, you can start the Nginx service with:
- To ensure that Nginx starts automatically at boot, enable it using:
sudo apt update
sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
Default Configuration
Nginx comes with a default configuration file located at /etc/nginx/nginx.conf. It's generally recommended to leave this file untouched and make site-specific configurations in server blocks.
The default server block is found at /etc/nginx/sites-available/default and is enabled by creating a symbolic link to it in the sites-enabled directory. You can view its contents with:
cat /etc/nginx/sites-available/default
Creating Server Blocks (Virtual Hosts)
To host multiple domains on a single server, you need to create separate server blocks for each domain.
- Create a new configuration file in the
sites-availabledirectory: - Add the following basic server block configuration, replacing
example.comwith your domain name and adjusting paths as necessary: - Create the document root directory:
- Set the correct permissions for the web root directory:
- Create a sample index file to test your configuration:
- Enable the new site by creating a symbolic link in the
sites-enableddirectory: - Test your Nginx configuration for syntax errors:
- If the test is successful, reload Nginx to apply changes:
sudo nano /etc/nginx/sites-available/example.com
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www
echo "Welcome to example.com!" | sudo tee /var/www/example.com/html/index.html
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Firewall Configuration
Ensure that your firewall allows traffic on port 80 (HTTP) and optionally port 443 (HTTPS).
- If you are using UFW, allow HTTP traffic:
sudo ufw allow 'Nginx Full'
Testing Your Setup
To verify that your Nginx server block is working correctly, open a web browser and navigate to your domain name or IP address. You should see the "Welcome to example.com!" message you created earlier.
Troubleshooting
- 403 Forbidden Error: Check that the permissions for your document root directory are set correctly and that Nginx has read access.
- 502 Bad Gateway Error: This often indicates an issue with a backend service. Ensure all services required by your site (e.g., PHP-FPM) are running.
- Nginx Not Starting: Check the Nginx error log for more information using
sudo tail -f /var/log/nginx/error.log.