Introduction
Nginx (pronounced “engine-x”) is a high-performance web server, reverse proxy, and load balancer trusted by millions of websites. This guide covers installation and basic configuration on Linux systems. Whether you’re setting up a personal blog or production infrastructure, these steps will get you started.
Prerequisites
- A Linux system (Ubuntu/Debian or CentOS/RHEL recommended)
- Terminal access with
sudo
privileges - Active internet connection
Step 1: Install Nginx
For Ubuntu/Debian:
sudo apt update
sudo apt install nginx -y
For CentOS/RHEL:
sudo yum install epel-release -y
sudo yum install nginx -y
Step 2: Manage Nginx Service
Start and enable auto-launch on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
Verify status:
sudo systemctl status nginx # Look for "active (running)"
Step 3: Configure Firewall
Allow HTTP/HTTPS traffic:
sudo ufw allow 'Nginx Full' # Ubuntu
# OR
sudo firewall-cmd --permanent --add-service={http,https} # CentOS/RHEL
sudo firewall-cmd --reload
Step 4: Test Default Installation
- Open your browser and visit:
http://your_server_ip
- You should see the “Welcome to Nginx” landing page.
If this fails, check:
sudo nginx -t # Tests configuration syntax
Step 5: Set Up a Server Block (Virtual Host)
- Create site directory:
sudo mkdir -p /var/www/your_domain/html
- Assign ownership:
sudo chown -R $USER:$USER /var/www/your_domain/html
- Create a test page:
echo " <h1>Hello World!</h1>" | sudo tee /var/www/your_domain/html/index.html
-
Create a config file:
sudo nano /etc/nginx/sites-available/your_domain
Paste:
server { listen 80; server_name your_domain www.your_domain; root /var/www/your_domain/html; index index.html; location / { try_files $uri $uri/ =404; } }
- Enable the site:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
- Reload Nginx:
sudo systemctl reload nginx
Step 6: Verify DNS Settings
Add a local hosts entry for testing (if DNS isn’t configured):
# On your local computer (not server):
sudo nano /etc/hosts
# Add line: your_server_ip your_domain www.your_domain
Visit http://your_domain
to see your test page.
Step 7: Key Configuration Files
File | Purpose |
---|---|
/etc/nginx/nginx.conf |
Main configuration |
/etc/nginx/sites-available/ |
Site-specific configs |
/var/log/nginx/access.log |
Access logs |
/var/log/nginx/error.log |
Error logs |
Troubleshooting Tips
- Port Conflict? Ensure Apache isn’t running:
sudo systemctl stop apache2 # Ubuntu sudo systemctl stop httpd # CentOS
- Permission Issues? Run:
sudo chmod -R 755 /var/www/your_domain
- Test configs before reloading:
sudo nginx -t # Essential before every reload!
Next Steps
- Enable HTTPS with Let’s Encrypt:
sudo apt install certbot python3-certbot-nginx # Ubuntu sudo certbot --nginx
- Set Up Reverse Proxy:
location /app { proxy_pass http://localhost:3000; }
- Optimize Performance: Adjust worker processes in
/etc/nginx/nginx.conf
.
Conclusion
You’ve now installed Nginx, configured a virtual host, and served custom content. Nginx’s flexibility makes it ideal for everything from small projects to enterprise applications. Check the official Nginx documentation for advanced configurations!
> Pro Tip: Always back up config files before editing:
> sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak