Linux: Web Servers and Core Services
Deploy and manage production Internet services on Linux. Master Nginx reverse proxy architecture, Apache virtual hosts, automated TLS certificates with Certbot, relational database operations, and storage protocols like NFS and Samba.
13.1 Nginx Web Server and Reverse Proxy
Nginx is an event-driven, asynchronous HTTP web server and reverse proxy renowned for its low memory footprint and high concurrency under heavy traffic loads.
13.1.1 Installation and Daemon Management
Install the official Nginx packages and manage configuration updates without dropping active client TCP connections:
Nginx Service Administration
- Debian / Ubuntu Installation:Execute
sudo apt update && sudo apt install -y nginx. - RHEL / Rocky Installation:Execute
sudo dnf install -y nginx && sudo systemctl enable --now nginx. - Syntax Validation (nginx -t):Always test configuration files before reloading:
sudo nginx -t. - Zero-Downtime Reload (systemctl reload nginx):Instructs worker processes to finish in-flight requests while spawning new workers with the updated configuration.
13.1.2 Server Blocks and Static Sites
Virtual server blocks allow a single Nginx instance to host multiple distinct domain names on the same IP address:
Server Block File Conventions
- /etc/nginx/sites-available/:Directory containing all server block definition files.
- /etc/nginx/sites-enabled/:Directory containing symbolic links to activated sites. Nginx includes all files in this directory via
include /etc/nginx/sites-enabled/*;. - Symlink Activation:Enable a site with
sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/. - Single Page Application Routing (try_files):Use
try_files $uri $uri/ /index.html;to route client-side navigation to the root HTML document.
13.1.3 Configuring Nginx as a Reverse Proxy
In modern production deployments, Nginx terminates public client TLS connections and forwards requests to upstream application runtimes (Node.js, Python Gunicorn, Go, or Java) while forwarding client identity headers:
Configure and enable the Nginx site
Write the site config, enable it, test, then reload.
# Write the config file
sudo tee /etc/nginx/sites-available/app.example.com > /dev/null <<'CONF'
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
# Forward real client identifiers to backend service
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
}
CONF
# Enable site configuration and apply changes
sudo ln -s /etc/nginx/sites-available/app.example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx13.2 Apache HTTP Server
The Apache HTTP Server is a modular web server supporting multi-processing modules (MPMs), dynamic directory configurations via .htaccess, and an ecosystem of modules.
13.2.1 Comparing Nginx and Apache Architecture
| Architectural Attribute | Nginx | Apache HTTP Server |
|---|---|---|
| Concurrency Model | Asynchronous event-driven worker loop | Process-per-connection or thread-pool (MPM event / worker) |
| Memory Footprint | Low and predictable under high connection counts | Scales proportionally with active thread and process counts |
| Directory Overrides | None (centralized configuration files only) | Decentralized per-directory .htaccess files |
| Primary Use Case | Reverse proxy, SSL termination, static file delivery | Dynamic application hosting (PHP, legacy CMS environments) |
13.2.2 Virtual Hosts, Modules, and .htaccess
Debian-based distributions provide dedicated command-line utilities to manage Apache virtual hosts and modules:
Apache Management Utilities
- a2ensite / a2dissite:Enables or disables a virtual host configuration file by managing symlinks inside
/etc/apache2/sites-enabled/. - a2enmod / a2dismod:Enables or disables modules:
sudo a2enmod rewrite ssl headers. - AllowOverride All:Directive inside
<Directory>that permits local.htaccessfiles to override routing rules via mod_rewrite. - Syntax Testing (apachectl configtest):Validates configuration syntax before reloading the daemon.
Configure and enable the Apache virtual host
Write the virtual host, enable it, test, then reload.
# Write the config file
sudo tee /etc/apache2/sites-available/example.conf > /dev/null <<'CONF'
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example/html
<Directory /var/www/example/html>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example_error.log
CustomLog ${APACHE_LOG_DIR}/example_access.log combined
</VirtualHost>
CONF
# Enable virtual host and reload service
sudo a2ensite example.conf
sudo apachectl configtest && sudo systemctl reload apache213.3 HTTPS and Certificates
Transport Layer Security (TLS/SSL) protects user data from interception and tampering. The Let's Encrypt certificate authority provides free, automated X.509 certificates managed through the EFF's Certbot client.
13.3.1 Provisioning Certificates with Certbot
Certbot ACME Workflow
- ACME Challenge Validation:Certbot answers an automated HTTP-01 challenge by placing a temporary cryptographic token in
.well-known/acme-challenge/to verify domain control. - Automated Nginx Configuration:Run
sudo certbot --nginx -d example.com -d www.example.comto issue the certificate and automatically inject SSL directives into your server block. - Standalone Mode:Run
sudo certbot certonly --standalone -d mail.example.comwhen no web server is running on port 80.
13.3.2 Auto-Renewal and Testing
Let's Encrypt certificates expire every 90 days. Certbot registers an automated systemd timer to renew certificates nearing expiration:
Renewal Management
- Dry-Run Simulation: certbot renew --dry-run:Simulates the complete renewal process against the Let's Encrypt staging environment without altering live production keys.
- Systemd Timer (certbot.timer):Checks expiration twice daily and renews certificates within 30 days of expiry:
systemctl list-timers | grep certbot. - Certificate Storage Path:Production certificates are saved in
/etc/letsencrypt/live/<domain>/fullchain.pemwith private keys inprivkey.pem.
13.4 Databases on Linux
Linux powers relational database engines. Administering databases requires installing the service daemon, creating dedicated application users with minimal privileges, and automating backups.
13.4.1 Comparing PostgreSQL and MySQL / MariaDB
| Database Engine | Default Port | CLI Console | Backup Tool | Authentication Model |
|---|---|---|---|---|
| PostgreSQL | 5432 | psql -U postgres | pg_dump | Ident / Peer authentication for local system accounts |
| MySQL / MariaDB | 3306 | mysql -u root -p | mysqldump | auth_socket / native password authentication |
13.4.2 User Provisioning and Automated Backups
Database Backup Strategies
- PostgreSQL Logical Dump:
pg_dump -U postgres app_db | gzip > /backup/app_db_$(date +%F).sql.gz. - PostgreSQL Restore:
gunzip -c /backup/app_db.sql.gz | psql -U postgres app_db. - MySQL Logical Dump:
mysqldump -u root -p --single-transaction app_db > /backup/app_db.sql. - MySQL Restore:
mysql -u root -p app_db < /backup/app_db.sql.
Database setup
Create a PostgreSQL database and user, then harden MariaDB.
# PostgreSQL: Create database and application user sudo -u postgres psql -c "CREATE DATABASE prod_db;" sudo -u postgres psql -c "CREATE USER appuser WITH ENCRYPTED PASSWORD 'secret';" sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE prod_db TO appuser;" # MariaDB: Run baseline security hardening script sudo mysql_secure_installation
13.5 Other Common Infrastructure Services
Enterprise Linux fleets rely on network infrastructure services to synchronize system clocks, export shared network storage, and resolve internal domain records.
13.5.1 Network File Sharing: NFS and Samba
| Protocol | Configuration File | Target Operating Systems | Mount Command |
|---|---|---|---|
| NFS (v4) | /etc/exports | Linux / Unix-to-Unix high performance | mount -t nfs 10.0.0.5:/srv/share /mnt |
| Samba (SMB) | /etc/samba/smb.conf | Windows, macOS, and Linux cross-platform | mount -t cifs //10.0.0.5/share /mnt |
NFS export and mount
Export the shares, then mount from a client.
# NFS Server setup in /etc/exports: # /srv/shared_data 192.168.1.0/24(rw,sync,no_subtree_check) # Export all shares defined in /etc/exports sudo exportfs -arv # Client mount command sudo mount -t nfs 192.168.1.10:/srv/shared_data /mnt/nfs
13.5.2 Mail and DNS Server Roles
Core Daemon Roles
- Mail Transfer Agent (MTA):Postfix or Exim transmits outbound emails and receives incoming messages over port 25.
- Mail Delivery Agent (MDA):Dovecot stores delivered emails into Maildir formats and serves them over secure IMAP (993) or POP3 (995).
- Authoritative & Recursive DNS:BIND9 (
named) or Unbound translates hostnames to IP addresses and manages local zone records.
13.5.3 Time Synchronization with NTP and Chrony
Synchronized clocks prevent authentication failures (TLS certificate validation, Kerberos, TOTP) and maintain accurate audit trail timelines:
Chrony Clock Synchronization
- timedatectl status:Displays system time, RTC clock state, active timezone, and NTP service synchronization status.
- chronyc tracking:Shows system time offset, reference clock identity, and frequency drift.
- chronyc sources -v:Displays detailed connection statistics for upstream NTP time servers.
13.6 Interactive Lab: Web and Port Inspection
Practice checking system time synchronization, reviewing network sockets, and inspecting service configuration files with the commands below. Press Load in Terminal to open them in the Linux terminal:
Web and Port Inspection
Load these in the terminal, then press Enter to run them.
$ date$ timedatectl status || echo "No systemd daemon in container"$ ss -tuln$ curl --version
Knowledge Check: Module 13 Web Servers and Core Services
1. Which command verifies Nginx configuration files for syntax errors before reloading the daemon?
2. In an Nginx server block, which directive forwards client requests to an upstream backend application?
3. Which utility enables a virtual host site configuration file on Apache in Debian-based distributions?
4. Which command tests the automated renewal process of Let's Encrypt certificates without altering live keys?
5. Which command-line tool generates a logical SQL dump backup of a PostgreSQL database?
6. Which configuration file defines shared directory exports for the Network File System (NFS) daemon?