Online Book Reader

Home Category

Apache Security - Ivan Ristic [86]

By Root 1923 0
vcommon

RewriteEngine On

RewriteMap LOWERCASE int:tolower

RewriteMap VHOST txt:/usr/local/apache/conf/vhost.map

# Translate the hostname to username using the

# map file, and store the username into the REQUSER

# environment variable for use later.

RewriteCond ${LOWERCASE:%{SERVER_NAME}} ^(.+)$

RewriteCond ${VHOST:%1|HTTPD} ^(.+)$

RewriteRule ^/(.*)$ /$1 [NS,E=REQUSER:%1]

# Change the URI to a ~username syntax and finish

# the request if it is not a PHP file.

RewriteCond %{ENV:REQUSER} !^HTTPD$

RewriteCond %{REQUEST_URI} !\.php$

RewriteRule ^/(.*)$ /~%{ENV:REQUSER}/$1 [NS,L,PT]

# Change the URI to a ~username syntax and finish

# the request if it is a PHP file.

RewriteCond %{ENV:REQUSER} !^HTTPD$

RewriteCond %{REQUEST_URI} \.php$

RewriteRule ^/(.*)$ /~%{ENV:REQUSER}/cgi-bin/php/~%{ENV:REQUSER}/$1 \

[NS,L,PT,E=REDIRECT_STATUS:302]

# The remaining directives make PHP work when content

# is genuinely accessed through the ~username syntax.

RewriteCond %{ENV:REQUSER} ^HTTPD$

RewriteCond %{REQUEST_URI} \.php$

RewriteRule ^/~(\w+)/(.*)$ /~$1/cgi-bin/php/~$1/$2

[NS,L,PT,E=REDIRECT_STATUS:302]

You will need to create a simple mod_rewrite map file, /usr/local/apache/conf/vhost.map, to map virtual hosts to usernames:

jelena.example.com jelena

ivanr.example.com ivanr

There can be any number of virtual hosts mapping to the same username. If virtual hosts have www prefixes, you may want to add them to the map files twice, once with the prefix and once without.

FastCGI

If mod_fastcgi (http://www.fastcgi.com) is added to Apache, it can work to make scripts persistent, where scripts support persistent operation. I like FastCGI because it is easy to implement yet very powerful. Here, I demonstrate how you can make PHP persistent. PHP comes with FastCGI support built-in that is compiled in by default, so you only need to install mod_fastcgi. The example is not PHP specific so it can work for any other binary that supports FastCGI.

To add mod_fastcgi to Apache 1, type the following while you are in the mod_fastcgi source folder:

$ apxs -o mod_fastcgi.so -c *.c

# apxs -i -a -n fastcgi mod_fastcgi.so

To add mod_fastcgi to Apache 2, type the following while you are in the mod_fastcgi source folder:

$ cp Makefile.AP2 Makefile

$ make top_dir=/usr/local/apache

# make top_dir=/usr/local/apache install

When you start Apache the next time, one more process will be running: the FastCGI process manager, which is responsible for managing the persistent scripts, and the communication between them and Apache.

Here is what you need to add to Apache configuration to make it work:

# Load the mod_fastcgi module.

LoadModule fastcgi_module modules/mod_fastcgi.so

# Tell it to use the suexec wrapper to start other processes.

FastCgiWrapper /usr/local/apache/bin/suexec

# This configuration will recycle persistent processes once every

# 300 seconds, and make sure no processes run unless there is

# a need for them to run.

FastCgiConfig -singleThreshold 100 -minProcesses 0 -killInterval 300

I prefer to leave the existing cgi-bin/ folders alone so non-FastCGI scripts continue to work. (As previously mentioned, scripts must be altered to support FastCGI.) This is why I create a new folder, fastcgi-bin/. A copy of the php binary (the FastCGI version) needs to be placed there. It makes sense to remove this binary from the cgi-bin/ folder to avoid the potential for confusion. A FastCGI-aware php binary is compiled as a normal CGI version but with the addition of the --enable-fastcgi switch on the configure line. It is worth checking for FastCGI support now because it makes troubleshooting easier later. If you are unsure whether the version you have supports FastCGI, invoke it with the -v switch. The supported interfaces will be displayed in the brackets after the version number.

$ ./php -v

PHP 5.0.2 (cgi-fcgi) (built: Nov 19 2004 11:09:11)

Copyright (c) 1997-2004 The PHP Group

Zend Engine v2.0.2, Copyright (c) 1998-2004 Zend Technologies.

This is what an suEXEC-enabled and FastCGI-enabled

Return Main Page Previous Page Next Page

®Online Book Reader