This guide explains how to:
- Install Nginx on Ubuntu
- Understand the Nginx directory structure
- Create and configure websites
- Enable and disable sites
- Configure reverse proxies
- Enable HTTPS with Let's Encrypt
- Troubleshoot common issues
# Update package information
sudo apt update
# Upgrade installed packages
sudo apt upgrade -y# Install Nginx
sudo apt install nginx -yVerify installation:
# Check service status
sudo systemctl status nginxStart and enable automatic startup:
# Start Nginx service
sudo systemctl start nginx
# Enable startup on boot
sudo systemctl enable nginxIf UFW is enabled:
# Allow HTTP and HTTPS traffic
sudo ufw allow 'Nginx Full'
# Reload firewall rules
sudo ufw reloadCheck firewall status:
sudo ufw status/etc/nginx/
├── nginx.conf # Main configuration file
├── sites-available/ # All website configurations
├── sites-enabled/ # Enabled websites (symlinks)
├── conf.d/ # Additional configuration files
├── snippets/ # Reusable configuration snippets
├── mime.types # MIME type mappings
└── modules-enabled/ # Enabled modules
Client Browser
│
▼
Port 80 / 443
│
▼
Nginx Master Process
│
▼
Worker Processes
│
▼
Match server_name
│
├── Serve static files
│
└── Reverse proxy to backend
# Create website root directory
sudo mkdir -p /var/www/mysiteSet ownership:
# Give current user ownership
sudo chown -R $USER:$USER /var/www/mysiteSet permissions:
# Owner: read/write/execute
# Others: read/execute
sudo chmod -R 755 /var/www/mysiteCreate index.html:
nano /var/www/mysite/index.htmlExample:
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<h1>Welcome to Nginx</h1>
<p>Website deployment successful.</p>
</body>
</html>Save:
CTRL + O
ENTER
CTRL + X
Create:
sudo nano /etc/nginx/sites-available/mysiteExample configuration:
server {
# Listen on HTTP port
listen 80;
# Domain names
server_name mysite.com www.mysite.com;
# Website root folder
root /var/www/mysite;
# Default files
index index.html index.htm;
# Access logs
access_log /var/log/nginx/mysite-access.log;
# Error logs
error_log /var/log/nginx/mysite-error.log;
location / {
# Check file, directory, or return 404
try_files $uri $uri/ =404;
}
}Create a symbolic link:
sudo ln -s \
/etc/nginx/sites-available/mysite \
/etc/nginx/sites-enabled/Nginx loads only configurations present inside:
/etc/nginx/sites-enabled/
Remove Ubuntu's default site:
sudo rm /etc/nginx/sites-enabled/defaultAlways test before reloading:
sudo nginx -tExpected output:
nginx: configuration file test is successful
sudo systemctl reload nginxDifference:
reload = Apply changes without downtime
restart = Stop and start service
Example:
sites-available/
├── blog
├── shop
└── api
Enabled:
sites-enabled/
blog -> ../sites-available/blog
shop -> ../sites-available/shop
api -> ../sites-available/api
Request routing:
blog.example.com
│
▼
server_name blog.example.com
│
▼
/var/www/blog
Suppose an application runs on:
127.0.0.1:3000
Nginx configuration:
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}Workflow:
Browser
│
▼
Nginx
│
▼
Node.js / Python / Docker
│
▼
Response
Install Certbot:
sudo apt install certbot python3-certbot-nginx -yGenerate certificate:
sudo certbot --nginx \
-d mysite.com \
-d www.mysite.comCertbot automatically:
- Verifies ownership
- Creates certificates
- Updates Nginx configuration
- Enables HTTPS redirection
# Validate configuration
sudo nginx -t
# Reload without downtime
sudo systemctl reload nginx
# Restart service
sudo systemctl restart nginx
# Check service status
sudo systemctl status nginx
# Follow service logs
sudo journalctl -u nginx -f
# View error logs
tail -f /var/log/nginx/error.log
# View access logs
tail -f /var/log/nginx/access.logInstall Ubuntu
│
▼
Update Packages
│
▼
Install Nginx
│
▼
Configure Firewall
│
▼
Create Website Directory
│
▼
Upload HTML/CSS/JS
│
▼
Create Site Configuration
│
▼
Enable Site
│
▼
Test Configuration
│
▼
Reload Nginx
│
▼
Configure DNS
│
▼
Enable HTTPS
│
▼
Production Deployment
- Always run
nginx -tbefore reloading. - Use
reloadinstead ofrestartwhenever possible. - Keep one configuration file per website.
- Store reusable configurations inside
snippets/. - Enable HTTPS for every public website.
- Maintain separate access and error logs for each site.
- Back up
/etc/nginx/before major changes.
This document is provided for educational and operational use.