Building DevSecOps solutions using AWS, Terraform and Kubernetes

Migrating PHP Tips

  • 7th January 2023
PHP vs. Sanity

Introduction

So the dreaded moment has finally arrived. You have just inherited your first PHP migration, you have no idea where to start and your sanity is already being questioned.

This article is aimed at Linux DevOps Engineers with no experience in PHP. I will assume you’re comfortable with the command line and know your way around a server.

Let’s look at the main things I check before running a PHP migration.

Check the PHP version

First, check the PHP version. The simplest way to check is with the CLI:

php -v

However, there can be multiple PHP versions running on a single server. Depending on your OS, your best bet is run something like this to see which versions are installed.

sudo update-alternatives --list php

Or, alternatively just list the php directory:

ls -al /etc/php

If there is more than one version installed then you may need to follow the breadcrumbs in your virtual host and php-fpm pools to see which site is using which version.

Check for security support

Now that you know your PHP version check to see if it’s still receiving security support.

PHP is one of the most accessible languages to get started with. Unfortunately, this means people with little experience can end up creating very financially successful sites with zero regard for security.

So always check to see how far out of date it is!

Check the modules

So you have the same PHP script running and the same PHP version but it’s still not working?! Well, let me introduce you to the hidden wonders of PHP modules.

Unlike languages like Golang where everything is compiled into a single binary, PHP makes use of modules. They’re extensions that are mostly written in C. Platforms like Magento and Wordpress require different modules for different PHP versions.

To see a list of modules you currently have installed, run this CLI command:

php -m

You can now run this command on both servers to quickly identify which modules are missing. This can be really handy when you’re upgrading php too.

For example:

/usr/bin/php8.1 -m > php81modules
/usr/bin/php8.2 -m > php82modules
diff php81modules php82modules

If you have any modules missing please consult Google, StackOverflow and your developers for help on how to install them.

Check the configuration

PHP stores its custom configuration in a php.ini file.

This covers settings like memory limits, timeouts, maximum post sizes, etc.

Important: Your webuser actually runs a separate php.ini file to the CLI. This might seem counter intuitive and frustrating at first, but it makes sense that you would want your CLI scripts to run for longer with more memory than your web requests.

Next, let’s look at where to find them.

1) Find the php.ini file directly

This is my preferred method, as it doesn’t expose anything to the world but does mean you need to go digging.

Here are some places to start your search:

  • CLI: /etc/php/8.1/cli/php.ini
  • Nginx with PHP-FPM: /etc/php/8.1/fpm/php.ini
  • Apache with PHP-FPM: /etc/php/8.1/fpm/php.ini
  • Apache with old school PHP: /etc/php/8.1/apache2/php.ini

Just remember to replace 8.1 with the PHP version your site uses.

Pro-tip, to see the values without all the noise run:

grep -v ';' /etc/php/8.1/cli/php.ini

This show all the active configration and ignores the defaults that have been commented out.

2) Still can’t find the file?

No problem! For the CLI php.ini info you can run:

php -i

For the webuser php.ini information you can create a one-off php script and view it via the website:

<?php
echo phpinfo();

But please remember to restrict access to this file, then delete it after. It’s not a good idea to let everyone in the world know your settings.

Check the php-fpm configuration

"FPM (FastCGI Process Manager) is a primary PHP FastCGI implementation containing some features (mostly) useful for heavy-loaded sites."

And no, I have no idea what the means either. Just think of it as better and faster. If you’re not using php-fpm then you can skip this step.

It’s important to know that the php-fpm.conf file can be used to override the php.ini settings for a single pool.

A couple tricks for finding them:

locate php-fpm.conf
locate www.conf

Note that different websites can be configured to have their own pools, so check with your virtual host file too.

Here is an example location I would expect to find them, but if they’re not there then dig around in that area and I’m sure you’ll find them.

/etc/php/8.1/fpm/php-fpm.conf
/etc/php/8.1/fpm/pool.d/www.conf

Conclusion

Now that you’ve checked everything you can slowly start to copy over the php version, modules and configuration that your site needs.

Hopefully this gives you some starting tips while migrating your first PHP site. But if all that fails then remember to blame the developer and live happy knowing that you tried.

Rhuaridh

Please get in touch through my socials if you would like to ask any questions - I am always happy to speak tech!