Skip to main content

Basic Configuration Guide for Apache2 and Nginx

Apache2 Basic Configuration

1. Default Configuration File

The main configuration file for Apache2 is located at:

/etc/apache2/apache2.conf

2. Virtual Hosts Configuration

To host multiple websites on the same server, configure virtual hosts:

  • Navigate to the sites-available directory:

    cd /etc/apache2/sites-available/
  • Create a new virtual host file:

    sudo nano yoursite.conf
  • Add the following basic virtual host configuration:

    <VirtualHost *:80>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    DocumentRoot /var/www/yourdomain
    <Directory /var/www/yourdomain>
    AllowOverride All
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
  • Enable the site and reload Apache:

    sudo a2ensite yoursite.conf
    sudo systemctl reload apache2

3. Enabling Modules

To enable useful modules like rewrite:

sudo a2enmod rewrite
sudo systemctl reload apache2

Nginx Basic Configuration

1. Default Configuration File

The main configuration file for Nginx is located at:

/etc/nginx/nginx.conf

2. Server Block Configuration

Nginx uses server blocks (similar to virtual hosts in Apache) to manage different websites:

  • Navigate to the sites-available directory:

    cd /etc/nginx/sites-available/
  • Create a new server block file:

    sudo nano yoursite
  • Add the following basic server block configuration:

    server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/yourdomain;
    index index.html index.htm;

    location / {
    try_files $uri $uri/ =404;
    }

    error_log /var/log/nginx/yourdomain_error.log;
    access_log /var/log/nginx/yourdomain_access.log;
    }
  • Enable the site by creating a symlink to sites-enabled:

    sudo ln -s /etc/nginx/sites-available/yoursite /etc/nginx/sites-enabled/
  • Test the configuration and reload Nginx:

    sudo nginx -t
    sudo systemctl reload nginx

Additional Notes

  • Always back up your configuration files before making changes.
  • Use sudo systemctl reload <service> after updating any configuration to apply changes.
  • Both servers allow you to enable SSL using tools like Certbot for Let's Encrypt.