Thursday, December 1, 2011

Apache2.2: Defining Virtual Hosts, Re-direction and LDAP authentication uncovered

I usually put references at the bottom of the article. But this time I want to put it in the beginning of the article. If you are Apache beginner, trust me you want to go through these references before you start doing anything.

References:
Apache Directives: http://httpd.apache.org/docs/2.2/mod/directive-dict.html#Context
Name-Based Virtual Hosts: http://httpd.apache.org/docs/2.2/vhosts/name-based.html
LDAP directory for authentication: http://httpd.apache.org/docs/2.3/mod/mod_authnz_ldap.html
LDAP and Active Directory Terminologies: http://rapidtechguide.blogspot.com/2011/07/directories-terminology-active.html


Practice Scenario:

Windows Server
Web Server: Apache2.2
IP add: 192.168.1.10
Port: 80
Websites hosted:
www1.test.org
www.mytest.org
www.urtest.org
www.ourtest.org
www.everyonetest.org
www.lovetest.org



Before you start testing your apache setup make sure that DNS is configured properly, hosts file is configured properly.

STEP1:
File: C:\Windows\System32\drivers\etc\hosts
192.168.1.10 www1.test.org
192.168.1.10 www.mytest.org
192.168.1.10 www.urtest.org
192.168.1.10 www.ourtest.org
192.168.1.10 www.everyonetest.org
192.168.1.10 www.lovetest.org


#everyonetest.org and lovetest.org are alias of ourtest.org

STEP2:
Configure DNS server to point those domains 'mytest.org, urtest.org, ourtest.org,everyonetest.org,lovetest.org' to 192.168.1.10. You can contact your DNS administrator

STEP3:
Say Apache2.2 is installed in C: drive
C:\apache

and you are publishing your web contents at C:\website
C:\website\mytest
C:\website\urtest
C:\website\ourtest

STEP4:
C:\apache\conf\httpd.conf# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
Listen 192.168.1.10:80

#uncomment these for LDAP authentication support
LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
LoadModule ldap_module modules/mod_ldap.so

ServerName www1.test.org

#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
DirectoryIndex index.htm index.html index.shtml index.html.var index.php index.pl default.html default.htm
</IfModule>

#Set LogLevel to debug if you are configuring Apache for first time so that you can collect enough logging information for troubleshooting purpose
LogLevel debug

<IfModule mime_module>
#Uncomment this line to have shtml MIME support
AddType text/html .shtml
</IfModule>

# Uncomment following line to add Virtual hosts in different file
Include conf/extra/httpd-vhosts.conf
STEP5:
C:\conf\extra\httpd-vhosts.conf
#
# Use name-based virtual hosting.
#
NameVirtualHost 192.168.1.10:80
#Regular Virtual Host Configuration Example# WWW.MYTEST.ORG BEGIN
# mytest.org is published at C:/website/mytest

<VirtualHost 192.168.1.10:80>
ServerName www.mytest.org
DocumentRoot C:/website/mytest

<Directory C:/website/mytest>
AllowOverride None
Order deny,allow
Allow from all
</Directory>

ErrorLog "logs/www.mytest.org-error.log"
CustomLog "logs/www.mytest.org-access.log" common

ErrorDocument 401 /unauth.shtml
ErrorDocument 403 /forbid.shtml
ErrorDocument 404 /notfound.shtml
ErrorDocument 500 /error.shtml
</VirtualHost>

# WWW.MYTEST.ORG END
#Re-Direction Configuration Example
# WWW.URTEST.ORG BEGIN
# urtest.org is simply re-directed to www.wikipedia.org

<VirtualHost 192.168.1.10:80>
ServerName www.urtest.org
Redirect permanent / https://www.wikipedia.org
</VirtualHost>

# WWW.URTEST.ORG END


#LDAP Authentication Configuration Example
# WWW.OURTEST.ORG BEGIN
# ourtest.org is published at C:/website/ourtest
# LDAP authentication is required to access the conents of C:/website/ourtest

<VirtualHost 192.168.1.10:80>
ServerName www.ourtest.org
ServerAlias everyonetest.org lovetest.org
DocumentRoot C:/website/mytest

<Directory C:/website/mytest>
AllowOverride None
Order deny,allow
Allow from all

AuthType Basic
AuthBasicProvider ldap
AuthzLDAPAuthoritative on

AuthLDAPBindDN "cn=ldapuser,ou=\"Super Accounts\",DC=ad,DC=test,DC=org"
AuthLDAPBindPassword ipassword

AuthName "Only for registered users"

AuthLDAPURL "ldap://ldap.test.org:389/ou=students,ou=toefl,dc=ad,dc=test,dc=org?name"

#Syntax for AuthLDAPRUL ldap://host:port/basedn?attribute?scope?filter
#For more info refer to page 3 of https://www.ietf.org/rfc/rfc2255.txt
require valid-user
</Directory>

ErrorLog "logs/www.mytest.org-error.log"
CustomLog "logs/www.mytest.org-access.log" common

ErrorDocument 401 /unauth.shtml
ErrorDocument 403 /forbid.shtml
ErrorDocument 404 /notfound.shtml
ErrorDocument 500 /error.shtml
</VirtualHost>

# WWW.OURTEST.ORG END
Some other tips:
I would prefer to use Internet Explorer(IE) for testing webpages as lots of users are using IE. In IE go to Tools--> Internet Options and Check 'Delete Browsing History on Exit". Sometimes Internet Browser displays webpages from it's temp files, so even if your apache/web server is stopped you might see webpages working (I have seen it and it took me quite a time to get around with it). Or sometimes if you make some content changes, your browser may not reflect those changes. So, while perfoming apache setup and testing 'Delete Browsing History' really helps. Close the browser. Re-open the browser and see if you got ur desired output.
Sometime you might have to flush local DNS cache. If you have to you can use following command
> ipconfig /flushdns

Good Luck!!!

Additional References:
http://www.yolinux.com/TUTORIALS/LinuxTutorialApacheAddingLoginSiteProtection.html


Also read per-User web directories documentation from Apache
http://httpd.apache.org/docs/2.0/howto/public_html.html

No comments: