LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 11-16-2009, 12:02 PM   #1
bahbahthelamb
Member
 
Registered: Jul 2006
Location: Fort Worth, Texas
Distribution: openSUSE 11, RHEL4 & 5, CentOS 5
Posts: 57

Rep: Reputation: 16
Dynamic Virtual Hosting with Multiple Domain Names


Using a RHEL 5.3 server, I am trying to setup dynamic virtual hosting where sub.companyname.com goes to /var/www/internal/sub and clientname.previewserver.com goes to /var/www/clients/clientname/dev

I have the latter working with


Code:
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon

<VirtualHost *:80>
  ServerName clients.previewserver.com
  CustomLog logs/access_log.clients vcommon
  VirtualDocumentRoot /var/www/clients/%1/dev
  ScriptAlias /cgi-bin/ /var/www/std-cgi
</VirtualHost>
so I tried to add...

Code:
<VirtualHost *:80>
  ServerName sub.companyname.com
  CustomLog logs/access_log.internal vcommon
  VirtualDocumentRoot /var/www/internal/%1
  ScriptAlias /cgi-bin/ /var/www/std-cgi
</VirtualHost>
however when I go to http://scripts.companyname.com it still looks in /var/www/clients/scripts/dev instead of /var/www/internal/scripts

I'm rather new with dynamic virtual hosting and I was lucky in getting the first implementation working, but obviously I need more information on the subject. Everywhere I searched, it was pushing for rewrite cond/rules, which I think is more complex than for my simple needs.

Thanks,
-Josh
 
Old 11-17-2009, 04:36 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
Did you graceful/restart apache after making the config changes and before testing?
 
Old 11-18-2009, 09:46 AM   #3
bahbahthelamb
Member
 
Registered: Jul 2006
Location: Fort Worth, Texas
Distribution: openSUSE 11, RHEL4 & 5, CentOS 5
Posts: 57

Original Poster
Rep: Reputation: 16
Yes, I did. See the reason why my first attempt did not work is that the separate dynamic virtual hosting configurations require separate IP bindings. It would have worked, if I had already migrated our other IPs to this new server since companyname.com is bound to its own IP on the same machine. However, I wanted to get everything working prior to migrating additional addresses, and I succeeded, but I had to use the RewriteEngine.

In case anyone is eager to see the solution, here is my one an only VirtualHost

Code:
<VirtualHost *:80>
  LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
  CustomLog logs/access_log vcommon
  RewriteEngine On

  # The first rewrite is to enforce the usage of www by throwing a 301 to non-www addresses
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]

  # The next rewrite is to search for people accessing our internal subdomains
  RewriteMap lowercase int:tolower
  RewriteCond ${lowercase:%{SERVER_NAME}} ^www\.[a-z-]+\.companyname\.com
  RewriteRule ^(.*) ${lowercase:%{SERVER_NAME}}$1 [C]
  RewriteRule ^www\.([a-z-]+)\.companyname\.com/(.*) /var/www/internal/$1/$2

  # The next rewrite is use a map file to point the extra domain names to their document roots
  RewriteMap vhost txt:/var/www/vhost.map
  RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
  RewriteCond ${vhost:%1} ^(/.*)$
  RewriteRule ^/(.*)$ /var/www%1/$1

  # Then finally, the preview server sites
  VirtualDocumentRoot /var/www/clients/%-3/dev
  ScriptAlias /cgi-bin/ /var/www/std-cgi
</VirtualHost>
I know that I should probably turn the last bit into a rewrite as well, as I know that if some clientname.otherdomainpointedatus.com will pull up the preview sites, but I'm not too worried about that, since these are the folders with robots.txt files to stop the search spidering. Also the vhost.map file contains...

Code:
www.clientlivesite.com /clients/clientname/livesite
www.anotherinternaldomainname.com /internal/ourservice
...et cetera...
I'm sure someone could probably propose some revisions to this, but at least it is working and I can start slowly migrating our sites to this new server and I don't have the reload httpd to add new vhosts now.

Last edited by bahbahthelamb; 11-18-2009 at 04:57 PM.
 
Old 11-18-2009, 10:19 AM   #4
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
Nice little tidbit to leave for those who come after you with a similar problem! Good Job.
 
Old 11-18-2009, 05:48 PM   #5
bahbahthelamb
Member
 
Registered: Jul 2006
Location: Fort Worth, Texas
Distribution: openSUSE 11, RHEL4 & 5, CentOS 5
Posts: 57

Original Poster
Rep: Reputation: 16
More revisions...

Code:
<VirtualHost *:80>
  LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
  CustomLog logs/access_log vcommon

  RewriteEngine On
  RewriteMap lowercase int:tolower

  # First to always add the WWW
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]

  # Here is all the redirects for Domain Forwards, using a forwarder map
  RewriteMap forwarder txt:/var/www/forwarder.map
  RewriteCond ${lowercase:%{HTTP_HOST}} ^(.+)$
  RewriteCond ${forwarder:%1} ^(/.*)$
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

  # Now the rewrites for the internal subdomains
  RewriteCond ${lowercase:%{SERVER_NAME}} ^www\.[a-z-]+\.companyname\.com
  RewriteRule ^(.*) ${lowercase:%{SERVER_NAME}}$1 [C]
  RewriteRule ^www\.([a-z-]+)\.companyname\.com/(.*) /var/www/internal/$1/$2

  # Now the rewrites for the preview sites
  RewriteCond ${lowercase:%{SERVER_NAME}} ^www\.[a-z-]+\.previewsite\.com
  RewriteRule ^(.*) ${lowercase:%{SERVER_NAME}}$1 [C]
  RewriteRule ^www\.([a-z-]+)\.previewsite\.com/(.*) /var/www/clients/$1/dev/$2

  # Now the rewrites for other domains, using a vhost map
  RewriteMap vhost txt:/var/www/vhost.map
  RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
  RewriteCond ${vhost:%1} ^(/.*)$
  RewriteRule ^/(.*)$ /var/www%1/$1
</VirtualHost>
This now works with 301 redirects based on a forwarders' map, but there is one issue, for some reason it won't forward unless it is just the root of the forwarded domain name. For now, I just have the forwarded domains still listed in the vhost.map file so that there's no 404 pages, but I have to figure this out. I've tried checking off of %{HTTP_HOST} as well as %{SERVER_NAME}.

What works:
http://www.olddomain.com/ -> http://www.newdomain.com// (Not sure why the extra slash gets added either, but not as big of a deal)

What doesn't work
http://www.olddomain.com/pg2.html -> http://www.newdomain/pg2.html

I'll be trying to figure this one out come tomorrow's work day, if anything stands out obviously wrong, someone speak up.

/var/www/forwarders.map is similar to the vhost.map
Code:
www.olddomain.com www.newdomain.com
...et cetera...
 
Old 11-19-2009, 02:58 PM   #6
bahbahthelamb
Member
 
Registered: Jul 2006
Location: Fort Worth, Texas
Distribution: openSUSE 11, RHEL4 & 5, CentOS 5
Posts: 57

Original Poster
Rep: Reputation: 16
Got this all working, I'm calling it RC1, since it is functional, but there is definitely more improvements that I can think of.

Code:
<VirtualHost *:80>
 # Set Logs
  LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
  CustomLog logs/access_log vcommon

 # Initialize mod_rewrite
  RewriteEngine On
  RewriteMap lowercase int:tolower

 # First to always add the WWW
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]

 # Here is all the redirects for Domain Forwards, using a forwarder map
   RewriteMap forwarder txt:/var/www/forwarder.map
   RewriteCond ${lowercase:%{HTTP_HOST}} ^(.+)$
   RewriteCond ${forwarder:%1} ^www\.(.*)$
   RewriteRule ^(.*)$ http://www\.%1$1 [R=301,L]

  # ------- /var/www/forwarder.map ------- #
  # www.old-domain.com www.new-domain.com  #
  # -------------------------------------- #

 # Now the rewrites for the internal subdomains
  RewriteCond %{REQUEST_URI} !^/error/ # Make sure global error folder is accessible
  RewriteCond ${lowercase:%{SERVER_NAME}} ^www\.[a-z-]+\.companyname\.com
  RewriteRule ^(.*) ${lowercase:%{SERVER_NAME}}$1 [C]
  RewriteRule ^www\.([a-z-]+)\.companyname\.com/(.*) /var/www/internal/$1/$2

 # Now the rewrites for the preview sites
  RewriteCond %{REQUEST_URI} !^/error/ # Make sure global error folder is accessible
  RewriteCond ${lowercase:%{SERVER_NAME}} ^www\.[a-z-]+\.aoipreview\.com
  RewriteRule ^(.*) ${lowercase:%{SERVER_NAME}}$1 [C]
  RewriteRule ^www\.([a-z-]+)\.aoipreview\.com/(.*) /var/www/clients/$1/dev/$2

 # Now the rewrites for other domains, using a vhost map
  RewriteMap vhost txt:/var/www/vhost.map
  RewriteCond %{REQUEST_URI} !^/error/ # Make sure global error folder is accessible
  RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
  RewriteCond ${vhost:%1} ^(/.*)$
  RewriteRule ^/(.*)$ /var/www%1/$1

  # --------------- /var/www/vhost.map --------------- #
  # www.client-domain.com clients/clientname/livesite  #
  # www.internal-domain.com internal/sitename/         #
  # -------------------------------------------------- #

 # Default root, in case nothing is found above
 VirtualDocumentRoot /var/www/internal/

</VirtualHost>
I was also thinking of making RewriteCond's to manage my robots.txt files since they are currently managed per server root (which is obviously a great quantity); that'll be v1.1

Last edited by bahbahthelamb; 11-19-2009 at 03:01 PM.
 
1 members found this post helpful.
Old 02-10-2010, 12:43 PM   #7
bahbahthelamb
Member
 
Registered: Jul 2006
Location: Fort Worth, Texas
Distribution: openSUSE 11, RHEL4 & 5, CentOS 5
Posts: 57

Original Poster
Rep: Reputation: 16
There is a serious issue that has risen from using this, the .htaccess files are not working now. I already have a few live sites on this new server, and now I have gotten to one that uses a custom 404 page. I was thinking no big deal, but after trying for a while I realized that it is not processing the .htaccess files at all. I checked to ensure that httpd.conf had
Code:
AccessFileName .htaccess
For testing I temporarily removed this from httpd.conf
Code:
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
</Files>
This was mostly to ensure that I didn't have some global location setting for the access file that I didn't know about. After removing this, and then sending a server request for /.htaccess, it does pull the local access file. I added this code to get it working and buy more time:
Code:
<Directory "/var/www/clients/clientname/livesite">
    ErrorDocument 404 /404.php
</Directory>
However, ultimately I would like to get Access Files working so I can manage site overloads in a decentralized manner without the need for restarting Apache with every minor changes. For this specific situation, I wouldn't mind a centralized, look for a local 404.php file first and then go to the global. I've been looking through the forums all morning trying to seek related cases, but it looks like there are too few people dealing with complex dynamic virtual hosting. Chances are I am going to find my own answer (and post it here for others), but if someone stumbles upon this and has some input, it is greatly appreciated.

Last edited by bahbahthelamb; 02-10-2010 at 12:58 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hosting multiple subdomains on 1 dynamic IP ABL Linux - Server 2 05-16-2009 07:11 PM
Dynamic virtual hosting and ulr without www tomekf Linux - Server 5 10-16-2006 03:10 AM
Domain names and hosting eva General 6 04-16-2006 11:52 AM
Virtual hosting/ domain question Kovacs Linux - Networking 2 11-23-2004 05:55 AM
Problems With Apache & Virtual Hosting Using Dynamic DNS moetjojo Linux - Networking 7 12-02-2002 04:51 AM


All times are GMT -5. The time now is 04:17 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration