Virtual Hosts & Multiple Sites

Running several websites on one server is economical and straightforward with Apache’s virtual hosts. Each vhost defines its own domain name, document root and log files.

Enabling vhost configuration

Create separate configuration files under /etc/apache2/sites-available and enable them using a2ensite. Here's an example for example.com:

<virtualhost *:80=""> ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example/public_html ErrorLog ${APACHE_LOG_DIR}/example-error.log CustomLog ${APACHE_LOG_DIR}/example-access.log combined </virtualhost>

Create another file for othersite.com with its own DocumentRoot. Then run:

sudo a2ensite example.com.conf sudo a2ensite othersite.com.conf sudo systemctl reload apache2

Ensure DNS records point to your server's IP address. HTTPS sites will need separate :443 vhosts with SSL certificates.