I have a local development machine that has Linux Mint 17 installed. To make my life as a web developer easier, I would like to configure my single machine to host multiple development environments. In addition, I would like to configure these environments such that the link would like similar to that in the “real world”.
After struggling for a bit, I finally come up with the steps to reliably configured it on my own set up.
Before I go into details of the steps to configure virtual hosts on my local machine, I should specify my machine configuration:
Apache | Server Version | Server Built |
---|---|---|
Apache/2.4.7 (Ubuntu) | Jul 22 2014 14:36:38 |
Linux | Kernel | Desktop | Distro |
---|---|---|---|
3.13.0-24-generic x86_64 (64 bit) | Gnome | Linux Mint 17 Qiana |
The closer your machine configuration to the ones I have, the more likely the step would be applicable to your configuration. That being said, I hope that these steps at least provide a starting point in getting your virtual hosts up and running in your local environment.
Setting up virtual host on your location machine boils down to 3 basic steps:
- Configure domain names on your machine,
- Configure virtual hosts with Apache,
- Testing the virtual host setup.
Setting Up Domain Names
Before I can set up virtual host configurations in Apache, I need to first make sure that my machine understands the domain names that I would like it to host. To do so, I need to edit the /etc/hosts
file.
## Changing directory to /etc [508][vrw.myrna: /home/vrw] $ cd /etc ## Making a back up copy of the /etc/hosts file [508][vrw.myrna: /etc] $ cp ./hosts ~/hosts.backup ## Editing the /etc/hosts file with an editor that you are comfortable with [510][vrw.myrna: /etc] $ gedit hosts
In the /etc/hosts
file, I would add the following line to the section above the IPv6 hosts.
127.0.0.1 vrw.dev.mt
After my addition, the /etc/hosts
file would look like the following:
127.0.0.1 localhost 127.0.1.1 myrna 127.0.0.1 vrw.dev.mt # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters
Once the changes to the file /etc/hosts
have been saved, we are done with configuring the domain name, vrw.dev.mt
.
Next stop is to configure Apache so that it would recognize vrw.dev.mt
as a web site on the machine.
Configuring Apache Virtual Host
In Linux Mint 17, Apache 2 relies on the configuration files in /etc/apache2/sites-enabled/
to tell it where and how to service the web site hosts on the machine.
To set up vrw.dev.mt
as a web address on a localhost machine, a corresponding virtual host file needs to be configured in the following directories:
/etc/apache2/sites-enabled/
/etc/apache2/sites-available/
It is actually just one single configuration file that needs setting up. And it needs to be set up in /etc/apache2/sites-available/
. Once that file is set up, there is a series of commands that we would execute to get Apache to link it to the /etc/apache2/sites-enabled/
directory and enable the configuration file.
So, let’s start with setting up the configuration file in /etc/apache2/sites-enabled/
.
Creating Apache virtual host configuration
To create a configuration file for vrw.dev.mt
, I would use the default configuration file, 000-default.conf
, as a template. To make it simpler to see which configuration file corresponds to which virtual host site, I am going to simply name the file with the same name as the domain name. I would append .conf
at the end of the configuration file name. In other words, for my site, my configuration file name would be vrw.dev.mt.conf
.
It is worth noting that the extension, .conf
, is an integral part of the filename. Without the .conf
extension, when it comes time to enabling the configuration, Apache will raise an error (ERROR: Site does not exist!
). So, do be very careful to append .conf
to the end of your virtual host configuration filename.
## Changing directory to /etc/apache2/sites-available [511][vrw.myrna: /home/vrw] $ cd /etc/apache2/sites-available ## Copying the default configuration as vrw.dev.mt vhost configuration [512][vrw.myrna: /etc/apache2/sites-available] $ sudo cp -p 000-default.conf vrw.dev.mt.conf ## Editing the vrw.dev.mt.conf file [513][vrw.myrna: /etc/apache2/sites-available] $ gedit vrw.dev.mt.conf
In the vrw.dev.mt.conf
file, the key elements to edit are the ServerName
and DocumentRoot
.
ServerName
needs to reflect the same name as the one defined in /etc/hosts
(in this case, vrw.dev.mt
).
DocumentRoot
needs to coincide with where I plan to put the web documents for the site. For the time being, I plan to put them in my local directory. So, my DocumentRoot
would be /home/vrw/public_html
.
Since this is a development version of the production site, I want to have a separate error log so that I can more readily debug any problems I may have. After adding in all the trimmings, the vrw.dev.mt.conf
would look like the following:
<VirtualHost *:80> ServerName vrw.dev.mt ServerAdmin webmaster@localhost DocumentRoot /home/vrw/public_html # 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}/vrw.error.log CustomLog ${APACHE_LOG_DIR}/vrw.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 <Directory /home/vrw/public_html> LogLevel debug </Directory> </VirtualHost>
After editing the file, I need to make sure that the configuration file has the right permission.
## Changing directory to /etc/apache2/sites-available [511][vrw.myrna: /home/vrw] $ cd /etc/apache2/sites-available ## Listing directory files and their ownership and permission. [525][vrw.myrna: /etc/apache2/sites-available] $ ls -lart 000-default.conf vrw.dev.mt.conf
I should see my newly created file having the same permission as the default configuration file.
-rw-r--r-- 1 root root 1354 Feb 2 19:06 000-default.conf -rw-r--r-- 1 root root 1341 Mar 12 19:00 vrw.dev.mt.conf
If this is not the case, I would need to use chmod
to change the file permission.
## Changing file permission. [526][vrw.myrna: /etc/apache2/sites-available] $ sudo chmod 644 /etc/apache2/sites-available/vrw.dev.mt.conf
Once the configuration has been set up, with the correct permission, I could then have Apache enable the configuration. Until Apache turns on the configuration, the virtual host, vrw.dev.mt
, will not take effect.
Enabling Virtual Host Configuration
Now that the new file, vrw.dev.mt.conf
, has been set up in the /etc/apache2/sites-available
directory, all that is left is to have Apache enable the configuration.
To enable the new virtual host configuration, do the following;
## Changing directory to /etc/apache2/sites-available [537][vrw.myrna: /home/vrw] $ cd /etc/apache2/sites-available ## Enabling virtual host vrw.dev.mt [538][vrw.myrna: /etc/apache2/sites-available] $ sudo a2ensite vrw.dev.mt.conf Enabling site vrw.dev.mt. To activate the new configuration, you need to run: service apache2 reload
Before reloading the Apache, run a configuration test:
## Checking Apache configuration [543][vrw.myrna: /etc/apache2/sites-available] $ sudo apachectl configtest Syntax OK
If the configuration test returns any error (status other than Syntax OK
), disable the vrw.dev.mt.conf
and debug the issue.
## Changing directory to /etc/apache2/sites-available [550][vrw.myrna: /home/vrw] $ cd /etc/apache2/sites-available ## Disabling virtual host vrw.dev.mt [551][vrw.myrna: /etc/apache2/sites-available] $ sudo a2dissite vrw.dev.mt.conf Site vrw.dev.mt disabled. To activate the new configuration, you need to run: service apache2 reload
If the configuration test returns Syntax OK
, proceed to reload Apache so that the new configuration can take effect:
## Reloading Apache configuration [553][vrw.myrna: /home/vrw] $ sudo service apache2 reload * Reloading web server apache2 *
At this point, with vrw.dev.mt
configured, it is time to test the site link.
Testing the Virtual Host Configuration
Remember the DocumentRoot
directive when configuring Apache virtual host? Well, that is where the test document will go to see if the configuration takes.
So, first go to the DocumentRoot
directory. In this case, it is /home/vrw/public_html/
.
## Changing directory to /home/vrw/public_html [552][vrw.myrna: /home/vrw] $ cd /home/vrw/public_html ## Use an editor your comfortable with to create a test file to test virtual host vrw.dev.mt [553][vrw.myrna: /home/vrw/public_html] $ cat > index.html <html> <head> <title>New virtual host created successfully - vrw.dev.mt</title> </head> <body> <h1>Hello, World!</h1> <strong>New virtual host created successfully - vrw.dev.mt</strong> </body> </html> ^C
Once the file index.html
is in place, change the file permission to make sure Apache can serve up the content.
## Changing directory to /home/vrw/public_html [552][vrw.myrna: /home/vrw] $ cd /home/vrw/public_html ## Changing file permission to index.html [553][vrw.myrna: /home/vrw/public_html] $ chmod 644 index.html
Now, launch a web browser and type in vrw.dev.mt
for the web address. Assuming that everything is set up correctly, the following will show up.
If you see something similar to this after your configuration, congratulations! You have successfully set up your virtual host site on your local machine!
Reference
There are quite a few good references on setting virtual hosts. Below were the ones I had consulted and had good result with:
- Apache Virtual Host Examples — (http://httpd.apache.org/docs/2.4/vhosts/examples.html)
- Setup Virtual Hosts In Apache On – Linux Mint 17 / Ubuntu 14.04 / Debian 7.6 — (http://www.2daygeek.com/setup-virtual-hosts-in-apache-on-linux-mint-17-ubuntu-14-04-debian-7-6/)
- Convert localhost to domain name for your project in Ubuntu/Mint — (http://technicalworldforyou.blogspot.com/2013/02/convert-localhost-to-domain-name-for.html)
Smart Wolf!