Choosing a Server Distribution
For servers, stability and long-term support are paramount. The most popular choices are:
- Ubuntu Server LTS — 5-year support, massive community, excellent documentation. The most popular choice for cloud deployments and VPS hosting.
- Debian Stable — Ultra-stable, thoroughly tested packages. Ideal for servers that need to run without intervention for years.
- Rocky Linux / AlmaLinux — RHEL-compatible enterprise distributions with 10-year support cycles. Perfect for enterprise environments.
- Alpine Linux — Ultra-minimal footprint, ideal for containers and microservices where every megabyte matters.
Initial Server Setup
After provisioning a fresh server (VPS, dedicated, or local), follow these essential first steps to create a secure foundation.
1. Update the System
sudo apt update && sudo apt upgrade -y
# Rocky/Alma/RHEL
sudo dnf update -y
2. Create a Non-Root User
Never operate your server as the root user. Create a dedicated admin account with sudo privileges:
usermod -aG sudo deployer
# Switch to the new user
su - deployer
3. Configure SSH Key Authentication
Password authentication is vulnerable to brute-force attacks. Set up SSH keys for secure, passwordless login:
ssh-keygen -t ed25519 -C "[email protected]"
# Copy the public key to the server
ssh-copy-id deployer@your-server-ip
# On the server, disable password authentication
sudo nano /etc/ssh/sshd_config
# Set: PasswordAuthentication no
sudo systemctl restart sshd
4. Configure the Firewall
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# Enable the firewall
sudo ufw enable
sudo ufw status
Web Server Setup
The two most popular web servers on Linux are Nginx and Apache. Nginx excels at serving static content and acting as a reverse proxy, while Apache offers powerful module support and per-directory configuration.
Installing Nginx
sudo systemctl enable nginx
sudo systemctl start nginx
# Verify it is running
sudo systemctl status nginx
curl http://localhost
Nginx Server Block Configuration
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
# Enable the site
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
SSL with Let's Encrypt
Secure your site with a free SSL certificate using Certbot:
sudo certbot --nginx -d example.com -d www.example.com
# Auto-renewal is configured automatically
sudo certbot renew --dry-run
Database Servers
MariaDB / MySQL
sudo mysql_secure_installation
# Create a database and user
sudo mysql -u root -p
CREATE DATABASE myapp;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON myapp.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;
PostgreSQL
sudo systemctl enable postgresql
# Create a user and database
sudo -u postgres createuser --interactive
sudo -u postgres createdb myapp
Process Management with Systemd
Systemd is the init system and service manager used by most modern Linux distributions. It manages services, timers, and the boot process.
sudo systemctl start nginx # Start a service
sudo systemctl stop nginx # Stop a service
sudo systemctl restart nginx # Restart a service
sudo systemctl enable nginx # Start on boot
sudo systemctl status nginx # Check status
journalctl -u nginx -f # View live logs
Creating a Custom Service
[Unit]
Description=My Application
After=network.target
[Service]
Type=simple
User=deployer
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/start.sh
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Monitoring and Maintenance
📊 Resource Monitoring
Use htop for real-time process monitoring, df -h for disk usage, and free -h for memory. For long-term monitoring, consider tools like Prometheus and Grafana.
🗃 Automated Backups
Schedule regular backups using cron jobs and tools like rsync, borgbackup, or restic. Always follow the 3-2-1 backup rule: 3 copies, 2 media types, 1 offsite.
🕒 Unattended Updates
Configure automatic security updates to ensure critical patches are applied promptly. On Ubuntu, install unattended-upgrades and enable automatic security updates.
Automated Backups with Cron
crontab -e
# Daily backup at 2 AM
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
# Weekly database dump on Sunday at 3 AM
0 3 * * 0 mysqldump -u root myapp | gzip > /backups/myapp_$(date +\%F).sql.gz
Docker and Containers
Containers have revolutionized server deployment. Docker allows you to package applications with all their dependencies into portable, isolated units.
curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker deployer
# Run a container
docker run -d -p 80:80 --name webserver nginx
# Docker Compose for multi-container apps
docker compose up -d
Secure your server setup next.
Linux Security Guide →