Jump to content
xisto Community
Sign in to follow this  
fffanatics

Virtual Hosts In Apache Running multiple sites on apache with different server names

Recommended Posts

I noticed that a lot of people were interested in snlildude87's tutorial on how to create their own personal local test server for their webpages. However, the way he had them test their sites was moving them to the htdocs folder in the XAMPP directory. Many people like myself have or maintain multiple sites at once and this would be a pain to have to use subdirectories for each site and move files all the time. Therefore, with apache you can create something call a virtual host.

Each virtual host acts like its own web server when there really is only one. Each one has their own home directory and their own server name. Basically this will let you reference each different site in a web browser by their name and not http:\\localhost\subdir\subdir ...

Alright to begin you will have to find your virtual hosts file in apache. If you are using XAMPP it is a bit different since they split the httpd.conf file into a few files to make it easier to reference. The virtual hosts file is httpd-vhosts.conf and is located in the apache\conf\extra from within the main XAMPP directory. If you are using apache that you install yourself the virtual hosts will be added directly in the httpd.conf file located in the apache\conf directory and the section should be near the end of the file.

Alright, now to begin. Open the virtual hosts file for editing. You will notice it just is a bunch of comments. To start off you need to set NameVirtualHost. To do this just add the following line

NameVirtualHost 127.0.0.1

since this is a local test server. If this was not local and you wanted outside visitors to be able to access the host you would put your actual ipaddress where the 127.0.0.1 is located.
Now you need to create the virtual hosts. The easiest way to explain this is going to be showing you an example and then explaining it. Here is one of the virtual hosts out of my vhosts file.
<VirtualHost 127.0.0.1>   ServerName fffanatics   DocumentRoot "C:/Website/fffanatics"   <Directory "C:/Website/fffanatics">         Options Indexes FollowSymLinks Includes ExecCGI         AllowOverride All         Order allow,deny         Allow from all    </Directory> </VirtualHost>

Alright the first section of the code is creating a virtual host on the localhost ip address. This address would change just like the NameVirtualHost would change. Then you get the ServerName. This is going to be how you reference your website in the browser. Next is the location in which the files are located. Beware that you have to use forward slashes and not backward slashes or else apache will not start.

The next block of code has to do with who all is allowed access to this host. Since i use this locally i have allowed anyone access since i am set Alloy from all. This means it will allow anyone to access the server in their browser. Since I have never used virtual hosts in an external fashion, i cannot completely say how these would change but you most likely would not allow everyone access and would limit some of the options.

Now that you have one virtual host, you can create as many more as you want. Most likely you will create atleast one more or else you would not have been interested in this tutorial. To do so just copy your previous code and change the ServerName and the DocumentRoot and you will be set.

But how do you access the virtual hosts? To do this you will need to edit your hosts file. All operating systems have these files but they are located in different places and might even be named something else. In Windows this file is located in WINDOWS\System32\drivers\etc and is called hosts (with no extension). Open this file. You will notice their is atleast 1 entry already in this file and that will be

127.0.0.1 localhost

What you will do is add 1 line per virtual host that you have to this file. Each one will start with the value of your NameVirtualHost (which probably will be 127.0.0.1) and instead of localhost you will enter your ServerName. This pairs your server name with the ip address and apache interprets that path to your DocumentRoot.

Now you can start up your apache server (or restart if it already was running so that the new conf files can be used). If the server does not start, go to the command promt and go to the XAMPP folder (or apache folder) and run apache_start.bat. This will give you the line number where it failed but keep in mind that XAMPP splits the conf file into multiple files so this is not the actual line in the httpd-vhosts.conf file but the total line number ran in the httpd.conf file.

Hopefully you made no mistakes :P and everything is up and running fine. Now just open any browser window and in the address bar type your server name (for my example it would be fffanatics) and it will take you to the correct webpage. If there is a document not found, then it is not apache's fault but your DocumentRoot may be wrong or you may not have a index file.

Hope this helps people out who do multiple php sites and want to test on the same box.

As a quick side note on XAMPP, if you use the .zip file to install it, you can put it on a thumbdrive and run it from there. You just have to run setup_xampp.bat each time you use it to make sure the configurations are fine and now you can work on all your sites on different computers. However, in order for the virtual hosts to work you would have to edit the hosts file on every computer that you use so that is a negative of having a mobile web server.

Share this post


Link to post
Share on other sites

Thank you very much for this tutorial! If only I would have known the other day, I wouldn't have gone searching through Google to find the answers to my questions... But you answered them there. Very well explained too! :P

Share this post


Link to post
Share on other sites

I agree with what shining said in the above post. You've explained all about virtual hosts in an easy to understand way.I don't really need to create virtual hosts at the moment, but they sound like a good way to have more than one website run on a local webserver at the same time (with different server names), making development a lot easier.If I ever do decide to manage more than one website at the same time by using virtual hosts, I will come back and read this tutorial.

Edited by sportytalk (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.