Check this Out!Visit our social network pages for recent updates!

Students question – Create Apache2 Virtual Hosts on Ubuntu

Apache2, a very popular webserver allows you to run multiple websites on one host. Apache2 virtualhost is a way to create multiple individual websites on a single host server.

With virtualhost, one can run example.com and example.org domains on the same computer as if they were individual website hosted on individual server. This brief tutorial shows you how to create Apache2 server virtualhost on Ubuntu.

New webmasters tend to host one website on a single host server. A single host is capable of running more than one websites easily. With a robust server, you can run many more.
This is made possible by Apache2 virtual host. Web servers virtual hosting is a way to allow for multiple websites to be hosted on one host. Each virtual host run its own website/application as if it was running on dedicated host computer.

This brief tutorial will describe how virtual hosting works with Apache2

When you install Apache2 on Ubuntu server, all of its configurations files and directories are saved in the /etc/apache2/ folder. Inside that directories, you’ll find other files and directories.

Apache2 global configuration file is saved at /etc/apache2/apache.conf

Apache2 global config file contains settings that are global and impact all Apache2 services and virtual hosts. Other important directories are listed below:

/etc/apache2/conf-available  ==> this directory contains available configuration files for apache2

/etc/apache2/conf-enabled ==> this directory contains symlinks to files in /etc/apache2/conf-available.

Same for /etc/apache2/mod-available and /etc/apache2/mod-enabled

/etc/apache2/sites-available ==> contains all virtual host configuration files. All Apache2 virtual host files live in this directory. When you enable a virtual host, it get enabled and symlink created in /etc/apache2/sites-enabled directory.

Step 1: Creating Virtual Host on Ubuntu

Now that you know where to create virtual host configuration files, let’s go and create a new virtual host. This virtual host will run the website for example.com To create a new virtual host for example.com domain, run the commands below. This will create a new file called example.conf

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

All virtual host files start with the tag <VirtualHost *:80>  or specify the IP address of the server <VirtualHost 192.168.1.1:80>

The virtual host files will end with </VirtualHost>

Below is an example virtual host 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 example.com
        ServerAlias www.example.com

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html/example.com

	# 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>

the most important thing to remember in the VirtualHost file is the DocumentRoot , SeverName and ServerAlias lines.  Others you can use default settings for those. When you’re done, save the file.

Step 2: Create Website DocumentRoot folder

After creating the VirtulHost file for the site, go and create the DocumentRoot folder. This should be what you defined in the virtual host file.

sudo mkdir  /var/www/html/example.com/

After creating that, change the permissions on the directory to give Apache2 control.

sudo chown -R www-data:www-data /var/www/html/example.com/

sudo chmod -R 755 /var/www/html/example.com/

Step 3: Enable the VirtualHost

Finally, run the commands below to enable the virtual host.

sudo a2ensite example.conf

Restart apache2 and the new site should be working. On Ubuntu server, there’s already a default site that is enabled. If you run into trouble, go and disable the Apache2 default VirtualHost file by running the commands below.

sudo a2dissite default.conf

Restart Apache2 and try again.

You can create a many virtual hosts as you like as long as your host server has that much resources to run all these sites.

Enjoy!