sudo apt update && sudo apt upgrade -y
Ubuntu’s default repo already includes Nginx.
sudo apt install nginx -y
Check status:
systemctl status nginx
You should see active (running).
If UFW is enabled:
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status
This opens 80 (HTTP) and 443 (HTTPS).
Open browser:
Code
http://your_server_ip
You should see the default Nginx welcome page.
Example for domain: example.com
sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com
Add a test file:
nano /var/www/example.com/html/index.html
Paste:
Welcome to example.com
sudo nano /etc/nginx/sites-available/example.com
Add:
nginx
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Enable it:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Disable default site (optional):
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
If OK:
sudo systemctl reload nginx
Install Certbot:
sudo apt install certbot python3-certbot-nginx -y
Run:
sudo certbot --nginx -d example.com -d www.example.com
Auto-renew test:
sudo certbot renew --dry-run
If you want to proxy to Node.js, Python, PHP-FPM, etc.:
nginx
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
Reload:
sudo systemctl reload nginx