Install Nginx on Centos9 Stream

Install Nginx on Centos9 Stream

Tue 09 July 2024

I had to upgrade my webserver to Centos9 Stream, this is my documentation.

Motivation

Due to Centos7 is end-of-life I had to create a new DigitalOcean droplet to run my website on. I also use the opportunity to swap apache with nginx just to try something new.

Install nginx webserver

First I install nginx package and create a 'vhost'.

# yum install nginx

The 'vhost' configuration file has some cache-control added for efficiency:

server {
        server_name ahost;
        root /usr/share/nginx/ahost;
        index index.html;

        # Media: images, icons, video, audio, HTC
        location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|mp4|ogg|ogv|webm|htc)$ {
          add_header Cache-Control "max-age=2592000";
          access_log off;
        }
        # CSS and Javascript
        location ~* \.(?:css|js)$ {
          add_header Cache-Control "max-age=31536000";
          access_log off;
        }

The 'nginx' configuration file has some gzip-control added for efficiency:

http {
...
    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types
      text/plain
      text/css
      application/json
      application/x-javascript
      text/xml
      application/xml
      application/xml+rss
      text/javascript;
...
}

Install certbot for the https automation

To be able to get automated certificate I need to install certbot from EPEL. But my droplet has just 1GB of memory so I have to create a swapfile beforehand to be able to use the EPEL repository.

# dd if=/dev/zero of=/swapfile bs=1024 count=1048576
# chmod 600 /swapfile
# mkswap /swapfile
# swapon /swapfile

Check that swap is as expected:

# swapon --show
# free -h

Add the new swap to '/etc/fstab' so it survives reboot:

# echo '/swapfile swap swap defaults 0 0' >> /etc/fstab

Now we can install needed packages for certbot:

# dnf install epel-release -y
# dnf install python-certbot-nginx

And that's about it.

Tagged as : linux centos nginx