How To Set Up suPHP With PHP4 And PHP5 - Page 2

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Sun, 2006-06-11 08:59. ::

4 Install PHP4-CGI

I will first describe how to use suPHP with PHP4, then later in this tutorial how to use it with PHP5, and at the end how to use it with both installed at the same time.

As I mentioned earlier, suPHP needs PHP as CGI instead of the Apache module. Therefore we install the php4-cgi package like this:

apt-get install php4-cgi


5 Install suPHP

Next we download and unpack the suPHP sources. At the time of this writing the current version was 0.6.1:

cd /tmp
wget http://www.suphp.org/download/suphp-0.6.1.tar.gz
tar xvfz suphp-0.6.1.tar.gz
cd suphp-0.6.1

Now we must edit src/apache2/mod_suphp.c because otherwise you'll get an error like this one:

Forcing reload of web server: Apache2Syntax error on line 49 of /etc/apache2/sites-enabled/000-default:
suPHP_AddHandler not allowed here

when you put some suPHP directives in your Apache vhosts. Open the file, and in the lines 316 and 317 (the line numbers might differ if you use a suPHP version other than 0.6.1) you will find the string ACCESS_CONF. Replace it with RSRC_CONF | ACCESS_CONF so that the two lines look like this:

vi src/apache2/mod_suphp.c

    AP_INIT_ITERATE("suPHP_AddHandler", suphp_handle_cmd_add_handler, NULL, RSRC_CONF | ACCESS_CONF, "Tells mod_suphp to handle these MIME-types"),
AP_INIT_ITERATE("suPHP_RemoveHandler", suphp_handle_cmd_remove_handler, NULL, RSRC_CONF | ACCESS_CONF, "Tells mod_suphp not to handle these MIME-types"),

(If you prefer to edit the file on your Windows system, make sure you save it with Unix line breaks instead of Windows line breaks before you transfer the file back to your Linux system!)

Then we compile and install suPHP:

./configure --prefix=/usr --sysconfdir=/etc --with-apache-user=www-data --with-setid-mode=paranoid --with-apxs=/usr/bin/apxs2
make
make install

Now we need to create the configuration file /etc/suphp.conf. We can do this by simply copying the template that comes with the sources:

cp /tmp/suphp-0.6.1/doc/suphp.conf-example /etc/suphp.conf

We must make a few changes to /etc/suphp.conf. Set the value of webserver_user to www-data (this is the username of the Apache user in Debian's default Apache package), and for x-httpd-php we must specify our php4-cgi binary which is /usr/bin/php4-cgi so the final file looks like this:

vi /etc/suphp.conf

[global]
;Path to logfile
logfile=/var/log/suphp.log

;Loglevel
loglevel=info

;User Apache is running as
webserver_user=www-data

;Path all scripts have to be in
docroot=/

;Path to chroot() to before executing script
;chroot=/mychroot

; Security options
allow_file_group_writeable=false
allow_file_others_writeable=false
allow_directory_group_writeable=false
allow_directory_others_writeable=false

;Check wheter script is within DOCUMENT_ROOT
check_vhost_docroot=true

;Send minor error messages to browser
errors_to_browser=false

;PATH environment variable
env_path=/bin:/usr/bin

;Umask to set, specify in octal notation
umask=0077

; Minimum UID
min_uid=100

; Minimum GID
min_gid=100


[handlers]
;Handler for php-scripts
x-httpd-php=php:/usr/bin/php4-cgi

;Handler for CGI-scripts
x-suphp-cgi=execute:!self

Then we have to add the suPHP module to the Apache configuration. Add the following line to /etc/apache2/httpd.conf:

vi /etc/apache2/httpd.conf

LoadModule suphp_module       /usr/lib/apache2/modules/mod_suphp.so

Then restart Apache:

/etc/init.d/apache2 restart


6 Apache Configuration

In this chapter I assume we have a web site www.example.com on the IP address 1.2.3.4 with the document root /var/www and the following vhost configuration:

<VirtualHost 1.2.3.4>
ServerName www.example.com
ServerAdmin webmaster@example.com
DocumentRoot /var/www
</VirtualHost>

In order to enable suPHP for the vhost www.example.com and let it run PHP scripts as the user testuser and the group test, we modify the vhost as follows:

<VirtualHost 1.2.3.4>
ServerName www.example.com
ServerAdmin webmaster@example.com
DocumentRoot /var/www

suPHP_Engine on
suPHP_UserGroup testuser test
AddHandler x-httpd-php .php .php3 .php4 .php5
suPHP_AddHandler x-httpd-php
</VirtualHost>

AddHandler x-httpd-php refers to suPHP_AddHandler x-httpd-php which then refers to the x-httpd-php line in /etc/suphp.conf which means that scripts with the extensions .php, .php3, .php4, and .php5 are interpreted by /usr/bin/php4-cgi.

Restart Apache:

/etc/init.d/apache2 restart

Now we create a simple phpinfo() script and access it in our browser:

vi /var/www/info.php

<?php
phpinfo();
?>

Go to http://www.example.com/info.php in your browser, and you should see that you got PHP4 running:

If you encounter problems, please check /var/log/suphp.log and /var/log/apache2/error.log for errors. Most of the time you can fix them by changing the appropriate option in /etc/suphp.conf or by changing permissions of files/directories.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.