Upgrading to PHP 8 Upgrading to PHP 8

April 15, 2021

laravel debian

With the launch of Laravel Octane, I decided it was time to update to PHP 8 on my debian box. This was the process, compiled from a number of webpages which seemed to have cut and paste the wget line wrongly. The php extensions are those needed for Laravel.

update-alternatives is a lovely little tool to select which php version to use.

    apt-get install lsb-release apt-transport-https ca-certificates
    wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
    echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
    apt-get update
    apt-cache search php 8
    apt install -y php8.0-{mysql,cli,common,snmp,ldap,curl,mbstring,zip,tidy,xml,opcache}
    update-alternatives --config php
    php -v

However, this only worked at the command line, to get apache to work with php8.0 i also ran

    apt-get install php8.0-fpm libapache2-mod-php8.0
    a2dismod php7.4
    a2enmod php8.0
    systemctl restart apache2

and viola! the phpinfo() was now version 8.0.3!

However, a tested a Laravel 5 app and it errored with a blank screen. So had to update composer to version 2 and run a composer upgrade like so

    composer self-update --2
    composer upgrade 

What an improvement in speed for composer! I had to update these lines in the composer json to

        "php": "^7.4|^8.0",
        "fzaninotto/faker": "^1.9.1",
        "phpunit/phpunit": "^9.3"    

I still have a laravel 5.5 app that i like to use, to downgrade back to php7.4

a2dismod php8.0
a2enmod php7.4
systemctl restart apache2

and to re-enable php 8.0

a2enmod php8.0
a2dismod php7.4
systemctl restart apache2

But there is a better way, edit the apache2-site-74.conf and add this to the virtual host conf

        <FilesMatch \.php$>
                # Apache 2.4.10+ can proxy to unix socket
                SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost/"
        </FilesMatch>

and enable the proxy module

a2enmod proxy_fcgi
systemctl restart apache2

and viola! both versions work, php8 is the default, with php7.4 enabled for the site.

Perfect!


If you would like to contact me with this form on londinium.com, ilminster.net or via Twitter @andylondon