Integrating PHP with Apache

Installing the software

Before we install any packages we should ensure that the correct use-flags are configured so that all required functionality is made available and unnecessary functionality is not included. The dev-lang/php package and its dependencies provide a variety of use-flags only some of which will be discussed further here. As usual feel free to add and remove use-flags at will although the minimum set which are required for using this guide in its entirety are shown below.

lisa emerge -pv php
 
These are the packages that would be merged, in order:

Calculating dependencies... done!
[ebuild      ] app-admin/php-toolkit-1.0.1
[ebuild      ] dev-libs/libmcrypt-2.5.8-r1
[ebuild      ] net-mail/mailbase-1  USE="pam"
[ebuild      ] mail-mta/ssmtp-2.62-r7  USE="ipv6 ssl -maxsysuid -md5sum"
[ebuild      ] dev-lang/php-5.2.13  USE="apache2 bzip2 cli crypt gdbm iconv ipv6 ldap ncurses nls pcre postgres readline reflection session spl ssl unicode zlib -bcmath -berkdb -calendar -cdb -cgi -cjk -concurrentmodphp -ctype -curl -curlwrappers -db2 -dbase -debug -discard-path -doc -exif -fastbuild -filter -flatfile -force-cgi-redirect -ftp -gd -gd-external -gmp -hash -imap -inifile -interbase -iodbc -json -kerberos -kolab -ldap-sasl -libedit -mcve -mhash -msql -mssql -mysql -mysqli -oci8 -oci8-instant-client -odbc -pcntl -pdo -pic -posix -qdbm -recode -sapdb -sharedext -sharedmem -simplexml -snmp -soap -sockets -spell -sqlite -suhosin -sysvipc -threads -tidy -tokenizer -truetype -wddx -xml -xmlreader -xmlrpc -xmlwriter -xpm -xsl -yaz -zip"
Caution:
Clearly the apache2 use-flag is critical if you intend to use PHP with apache. If this flag is not specified then the mod_php module will not be built and it will therefore be impossible to configure apache to work with PHP. If you do not wish to enable the apache2 use flag globally then feel free to add an entry for dev-lang/php in /etc/portage/package.use instead as this use-flag is not required by other packages for this guide.
 

Once you are confident that the correct use-flags are set for the dev-lang/php package, and any dependencies it may require, you can proceed with the installation by issuing the emerge command shown below.

lisa emerge php

Selecting the PHP module for Apache

lisa eselect php list apache2
  [1]   php5.3 
lisa eselect php set apache2 1
  [1]   php5.3 
lisa eselect php list apache2
  [1]   php5.3 * 
Caution:
Failure to configure the php module for apache will result in an error similar to that shown below when starting the apache server. apache2: Syntax error on line 173 of /etc/apache2/httpd.conf: Syntax error on line 4 of /etc/apache2/modules.d/70_mod_php5.conf: Cannot load /usr/lib64/apache2/modules/libphp5.so into server: /usr/lib64/apache2/modules/libphp5.so: cannot open shared object file: No such file or directory
 
lisa eselect php list cli
  [1]   php5.3 
lisa eselect php set cli 1
  [1]   php5.3 

Configuring the PHP module for Apache

/etc/php/apache2-php5.3/php.ini
date.timezone = "GMT"
/etc/php/cli-php5.3/php.ini
date.timezone = "GMT"

Integrating PHP with Apache

When the PHP application has been successfully installed support for scripts written in the PHP language can be easily added to an existing Apache installation by modifying the main configuration file as shown below.

/etc/conf.d/apache2
APACHE2_OPTS="-D DEFAULT_VHOST -D SSL -D SSL_DEFAULT_VHOST"
APACHE2_OPTS="-D DEFAULT_VHOST -D SSL -D SSL_DEFAULT_VHOST -D PHP5"

No additional configuration is required for the virtual hosts as the global configuration supplied with the dev-lang/php package enables PHP support globally. As always when installing a new module the apache2 daemon will require restarting for the changes to take effect. This can be easily accomplished with the command shown below.

lisa /etc/init.d/apache2 restart