How to Update PHP on Ubuntu Linux | PHP 8.3 FPM | Wordpress | Step-by-Step Guide

How to Update PHP on Ubuntu Linux | PHP 8.3 FPM | WordPress | Step-by-Step Guide

PHP (Hypertext Preprocessor) is one of the most popular server-side scripting languages, widely used for web development. If you’re running Ubuntu Linux, installing and enabling PHP is straightforward. This guide will walk you through the process of installing PHP, and enabling it for your web server.




This guide is based on a server running Ubuntu Linux 20.04 with WordPress. We will be upgrading the server from PHP 7.4 to PHP 8.3-FPM. Both installation methods will be covered: installing PHP as an Apache module and using PHP-FPM.

This guide is adaptable to any PHP version. Simply change the version numbers in the examples. For instance, if you’re updating to PHP 8.4, replace any instance of php8.3 with php8.4.




Part 1: Prerequisites


Step 1 – Backup

It is crucial that you back up the server before doing anything else. This will save you from a major headache if something breaks during the PHP update process.

It is also important to back up, or copy and paste, the contents of your current php.ini file if you have made any advanced changes to it. This way, you can easily reference the old file and ensure everything matches up in the new one.

To find your current php.ini file, navigate to /etc/php/x.x (replace x.x with your current PHP version). Depending on the handlers you have installed, you may see up to three directories: apache2, cli, and fpm. The folder that contains the correct php.ini file will correspond to the PHP handler. If you’re using FPM, it will be under the fpm directory. If PHP was installed as an Apache module, it will be under the apache2 directory. If you’re not sure, it’s a good idea to back up all of them.


Step 2 – Make Note of Installed PHP Modules

Although this guide was created to upgrade PHP for a WordPress installation, the same method should work for any application that requires PHP. However, you will need to take note of the PHP modules currently installed so that, when you upgrade, you have a list of each module that needs to be installed.

This can be accomplished by issuing the php -m command, which will output all of the PHP modules currently installed.

An example is shown below:

php -m

Sample Output:

[PHP Modules]
calendar
Core
ctype
date
exif
FFI
fileinfo
filter
ftp
gettext
hash
iconv
json
libxml
openssl
pcntl
pcre
PDO
Phar
posix
random
readline
Reflection
session
shmop
sockets
sodium
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
Zend OPcache
zlib

[Zend Modules]
Zend OPcache

Copy and paste the list of modules from your output into a program like Notepad, for example.





Step 3 – Install The PHP Repository

To ensure you have access to the latest PHP packages, you should install the PHP repository, which can be done using the following steps:

Update your system:

You’re now ready to install PHP. Although running sudo apt update and sudo apt upgrade likely installed most of the necessary packages, it’s still a good idea to manually install any additional packages or modules you need. The next section covers installing PHP, both as an Apache module and as FPM.


Part 2: Installing PHP


Step 1 – Install The Base PHP Packages


Select the tab below that corresponds to the PHP handler you wish to install:


Step 2 – Install PHP Common Packages

This is not mandatory, but it is highly recommended. PHP Common will install the following packages:

calendar, ctype, exif, ffi, fileinfo, ftp, gettext, iconv, pdo, phar, posix, shmop, sockets, sysvmsg, sysvsem, sysvshm, and tokenizer.

sudo apt install php8.3-common

PHP 8.3 is used in this example. Remember to change the version if needed.


Step 3 – Install All Other PHP Modules

Remember how it was recommended at the beginning to note your currently installed packages? This is why. You need to know which packages to install so the new version of PHP works with your application the same way the old version did.

The example below lists the recommended packages for a WordPress installation:

sudo apt install php8.3-{gd,curl,xml,zip,intl,mbstring,apcu,bcmath,gmp,imagick,igbinary,mysql,redis,cli,opcache,readline}

PHP 8.3 is used in this example. Remember to change the version if needed.


Step 4 – Disable the Previous PHP Version

The context of the following command depends heavily on the current version of PHP that is active. To ensure that PHP is properly disabled, the quickest way is to copy and paste the following:


Part 3: Enabling PHP


Step 1 – Enable Modules

The command(s) will vary depending on which PHP handler you have decided to install. Select the tab below that corresponds to the PHP handler you installed:

PHP 8.3 is used in this example. Remember to change the version if needed.

PHP 8.3 is used in this example. Remember to change the version if needed.


Step 2 – Restart Apache


Step 3 – Change PHP CLI Version

PHP 8.3 is used in this example. Remember to change the version if needed.


Step 4 (Optional) – Purge Previous/Unused PHP Versions

Example:






Congrats! You’re Done!

Test your PHP application to confirm that everything is working as intended.

Also, don’t forget to configure php.ini if needed.




Did you find this page helpful?

PID: 20241121-00001