Nginx is one of the most popular web servers in the world — fast, lightweight and reliable. In this guide we'll walk through installing and configuring nginx on a VPS running Ubuntu, including virtual host setup and SSL certificate.

Step 1 — Install Nginx

Connect to your VPS via SSH and run:

sudo apt update sudo apt install nginx -y

Check if nginx is running:

sudo systemctl status nginx

You should see active (running). Nginx starts automatically on server reboot.

Step 2 — Open Firewall Ports

If you're using UFW firewall, open ports 80 and 443:

sudo ufw allow 'Nginx Full' sudo ufw status
⚠️ NAT VPS

With NAT VPS from VPSStorages, ports are managed through NAT rules. Contact support to open additional ports.

Step 3 — Virtual Host

Create a configuration file for your domain:

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

Add the following content:

server { listen 80; server_name mysite.com www.mysite.com; root /var/www/mysite; index index.html index.php; location / { try_files $uri $uri/ =404; } }

Enable the configuration:

sudo ln -s /etc/nginx/sites-available/mysite.com /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx

Step 4 — Create Directory and Test Page

sudo mkdir -p /var/www/mysite sudo chown -R www-data:www-data /var/www/mysite echo "<h1>Nginx is working!</h1>" | sudo tee /var/www/mysite/index.html

Step 5 — SSL with Let's Encrypt

Install Certbot for a free SSL certificate:

sudo apt install certbot python3-certbot-nginx -y sudo certbot --nginx -d mysite.com -d www.mysite.com

Certbot automatically updates the nginx configuration for HTTPS. The certificate renews automatically every 90 days.

Step 6 — Performance Optimization

Add the following settings in /etc/nginx/nginx.conf for better performance:

http { # Compression gzip on; gzip_types text/plain text/css application/json application/javascript; gzip_min_length 1000; # Static file caching location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 30d; add_header Cache-Control "public, no-transform"; } # Hide nginx version server_tokens off; }
sudo nginx -t && sudo systemctl reload nginx

Useful Commands

Common Nginx Commands
  • sudo nginx -t — test configuration
  • sudo systemctl reload nginx — reload without downtime
  • sudo systemctl restart nginx — full restart
  • sudo tail -f /var/log/nginx/error.log — view errors
  • sudo tail -f /var/log/nginx/access.log — view access log

Conclusion

Nginx is a powerful and lightweight web server, ideal for VPS hosting. With proper configuration it can handle thousands of concurrent requests with minimal resource usage. Combine it with VPS Plus from VPSStorages for optimal performance at an affordable price.