Ravindra BagaleCourses & study guides

8. Hosting a Static Website and Changing the Configuration

8.4 Ubuntu + Nginx

sudo service apache2 stop 2>/dev/null
sudo systemctl disable apache2 2>/dev/null
sudo apt update && sudo apt install -y nginx

sudo tee /etc/nginx/sites-available/mysite > /dev/null <<'EOF'
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    root /var/www/mysite;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;

    access_log /var/log/nginx/mysite_access.log;
    error_log  /var/log/nginx/mysite_error.log;
}
EOF

sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/mysite
sudo rm -f /etc/nginx/sites-enabled/default          # disable the Ubuntu welcome site
sudo chown -R www-data:www-data /var/www/mysite       # optional; 644/755 is enough to read
sudo nginx -t && sudo service nginx reload
curl -s http://localhost | head -5

Ravindra Bagale's Tip

Ubuntu var sites-available madhe file banavli pan sites-enabled madhe symlink banavla nahi – mhanun site chalat nahi. Khup students hi step visartat. ls -l /etc/nginx/sites-enabled/ ne symlink aahe ka te nehmi check kara.

Lab

Host mysite with Nginx on Ubuntu, disable the default site, and confirm with curl -s http://localhost | head -5.