Setting up PHP5 in parallel with PHP4 in SuSE 10.1
In a previous Mini-Howto (see How to setup PHP5 on SuSE 9.2) we have discussed a similar setup with SuSE 9.2. The major difference is that this article will show how to have simultaneous installation of both PHP4 and PHP5 at the same time without the need to run a switching script to select which PHP version to activate. In this new setup we have opted to configure Apache with two virtual hosts based on IP-address, one host running PHP4 as a SAPI module and the other virtual host running PHP5 as a CGI module.
Configuration files and directories for Apache2 in SuSE 10.1
Dir: /etc/apache2/
Purpose: General configuration directory for Apache2, this is where the "httpd.conf" lives.Dir: /etc/apache2/conf.d/
Purpose: Module configuration files for loaded modules, for example php4.conf. All the configuration files in this directory will be automatically read by the main httpd.conf by means of an "include conf.d/*.conf" command so the exact name doesn't really matter as long as the file ends in "*.conf".Dir: /etc/apache2/vhosts.d/
Purpose: Virtual host configuration files. All files in this directory will be automatically read by the main httpd.conf the exact name doesn't really matter as long as the file ends in "*.conf". Note: When yast2 is used to edit virtual hosts it will add its "yast2_*.conf" in this directory. Unfortunately the virtual host configuration in yast2 in not without problem (bugs) for IP based virtual hosts so we prefer to create the configuration files manually. This will be shown later on in this article.File: /etc/sysconfig/apache2
Purpose: This is the main Apache2 configuration file. This file is the one that is really used to configure apache when it is started. This is also the file that the "Yast2" HTTPD-module edits.From our point of view the most important thing is that this is the place where we tell Apache2 what external modules to load.
In the SuSE configuration this is done by listing all the modules in the string variable APACHE_MODULES. In the SuSE configuration there are no static "AddModule" directives in any of the configuration files for Apache. Instead this is dynamically generated each time apache is started (for example by /etc/init.d/apache2 start)
The generation of the actual module file names is quite clever in that the script looks at the core module name in the APACHE_MODULE variable and automatically determines the name of the file name of the load modules. This means that for PHP we only have to give the name "php4" or "php5" as the name of the module.
The script will then discover that the name of the file load module is in fact "libphp4.so" or "libphp5.so" automatically. The dynamically created list of load modules will be written to "/etc/apache2/sysconfig/loadmodule.conf" just before the startup script activates apache2 daemon which will then read the modules from this file which is included from the main "httpd.conf" file.
Making sure you have the correct Apache2 setup
If you use Yast2 to install Apache2 and the prefork module then all this will be automatically setup.
Before continuing please make sure that You have successfully installed Apache2 on your server. For example by directing your favorite browser to "http://localhost/"
Approaches to running multiple PHP versions
- Running multiple instances of the HTTPD demon where each instance listens on separate addresses and/or ports.
Advantage: This is the only way to run multiple versions of PHP as (SAPI) modules in Apache2. In addition this has some better security since potential crashes will be isolated and not effect the other HTTPD demons.
Drawback: Running multiple HTTPD instances will need more system resources in terms of memory and file handlers. - Running one instance of the HTTPD demon which is configured to serve multiple virtual hosts. This is the approach we have chosen.
Advantage: Minimum system overhead and relatively easy to setup.
Drawback: Only one PHP version can be run as a (SAPI) Apache module the other PHP versions must be configured/installed as CGI modules. This has a slight performance impact and might not be suitable for heavily loaded production sites. (Note: that could be overcome with the use of fast-cgi which works by pre-loading an instance of PHP in memory which will then be used by the Apache process. See Apache2 documentation regarding fast-cgi for more details).Note: There are actually two versions of virtual hosts with apache. By name pr by IP-address. In this example we have chosen to match the virtual hosts by IP address since for a development server we want to be able to use plain IP addresses and not have the added complexity of setting up a full DNS server. For more details about other differences please see the excellent Apache2 documentation.
Outline of the remaining article
- Part 1 Installing PHP4 as a SAPI module in Apache.
This sections details how to configure and compile PHP4 as a SAPI module and then do the necessary Apache configuration modifications to enable this new module. By the end of this section we will have the ability to run PHP4 scripts on our server. - Part 2 Creating a virtual host
By assigning an alias IP-address on the server we can configure Apache with a virtual server based on this address. This new virtual server will have its own "cgi-bin/" as well as "htdocs/" directories. This part shows how to enable this by adding suitable configurations in Apache. By the end of this section our server will accept HTTP calls on a secondary IP-Address and use the specified document root for this new IP-address. - Part 3 Installing PHP5 as a CGI module on the virtual host.
This final part shows how to configure and compile PHP5 as a CGI module that we then make available for the newly created virtual host in part 2. By the end of this module we will have PHP4 running on the default server address and PHP5 running on the secondary virtual host.
Part 1 - Installing PHP4
Step one; Compiling PHP4 as a module for Apache2
First download the latest PHP4 tar-ball from php.net or the closest mirror and unpack it in a temporary directory.Since we will compile PHP4 ourself we need first to make sure a number of libraries and the corresponding header files are installed in the system in order to be able to compile PHP4. This is done by installing a number of "*-devel.rpm" on your server. Depending your wanted configuration different development libraries must be made available.
At the very minimum you will need the "apache2-devel.rpm" which provides the "/sbin/apxs2" (Apache eXtenSion 2) command used to build modules with Apache2. Other modules you might need are
- jpeg-devel.rpm
- png-devel.rpm
- mm-devel.rpm
- xml2-devel.rpm
- mysql-devel.rpm
- ...
We use a small shell script called "mkphp4-sapi" to avoid having to re-type all the options each time we compile a new version of PHP. The options we use for a typical development server are (you might want to use other options)
#! /bin/sh ./configure --prefix=/usr/share \ --datadir=/usr/share/php4 \ --with-apxs2=/usr/sbin/apxs2 \ --libdir=/usr/share \ --includedir=/usr/include \ --bindir=/usr/bin \ --with-config-file-path=/etc/php4/apache2 \ --enable-mbstring --enable-mbregex \ --with-mysql \ --with-gd --enable-gd-imgstrttf --enable-gd-native-ttf \ --with-zlib-dir=/usr/lib \ --with-png-dir=/usr/lib \ --with-jpeg-dir=/usr/lib --with-xpm-dir=/usr/X11R6 \ --with-tiff-dir=/usr/lib --with-ttf-dir=/usr/lib \ --with-freetype-dir=/usr/lib \ --enable-ftp \ --enable-memory-limit --enable-safe-mode \ --bindir=/usr/bin \ --enable-bcmath -enable-calendar \ --enable-ctype --with-ftp \ --enable-magic-quotes \ --enable-inline-optimization \ --with-bz2 \ --with-iconvHowever there are one thing You should take notice of. We have specified the config file path (where the php.ini resides) to "/etc/php4/apache2/" as You can probably guess from this naming convention it will make it possible to have different ini files for both PHP4 and PHP5. In fact we have four different ini files according to
- "/etc/php4/apache2/php.ini"
Used by the apache SAPI module version of PHP4 - "/etc/php4/cli/php.ini"
Used by the standalone client version of PHP4 (/usr/bin/php4) - "/etc/php5/apache2/php.ini"
Used by the apache CGI version of PHP5 - "/etc/php5/cli/php.ini"
Used by the standalone client version of PHP5 (/usr/bin/php5)
So for example if you get an error like "Cannot find PNG libraries. Please check your that the corresponding "png-devel" library is installed and if not go back to Yast2 and install the needed "*-devel.rpm" versions of the libraries.
When You have been able to successfully run the ./configuration command it is time to compile. Type "make" as usual but do not type "make install", now wait until the compilation finishes.
The resulting PHP4 that you have built can be found in ".libs/libphp4.so". Now we only want to copy this file to the location of the other Apache2 modules.
So the only thing that now remains is to copy ".libs/libphp4.so" to "/usr/apache2-prefork/" in order for Apache to find PHP4 as a module.
Step two; Enable the PHP4 module in the Apache2 configuration
There are three steps to needed to enable PHP4 in Apache.- Add php4 to the APACHE_MODULE string in "/etc/sysconfig/apache2" in order so that the startup script in SuSE will add the appropriate LoadModule statement so that Apache will load PHP4 as a module. In our case our module string will look like
APACHE_MODULES="access actions alias auth auth_dbm autoindex cgi \ dir env expires include log_config mime negotiation setenvif ssl \ suexec userdir dav dav_svn php4 "
- Telling Apache to run files ending in *.php through the PHP4 module. This is done by specifying the MIME type which the PHP4 module registered itself with. In addition we also tell Apache to search for the appropriate PHP index files in case a directory name is given as the URL. We do this by creating a file "php4.conf" with the following content
<IfModule sapi_apache2.c> AddType application/x-httpd-php .php3 AddType application/x-httpd-php .php4 AddType application/x-httpd-php .php AddType application/x-httpd-php-source .php3s AddType application/x-httpd-php-source .php4s AddType application/x-httpd-php-source .phps DirectoryIndex index.php3 DirectoryIndex index.php4 DirectoryIndex index.php </IfModule>
and place it in the "/etc/apache2/conf.d/" directory. This will guarantee that it will be read upon startup. The "IfModule" statement in the beginning is just to avoid the statements to be executed in case the PHP4 module is not loaded (we test this by checking if the "sapi_apache2.c" has been activated in Apache). - The final step now is to restart Apache by doing (as root)
$> /etc/init.d/apache2 restart
<?php phpinfo(); ?>and name it as "phpinfo.php" . If you now go to your favorite browser and run this script as "http://localhost/phpinfo.php" you should get the standard PHP4 information presented as a quite big table.
Part 2 - Creating a virtual host
Step 1; Adding an alias IP-address to Your server
In this example we will assume that the server is called "gamma" and have the primary address "192.168.0.50". The virtual host will be called "gamma2" and will be located at address "192.168.0.51". The easiest way to add another address alias is to use yast2 and the network configuration module and simple add a new alias.Step 2; Creating different document and cgi roots
In preparation of the new virtual host we want it to have a separate document and cgi (where we will store the PHP5 binary) roots compared with the standard server. For this purpose we add two new directories "/srv/www/gamm2-htdocs/" and "/srv/www/gamma2-cgi-bin/" on the server.Step 3; Configure Apache with a virtual host
For his we add a new small config file named "gamma2_vhost.conf" (the exact name is not important as long as it ends in *.conf) in the "/etc/apache2/vhosts.d/" directory. The script we add is# Setup gamma2 on secondary IP-address <VirtualHost 192.168.0.51> DocumentRoot /srv/www/gamma2-htdocs/ ServerName gamma2 ServerAdmin root@localhost # We use a separate CGI directory ScriptAlias /cgi-bin/ /srv/www/gamma2-cgi-bin/ # For good measure we also add recognition of PHP5 index DirectoryIndex index.php5 # This is the two critical statement for this virtual # host we activate PHP5 as a CGI module Action php5-cgi /cgi-bin/php AddHandler php5-cgi .php5 .php <Directory /srv/www/gamma2-cgi-bin/> AllowOverride None Options +ExecCGI -Includes Order allow,deny Allow from all </Directory> <Directory "/srv/www/gamma2-htdocs/"> Options None AllowOverride None Order allow,deny Allow from all DirectoryIndex index.html index.php </Directory> UserDir public_html </VirtualHost>We do not go into any more detail of this configuration since it should be fairly easy to understand. For details we refer to the Apache documentation.
What we have accomplished with this file is that when we call the server on the second address any php file will be recognized by apache as a file to be handled by the "php5-cgi" action. This in turn means that whenever Apache encounters a *.php5 (or *.php) file it will run the program "/cgi-bin/php". This path in turn will be expanded to " /srv/www/gamma2-cgi-bin/php".
In the next section we will show how to compile PHP5 and put the executable CGI version in this directory.
Part 3 - Installing PHP5
#! /bin/sh ./configure --prefix=/usr/share \ --datadir=/usr/share/php \ --libdir=/usr/share --includedir=/usr/include \ --enable-force-cgi-redirect \ --bindir=/usr/bin \ --with-config-file-path=/etc/php5/apache2 \ --enable-mbstring --enable-mbregex \ --with-mysql \ --with-gd --enable-gd-imgstrttf --enable-gd-native-ttf \ --with-zlib-dir=/usr/lib \ --with-png-dir=/usr/lib --with-jpeg-dir=/usr/lib \ --with-xpm-dir=/usr/X11R6 \ --with-tiff-dir=/usr/lib --with-ttf-dir=/usr/lib \ --with-freetype-dir=/usr/lib \ --enable-ftp \ --enable-memory-limit --enable-safe-mode \ --bindir=/usr/bin \ --enable-bcmath -enable-calendar \ --enable-ctype --with-ftp \ --enable-magic-quotes \ --enable-inline-optimization \ --with-bz2 \ --with-iconvNotice that as we said before we have a different configuration path for PHP5 compared with PHP4 as shown above. Also note that in order to build the CGI module we do not configure the "apxs2" option. After successful configuration type "make" but do not type "make install" in order to compile PHP5.
After the compilation have finished copy "sapi/cgi/php" to "/srv/www/gamma2-cgi-bin/php" since this is the place where our virtual host expects to find the PHP5 CGI module.
Part 4 - Verifying the setup
- Increase maximum allowed memory to 32MB
- Increase maximum allowed script running time to 30s
- Set full error reporting
Assuming the IP-addresses shown in the configuration above we are now ready to test out setup. In order to do this make sure that each document root have the "phpinfo.php" test script (see above).
We can now test the different setups by using the URLs
- http://192.168.0.50/phpinfo.php
This URL would send back configuration showing that the server is running PHP4. Verify that the config path used is set to "/etc/php4/apache2/". - http://192.168.0.51/phpinfo.php
This URL would send back configuration showing that the server is running PHP5 Verify that the config path used is set to "/etc/php5/apache2/".
Trouble shooting:
- If your browser asks you to download content with mime-type "mime/x-httpd-application" when you try to visit the PHP script it means that Apache does not yet run PHP as a module (now module have accepted to handle the x-http-application mime type). Make sure you have included the "php4" in the APACHE_MODULES string as described above and that you added the "php4.conf" file in the "/etc/apache2/conf.d/" directory.
- We had some issues with FireFox insisting on downloading "phpinfo.php" as a file even when other browser showed the page properly, using "etherreal" we could confirm that Firefox was using a previous cahced version before we had enabled PHP in the apache configuration. To solve this we had to clear the Firefox cache.
'Web > PHP' 카테고리의 다른 글
엑셀 다운로드 헤더 (0) | 2010.05.28 |
---|---|
참조연산자 (&) (0) | 2010.04.29 |
스팸으로부터의 이메일 주소 보호 함수 (0) | 2010.02.11 |
cURL (0) | 2010.01.20 |
간단한 달력 소스 (0) | 2009.12.23 |