For example: We will be assuming that www.hosangit.com exists and points to the our web server IP 10.0.1.204. The webroot for example.com is located in /home/vhosts/hosangit.com/public_html.
vim /etc/httpd/conf/httpd.conf / DocumentRootchange
Install Apache:
yum -y install httpdNOTE:Centos httpd package includes mod_rewrite + mod_userdir + mod_suexec.
Verify Apache installed by clicking navigating to http://youripaddress
Configure mass virtual hosting and add the code somewhere towards the top
vim /etc/httpd/conf/httpd.conf
Quote
LoadModule rewrite_module modules/mod_rewrite.so
Quote
## get the server name from the Host: header
UseCanonicalName Off
## splittable logs
LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon
RewriteEngine On
## Create a handle to convert upper or mixed-case to lower-case
RewriteMap lowercase int:tolower
##-----------------------------------
## where hostname has www prefix
##-----------------------------------
## Firstly create custom variable that contains the host without the www prefix
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule .? - [E=noWWWHost:%1]
## Map the virtualhost to the documentroot
RewriteCond %{REQUEST_URI} !^/~
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^/(.*)$ /home/vhosts/${lowercase:%{ENV:noWWWHost}}/public_html/$1
##-----------------------------------
## where hostname *does not* have www prefix
##-----------------------------------
## Map the virtualhost to the documentroot
RewriteCond %{REQUEST_URI} !^/~
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^/(.*)$ /home/vhosts/${lowercase:%{HTTP_HOST}}/public_html/$1
UseCanonicalName Off
## splittable logs
LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon
RewriteEngine On
## Create a handle to convert upper or mixed-case to lower-case
RewriteMap lowercase int:tolower
##-----------------------------------
## where hostname has www prefix
##-----------------------------------
## Firstly create custom variable that contains the host without the www prefix
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule .? - [E=noWWWHost:%1]
## Map the virtualhost to the documentroot
RewriteCond %{REQUEST_URI} !^/~
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^/(.*)$ /home/vhosts/${lowercase:%{ENV:noWWWHost}}/public_html/$1
##-----------------------------------
## where hostname *does not* have www prefix
##-----------------------------------
## Map the virtualhost to the documentroot
RewriteCond %{REQUEST_URI} !^/~
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^/(.*)$ /home/vhosts/${lowercase:%{HTTP_HOST}}/public_html/$1
- Our websites are located in /home/vhosts - each subfolder is the domain name of the website (without the www prefix) e.g. /home/vhosts/example.com.
- If a request hits the server for www.example.com it will be dynamically re-written to example.com.
- The documentroot directory is 'public_html' - this name is required by suEXEC as we'll see later. If you don't intend to use suEXEC then you make this whatever you like & update the rewriterule accordingly.
echo "index.html Hello World" > /home/vhosts/hosangit.com/public_html/index.html
Start Apache:
/etc/init.d/httpd start
Now, browsing to http://www.hosangit.com/ should result in 'index.html Hello World' being displayed. If this doesn't happen, check the Apache error log:
tail /var/log/httpd/error_log
As this is a shared web hosting platform, with many different users & websites we want to execute CGI scripts as the owner of the website rather than as the webserver process. suEXEC allows us to do this:
a) statically using virtualhost config or
b) dynamically using mod_userdir.
Firstly, each website must be owned by a user with the same name as the website's domain name. For example website hosangit.com:
useradd -d /home/vhosts/hosangit.com hosangit.com
This will create a user hosangit.com whose home directory is /home/vhosts/hosangit.com. All the webfiles for www.hosangit.com will go into /home/vhosts/hosangit.com/public_html.
You could either add each user manually to /etc/passwd or set up your host to look up an LDAP directory for account information.
Enable mod_userdir:
vim /etc/httpd/conf/httpd.conf
Ensure the following lines appear somewhere in your config:
Quote
LoadModule userdir_module modules/mod_userdir.so
UserDir public_html
UserDir public_html
Now when we request a page from the webserver using the form http://10.0.1.204/~hosangit.com Apache will look in the home directory for username hosangit.com for the public_html directory. This by itself isn't very useful as we don't want people to have to use the ~/ bit in their URLs. Instead we will silently rewrite the URL from www.hosangit.com to http://10.0.1.204/~hosangit.com behind the scenes.
This particular rewrite doesn't need to be done for every page - only for CGI scripts that we want to run under suEXEC. Other pages will get handled by the earlier re-write rules.
Edit /etc/httpd/conf/httpd.conf. Below the existing rewrite rules, insert the following:
Quote
## Rewrite script to userdir so we can use suEXEC
RewriteCond %{REQUEST_URI} !^/~
RewriteCond %{SCRIPT_FILENAME} /home/vhosts/(.*)/public_html/(.*\.(pl|cgi))
RewriteRule .* /~%1/%2 [PT,L]
AddHandler cgi-script .pl .cgi
Options +ExecCGI
RewriteCond %{REQUEST_URI} !^/~
RewriteCond %{SCRIPT_FILENAME} /home/vhosts/(.*)/public_html/(.*\.(pl|cgi))
RewriteRule .* /~%1/%2 [PT,L]
AddHandler cgi-script .pl .cgi
Options +ExecCGI
/etc/init.d/httpd reload
Create the Perl script test.pl in the virtual host's public_html with the following contents:
Quote
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "
print "Content-type: text/html\n\n";
print "
test.pl Hello World
\n";chmod +x test.pl chown hosangit.com:hosangit.com test.pl
Now, browsing to http://www.hosangit.com/test.pl should result in 'test.pl Hello World' being displayed. If this doesn't happen, check the following log files:
tail /var/log/httpd/error_log tail /var/log/httpd/suexec.log
Extending suEXEC processing to include PHP & Python (or anything else) is simple. First, make sure php-cli and python packages and their dependencies are installed:
yum install php-cli yum install python
Create a couple of 'hello world' scripts in your public_html folder as follows:
test.py
Quote
#!/usr/bin/python
print "Content-type: text/html\n\n"
print "test.py Hello world!"
print "Content-type: text/html\n\n"
print "test.py Hello world!"
test.php
Quote
#!/usr/bin/php-cgi
echo "test.php Hello world!";
?>
echo "test.php Hello world!";
?>
Note: each script needs to have the interpreter specified on the first line e.g. #!/usr/bin/python. To avoid having to do this with each file, you can do the following:
echo ":PHP:E::php::/usr/bin/php-cgi:" > /proc/sys/fs/binfmt_misc/register echo ":Python:E::py::/usr/bin/python:" > /proc/sys/fs/binfmt_misc/registerNow modify Apache config to rewrite requests for files with .php and .py extensions. We also need to add these extensions to the handler for cgi-script
/etc/httpd/conf/httpd.conf
Quote
## Rewrite script to userdir so we can use suEXEC
RewriteCond %{REQUEST_URI} !^/~
RewriteCond %{SCRIPT_FILENAME} /home/vhosts/(.*)/public_html/(.*\.(pl|cgi|php|py))
RewriteRule .* /~%1/%2 [PT,L]
AddHandler cgi-script .pl .cgi .php .py
Options +ExecCGI
RewriteCond %{REQUEST_URI} !^/~
RewriteCond %{SCRIPT_FILENAME} /home/vhosts/(.*)/public_html/(.*\.(pl|cgi|php|py))
RewriteRule .* /~%1/%2 [PT,L]
AddHandler cgi-script .pl .cgi .php .py
Options +ExecCGI
Reload Apache config:
/etc/init.d/httpd reload
Now, browsing to http://www.hosangit.com/test.py or http://www.hosangit.com/test.php should result in 'Hello World' being displayed. If this doesn't happen, check the following log files:
tail /var/log/httpd/error_log tail /var/log/httpd/suexec.log
Catchall
If you'd like to direct requests for non-existent virtual hosts to one catchall site, make this your last rewrite rule in /etc/httpd/conf/httpd.conf:
Quote
## Redirect non-existent virtualhosts
RewriteCond %{REQUEST_URI} !^/~
RewriteCond %{SCRIPT_FILENAME} (/home/vhosts/.*)/public_html/.*
RewriteCond %1 !-d
RewriteRule .? http://www.google.com [R,NS,L]
RewriteCond %{REQUEST_URI} !^/~
RewriteCond %{SCRIPT_FILENAME} (/home/vhosts/.*)/public_html/.*
RewriteCond %1 !-d
RewriteRule .? http://www.google.com [R,NS,L]

















