← Back to documentation

Installing and Configuring NGINX

Step-by-step guide to install and configure NGINX web server on your VPS

nginxweb-serverconfigurationssl

Introduction

NGINX is a powerful, high-performance web server and reverse proxy. This guide will walk you through installing and configuring NGINX on your VPS.

Prerequisites

  • A VPS with Ubuntu/Debian installed
  • Root or sudo access
  • Basic command-line knowledge
  • Domain name (optional, for SSL configuration)

Step 1: Install NGINX

Update your package list and install NGINX:

sudo apt update
sudo apt install nginx -y

Verify Installation

Check if NGINX is running:

sudo systemctl status nginx

You should see an active (running) status.

Step 2: Configure Firewall

Allow HTTP and HTTPS traffic through the firewall:

sudo ufw allow 'Nginx Full'
sudo ufw status

Step 3: Test Your Web Server

Open your browser and navigate to your server’s IP address:

http://your-server-ip

Step 4: Configure Server Block

Server blocks (similar to virtual hosts in Apache) allow you to host multiple websites. Let’s create one:

Create Directory Structure

sudo mkdir -p /var/www/your-domain.com/html
sudo chown -R www-data:www-data /var/www/your-domain.com/html
sudo chmod -R 755 /var/www/your-domain.com

The www-data user is the default user that NGINX runs as, ensuring proper file access and security.

Create Sample Page

sudo nano /var/www/your-domain.com/html/index.html

Add some content:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to Your Domain</title>
</head>
<body>
    <h1>Success! Your server is running.</h1>
    <p>This is hosted on NGINX.</p>
</body>
</html>

Create Server Block Configuration

sudo nano /etc/nginx/sites-available/your-domain.com

Add the following configuration:

server {
    listen 80;
    listen [::]:80;

    root /var/www/your-domain.com/html;
    index index.html index.htm;

    server_name your-domain.com www.your-domain.com;

    location / {
        try_files $uri $uri/ =404;
    }
}

Enable the Server Block

sudo ln -s /etc/nginx/sites-available/your-domain.com /etc/nginx/sites-enabled/

Test Configuration

Before restarting, test the configuration:

sudo nginx -t

If successful, reload NGINX:

sudo systemctl reload nginx

Step 5: Set Up SSL with Let’s Encrypt

Secure your site with a free SSL certificate:

Install Certbot

sudo apt install certbot python3-certbot-nginx -y

Obtain SSL Certificate

sudo certbot --nginx -d your-domain.com -d www.your-domain.com

Follow the prompts to complete the setup. Certbot will automatically configure NGINX to use SSL.

Auto-Renewal

Test the renewal process:

sudo certbot renew --dry-run

Certbot sets up automatic renewal, but it’s good to verify it works.

Step 6: Optimize NGINX Configuration

Enable Gzip Compression

Edit the main configuration:

sudo nano /etc/nginx/nginx.conf

Uncomment or add these lines in the http block:

gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml text/javascript 
           application/json application/javascript application/xml+rss;

Configure Cache Headers

Add to your server block:

location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 1y;
    add_header Cache-Control "public, immutable";
}

Common NGINX Commands

Here are some useful commands for managing NGINX:

CommandDescription
sudo systemctl start nginxStart NGINX
sudo systemctl stop nginxStop NGINX
sudo systemctl restart nginxRestart NGINX
sudo systemctl reload nginxReload configuration
sudo nginx -tTest configuration
sudo systemctl status nginxCheck status

Troubleshooting

Port Already in Use

If you get a “port already in use” error:

sudo lsof -i :80
sudo lsof -i :443

This will show what’s using the ports.

Configuration Errors

Always test your configuration before reloading:

sudo nginx -t

Check error logs for details:

sudo tail -f /var/log/nginx/error.log

Permission Denied

If you see permission errors, verify the correct ownership:

ls -la /var/www/your-domain.com

The files should be owned by www-data:www-data. If not, fix the ownership:

sudo chown -R www-data:www-data /var/www/your-domain.com
sudo chmod -R 755 /var/www/your-domain.com

Security Best Practices

Note: Security is crucial for production servers. Always follow best practices:

  • Keep NGINX updated: sudo apt update && sudo apt upgrade
  • Use SSL/TLS certificates
  • Configure rate limiting
  • Disable server tokens: server_tokens off;
  • Set up fail2ban for additional protection

Next Steps

Now that you have NGINX running:

  • Set up a database server (MySQL/PostgreSQL)
  • Deploy your application
  • Configure monitoring and logging
  • Set up automated backups

Additional Resources