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:
sudo add-apt-repository ppa:ondrej/php
Update your system:
sudo apt update
sudo apt upgrade
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
PHP can be installed either as an Apache module or as FPM. You will need to
decide which one to install, as the installation procedure differs between the two.
Select the tab below that corresponds to the PHP handler you wish to install:
sudo apt install php8.3 libapache2-mod-php8.3
PHP 8.3 is used in these examples. To install a different version of PHP, simply
change the version numbers. For example, if you want to install PHP 8.2, replace
php8.3 and libapache2-mod-php8.3 with php8.2 and libapache2-mod-php8.2.
This applies to the rest of the installation as well.
sudo apt install php8.3-fpm libapache2-mod-fcgid
PHP 8.3 is used in these examples. To install a different version of PHP, simply
change the version numbers. For example, if you want to install PHP 8.2, replace
php8.3-fpm with php8.2-fpm This applies to the rest of the installation as well.
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:
sudo a2dismod php7.0
sudo a2dismod php7.1
sudo a2dismod php7.2
sudo a2dismod php7.3
sudo a2dismod php7.4
sudo a2dismod php8.0
sudo a2dismod php8.1
sudo a2dismod php8.2
sudo a2dismod php8.3
sudo a2disconf php7.0-fpm
sudo a2disconf php7.1-fpm
sudo a2disconf php7.2-fpm
sudo a2disconf php7.3-fpm
sudo a2disconf php7.4-fpm
sudo a2disconf php8.0-fpm
sudo a2disconf php8.1-fpm
sudo a2disconf php8.2-fpm
sudo a2disconf php8.3-fpm
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:
sudo a2enmod php8.3
PHP 8.3 is used in this example. Remember to change the version if needed.
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.3-fpm
PHP 8.3 is used in this example. Remember to change the version if needed.
Step 2 – Restart Apache
sudo service apache2 restart
Step 3 – Change PHP CLI Version
sudo update-alternatives –set php /usr/bin/php8.3
PHP 8.3 is used in this example. Remember to change the version if needed.
Step 4 (Optional) – Purge Previous/Unused PHP Versions
If you want to purge all unused versions of PHP from the server, use the following command.
Replace ‘x.x‘ with the PHP version you wish to purge:
sudo apt purge phpx.x*
Example:
sudo apt purge php8.2*
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