Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Virtual Hosts In Apache, Running multiple sites on apache with different server names
fffanatics
post Jan 10 2007, 08:53 PM
Post #1


Privileged Member
*********

Group: [HOSTED]
Posts: 936
Joined: 14-April 05
From: West Chester, PA
Member No.: 5,636



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
QUOTE
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.
CODE

<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
QUOTE
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 biggrin.gif 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.
Go to the top of the page
 
+Quote Post
QuickSilva
post Jan 15 2007, 08:00 PM
Post #2


Premium Member
********

Group: Members
Posts: 181
Joined: 15-January 07
From: Rotherham, UK
Member No.: 37,245



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! wink.gif
Go to the top of the page
 
+Quote Post
sportytalk
post Jan 21 2007, 10:54 PM
Post #3


Super Member
*********

Group: Members
Posts: 326
Joined: 7-October 05
Member No.: 12,650



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.

This post has been edited by sportytalk: Jan 21 2007, 10:57 PM
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. How To Create Virtual Drives(21)
  2. Server Status(12)
  3. How To Make A Sig Rotator(85)
  4. Automating Tape Backups With Amanda, Part 2(0)
  5. Set Os X Server 10.3 Dhcp To Filter Desks(0)
  6. A Nice Mysql Server Check(4)
  7. Getting Content To Blog About(0)
  8. Run Your Own Mail / Radio Server(24)
  9. Hide Names In Welcome Screen(0)
  10. Multiple Classes In Html(8)
  11. A Tutorial On How To Change Your Window's Screen Name's Icon(0)
  12. Checking For Open Ports From Php(3)
  13. Installing Multiple Os On Your Pc(0)
  14. Installing Php + Mysql + Apache + Phpmyadmin On Windows Part 2(0)
  15. Making A Dynamic Page On Blogspot(5)
  1. Download Videos From Sites & Convert It.(28)
  2. Check Referrer To Prevent Linking Yours From Other Sites(8)
  3. Starting Or Stopping Apache And Mysql Server Via Batch File(0)
  4. How To Group Multiple Sets Of Data In Microsoft Excel 2007(0)
  5. Wordpress Contact Form That Work With Free Web Hosts(1)
  6. Dynamic Signature - Yet Another Way To Do It(0)
  7. How To Create Counter-strike 1.6 Server In Console Mode(5)
  8. Start Multiple Programs With One Shortcut(15)
  9. Installing Apache And Php(2)
  10. How To Make Your Own Counter Strike Source Dedicated Server!(40)
  11. Best Way To Add Paypal To Mediawiki Sites(0)
  12. How To Download Any Flv Files From Any Sites(6)
  13. Sharepoint Calendars Integrated Into Outlook 2007(0)


 



- Lo-Fi Version Time is now: 1st December 2008 - 03:50 PM