Solved

Running multiple websites on one server with Apache

Posted on 2008-06-25
14
598 Views
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.
0
Comment
Question by:tech1984
  • 5
  • 4
  • 3
  • +2
14 Comments
 
LVL 7

Expert Comment

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

Expert Comment

by:agriesser
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
        </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.
0
 
LVL 29

Expert Comment

by:fosiul01
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.
0
 
LVL 14

Expert Comment

by:agriesser
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.
0
 
LVL 9

Expert Comment

by:tkalchev
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)
0
 

Author Comment

by:tech1984
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.
0
 
LVL 14

Expert Comment

by:agriesser
ID: 21877736
Oh, is this apache 1.0?
On 1.0, things are different...
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 7

Expert Comment

by:zhuba
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.
0
 

Author Comment

by:tech1984
ID: 21879200
Thanks zhuba!

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

Expert Comment

by:zhuba
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.
0
 
LVL 14

Accepted Solution

by:
agriesser earned 500 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:

NameVirtualHost 192.168.1.1:80

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:

<VirtualHost 192.168.1.1:80>
  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
</VirtualHost>

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

<VirtualHost _default_:*>
   [...]
</VirtualHost>

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

Author Comment

by:tech1984
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.
0
 
LVL 14

Expert Comment

by:agriesser
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

etc.

That way, nothing overlaps.
0
 

Author Comment

by:tech1984
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.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now