Running multiple websites on one server with Apache

Posted on 2008-06-25
Medium Priority
Last Modified: 2010-12-09
I am running a Red Had Enterprise 3 server in our office and hosting our Intranet page on it for internal company purposes.  We also develop web sites and they go to another server off site.  I would like to use our internal server to test the web sites before deploying them.

Currently we have /FolderA which contains our Intranet site.
We want to also get Apache to access /FolderB/websitename

I don't particularly care how the URL is going to read in the office, just as long as the website will pull up for testing and the PHP will work with it. (PHP is currently working fine with the Intranet site).

We have about 50 sites in /FolderB/ and that changes at all time.  I don't want to have to assign massive amounts of IPs to the server and constantly be editing the config.

I haven't messed with Apache's config much so the more detail the better.  

Thanks in advance.
Question by:tech1984
  • 5
  • 4
  • 3
  • +2

Expert Comment

ID: 21871076
Can't you just setup FolderA and FolderB and then refer to all sites as http://serverip/sitename1 -> FolderB/sitename1 ?
LVL 14

Expert Comment

ID: 21872457
I think you're looking for namebased virtual hosting. Using namebased virtual hosting, you can assign unlimited domains to just one IP address (that's what all providers do, in fact).

In the apache configuration (/etc/apache2/apache2.conf) you need to make sure that this configuration option is enabled:

# Include the virtual host configurations:
Include /etc/apache2/sites-enabled/

There are two directories in /etc/apache2 used to configured VirtualHosts: sites-available and sites-enabled.
Start with adding your virtual hosts to sites-available. To do this, simply create a new file called "testdomain" in there that looks like this:

<VirtualHost *>
        ServerAdmin someone@somewhere.com
        ServerName testdomain.local
        ServerAlias www.testdomain.local web.testdomain.local

        DocumentRoot /var/www/FolderB/testdomain
        <Directory /var/www/FolderB/testdomain>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All

        ErrorLog /var/log/apache2/testdomain-error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/testdomain-access.log combined
        ServerSignature On


Make sure that the files /var/log/apache2/testdomain-{access,error}.log are writable by your webserver, to be sure, touch them:

touch /var/log/testdomain-{access,error}.log

Now you need to symlink this testdomain to the sites-enabled directory by doing this:

cd /etc/apache2/sites-enabled
ln -s ../sites-available/testdomain

For the changes to take effect, reload apache2:

/etc/init.d/apache2 reload

Now you have a namebased virtual host running that listens to "testdomain.local" as well as "www.testdomain.local" and "web.testdomain.local".

If you're using a DNS server, make sure that your DNS server contains an A record for this domain so that the clients get redirected to your webserver using this domain name. If you don't use DNS at your office, add this domain to your hosts file in c:\windows\system32\drivers\etc\hosts or on linux, add it to /etc/hosts.
LVL 29

Expert Comment

ID: 21872489
IN linux, if you want to host more then 1 web site under one Ip, then you could of use vhosts concept.

but here  you dont want to call web site by real name, you just want to open those from internal network .

in thats case you dont need vhosts concept.

as zhuba said, Create another folder(folderb) under /var/www/html ( Same way you have created forlder1 and intranet web site

and call them http://linuxservername/folderb/website1/

but  if you want to host more then 1 web site, in one IP, then you will have to use vhosts.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 14

Expert Comment

ID: 21872511
I do occasionally create some websites and am also in the need of testing them locally before uploading.
Believe me, you can get the best experience and the least needs for modifications using vhosts when testing, because that's exactly what's available on the other end at the providers webserver.

Imagine you want to link to files that reside in a directory called /images, or you want to make use of cgi scripts, you want to test your favicon.ico, etc.etc.

All that requires the use of vhosts to work properly.

Expert Comment

ID: 21873119
You can also use different ports if you wish, so you can have http://192.168.x.x pointing fo folderA, http://192.168.x.x:81 pointing to folderB and so on, just make vhosts and assign different ports ( <VirtualHost 192.168.x.x:81> ). Also make sure that you add every port you use in your Listen directive and open the firewall to allow there ports (if you have any)

Author Comment

ID: 21877547
Thanks for the great replies!

I tried setting up the Virtual Hosts as is sounds like the best option.   Let me start by saying that my directory layout isn't the same as described but I don't think it matters.  My config is in /etc/httpd/conf/httpd.conf    

I added the line to include /etc/httpd/sites-enabled
I created directories sites-enabled and sites-available
I created the testdomain
Linked the test domain
touched the log files and then restarted Apache
After resolving some permissions issues, I was able to see the page.

I wasn't able to see my Intranet page any more though.  I created a Virtual Host for it by repeating the instructions, but still didn't work.
I went ahead and tried another Virtual Host for another one of the domains I want to work.  Once again repeating the instructions.  This one came up fine, but now the first domain didn't come up anymore.

Basically what I am figuring out is that only one domain seems to work at a time and it processes them in alphabetical order.

Any suggestions?  I had to comment out the line in the config file so my Intranet site would come back up for now.
LVL 14

Expert Comment

ID: 21877736
Oh, is this apache 1.0?
On 1.0, things are different...

Expert Comment

ID: 21879111
When you set up virtual hosts in Apache 1 you also need to create a default vhost (which ends up closest to the top of the config file) which would point to your intranet. Then every vhost after that will only work if the domain name matches, otherwise it defaults to the first declared virtual host.

It looks like you might have set up virtualhosts for the sites in FolderB without having first set up the first virtual host as pointing to FolderA.

To be honest I had a bit of a nightmare the first time I set up virtual hosts on my apache1 deployment but if I remember rightly I kept jumbling around the order of the virtual hosts and tweaking minor things - I'll take a proper look through my config when I get home - but the good news is that once you have it working you can append virtual hosts without any trouble at all.

Author Comment

ID: 21879200
Thanks zhuba!

So what you are saying is that all the vhosts are in one config file not individual ones?  

Expert Comment

ID: 21879242
You can put them in individual ones if you so wish, but I have a feeling apache 1's handling of them doesn't give the desired effect, so I combine them all into the one file so as to keep them in the order that I want them to be in.
LVL 14

Accepted Solution

agriesser earned 2000 total points
ID: 21881538
OK, I found an old apache 1.x configuration file.
You need to uncomment the following line in your httpd.conf:


This directive tells apache to listen for namebased virtual hosts at the given IP address and the given port, so that should be probably set to the internal IP of your webserver.

Now you can specify the virtualhosts either directly in httpd.conf, or you can create separate files and include them in the main httpd.conf.

To include additional files, use the include directive:

Include /path/to/vhosts-file

Such a vhosts-file/entry would then look like this:

  ServerAdmin   someone@somewhere.com
  DocumentRoot  /var/www/FolderB/website1
  ServerName    www.somedomain.com
  UserDir       /var/www/FolderB/website1
  ScriptAlias   /cgi-bin/ /var/www/FolderB/website1/cgi/
  Options ExecCGI
  User www-data
  Group nogroup
  CustomLog /var/log/apache/somedomain-access.log combined
  ErrorLog /var/log/apache/somedomain-error.log

If you want, you can add a default virtualhost entry that looks like this:

<VirtualHost _default_:*>

That way, all domains pointing to your webserver which are not explicitly listed in the namebased virtual host definitions will go here.

Author Comment

ID: 22007007
Sorry it has taken so long to get back to this question.  After I couldn't get the original suggestions to work, I simply made a symbolic link to the directory as a workaround.  So now they go to http://server/vhosts/sitename.com

I don't particularly like this solution, but the web team seems happy with it.  I just got done trying the config provided by Agriesser and I wasn't able to get it work.  I did get Apache restarted after some modifications to the VirtualHost section, but it gave errors about overlapping virtual hosts and only routed to the first site.  

I don't mind continuing to work on this problem just to know how to do it, but I don't know if I will get the web team to change at this point.

Thanks for the help.
LVL 14

Expert Comment

ID: 22010548
Overlapping directories? What did you set the DocumentRoots of your virtualhosts to?

Usually, you should have a structure like this:

/var/www    <- base directory
/var/www/host1    <- docroot for vhost1
/var/www/host2    <- docroot for vhost2


That way, nothing overlaps.

Author Comment

ID: 23864704
Sorry for not getting back to this question.  I never had time (or the need) to test the solution, but I am accepting what I feel is the correct answer.  Thanks for all the help.

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month12 days, 23 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question