Linux

Setup Apache virtual hosts

In this piece we will be exploring how to configure apache virtual hosts on Linux(Debian or Ubuntu based system). In order to configure Apache Virtual Hosts we need to have per-installed LAMP server. To Learn how to set up LAMP server on Linux you can follow the link here.

Virtual hosting is a way for hosting multiple domain names on a single system but each domain name would be handled in separate space. This allows system administrator to share the single hardware system resource to serve multiple domains.

So at this point we are going to proceed with the assumption that you have already setup your LAMP stack on your web server.

Adding User for apache virtual hosts

First we are going to create users to handle Apache Virtual hosts. Go to terminal and create userA and userB by issuing following commands.

#sudo useradd -m userA

Now create the user directory under home directory

#sudo mkdir /home/userA/public_html

Now similarly do the same thing to create userB

#sudo useradd -m userB
#sudo mkdir /home/userB/public_html

After creating the users and their home directories, Create a file index.html for userA. We are using nano file editor to do this.

#sudo nano /home/userA/public_html/index.html

Now write a markup as you want to represent your virtual host and then save the file by pressing CTRL+X.  Example shown below.

<html>
<body>
<title>Welcome to virtual host userA</title>
<h1>Hi. This is a Virtual host page for userA</h1>
</body>
</html>

Now do same for userB.

#sudo nano /home/userB/public_html/index.html
<html>
<body>
<title>Welcome to virtual host userB</title>
<h1>Hi. This is a Virtual host page for userB</h1>
</body>
</html>

Configuring Apache

Now goto your apache directory.

#cd /etc/apache2/sites-available

Here you will find all the configuration files for you apache web server. just issue the ls command to display the contents of this directory.

Go to the directory sites-available. Here you will find a default configuration file named 000-default.conf. Now you need to copy that file format for both the users that we created.

#sudo cp 000-default.conf userA.conf

also

#sudo cp 000-default.conf userB.conf

Now go ahead and edit the files for each users or virtual host.

#sudo nano userA.conf

After opening the file in nano editor make the following changes.

<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@userA.com
ServerName www.usera.com
DocumentRoot /home/userA/public_html

<Directory /home/userA/public_html>
Options Indexes FollowSymlinks
AllowOverride All
Require all granted
</Directory>

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>

Do same for userB.

#sudo nano userB.conf

Make the following changes for userB configuration file.

<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@userB.com
ServerName www.userB.com
DocumentRoot /home/userB/public_html

<Directory /home/userB/public_html>
Options Indexes FollowSymlinks
AllowOverride All
Require all granted
</Directory>

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>

DocumentRoot is your public HTML directory. By default for apache it is set to www so we need to change it as per our requirement. Here in our case, it will be /home/userA/public_html for userA and /home/userB/public_html for userB.

ServerName is your domain name.

ServerAdmin is your admin email for a particulate domain. Left it as usual or set your email.

<Directory> directive is used to map file-system objects for virtual host or domain.

FollowSymLinks means if a directory is a symbolic link then apache follow the link.

AllowOverride directive is used to allow the use .htaccess within the web-serer to allow overriding of  apache configuration per directory.

Require all granted means no IP address is blocked from accessing the service.

Anything that starts with # is a comment.

Enabling Apache virtual hosts

For userA.com

#sudo a2ensite userA.conf

For userB.com

#sudo a2ensite userB.conf

Now you have to reload apache.

#sudo systemctl reload apache2

Setting up your domain names

Now this is the time for final step which is to identity the domain names in local environment. Go ahead and add a domain name for a locahost in file /etc/hosts.

127.0.0.1 localhost
127.0.1.1 WarMachine.agu WarMachine

# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.0.0.1 www.usera.com
127.0.0.1 www.userb.com

Now fire up your browser and access your domain names. In our case we have www.usera.com and www.userb.com.

Follow us here!