skip to Main Content

I’m trying to update my server to use PHP8.1 and I ended up disabling http/2 and also can’t figure out how to re-enable it and actually get PHP8.1 to run.

The first steps I did were:

sudo a2dismod php7.4
sudo a2enmod php8.1

The second command had a conflict with mpm_event so I then ran:

sudo a2dismod mpm_event
sudo a2enmod mpm_prefork

After running "sudo a2enmod php8.1" again as well as "sudo systemctl restart apache2", it seemed okay.

However, whenever I run "php -v", it says that the CLI is "8.1" but using "phpinfo()" on the live site still returns "7.4.26".

I also ran these commands which all show that 8.1 is currently running:

sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
sudo update-alternatives --config phpize
sudo update-alternatives --config php-config

After all of this, I noticed that my webpages stopped serving via http/2. Upon trying "sudo a2dismod mpm_prefork" so I can enable mpm_event again, it says that it can’t disable mpm_prefork because there’s a conflict with the PHP module. When I disable the PHP module, I can disable mpm_prefork and enable mpm_event but then I wouldn’t have PHP anymore (I think? It’s all so confusing…)

Edit: I tried disabling the PHP module and enabling mpm_event but it made the website not work anymore so I reverted those changes.

For clarity, when I try "sudo a2dismod mpm_prefork", I get this error:

ERROR: The following modules depend on mpm_prefork and need to be disabled first: php8.1

If it helps, phpinfo() says that I’m currently running:

PHP Version 7.4.26
Server API  FPM/FastCGI
Configuration File (php.ini) Path   /etc/php/7.4/fpm

Also, I now have PHP8.1 in my mods_enabled folder and I swear there were no PHP modules at all there before all this.

All in all, I’m very new to server management but I simply want to run PHP8.1 (fpm version?) while using http/2 but am running around in circles and I also took a step back with accidentally disabling http/2.

Edit: My question is basically three parts:

  1. Why did my site go offline after I disabled the PHP8.1 module? And,
    how do I prevent that from happening again?
  2. How do I safely re-enable mpm_event (so that http/2 will work again)? Keep in mind that it seems to
    only be an option when both PHP8.1 and mpm_prefork modules are
    disabled.
  3. How do I upgrade to PHP 8.1-fpm?

If an answer can provide steps for digging myself out of the hole that I dug myself into while also enabling PHP8.1-fpm then that would be awesome.

Oh, and just so it’s clear; my current setup is:

  • PHP8.1 and mpm_prefork modules are enabled (mpm_event is disabled)
  • The live website is using PHP7.4-fpm without http/2
  • The CLI version of PHP is PHP8.1
  • I do not have PHP8.1-fpm installed

3

Answers


  1. You need a package named php8.1-fpm to be able to use the mpm_event instead of mpm_prefork. For example this is mentioned in this tutorial:

    Depending on the web server you use, you will need to install additional packages to integrate with the web server.

    For Apache using mpm_event, …, php8.0-fpm package provides integration with PHP 8.0 via FPM.

    Of course you will want the php8.1-fpm package.

    Login or Signup to reply.
  2. I think that your thrid question answers second, combined them into one list of commands

    Why did my site go offline after I disabled the PHP8.1 module?

    When you switch off apache php module, web server treats php code as plain text, which is a default behavior for unknown content type. Its kinda working, but your code is not interpreted at all.

    And, how do I prevent that from happening again?

    In order to prevent that either use varnish, load balancer(apache/nginx), make snapshots of the system when its stable or containerize your application with docker. Whatever will be suitable for your needs.

    In varnish case it will create cached versions of your web pages and will return them in response omitting your webserver until no such cached version of web page exist(no cache hits in other words). But before, you need to write script which will cache all pages. Then varnish will simply send html responses.

    In load balancer case if one node fail then it will redirect traffic to another alive node. Each node is basically a copy of your website.

    Snapshots are images of your OS which you can use in different VMs for example. So if you messed up then you can simply delete snapshot and install initial one again.

    In docker you can simply split your app into 2 services, which you can configure however you want without being afraid of making everything bad )

    1 service will apache2

    2 service will be php-fpm

    Then you can simply build each service as an image and rotate images like in VM(Virtual Machine).

    How do I upgrade to PHP 8.1-fpm?

    # step 1, optional if you did it before
    sudo apt update
    sudo apt upgrade
    
    # step 2, dont see any issues with simply installing newer version of php-fpm
    sudo apt install php8.1-fpm
    
    # check if user and group in php-fpm config are correct(/etc/php/8.1/fpm/pool.d/www.conf). 
    # User and group should be the same as webserver user and group.
    # if you want to check under which user webserver runs
    # sudo ps aux| grep apache2 
    listen.owner = www-data
    listen.group = www-data
    # and also check if socket/port for php-fpm are created/opened and its running normally
    
    # step 3
    # disable modules and restart configured services. For apache better to reload instead of restart
    sudo a2dismod php8.1
    sudo a2dismod mpm_prefork
    sudo a2disconf php7.4-fpm
    sudo a2enmod mpm_event
    # optional in case its enabled
    sudo a2enmod proxy
    # optional in case its enabled
    sudo a2enmod proxy_fcgi
    sudo a2enconf php8.1-fpm
    sudo systemctl restart php8.1-fpm
    sudo systemctl restart apache2
    # manually set php version for CLI
    sudo update-alternatives --set php /usr/bin/php8.1
    sudo update-alternatives --set phar /usr/bin/phar8.1
    sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.1
    
    # step 4
    # Mask the old PHP-FPM daemon (tell the system to not start the old php-fpm daemon)
    sudo systemctl mask php7.4-fpm
    

    Hope that something in this list will be helpful )

    Login or Signup to reply.
  3. In my Apache config I had to change this line to use the right socket:

      <FilesMatch .php$>
              SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost/"
      </FilesMatch>
    

    Infos about the socket:

     # ls -l /var/run/php/
    total 4
    -rw-r--r-- 1 root     root      5 Sep 22 12:42 php8.1-fpm.pid
    srw-rw---- 1 www-data www-data  0 Sep 22 12:42 php8.1-fpm.sock=
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search