Create and set up an Apache virtual host

A virtual host is a web server entry point. It allows to define and configure the addresses handled by Apache.

This post describes how to create and configure an Apache virtual host.

Prerequisites

Create the virtual host’s root directory

The root directory contains resources (pages, scripts, images, videos, files…) needed to display the website corresponding to the virtual host.

I recommend creating the root directory in /home/myuser, and using a domain name as the root directory’s name.

mkdir /home/myuser/mydomain.com
chown myuser:myuser /home/myuser/mydomain.com

Create a virtual host

I recommend using a domain name as virtual host’s name.

Line AssignUserID myuser myuser is only useful if the apache2-mpm-itk package for customizing the Apache user is installed on the server.

nano /etc/apache2/sites-available/mydomain.com

<VirtualHost *:80>
    ServerAdmin webmaster@mydomain.com
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    DocumentRoot /home/myuser/mydomain.com
    Options -Indexes
    AssignUserID myuser myuser
</VirtualHost>

Enable virtual host

a2ensite mydomain.com
service apache2 reload

Apache now handles the addresses http://mydomain.com and http://www.mydomain.com

Disable virtual host

a2dissite mydomain.com

Leave a Reply

Your email address will not be published. Required fields are marked *

*