Wednesday, March 09, 2022

Downgrading to php7.3 on Amazon Linux 2

Installing an up to date version of PHP on Amazon Linux 2 is straightforward. You can follow this tutorial, which makes use of the amazon-linux-extras command.

I found myself, however, in the unusual position of needing to run run an out of date version of PHP on AWS. The issue is that one of my clients is developing a WordPress plugin, and this plugin must run on PHP 7.3. The standard on Amazon Linux 2 these day is PHP 7.4.

Here are the steps I went through to coax my system from PHP 7.4 down to 7.3. I hope you don't find yourself needing these instructions, but if you do, enjoy!

# Grab a list of all the PHP modules currently installed
$ rpm -qa |grep php > all.installed
$ cat all.installed

# The 'remi' repo offers access to specific version of PHP.
# Here, I'm installing PHP 7.3 using the remi-php73 repo.
$ sudo yum install
$ sudo  yum-config-manager --enable remi-php73

# Bye-bye up to date version of PHP.
$ sudo yum -y remove php*
$ sudo amazon-linux-extras disable php7.4

# Hello old, crufty version of PHP.
# Note: to access PHP modules the prefix is now 'php73-'
# So php-pdo is installed as php73-php-pdo.
$ sudo yum install php73
$ sudo yum install $(cat all.installed  | grep ^php-| sed 's/-7.4.*//' | sed 's/^/php73-/')

# Properly install php73 as /usr/bin/php
$ sudo update-alternatives --install /usr/bin/php php /usr/bin/php73 10

# Enable and start the php-fpm service
$ sudo systemctl enable php73-php-fpm
$ sudo systemctl start php73-php-fpm

# Where the heck are the config files for php-fpm? Let's ask rpm.
$ rpm -ql php73-php-fpm

# Found it. Now, where php-fpm listening?
$ cat /etc/opt/remi/php73/php-fpm.d/www.conf |grep ^listen
listen =
listen.allowed_clients =

# Use this info to create /etc/httpd/conf.d/php.conf
# so that Apache can talk to php-fpm and support PHP.
$ cat /etc/httpd/conf.d/php.conf
<FilesMatch "\.php$">
    # Note: The only part that varies is /path/to/app.sock
    SetHandler  "proxy:fcgi://localhost:9000"

DirectoryIndex index.php

# Restart httpd and php-fpm
$ sudo systemctl restart httpd php73-php-fpm

And browsing to my server shows me this all worked. Amazing. Uh, I mean, I never had a doubt.

No comments:

Post a Comment