How to Install and Configure Nginx on Ubuntu
Nginx is a fast, lightweight web server and reverse proxy. This guide covers installing it on Ubuntu and setting up a basic website server block.
1. Install Nginx
sudo apt update
sudo apt install -y nginx2. Start and enable the service
sudo systemctl enable --now nginx
sudo systemctl status nginx3. Allow HTTP/HTTPS through the firewall
sudo ufw allow 'Nginx Full'
sudo ufw reload4. Create a server block for your site
Create a config file for your domain:
sudo nano /etc/nginx/sites-available/example.comPaste the following, replacing example.com with your domain:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public;
index index.html index.php;
location / {
try_files $uri $uri/ =404;
}
}5. Enable the site and reload
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginxYour site is now served by Nginx. For HTTPS, install a free Let’s Encrypt certificate with sudo apt install certbot python3-certbot-nginx and run sudo certbot --nginx.