How to install Nginx
Need a fast web server? Nginx is perfect for your VPS. It's lightweight, handles lots of traffic, and is easy to configure. Let's get it running with multiple websites.
Step 1: Update System
# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y
# CentOS/RHEL
sudo yum update -yStep 2: Install Nginx
Ubuntu/Debian:
sudo apt install nginx -yCentOS/RHEL 8+:
sudo dnf install nginx -yCentOS/RHEL 7:
sudo yum install nginx -yStep 3: Start and Enable
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginxShould show "active (running)".
Step 4: Configure Firewall
Allow HTTP and HTTPS:
Ubuntu/Debian (UFW):
CentOS/RHEL (firewalld):
Step 5: Test Installation
Visit in browser:
You should see the Nginx welcome page.
Step 6: Nginx Directory Structure
Important directories:
/etc/nginx/- Configuration files/var/www/html/- Default web root/var/log/nginx/- Log files/usr/share/nginx/html/- Default files (CentOS)
Step 7: Create Your First Website
1. Create website directory:
2. Set permissions:
3. Create test page:
Add:
Step 8: Create Virtual Host (Server Block)
Ubuntu/Debian:
CentOS/RHEL:
Add this configuration:
Enable site (Ubuntu/Debian only):
Step 9: Test and Reload
Step 10: Test Your Website
Visit http://your-server-ip (or your domain if DNS is set).
Step 11: Basic Performance Tuning
Edit main config:
Inside http block, add:
Restart:
Step 12: Set Up PHP with Nginx (Optional)
Install PHP-FPM:
Configure Nginx for PHP:
Edit your site config, add:
Test PHP:
Visit http://your-server-ip/info.php (remove after testing!).
Common Issues
"Nginx won't start"
"403 Forbidden"
"502 Bad Gateway"
Useful Commands
Check Nginx version:
Reload configuration (no downtime):
Check active connections:
View access logs:
Security Tips
Remove default Nginx page
Hide Nginx version in headers
Set up proper file permissions
Regular updates:
sudo apt upgrade nginx
That's it! Your Nginx server is ready to host websites. 🎉
Last updated