Key takeaways
- Each site gets its own server block in sites-available.
- Always run nginx -t before reloading.
- Certbot adds HTTPS and auto-renewal once DNS points to the server.
1. Install Nginx
sudo apt update
sudo apt install nginx
sudo ufw allow 'Nginx Full'
Visit your server’s IP address — you should see the Nginx welcome page.
2. Create a folder for your site
sudo mkdir -p /var/www/example.com
sudo chown -R $USER:$USER /var/www/example.com
echo '<h1>It works</h1>' > /var/www/example.com/index.html
3. Create a server block
Create /etc/nginx/sites-available/example.com:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Enable it and reload:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
4. Point DNS
Create A records for example.com and www pointing to your server’s IP.
5. Add HTTPS with Certbot
Once DNS resolves to the server:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
Certbot installs the certificate, adds the HTTPS redirect and sets up automatic renewal. Test renewal with:
sudo certbot renew --dry-run