Tips on managing a server

2022-10-20

Learned some stuff while maintaining a VPS that hosts newsbyside.com.


Disclaimer that i'm not a sysadmin, probably didn't do some stuff correctly, blah blah blah.




lazydocker

Very helpful tool, helps a lot when you need to overview all of the docker stuff.

Don't dockerize nginx

I'm not an expert on docker, so dockerizing nginx and connecting it to other running images was quite problematic. Spent like 2 days trying to do it, with no progress. Figured out that you can just run it without docker. duh. Automatic SSL Certs can be setup with certbot. There are some great tutorials on youtube that explain the process in more detail.

Caching specific files for a reverse proxy in nginx

If you want to cache some of the static frontend files with nginx, you can configure the nginx config in the following way. Add the lower location ~* \.(?:css| block in the frontend block and edit it to match your settings (after generating the SSL Certs, by running certbot).

# frontend block
location / {
    proxy_pass http://localhost:3000;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $server_name;
    
    # specify what type of file you want to cache
    location ~* \.(?:css|js|svg|ico|ttf)$ {
        # specify for how long you want to cache them
        expires 10d;
        add_header Vary Accept-Encoding;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        add_header Cache-Control private;
        proxy_pass http://localhost:3000;
    }

        ...

Analytics & Goaccess

Frontend analytics scripts are great, but I have a feeling that they get blocked more often than not, resulting in bad analytics.


One of the best alternatives for this is a tool called goaccess. It uses logs and converts them into extensive analytics about the requests to the server. The tool also allows you to export the logs into a single html file, which is great for backups.

# specify which files to concat together
# and export them to an html file.
# The name of the html file can be a date, 
# to version control stuff easier.
sudo cat                            \
    /var/log/nginx/*.log            \
    /var/log/nginx/*.log.1          \
    /var/log/nginx/*.log.*  |       \
goaccess --log-format=COMBINED > 2022-10-18.html

Full analytics about the requests to the server, no annoying GDPR cookies. Simple and informative UI for the data. 10/10 tool.

Why do nginx logs dissapear?

If you notice that the nginx logs dissapear / reset after some time, one of the reasons for this might be logrotate. Check if there's a /etc/logrotate.conf file.


If it exists, there's a /etc/logrotate.d dir, which holds custom config files for each thing that can log events on the server. Read up more on how to configure /etc/logrotate.d/nginx to perserve logs for a longer time.

VSCode ssh extension

If you're not comfortable in the terminal, and need to change the code of the apps a lot to debug the setup, VSCode has an ssh extension that can be a big timesaver.

Bash script to SSH with password automatically

If you're using a password to connect to the server, pasting it everytime in the terminal starts to get tedious at some point. To ssh into the server automatically, you can use this script

connect.sh

#!/usr/bin/expect

# chmod +x ./connect.sh
spawn ssh MY_SERVER
expect "password"
send "MY_PASSWORD\r"
interact