shadowmac Posted August 8, 2009 Share Posted August 8, 2009 One method of doing mass virtualhosting using mod_rewrite to dynamically map a list of directories on your filesystem to virtual hosts. Additionally, by rewriting the incoming URL to the user's home directory we can make use of suEXEC to have Apache execute CGI scripts as the owner of the script. 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 / DocumentRoot change to Install Apache: yum -y install httpd NOTE: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 LoadModule rewrite_module modules/mod_rewrite.so At the bottom of the file add the following: ## get the server name from the Host: headerUseCanonicalName 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 .? - ## 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 NOTES: 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. Create index.html in the virtual host's public_html: 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 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: LoadModule userdir_module modules/mod_userdir.so 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: ## Rewrite script to userdir so we can use suEXECRewriteCond %{REQUEST_URI} !^/~ RewriteCond %{SCRIPT_FILENAME} /home/vhosts/(.*)/public_html/(.*\.(pl|cgi)) RewriteRule .* /~%1/%2 AddHandler cgi-script .pl .cgi Options +ExecCGI Reload Apache config: /etc/init.d/httpd reload Create the Perl script test.pl in the virtual host's public_html with the following contents: #!/usr/bin/perlprint "Content-type: text/html\n\n"; print " test.pl Hello World\n"; Give the script executable permissions & change ownership to the correct user: 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 #!/usr/bin/pythonprint "Content-type: text/html\n\n" print "test.py Hello world!" test.php #!/usr/bin/php-cgiecho "test.php Hello world!"; ?> Chown the files to be owned by the correct user, and set the executable bit with chmod +x. 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/register Now 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 ## Rewrite script to userdir so we can use suEXECRewriteCond %{REQUEST_URI} !^/~ RewriteCond %{SCRIPT_FILENAME} /home/vhosts/(.*)/public_html/(.*\.(pl|cgi|php|py)) RewriteRule .* /~%1/%2 AddHandler cgi-script .pl .cgi .php .py Options +ExecCGI PHP NOTE: Set cgi.force_redirect = 0 in /etc/php.ini to avoid CGI REDIRECT_STATUS errors 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: ## Redirect non-existent virtualhostsRewriteCond %{REQUEST_URI} !^/~ RewriteCond %{SCRIPT_FILENAME} (/home/vhosts/.*)/public_html/.* RewriteCond %1 !-d RewriteRule .? http://www.google.com Link to comment Share on other sites More sharing options...
shadowmac Posted August 8, 2009 Author Share Posted August 8, 2009 Install CentOS 5.3 as per these instructions: Assumptions: 512Mb RAM 250GB HDD Boot from DVD CentOS-5.3-i386-bin-DVD.iso type: linux text and press return Skip Media Check OK Welcome to CentOS English and then OK Language Selection us and then OK Keyboard Selection Yes Warning ... erasing ALL DATA? Create custom layout and then OK Partitioning Type To create the / partition ‘root’: * On the Partitioning screen, click New. * In the Mount Point field, type / . * For the Filesystem type select ext3. * In the Size (MB) field, type 15360, then click OK. To create the /boot partition: * On the Partitioning screen, click New. * In the Mount Point field, type /boot. * For the Filesystem type select ext3. * In the Size (MB) field, type 500, then click OK. To create the /tmp partition * On the Partitioning screen, click New. * In the Mount Point field, type /tmp . * For the Filesystem type select ext3. * In the Size (MB) field, type 2048, then click OK. To create the /usr partition * On the Partitioning screen, click New. * In the Mount Point field, type /usr . * For the Filesystem type select ext3. * In the Size (MB) field, type 40960, then click OK. To create the /var partition * On the Partitioning screen, click New. * In the Mount Point field, type /var . * For the Filesystem type select ext3. * In the Size (MB) field, type 40960, then click OK. To create the swap partition * On the Partitioning screen, click New. * For the Filesystem type field, select swap. * In the Size (MB) field, enter a number that is twice the current RAM (1024 If you are using 512MB Ram), then click OK. To create the /home partition * On the Partitioning screen, click New. * In the Mount Point field, type /home. * For the Filesystem type select ext3. * In the Size (MB) field, select Fill all available space, then click OK then click OK to exit Partitioning Ok Boot Loader Configuration (Use GRUB Boot Loader selected) OK Boot Loader Configuration (leave blank and unchecked) OK Boot Loader Configuration (grub password leave blank and unchecked) OK Boot Loader Configuration (Default OS to boot) OK Boot Loader Configuration (where to install boot loader, leave default) Yes Configuration Network Interface Check Activate on boot and also Enable IPv4 support Network Configuration for eth0 and then OK Manual address configuration, our example we use 10.0.1.200 255.255.255.0 then OK IPv4 Configuration for eth0 Gateway: 10.0.1.1 Primary DNS: 10.0.1.1 then OK Miscellaneous Network Settings manually (only option if you manually set IP address previously) enter hostname: hosting1 and then OK Hostname Configuration System clock uses UTC, select timezone and then OK Time Zone Selection Enter root password twice and then OK Root Password uncheck everything but Server and then select Customize software selection and then OK Package Selection Package Selections are as follows: DNS name server - bind-chroot Editors - vim-enhanced FTP server Mail server - dovecot Mail server - spamassassin Mail server - postfix Mysql Database - mysql-server Web server - mod_ssl Web server - webalizer Web server - php Web server - php-pear Web server - http-suexec Web server - php-mysql OK to begin installation Reboot Login: root vi disable_service.sh apaste the following#!/bin/bash## Andrew # Quick dirty script to disable unwanted services# 03-02-2008#services="acpid anacron apmd autofs bluetooth cups firstboot gpm haldaemon messagebus mdmonitor hidd ip6tables kudzu lvm2-monitor netfs nfslock pcscd portmap rpcgssd rpcidmapd sendmail smartd yum-updatesd"for service in $services; do service $service stop chkconfig --del $servicedone :wq chmod +x disable_service.sh./disable_service.sh More found here Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now