Ultimate Guide: How to Set Up Apache or Nginx Web Server on Ubuntu

📋 Prerequisites Checklist

  • Ubuntu Server 22.04 LTS (or newer)
  • Terminal access with sudo privileges
  • Domain name (recommended for SSL)
  • Basic Linux command knowledge
  • Minimum 1GB RAM (2GB recommended)

🛠 Initial Server Setup

# Update system packages
sudo apt update && sudo apt upgrade -y

# Install essential tools
sudo apt install -y curl wget

🔥 Firewall Configuration

ServicePorts AllowedCommand
Apache80, 443sudo ufw allow 'Apache Full'
Nginx80, 443sudo ufw allow 'Nginx Full'
SSH22sudo ufw allow OpenSSH

Enable firewall: sudo ufw enable


🌐 Web Server Comparison

FeatureApacheNginx
ArchitectureProcess-drivenEvent-driven
Best ForTraditional web appsHigh traffic/static content
Config Syntax.htaccess filesServer blocks
PerformanceGoodExcellent
SSL Setupmod_sslOpenSSL

🐧 Apache Setup Guide

1. Install Apache

sudo apt install apache2 -y

2. Virtual Host Configuration

File: /etc/apache2/sites-available/yourdomain.conf

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName yourdomain.com
    DocumentRoot /var/www/yourdomain
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable configuration:

sudo a2ensite yourdomain.conf
sudo systemctl reload apache2

🚀 Nginx Setup Guide

1. Install Nginx

sudo apt install nginx -y

2. Server Block Configuration

File: /etc/nginx/sites-available/yourdomain

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/yourdomain;
    
    location / {
        try_files $uri $uri/ =404;
    }
}

Enable configuration:

sudo ln -s /etc/nginx/sites-available/yourdomain /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

🔒 SSL Setup with Let’s Encrypt

For Apache:

sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache --agree-tos -m [email protected] -d yourdomain.com

For Nginx:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx --agree-tos -m [email protected] -d yourdomain.com

Auto-Renewal Test:

sudo certbot renew --dry-run

🧪 Verification Tests

  1. HTTP Status Check: curl -I http://yourdomain.com
  2. SSL Validation: openssl s_client -connect yourdomain.com:443
  3. Server Header Check: curl -I | grep "Server"

🚨 Troubleshooting Tips

  • Permission Issues: sudo chown -R www-data:www-data /var/www/yourdomain
  • Port Conflicts: sudo lsof -i :80
  • Config Errors: sudo journalctl -xe
  • SSL Renewal: sudo certbot renew --force-renewal

📚 Essential Resources

  1. Apache Security Hardening Guide
  2. Nginx Performance Tuning
  3. Let’s Encrypt Documentation
  4. Ubuntu Server Security

💡 Pro Tips

  • Use systemd for service management:
sudo systemctl [start|stop|restart|status] apache2/nginx
  • Enable HTTP/2 for better performance
  • Set up automated backups with rsync
  • Monitor traffic with goaccess or ngxtop

Final Recommendation:
For most users, Nginx offers better performance out-of-the-box, while Apache provides greater flexibility through .htaccess files. Choose based on your specific application requirements.

Stay Tuned for the Next Article

In our next article, we’ll dive into file sharing services on Ubuntu! You’ll learn how to:

  • Configure FTP for seamless file transfers.
  • Set up Samba to share files with Windows and other operating systems.
  • Use NFS (Network File System) to share files across Linux devices.

These file-sharing techniques will help you connect and collaborate across devices, making your Ubuntu server even more versatile. Don’t miss it!

Leave a Comment