• How To Create Multiple Websites Using Apache / XAMPP

    by  • December 18, 2012 • Apache, Tutorials • 0 Comments

    Virtual HostsThis tutorial will explain how to add additional websites to your Apache web server.
    You may be wondering “Do I need to install Apache for a second website?” and the answer is NO.

    There should only be one instance of Apache installed and running on your server computer and trying to install another instance of it will only cause problems.  The same goes with PHP.

     

    The only server software you will probably ever need to install another instance of is if you want/need a separate MySQL database server, and then you would install and configure it separate from the first.

    This will be an easy and simple to follow tutorial on how to create multiple websites on Apache.

    HOW IT WORKS

    Apache refers to this as PHP or vhosts.  Once Apache is running and you have PHP running on your server, there isn’t any additional software needed for a different website.  That would be like saying to open another text document you would need to start another instance of Windows.  Once you’ve already started Windows once, you can create and open all the files you want as long as Windows stays running.  A web server works the same way.

    So by now you’ve probably realized your first website is in the folder called htdocs.  Any files you put in that folder you can reach through your web browser.  This is done in a simple configuration file that links your web address to that folder.  It is defined in MySQL database server file.  If you open that file with Notepad, you will find a line that says:

    This gives the website the local domain name “localhost“, which will be accessed on port 80.  Port 80 is the default port for web page traffic.  So when you open your browser and type localhost, Apache recognizes it and tells it where to go, which is defined by this line:

    So this makes htdocs your root folder, meaning whatever is put in htdocs can be reached by going to localhost.  You may be wondering where “http://httpd.apache.org/docs/2.4/vhosts/” comes from and it’s actually a name assigned automatically by Windows.  You can create other names, which I’ll cover later in another tutorial.  Do note however these DO NOT work on any other computer besides the one the Apache software is running on.

    Now there is not a way to have multiple websites publicly accessible using just your IP address alone.  You can only specify one website when using your IP alone without domain name(s).  The reason being is because even though you only have one IP with domain names, Apache recognizes the domain name someone is going to your IP from and directs them to the correct folder based on that.

    I’m going to use domain names in my example simply because this is the most likely used method for multiple websites and the one I use myself to host several websites on my server.

    httpd.conf is only used to define a single/default website on the web server.  If you want to host more than one website on your server, you need to include the VirtualHosts configuration file.  So in httpd.conf, find the following line and make sure it’s enabled by removing the comment from in front of it:

    If it has a # or in front of it, make sure and remove it.  Save and close the file.

    Now go to /apache/conf/extra/httpd-vhosts.conf and open it with Notepad.

    I personally perfer to find a good example setting and delete everything in this file, then copy/paste the example in there to make it easier to manage.  All of the comments are great if you want everything explained, but it’s much easier to manage this file with them all removed.  So here’s an example you can use of a multiple site configuration.  Be SURE to remove any entries you don’t use or comment them out by putting a # at the beginning of every line not used.

    Now that looks really complicated, but it’s actually quite simple.  If you look at my entry for myfirstdomain.com, you notice there’s not near as much text under it and you are probably wondering why.

    Technically all you need is an entry like that unless you want to define permissions or need to do so.  You may encounter an ERROR 403 and need to define permissions for that folder, so then you would need to add permissions as I’ve done for my second and third domain.  You can also limit access with the values defined here, but that is beyond this tutorial.  The permissions I’ve used above may or may not work for you and you may need use other directives to fix your particular needs.  If you want more information on possible permission configurations, you can view Apache’s documentaton on it by following these links:

    http://httpd.apache.org/docs/2.4/vhosts/
    http://httpd.apache.org/docs/2.4/vhosts/examples.html

    http://httpd.apache.org/docs/2.4/mod/quickreference.html

    I feel the other entries are pretty self-explanatory, although I will explain a couple of things about them.  First, make SURE you have correctly spelled both instances of the directory paths.  I’ve gotten 403 errors that have left me puzzled only to find out I didn’t spell the second directory path correctly, so verify that it’s correct.

    I added the lines ##  My First Domain  ##, which are just comments and not required.  If you have a lot of domains with sub-domains  you will have several entries here and using comments to organize them will save you time when searching for something.  You can use # or // to define comments.  If you don’t properly comment the line(s) out, you will get an error because it will try to read the line as part of the code.

    Now if you look at the third domain entry, you will see the drive letter is different and the folder is in a completely different place away from the other files.  I did this to show how you can put the websites on other drives besides the one you installed Apache or XAMPP on.  Apache, PHP, etc. isn’t drive specific, so as long as the website’s folder is on the same computer and correctly defined, it can be placed anywhere you like.

    This can be particularly useful if you’re running out of space and need to put additional websites on the computer.


    <!–

    That pretty much sums up how to create multiple websites on the same server.  There's a few options as to how to use these and as I stated above, the most common would be using domain names.

    If you’re doing this only for local development or testing purposes and do not intend on launching them as publicly accessible, then the local hosts/fake domains is a great way to test multiple websites.

    Another cool option you may consider if you’re wanting to add folders to your website(s) from another drive without moving your whole website to that drive, you can use Aliases, which I have more information on that in this article.

    The final note to consider is if you are planning on making your website accessible to the public, you need to make sure your ports are properly forwarded and configured, which you can read this article on Port Forwarding.

    –>

    About

    I'm a freelance PHP developer spending much of my time deep in WordPress theme and plugin development. My personal info can be found at http://amereservant.com .

    http://myownhomeserver.com

    Leave a Reply