Link to home
Start Free TrialLog in
Avatar of tech1984
tech1984

asked on

Running multiple websites on one server with Apache

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.
Avatar of zhuba
zhuba
Flag of New Zealand image

Can't you just setup FolderA and FolderB and then refer to all sites as http://serverip/sitename1 -> FolderB/sitename1 ?
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
        </Directory>

        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

</VirtualHost>

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.
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.
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.
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)
Avatar of tech1984
tech1984

ASKER

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.
Oh, is this apache 1.0?
On 1.0, things are different...
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.
Thanks zhuba!

So what you are saying is that all the vhosts are in one config file not individual ones?  
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.
ASKER CERTIFIED SOLUTION
Avatar of agriesser
agriesser
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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

etc.

That way, nothing overlaps.
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.