Chapter 7: Static Website Hosting (3 OSes × 2 Web Servers)
7.8 Combination 3 — 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