Installation
Connect over SSH asroot: Connection. The commands are for Ubuntu/Debian, as a regular user add sudo.
-
Package – the service starts by itself:
-
Check – open
http://your-server-IPin a browser, the “Welcome to nginx!” page should appear. It doesn’t open – port 80 is most likely closed: Firewall.
apt install apache2. Same principle, different configs and syntax.
Static site
/etc/nginx/sites-available/– configs of all sites./etc/nginx/sites-enabled/– symlinks to the enabled sites.
-
Site files:
-
Config – create the file
/etc/nginx/sites-available/site:/etc/nginx/sites-available/site -
Enable – symlink, syntax check, and apply without dropping connections:
Always run
nginx -tbeforereload. If the config has an error,reloadwon’t apply it and the site keeps running on the old configuration.
default config intercepts requests. Remove its symlink and reload again: rm /etc/nginx/sites-enabled/default.
PHP
PHP-FPM lets Nginx handle.php: WordPress, scripts.
-
PHP-FPM:
-
Socket – its name depends on the PHP version, for example
php8.3-fpm.sock: -
Handler – add the block inside
server { ... }and put in your socket name. With a wrong name Nginx returns a 502 error: -
Apply:
-
Check – create
/var/www/site/info.phpwith the contents<?php phpinfo();and openhttp://example.com/info.php: a table of PHP information appears. Then delete the file, it exposes the server configuration.
HTTPS
A free Let’s Encrypt certificate is issued by thecertbot tool: SSL and Let’s Encrypt. For the site to be reachable from outside, open ports 80 (HTTP) and 443 (HTTPS) in the firewall: Firewall.
Next: SSL and Let’s Encrypt, Server hardening.