Tuesday, April 12, 2011

Running more than one website in a computer -virtual host


1) Modify httpd.conf to listen two ports in one IP
Listen 80
Listen 8000
If there are two interfaces, we use
Listen 192.0.2.1:80
Listen 192.0.2.5:8000

To debug virtual server configuration, we can use:
/usr/local/apache2/bin/httpd -S

2) Use VirtualHost  to specify the behavior of the ports.
Example of my virtual host:

<VirtualHost *:808>
     ServerName egps.localhost:808
     DocumentRoot C:/Users/jiansen/Desktop/CAS/jiansen_dir/www_GNSH/gnsh
     <Directory />
        Options FollowSymLinks
        AllowOverride None

        Order allow,deny
        Allow from all
       
       <IfModule rewrite_module>
          #RewriteEngine On
          #RewriteRule !maintenance_msg\.php$ /maintenance_msg.php [R,L]
         
          RewriteEngine On
          RewriteCond %{HTTP_REFERER} !^$
          RewriteCond %{HTTP_REFERER} !^http://localhost/.*$ [NC]     
          RewriteRule .*\.(jpe?g|png|gif|bmp|swf|flv) - [NC,F,L]
       </IfModule>   
     </Directory>
</VirtualHost>

If your sever name is www.example.com, you can use
ServerName www.example.com
Here HTTP_REFERER will contain the page that the browser claims it last visited.
^ means conition (is)

You also use same port for different server names
Example:
NameVirtualHost *:80

<VirtualHost *:80>
ServerName www.domain.tld
ServerAlias domain.tld *.domain.tld
DocumentRoot /www/domain
</VirtualHost>

<VirtualHost *:80>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
</VirtualHost>


The server  www.domain.tld and domain.tld  point to same www_root
while www.otherdomain.tld points to different folder
(reference: http://httpd.apache.org/docs/2.2/vhosts/name-based.html)

For running different sites with different ports:
Listen 80
Listen 8080

NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080

<VirtualHost 172.20.30.40:80>
ServerName www.example.com
DocumentRoot /www/folder1
</VirtualHost>


<VirtualHost 172.20.30.40:8080>
ServerName www.example.org
DocumentRoot /www/folder2
</VirtualHost>
 

http://httpd.apache.org/docs/2.2/vhosts/

No comments:

Post a Comment