5.0 KiB
Setting Up a Web Server on Nginx
This guide covers the steps to configure Nginx as a basic web server to serve static HTML files and handle HTTP requests. We'll set up a sample web server on a Debian-based system, but the steps are similar for other Linux distributions.
Prerequisites
- A server with Nginx installed. If you haven’t installed Nginx yet, follow the installation instructions in the Nginx Installation Guide (or use the provided installation commands).
- Root or sudo privileges to edit configuration files and restart Nginx services.
Step 1: Set Up the Web Directory
-
Create a Directory for Your Website Nginx typically serves content from
/var/www/. Create a new directory for your website content.sudo mkdir -p /var/www/example.com/html -
Set Permissions Ensure that the Nginx user (usually
www-data) has permission to read files in this directory.sudo chown -R $USER:$USER /var/www/example.com/html sudo chmod -R 755 /var/www/example.com -
Add a Sample HTML File Create a simple HTML file to confirm the setup.
echo "<html> <head> <title>Welcome to Example.com!</title> </head> <body> <h1>Success! Nginx is serving your website.</h1> </body> </html>" | sudo tee /var/www/example.com/html/index.html
Step 2: Configure Nginx to Serve the Website
-
Create a Server Block Configuration File Nginx server blocks (similar to Apache virtual hosts) allow you to host multiple sites on the same server. Create a new configuration file for your site.
sudo vim /etc/nginx/sites-available/example.com -
Add Server Block Configuration Paste the following configuration into the file, replacing
example.comwith your domain or IP address:server { listen 80; server_name example.com www.example.com; root /var/www/example.com/html; index index.html; location / { try_files $uri $uri/ =404; } } -
Enable the Server Block Link the configuration file to
sites-enabledto enable it in Nginx:sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ -
Test the Nginx Configuration Run the following command to check for any syntax errors in the configuration:
sudo nginx -t -
Reload Nginx to Apply Changes If the syntax test passes, reload Nginx to apply the new configuration.
sudo systemctl reload nginx
Step 3: Configure DNS (Optional)
If you have a domain name, point it to your server’s IP address in your DNS provider’s settings. Create an A record for example.com and, if desired, www.example.com to direct traffic to your server’s IP address.
Step 4: Access Your Website
In a web browser, navigate to http://example.com (replace example.com with your domain or IP address). You should see the sample HTML page you created, confirming that Nginx is serving your web content.
Optional: Enabling HTTPS with SSL/TLS
For added security, you can configure HTTPS on your Nginx web server. One free and easy way to do this is by using Let’s Encrypt.
-
Install Certbot and the Nginx Plugin
sudo apt install certbot python3-certbot-nginx -
Obtain and Install a Certificate Run the following Certbot command to automatically obtain and configure an SSL certificate for your website:
sudo certbot --nginx -d example.com -d www.example.com -
Verify Renewal Process Certificates from Let’s Encrypt expire every 90 days. To automatically renew the certificates, add a cron job or use Certbot’s built-in renewal service:
sudo certbot renew --dry-run
Now your website will be accessible securely at https://example.com.
Nginx Configuration Summary
Here's a quick reference for the key commands and file paths:
- Site root directory:
/var/www/example.com/html - Nginx configuration files:
- Site-specific:
/etc/nginx/sites-available/example.com - Enabled sites:
/etc/nginx/sites-enabled/
- Site-specific:
- Commands:
- Check configuration syntax:
sudo nginx -t - Reload Nginx:
sudo systemctl reload nginx
- Check configuration syntax:
Troubleshooting Common Issues
-
Error: 403 Forbidden
- Check that Nginx has the necessary permissions to access files in the root directory (
/var/www/example.com/html). Usechmod 755andchowncommands as shown above.
- Check that Nginx has the necessary permissions to access files in the root directory (
-
Error: 404 Not Found
- Ensure the
index.htmlfile exists in the specified directory and thattry_filesdirective is correctly pointing to it.
- Ensure the
-
Configuration Errors
- Always run
sudo nginx -tto check configuration changes before reloading Nginx.
- Always run
-
SSL Issues
- If HTTPS fails, make sure Certbot successfully installed the certificate and that the DNS settings correctly point to your server’s IP address.