There are two main methods to install a new PHP version and tell Apache to use it: mod_php
and php-fpm
.
Note: The preffered method is php-fpm
, and many new distributions (including Fedora) are using it by default.
Install PHP as Apache SAPI module
Here is the guide around this for Unix systems, from the official documentation. It has some missing points (at least for my setup), so I walk through the steps:
Build PHP from source. In the ./configure
step, use --with-apxs2
. This will build shared Apache 2 handler module for you.
Make sure you have apxs
command defined in your path, or specify its path as the option's value (i.e. --with-apxs2=/path/to/apxs
).
For instance, if you installed Apache system-wide and want to install this command as well (e.g. on a local environment), on Fedora and its derivatives, you can install it by:
sudo dnf install httpd-devel
After building is done (i.e. after make
or make test
), run make install
(as root, perhaps). Obviously, this will install PHP to the path you specified (i.e. with --prefix
).
What is done just before PHP being installed is, because of the --with-apxs2
option, installing PHP apache2handler SAPI module. It prepares the shared object (e.g. in /usr/lib64/httpd/modules/libphp.so
), and activates the module (e.g. in /etc/httpd/conf/httpd.conf
), by adding one of the following lines to the Apache configuration file, depending on the PHP version you installed (the line is inserted below the section "Dynamic Shared Object (DSO) Support"):
# In the case of PHP8
LoadModule php_module /usr/lib64/httpd/modules/libphp.so
# In the case of PHP7
LoadModule php7_module /usr/lib64/httpd/modules/libphp.so
Note: While switching on the PHP versions, specially major ones, keep in mind to check there is only one of these LoadModule
s for PHP available, otherwise the Apache server refuses to start.
Install PHP as PHP-FPM (FastCGI Process Manager)
TBD. :)
Final steps
PHP 8.0 and above extra step
In the case your distribution does not support PHP8 yet (e.g. Fedora 34) and you installed Apache2 from the package manager, you should take one more step.
The problem is, from PHP 8.0 onwards, Apache2 uses different identifiers for SAPI modules than before. These identifier are used in order to detect whether you are using mod_php
or php-fpm
, in the configuration files. For PHP5, it was php5_module
and mod_php5
; for PHP7, it is php7_module
and mod_php7
, and for PHP8, it is php_module
and mod_php
.
In this case, navigate to Apache2 configurations directory (e.g. /etc/httpd
), and start editing the file conf.d/php.conf
. There are two IfModule
sections there: One for enabling php-fpm
if you don't use mod_php
, and the other for enabling mod_php
if you are using it (i.e. using LoadModule
somewhere in the configurations). You should update these conditions to cover PHP8 as well.
For instance, consider the following:
<IfModule !mod_php5.c>
<IfModule !mod_php7.c>
# Enable PHP-FPM configuration
</IfModule>
</IfModule>
You should surround the core configuration with one more IfModule
section like this:
<IfModule !mod_php.c>
# Enable PHP-FPM configuration
</IfModule>
And do the same for the mod_php
enabler configuration section as well.
Last step
Restart the Apache service:
service httpd restart
Now, phpinfo()
should show you the new PHP version you just installed. You should be happy now. :)