This page puts a website on your server with the web server Nginx and a free HTTPS certificate from Let's Encrypt. The same setup also puts a program that runs on your server, for example on port 8000, behind your domain with HTTPS. Compared with Ubuntu, three things are different: the settings files are in another folder, Nginx does not start by itself after the installation, and SELinux decides which files and ports Nginx may use. The steps were tested on Rocky Linux 9 and AlmaLinux 10 with a real certificate, and the SELinux parts also on a dedicated server with AlmaLinux 9; run the commands as root.
On our AlmaLinux 9 server the installer had switched SELinux off (getenforce answers Disabled). The SELinux commands on this page then change nothing and give no error; run them anyway, so that everything keeps working if you switch SELinux on later (SELinux on AlmaLinux and Rocky Linux).
You need a domain (or subdomain) whose A record points to your server's IP address. Below it is called example.com; use your own everywhere.
1. Install Nginx and Open the Firewall
dnf install nginx systemctl enable --now nginx firewall-cmd --permanent --add-service=http --add-service=https firewall-cmd --reload
The second line starts Nginx now and after every restart. The last two open ports 80 and 443 in firewalld; leave them out if you do not use it (Protect Your AlmaLinux or Rocky Linux Server With firewalld and Fail2ban). http://YOUR.SERVER.IP now shows the test page of Nginx.
2. Create the Website
Put your files in a folder under /var/www, here a test page:
mkdir -p /var/www/mysite echo "<h1>Hello from my server</h1>" > /var/www/mysite/index.html restorecon -Rv /var/www/mysite
The last line gives the files the SELinux label for web content. Without it, Nginx answers 403 Forbidden: the new folder has the label var_t, which Nginx may not read. restorecon prints Relabeled … to …httpd_sys_content_t, and the page works.
Nginx reads every file that ends in .conf in /etc/nginx/conf.d/. Create the file /etc/nginx/conf.d/mysite.conf:
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/mysite;
index index.html;
}
Check the settings and load them:
nginx -t systemctl reload nginx
nginx -t must answer test is successful. Open http://example.com: you see the test page.
A file moved with mv keeps its old label. For example, a page moved from /root into /var/www/mysite keeps the label admin_home_t and gives 403 Forbidden until you run restorecon -Rv /var/www/mysite again. Run it after every upload of new files. A website folder outside /var/www, for example /srv/mysite, first needs its own rule: semanage fcontext -a -t httpd_sys_content_t "/srv/mysite(/.*)?", then restorecon -Rv /srv/mysite. See SELinux on AlmaLinux and Rocky Linux.
3. Get the HTTPS Certificate
Certbot comes from EPEL, an extra package source (see Install and Update Software on AlmaLinux and Rocky Linux):
dnf install epel-release dnf install certbot python3-certbot-nginx certbot --nginx -d example.com --redirect
The first time, Certbot asks for your e-mail address (for warnings about expiring certificates) and for your agreement to the Let's Encrypt terms. It then gets the certificate, adds it to mysite.conf and sends all http:// visits to https://. At the end it reports Congratulations! You have successfully enabled HTTPS.
The certificate is valid for 90 days. After the installation, the renewal timer certbot-renew.timer is already switched on. To test the renewal:
certbot renew --dry-run
The answer ends with all simulated renewals succeeded.
Put a Program Behind HTTPS
If a program on your server answers on a port, for example 8000, Nginx can pass the visits on to it. In /etc/nginx/conf.d/mysite.conf, replace the lines root … and index … with:
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
SELinux does not let Nginx connect to other programs until you allow it once:
setsebool -P httpd_can_network_connect 1 nginx -t systemctl reload nginx
Without the first line, the website answers 502 Bad Gateway, and /var/log/nginx/error.log says connect() to 127.0.0.1:8000 failed (13: Permission denied). With it, the program's page appears. -P keeps the setting after a restart. The program's port no longer needs to be open in the firewall, because Nginx reaches it on the server itself.
Check that the domain points to the server (ping example.com shows your server's address) and that ports 80 and 443 are open in firewalld (firewall-cmd --list-services shows http https). Let's Encrypt has to reach your server on port 80 to issue the certificate.
The same steps on Ubuntu: Website With HTTPS on a Linux Server: Nginx and Let's Encrypt.