Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 619
  • Last Modified:

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.
0
tech1984
Asked:
tech1984
  • 5
  • 4
  • 3
  • +2
1 Solution
 
zhubaCommented:
Can't you just setup FolderA and FolderB and then refer to all sites as http://serverip/sitename1 -> FolderB/sitename1 ?
0
 
agriesserCommented:
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
 
fosiul01Commented:
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
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

 
agriesserCommented:
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
 
tkalchevCommented:
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
 
tech1984Author Commented:
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
 
agriesserCommented:
Oh, is this apache 1.0?
On 1.0, things are different...
0
 
zhubaCommented:
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
 
tech1984Author Commented:
Thanks zhuba!

So what you are saying is that all the vhosts are in one config file not individual ones?  
0
 
zhubaCommented:
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
 
agriesserCommented:
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
 
tech1984Author Commented:
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
 
agriesserCommented:
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
 
tech1984Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now