40

Ubuntu 10.04, MySQL 5.1, Apache 2.2, and PHP 5.2/5.3:

I just discovered that I am using the wrong version of PHP for a CRM application. Once I figured out how to make a simple phpinfo() script to tell me what Apache2 is using, I tried changing the php.ini such that my webserver would use the PHP I want. Well, this is my problem. Not sure how to do that.

I compiled the version of PHP I want to /etc here:

/etc/php-5.2.8/

Inside this, there was a php.ini-recommended file that I made some changes to and renamed to php.ini so PHP would use it. But when I opened my browser and cleared my history and went to the http://localhost<CRM dir>/install.php address, the wizard still says I'm not usign the correct version of PHP.

Based on this post what do I have to do to change the version of PHP that shows up after I run my test.php script? In other words, phpinfo() says I'm running PHP 5.3.2, but I want to change it to my compiled 5.2.8 version located in /etc.

4 Answers 4

11

Depending on your server, you should be looking at Apache, not PHP.

(For RHEL/CentOS) look at /etc/httpd/conf.d/php.ini

#
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated web pages.
#
LoadModule php5_module modules/libphp5.so
#
# Causes the PHP interpreter to handle files with a .php extension.
#
AddHandler php5-script .php
AddType text/html .php

You'll see that your PHP module is modules/libphp5.so.

AddHandler php5-script .php tells Apache to run PHP on any file with the extension .php.

If you are using an RPM based OS it's probably easier to uninstall (assuming you can do that) the current version of PHP, and reinstall the version you are looking for.

rpm -qa | grep php

will show you what version of PHP is currently installed.

5
  • 3
    It's worth mentioning you should bounce Apache after changing the config so it is picked up. Jun 7, 2010 at 22:49
  • I really wish it was this easy. The only php.ini file I have in the server directory is /etc/apache2/php.ini but not where you suggest. This file doesn't have these components you speak of: LoadModule, AddHandler, AddType, etc. Jun 7, 2010 at 23:44
  • interesting. What OS flavor are you running on? Was it configured via YUM, APT-Get, or YAST, (or some other package manager)?
    – grufftech
    Jun 8, 2010 at 18:11
  • Ubuntu 10.04. The new version of PHP (5.3.2) can easily be installed unsing apt-get, which I have done. But hte PHP 5.2.8 and 5.2.13 I compiled, using files from php.net/releases. I actually kind of gave up trying to get older version of PHP to run. I think alternate approaches are in order for the sake of brevity. Thanks for your help, nonetheless... Jun 8, 2010 at 22:38
  • php.ini could be extended via includes: /etc/httpd/conf.modules.d/15-php.conf: LoadModule php7_module modules/libphp7.so LoadModule php7_module modules/libphp7-zts.so Oct 23, 2020 at 13:51
89

If you've already installed another version of php, you only need to change the php* module used by apache.

for example, I have php5 and php7.0. when I want apache use php7.0, I only need to enable his module and disable php5 module.

sudo a2dismod php5
sudo a2enmod php7.0
3
  • 9
    This doesn't work if your Apache installation is not using the system php. Jan 31, 2020 at 16:24
  • You can make your php installation system with module enable or scl if you use remi RPMs Oct 23, 2020 at 13:52
  • 2
    also had to run sudo a2dismod php7.2 to upgrade to php7.4 from php7.2
    – ruwan800
    Jun 6, 2022 at 15:00
1

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:

  1. 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
    
  2. 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 LoadModules 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. :)

0

I had the same problem where phpinfo() would show version 7.0 even though I had upgraded to version 7.3. And it turns out I just had to restart apache.

I used sudo systemctl restart httpd on centos7

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .