How To Create Multiple Websites using Apache/XAMPP for Linux.

HOW TO CREATE MULTIPLE WEBSITES USING APACHE/XAMPP.

This tutorial will explain how to add additional websites to your Linux 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 a separate MySQL database, and then you would install and configure it separate from the first.

If you would like to use the video tutorial, you can visit this article:

How to create second/multiple websites using Apache/XAMPP



HOW IT WORKS:

Apache refers to this as VirtualHosts 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 Linux. Once you've already started Linux once, you can create and open all the files you want as long as Linux 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 /apache/conf/httpd.conf file. If you open that file with a text editor, you will find a line that says:

ServerName localhost:80

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:

DocumentRoot "/opt/lampp/htdocs"

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 "localhost" comes from and it's actually a name assigned automatically by the hosts file, which is located in /etc/. You can create different names by reading my tutorial, How to setup local domains or fake domains . Do note however these DO NOT work on any other computer besides the one the Apache software is running on.

So in order to make another website, we just need to make more definitions that tell different names to go to a different folders. This is where the VirtualHosts come in at when using Apache.  You can use the article mentioned above or if you're wanting to setup multiple public domain names, you can read this tutorial on how to do that.

Now there is not a way to have multiple websites publicly accessible using just your IP address. You can only specify one website with only your IP. This is why you use domain names.  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 website on the web server. If you want to host more than one website on your server, you need to enable VirtualHosts. So in httpd.conf, find the following line and make sure it's enabled by removing the comment from in front of it:

Include etc/extra/httpd-vhosts.conf

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

Now go to /opt/lampp/etc/extra/httpd-vhosts.conf and open it with a text editor.

I personally prefer 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 either remove or comment out any entries below you do not use.

NameVirtualHost *:80

<VirtualHost *:80>
ServerName localhost
DocumentRoot "/opt/lampp/htdocs"
</VirtualHost>



## My First Domain ##

<VirtualHost *:80>
ServerName myfirstdomain.com
ServerAlias www.myfirstdomain.com
DocumentRoot "/opt/lampp/htdocs"
</VirtualHost>


## My Second Domain ##

<VirtualHost *:80>
ServerName myseconddomain.com
ServerAlias www.myseconddomain.com
DocumentRoot "/opt/lampp/htdocs2"
<Directory "/opt/lampp/htdocs2">
Options +Indexes FollowSymLinks +ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

## My Third Domain ##

<VirtualHost *:80>
ServerName mythirddomain.com
ServerAlias www.mythirddomain.com
DocumentRoot "/opt/lampp/mythirddomain"
<Directory "/media/HPV100W/mythirddomain">
Options +Indexes FollowSymLinks +ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

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 and so then you would need to add permissions as I've done. You can also limit access with the values defined here, but that is beyond this tutorial.  The permissions shown above are basic permissions and they may or may not work for you and you may need to read more on the different directives for your particular need. If you want more information on possible permission configurations, you can view Apache's documentation on it by visiting these links:

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

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

I feel the 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 path 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.

 

NOTE ABOUT THE LINKS IN THIS ARTICLE

If you clicked a link and you're reading this, I haven't created the necessary article for the link yet or I forgot to update the link.  I copied this article from my Windows tutorials and made the necessary corrections to adapt it to Linux.  Instead of deleting the links, I've temporarily disabled them until I get all of the articles written for Linux and then I will re-link them to the new articles.  I will delete this section once this is corrected and I will also make note of it in this website's blog to keep everyone updated.

Thank you for your patience!



Comments
Add New Search RSS
Leave feedback
Name:
Your email:
 
Website:
Message Title:
Formatting:
[b] [i] [u] [url] [quote] [code] [img] 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
Enter the text as you see it in the image.

!joomlacomment 4.0 Copyright (C) 2009 Compojoom.com . All rights reserved."

Last Updated (Wednesday, 04 February 2009 05:24)

 

!!REMEMBER: Support Forums!!

I still keep getting requests for support in the comments of posts and unfortunately I cannot give adequate responses due to the fact there's limited space and it just isn't the right place for support.
I therefore am encouraging visitors to PLEASE go to http://forum.myownserver.info for any support related questions. You will NOT receive any spam or anything you don't specifically subscribe to! I'm very meticulous about this myself and if at any time you need help stopping notifications from posts you previously subscribed to, contact me and I will promptly fix the issue.

Thank you for your participation in this!

Open SourceApache HTTP Server ProjectMySQLPHP
Twitter Feed
Make a Donation
This site does not run ads nor does anyone fund it. The owner and author is unemployed and pays for it himself, so if you're feeling generous, please make a donation of any amount to help out. Thank you very much.

Powered by easy paypal donation

Survey
Which is better?
 
Search
User Login



Visitors


Countries

30.2%United States United States
8.5%United Kingdom United Kingdom
7.7%India India
4.7%Australia Australia
4.5%Canada Canada

Visitors

Today: 26
Yesterday: 192
This Week: 26
Last Week: 1223
This Month: 895
Last Month: 4933
Total: 52613


JoomlaWatch Stats 1.2.9 by Matej Koval