creating a Virtual Host in Apache

In this guide, we will explain how to set up a virtual host (vhost) on an Apache server. This is another article in a series of articles on server maintenance and Linux. By default, Apache serves its content from a directory located in /var/www/ using the configuration included in /etc/apache2/sites-available/000-default.conf. Instead of modifying the default website configuration file, we will create a new virtual host. Virtual hosts allow us to configure multiple domains on a single Apache server. To do this, we will create a directory in /var/www/ for a sample website called your_domain. Create the root directory for your_domain as follows:

sudo mkdir /var/www/your_domain

Now we will set permissions on the directory with the $USER environment variable, which should refer to your current system user:

sudo chown -R $USER:$USER /var/www/your_domain

Open a new configuration file under Apache's sites-available directory using a command line editor. In our example, we are using nano:

sudo nano /etc/apache2/sites-available/your_domain.conf

Paste the following settings:

<VirtualHost *:80> ServerName your_domain ServerAlias www.your_domain ServerAdmin webmaster@localhost DocumentRoot /var/www/your_domain ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

With these VirtualHost settings, we are essentially telling Apache to serve the content of your domain in the following directory:

/var/www/your_domain

You can test Apache without a domain name by removing the ServerName and ServerAlias options or by adding a # at the beginning of each option's line. Now you can use a2ensite to activate this virtual host:

sudo a2ensite your_domain

For convenience and security reasons, it is recommended to disable the default website that comes with Apache. To disable the default Apache website, type:

sudo a2dissite 000-default

To ensure that your configuration file does not contain any syntax errors, you can run:

sudo apache2ctl configtest

Finally, reload Apache for the changes to take effect:

sudo systemctl reload apache2

Your new site is active, but its root directory—/var/www/your_domain—is empty.


2026 © Linux Hosting - Web Hosting since 2011