Hosting a Webpage with Nginx
Setting up Nginx
-
Install ngingx:
apt install nginx
-
Enable and start the systemd service:
systemctl enable nginx.service systemctl start nginx.service
-
Configure and enable the webpage: Under /etc/nginx/ should be two directories listed.
sites-available and sites-enabled.
sites-available contains config files for exisitng webpages, while sites-enabled contains softlinks to config files in sites-available.- Create a config file (e.g. example.com.conf) in sites-available with the following content:
server { listen 80; server_name example.com; root path/to/index.html; index index.html; location / { index index.html index.html; } }
Make sure to check the nginx documentationThis config is really basic. There is much more to be configured. - Create the softlink in sites-enabled:
cd /etc/nginx/site-enabled ln -s /etc/nginx/sites-available/example.com.conf example.com.conf
- Reload config:
nginx -s reload
- Create a config file (e.g. example.com.conf) in sites-available with the following content:
HTTPS
I suggest using Let's Encrypt for this.
Basic Authentication
- Install apache2-utils
- Generate a new password file
sudo htpasswd -c /etc/nginx/.htpasswd admin
- Turn on basic http authentication
location /admin { try_files $uri $uri/ =404; auth_basic "Restricted Content"; auth_basic_user_file /etc/nginx/.htpasswd; }