Skip to content

DevOps cheat sheet — Nginx, systemd, cron and Git hooks

The Nginx, systemd, cron, webhook and Git hook commands and config snippets that the Modern DevOps course uses, on one printable page.

Nginx

CommandWhat it does
nginx -tcheck the configuration
systemctl reload nginxapply it without dropping connections
tail -f /var/log/nginx/error.logwatch errors
upstream api { server 127.0.0.1:8000; server 127.0.0.1:8001; }

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://api;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    location /static/ { root /srv/app; expires 7d; }
    gzip on;
}

systemd service

# /etc/systemd/system/hook.service
[Unit]
Description=Webhook receiver
After=network.target

[Service]
User=deploy
WorkingDirectory=/srv/hook
ExecStart=/usr/bin/python3 server.py
Restart=on-failure

[Install]
WantedBy=multi-user.target

systemctl daemon-reload after editing · systemctl enable --now hook · journalctl -u hook -f.

cron

# m  h  dom mon dow  command
*/5  *  *   *   *    /usr/local/bin/check.sh >> /var/log/check.log 2>&1
0    2  *   *   0    /usr/local/bin/backup.sh

crontab -e edit · crontab -l list · cron has a short PATH: use full paths. Read any line in the cron explainer.

Git hooks

HookRunsUse
pre-commitbefore a commit is madelint, tests; non-zero exit refuses the commit
pre-pushbefore a pushslower tests
post-receiveon the server, after a pushdeploy
#!/usr/bin/env bash
# hooks/post-receive on a bare repository
set -euo pipefail
GIT_WORK_TREE=/srv/app git checkout -f main
cd /srv/app && ./build.sh && systemctl restart app

Make it executable: chmod +x hooks/post-receive.

Checking a service by hand

curl -fsS http://127.0.0.1:8000/health (fails on an HTTP error) · ss -tlnp | grep 8000 · systemctl is-active app.

Practise it on a real machineModern DevOps course
Open the lab