Batch 4: Cloud Ansible AWS & Azure Docker & Podman Apache & NGINX ← Full TOC

🌐 Chapter 41 — Apache

Complete Apache and NGINX reference: VirtualHost, mod_rewrite, SSL, CGI, reverse proxy, load balancing.
Part of The Direct Path to Linux Ubuntu — free for all.

2Topics
59+Commands
2Examples
FreeNo login
🪶 Apache Config 🟢 NGINX Config ⚡ Examples

🪶 Apache Config

🪶 Apache ConfigurationVirtualHost, .htaccess, mod_rewrite, SSL, CGI

🟢 NGINX Config

🟢 NGINX ConfigurationNGINX server blocks, reverse proxy, load balancing, SSL

⚡ Quick Examples

Apache Quick Reference

Essential Apache commands and config snippets.

# ── Service management ────────────────────────────────────────
sudo systemctl start   apache2
sudo systemctl stop    apache2
sudo systemctl restart apache2
sudo systemctl reload  apache2   # graceful reload (no downtime)
sudo systemctl status  apache2
sudo apache2ctl -t               # test configuration

# ── Enable/disable sites and modules ─────────────────────────
sudo a2ensite  mysite.conf       # enable site
sudo a2dissite 000-default.conf  # disable default site
sudo a2enmod   rewrite ssl headers proxy proxy_http cgid expires deflate
sudo a2dismod  autoindex         # disable directory listing
sudo systemctl reload apache2    # apply changes

# ── Log files ────────────────────────────────────────────────
sudo tail -f /var/log/apache2/error.log
sudo tail -f /var/log/apache2/access.log
sudo tail -f /var/log/apache2/mysite_error.log

# ── .htaccess patterns ────────────────────────────────────────
# Redirect HTTP to HTTPS
cat > /var/www/html/.htaccess << 'EOF'
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Custom 404
ErrorDocument 404 /404.html

# Deny access to hidden files
<FilesMatch "^\.">
    Require all denied
</FilesMatch>

# Cache static assets
<FilesMatch "\.(jpg|jpeg|png|gif|ico|css|js|woff2)$">
    Header set Cache-Control "max-age=2592000, public"
</FilesMatch>
EOF

# ── Let's Encrypt ─────────────────────────────────────────────
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d mywebuniversity.com -d www.mywebuniversity.com
sudo certbot renew --dry-run    # test auto-renewal
sudo crontab -e
# Add: 0 3 * * * certbot renew --quiet

# ── CGI setup ────────────────────────────────────────────────
sudo a2enmod cgid
sudo mkdir -p /usr/lib/cgi-bin/MyChapter
sudo cp MyChapter-Reference.cgi /usr/lib/cgi-bin/MyChapter/
sudo chmod 755 /usr/lib/cgi-bin/MyChapter/MyChapter-Reference.cgi
sudo chown www-data:www-data /usr/lib/cgi-bin/MyChapter/MyChapter-Reference.cgi
sudo systemctl reload apache2

# Test CGI:
curl http://localhost/cgi-bin/MyChapter/MyChapter-Reference.cgi

NGINX Quick Reference

Essential NGINX commands and config patterns.

# ── Service management ────────────────────────────────────────
sudo systemctl start   nginx
sudo systemctl stop    nginx
sudo systemctl restart nginx
sudo systemctl reload  nginx     # graceful reload (no downtime)
sudo nginx -t                    # test config
sudo nginx -T                    # test + dump full config
sudo nginx -s reload             # reload without systemctl

# ── Site management ───────────────────────────────────────────
# Enable site:
sudo ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/
# Disable site:
sudo rm /etc/nginx/sites-enabled/mysite.conf
sudo nginx -t && sudo systemctl reload nginx

# ── Log monitoring ────────────────────────────────────────────
sudo tail -f /var/log/nginx/error.log
sudo tail -f /var/log/nginx/access.log
# Parse logs
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -20

# ── Common patterns ───────────────────────────────────────────
# Simple static site
server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    index index.html;
    location / { try_files $uri $uri/ =404; }
}

# Reverse proxy to Node.js
server {
    listen 80;
    server_name api.example.com;
    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

# Load balancer
upstream myapp {
    ip_hash;                        # sticky sessions
    server 10.0.1.10:8080;
    server 10.0.1.11:8080;
    server 10.0.1.12:8080;
}

# Let's Encrypt
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
# Auto-renewal already configured by certbot

# Performance tuning (nginx.conf)
worker_processes auto;
worker_connections 1024;
keepalive_timeout 65;
gzip on; gzip_comp_level 5;
client_max_body_size 50M;
sendfile on; tcp_nopush on; tcp_nodelay on;