Installing Software and Running Services
Servers need software installed and kept updated, and apps need to run reliably in the background and restart after crashes or reboots. apt and systemd handle both, and are the standard tools on Ubuntu servers.
Package managers
On Ubuntu and Debian, apt installs software from trusted servers, much like an app store:
Example
sudo apt update # refresh the list of available versions
sudo apt upgrade # install updates (do this regularly - security fixes!)
sudo apt install nginx git # install programs
sudo apt remove nginx # uninstall
apt list --installed | grep nginxOther systems use dnf (Fedora, Amazon Linux 2023, Rocky) - the commands are similar.
systemd services
A service is a program Linux keeps running in the background - a web server, a database, your API. systemctl controls them:
Example
sudo systemctl status nginx # is it running? recent log lines
sudo systemctl restart nginx # restart after changing settings
sudo systemctl enable nginx # start automatically when the server boots
journalctl -u nginx -n 50 # last 50 log lines of the service
journalctl -u nginx -f # follow the log liveYour own app as a service
Example
# /etc/systemd/system/shop-api.service
[Unit]
Description=Shop API
After=network.target
[Service]
User=shop
WorkingDirectory=/opt/shop-api
EnvironmentFile=/opt/shop-api/.env
ExecStart=/usr/bin/node server.js
Restart=always
[Install]
WantedBy=multi-user.targetExample
sudo systemctl daemon-reload # read the new file
sudo systemctl enable --now shop-api # start now and on every bootRestart=always brings your app back if it crashes. Run apps as their own limited user (here shop), never as root.Key points
- apt install / update / upgrade manage software.
- systemctl starts, stops and enables services.
- journalctl shows service logs.
📝 Test yourself
1. Which command installs software on Ubuntu?
2. What does systemctl enable do?
3. Fill in the command that shows a service's logs:
-u nginx -n 50
