How to Set Up Nginx Web Server on Your VPS
What is Nginx?
Nginx is a web server that serves your websites to visitors. It's fast, secure, and very popular.
Nginx (pronounced "engine-x") is one of the best web servers for VPS. It's faster than Apache and uses less memory.
Why Choose Nginx?
- Faster - Serves websites quicker than other web servers
- Uses less memory - Good for small VPS with limited RAM
- Better for static files - Images, CSS, and JavaScript load faster
- Reverse proxy - Can work as a front-end for other applications
- Easy to configure - Simple configuration files
Installing Nginx
Step 1: Update Your System
sudo apt update
sudo apt upgrade -yStep 2: Install Nginx
sudo apt install nginx -yStep 3: Start and Enable Nginx
# Start Nginx
sudo systemctl start nginx
# Make Nginx start automatically when server boots
sudo systemctl enable nginx
# Check if Nginx is running
sudo systemctl status nginxStep 4: Allow Nginx Through Firewall
# Allow HTTP (port 80)
sudo ufw allow 'Nginx Full'
# Or if you don't have UFW, allow the ports manually
sudo ufw allow 80
sudo ufw allow 443Testing Your Installation
After installation, you can test if Nginx is working:
- Visit your server's IP address in a web browser
- You should see the Nginx welcome page
Or test from the command line:
curl http://localhostSetting Up Your First Website
Step 1: Create Your Website Files
# Create a directory for your website
sudo mkdir -p /var/www/mywebsite
# Create a simple HTML file
sudo nano /var/www/mywebsite/index.htmlAdd this content to the file:
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<h1>Welcome to My Website!</h1>
<p>This is my first website on my VPS.</p>
</body>
</html>Step 2: Set Proper Permissions
# Give ownership to the web server user
sudo chown -R www-data:www-data /var/www/mywebsite
# Set proper permissions
sudo chmod -R 755 /var/www/mywebsiteStep 3: Create Nginx Configuration
sudo nano /etc/nginx/sites-available/mywebsiteAdd this configuration:
server {
listen 80;
server_name your-domain.com www.your-domain.com;
root /var/www/mywebsite;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}Step 4: Enable Your Website
# Create a link to enable the site
sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/
# Remove the default site (optional)
sudo rm /etc/nginx/sites-enabled/default
# Test the configuration
sudo nginx -t
# Restart Nginx
sudo systemctl restart nginxSetting Up Multiple Websites
You can host many websites on one VPS. Just create a new configuration for each:
Website 1: mywebsite.com
server {
listen 80;
server_name mywebsite.com www.mywebsite.com;
root /var/www/mywebsite;
index index.html;
}Website 2: anothersite.com
server {
listen 80;
server_name anothersite.com www.anothersite.com;
root /var/www/anothersite;
index index.html;
}Common Nginx Commands
# Check Nginx status
sudo systemctl status nginx
# Start Nginx
sudo systemctl start nginx
# Stop Nginx
sudo systemctl stop nginx
# Restart Nginx
sudo systemctl restart nginx
# Reload configuration (without stopping)
sudo systemctl reload nginx
# Test configuration
sudo nginx -t
# View Nginx error logs
sudo tail -f /var/log/nginx/error.log
# View Nginx access logs
sudo tail -f /var/log/nginx/access.logPerformance Tips
Enable Gzip Compression
Edit the main Nginx configuration:
sudo nano /etc/nginx/nginx.confAdd this inside the http block:
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;Set Up Browser Caching
Add this to your server block:
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}Troubleshooting
Problem: "502 Bad Gateway"
Solution: Check if your application is running and the port is correct.
Problem: "403 Forbidden"
Solution: Check file permissions and ownership:
sudo chown -R www-data:www-data /var/www/yourwebsite
sudo chmod -R 755 /var/www/yourwebsiteProblem: "404 Not Found"
Solution: Check that your root path in Nginx config is correct.
Problem: Nginx won't start
Solution: Check the configuration:
sudo nginx -tSecurity Tips
- Keep Nginx updated - Regular updates fix security holes
- Use HTTPS - Set up SSL certificates for all websites
- Hide Nginx version - Add
server_tokens off;to your config - Limit file uploads - Set
client_max_body_sizeif needed
Great Job!
Your Nginx web server is now running! You can host multiple websites and they will load fast for your visitors.