Tutorial| Housekeeping

by allsparkinfinite on 2025-03-15

Hosting a server requires some tasks to be done regularly. Some of these can be automated.

Domain Renewals

If you have a domain name, the most obvious housekeeping task is to renew the domain name when it is up for renewal. Setting up an auto-renew is possible on most registries, but I prefer to make the payments manually when the registry sends me a 30-day warning email.

Request Routing

On NGINX, you can route requests to a specific port based on the domain they are requesting by saving the following:

server {
  server_name <domain_name>;
  location /{
    proxy_pass http://localhost:<port_number>;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $host;
  }
}

to a file in /etc/nginx/sites-enabled, and then running sudo systemctl restart nginx to make sure the changes take effect. This only needs to be done once.

Certificates

Running sudo apt-get install certbot python3-certbot-nginx installs certbot and its NGINX plugin. This only needs to be done once.
To generate and deploy certificates, run sudo certbot --nginx. Information flowing between your server and the client is now secured against anyone listening in on the conversation. This should also set up a job to renew the certificates automatically, but I recommend checking the validity of your certificates weekly by running sudo certbot certificates

Software Upgrades

Patch Upgrades And Minor Upgrades

The computer running your server requires security updates from time to time. A lot of packages on the system - many of which you may depend on - receive patch updates which don't break existing code but add enhancements such as bug fixes, performance improvements, vulnerability fixes, and (occasionally) minor features. If you are running a service which is developed by someone else - Nextcloud, for example - you will need to keep up with the updates. I recommend checking for package updates weekly. This can be automated via a cron job for most packages.

Major Upgrades

Major upgrades introduce breaking changes, and therefore require manual updates, preferably after testing them. Keep an eye out for major releases by subscribing to the software's newsletter, news feed, or social media pages. More importantly, keep an eye out for the end-of-support deadline (sometimes called end-of-life date) for the software you are running. Make sure to monitor the feedback channels for any major bugs before upgrading.

Power Cuts And System Restarts

Ideally, you have picked a server with reliable power backups. This server runs on a Raspberry Pi at my home, and my home has power backup with a battery and inverter. However, electrical work involving the inverter - or even the switchboard that the Raspberry Pi is plugged into - means that the server loses power. Upon restarting the computer, all services need to be restarted. This better be done automatically, especially if one of the services is needed for you to log in to the computer.

Bash Scripts

Anything you need to do on a Linux server can be done with a bash command. You can put these bash commands in scripts that end in .sh, and mark them as executable with chmod u+x <filename>. I recommend adding a shebang to the file - place #!/bin/bash in the first line - to specify the interpreter that runs this file. You may want to create a new user that owns this file and disallow other users from editing the file:

sudo adduser <username>
sudo chown <filename> <username>:<username>
sudo chmod go-w <filename>

Service Files

You can set the bash script to be run on startup with a service file. Create a service file in the right folder with sudo nano /lib/systemd/system/<service_name>.service and write the following in it:

[Unit]
After=network.target
Description="<Service Name>"
[Service]
ExecStart=<path/to/script>
User=<user_who_owns_the_script>
[Install]
WantedBy=multi-user.target

Now run sudo systemctl daemon-reload to load the changes, sudo systemctl start <service_name> to test it out, and then use sudo systemctl enable <service_name> to have it start up on boot automatically.