There are two main methods to install a new PHP version and tell Apache to use it:
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.
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 test), run
make install (as root, perhaps). Obviously, this will install PHP to the path you specified (i.e. with
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
LoadModules for PHP available, otherwise the Apache server refuses to start.
Install PHP as PHP-FPM (FastCGI Process Manager)
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
php-fpm, in the configuration files. For PHP5, it was
mod_php5; for PHP7, it is
mod_php7, and for PHP8, it is
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:
# Enable PHP-FPM configuration
You should surround the core configuration with one more
IfModule section like this:
# Enable PHP-FPM configuration
And do the same for the
mod_php enabler configuration section as well.
Restart the Apache service:
service httpd restart
phpinfo() should show you the new PHP version you just installed. You should be happy now. :)