How to Install and Configure Nginx on an Ubuntu VPS Print

  • Nginx, Nginx server block, install Nginx Ubuntu, Certbot, Nginx Ubuntu 26.04, Nginx virtual host, Linux web server, VPS hosting, Nginx configuration, KVM VPS, Ubuntu VPS, Nginx SSL, Nginx Ubuntu 24.04, Let's Encrypt, Owned Networks
  • 0

How to Install and Configure Nginx on an Ubuntu VPS

Nginx is a high-performance web server widely used to host websites, applications, and reverse proxies. Its event-driven architecture makes it an excellent choice for handling concurrent connections while maintaining efficient resource usage.

In this guide, you will learn how to install Nginx on Ubuntu 24.04 LTS or Ubuntu 26.04 LTS, configure a virtual host, enable firewall access, and secure your website with a free Let's Encrypt SSL certificate.

Prerequisites

  • An Ubuntu 24.04 LTS or Ubuntu 26.04 LTS VPS.
  • Root access or a user with sudo privileges.
  • A domain name pointing to your VPS public IP address.
  • Ports 80 and 443 accessible from the internet.

If you need a Linux server, explore Owned Networks KVM VPS hosting for full root access and reliable VPS infrastructure.

1. Update Ubuntu and Install Nginx

Update your system packages and install Nginx:

sudo apt update && sudo apt upgrade -y
sudo apt install -y nginx

Enable Nginx to start automatically and verify its status:

sudo systemctl enable --now nginx
sudo systemctl status nginx --no-pager

You can also check the installed version:

nginx -v

2. Configure the Ubuntu Firewall

If UFW is enabled, allow HTTP and HTTPS connections:

sudo ufw allow 'Nginx Full'
sudo ufw status

The Nginx Full profile allows incoming connections on ports 80 (HTTP) and 443 (HTTPS).

Important: If you are enabling UFW for the first time, allow your SSH port before running sudo ufw enable to avoid losing remote access.

3. Create a Website Directory

Create a directory for your website. Replace example.com with your actual domain:

sudo mkdir -p /var/www/example.com/html
sudo chown -R "$USER":www-data /var/www/example.com
sudo chmod 755 /var/www/example.com /var/www/example.com/html

Create a simple HTML page to verify the configuration:

cat > /var/www/example.com/html/index.html <<'EOF'
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Nginx on Ubuntu VPS</title>
</head>
<body>
    <h1>Nginx is running successfully!</h1>
    <p>Hosted on an Ubuntu VPS with Owned Networks.</p>
</body>
</html>
EOF

4. Configure an Nginx Server Block

Nginx uses server blocks, also known as virtual hosts, to host multiple websites on the same server.

Create a configuration file:

sudo nano /etc/nginx/sites-available/example.com

Add the following configuration:

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

    server_name example.com www.example.com;

    root /var/www/example.com/html;
    index index.html index.htm;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

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

Enable the website by creating a symbolic link:

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

Disable the default website if it is no longer needed:

sudo rm -f /etc/nginx/sites-enabled/default

Test the configuration and reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

Visit http://example.com to verify that your website is accessible.

5. Install Certbot and Enable Free SSL

Let's Encrypt provides free SSL/TLS certificates that enable HTTPS encryption for your website.

Install Certbot and the Nginx integration:

sudo apt install -y certbot python3-certbot-nginx

Before requesting a certificate, ensure both example.com and www.example.com resolve to your VPS and that port 80 is publicly accessible.

Request an SSL certificate:

sudo certbot --nginx -d example.com -d www.example.com

Follow the prompts to complete domain validation and configure HTTPS.

If you do not use the www subdomain, request a certificate only for your primary domain:

sudo certbot --nginx -d example.com

Verify certificate renewal:

sudo certbot renew --dry-run

Certbot installations typically configure automatic certificate renewal through a systemd timer or scheduled task.

6. Configure Basic Nginx Security Headers

Security headers provide additional browser-side protection for websites hosted on your VPS.

Create a reusable security headers configuration:

sudo nano /etc/nginx/snippets/security-headers.conf

Add:

add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header X-Frame-Options "SAMEORIGIN" always;

Include this file inside the HTTPS server block for your website:

include /etc/nginx/snippets/security-headers.conf;

Validate and reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

Note: Review your application's requirements before applying additional headers such as Content-Security-Policy or HTTP Strict Transport Security (HSTS). These can affect application functionality if configured incorrectly.

7. Useful Nginx Management Commands

Use the following commands to manage and troubleshoot your Nginx installation:

# Check Nginx status
sudo systemctl status nginx --no-pager

# Validate configuration
sudo nginx -t

# Reload configuration
sudo systemctl reload nginx

# Restart Nginx
sudo systemctl restart nginx

# View recent errors
sudo tail -n 50 /var/log/nginx/error.log

# View active listening ports
sudo ss -tlnp | grep nginx

# Check HTTPS response headers
curl -I https://example.com

8. Troubleshooting Common Nginx Problems

502 Bad Gateway: This usually indicates that Nginx cannot communicate with an upstream application or PHP-FPM service. Check the upstream service status and Nginx error logs.

403 Forbidden: Verify directory permissions, the configured document root, and whether an index file exists.

404 Not Found: Confirm the requested file exists and review the root and try_files directives.

SSL certificate errors: Verify DNS records, certificate validity, and the domain names included in the certificate.

Nginx fails to start: Check for configuration errors or another application already listening on ports 80 or 443:

sudo nginx -t
sudo journalctl -u nginx -n 50 --no-pager
sudo ss -tlnp | grep -E ':80|:443'

Related Ubuntu VPS Tutorials

Continue configuring your Linux server with these Owned Networks knowledgebase guides:

Host Your Website with Owned Networks

Deploy your website or application on a reliable Owned Networks KVM VPS with full root access, scalable resources, and SSD/NVMe storage options.

Whether you are hosting Nginx, WordPress, Docker applications, or custom development environments, a VPS gives you greater control over your hosting infrastructure.

Explore Owned Networks VPS Hosting and get started with your Linux server today.


Was this answer helpful?

KVM VPS & Dedicated Servers

Deploy a Linux VPS in under 2 minutes — AlmaLinux, Rocky Linux, Ubuntu, Debian. Full root access, KVM virtualization, US and EU locations.

« Back