The most recent version of PHP, 8.2, was published in 2022. You will learn how to install the most recent PHP version, which is 8.2, on your Ubuntu 22.04 machine or server and configure it with Apache and Nginx in this tutorial.
Install PHP 8.2 on Ubuntu 22.04
Also, read Check Code Quality in Jenkins
PHP is an excellent programming language that is used to create web-based applications. There are several ways to install PHP on Ubuntu 22.04. This section outlines the best ways to install PHP 8.2 on Ubuntu 22.04.
Prerequisites
Basic knowledge of using SSH Terminal on Linux.
Getting Started
Make sure your Ubuntu server is having the latest packages by running the following command.
sudo apt updatesudo apt upgrade
This will update the installed packages up to date as well as the package index.
Add PHP PPA
If you want to install PHP 8.1, you can skip this setup. However, if you intend to install any older or newer versions of PHP, such as 7.4 or 8.2, you must add this PPA.
sudo apt install software-properties-commonsudo add-apt-repository ppa:ondrej/phpsudo apt update
Only add this if you are about to install a PHP version other than version 8.1.
Install PHP 8.2 for Apache
Execute the following command to install PHP 8.2.
sudo apt install php8.2
After the installation has been completed, you can confirm the installation using the following command
php -v
Install PHP 8.2 FPM for Nginx
For Nginx, you need to install FPM. Execute the following command to install PHP 8.2 FPM
sudo apt install php8.2-fpm
After the installation has been completed, confirm that PHP 8.2 FPM has been installed correctly with this command.
php-fpm8.2 -v
Install PHP 8.2 Extensions
Installing PHP extensions are simple with the following syntax.
sudo apt install php8.2-extension_name
Now, install some commonly used php-extensions
with the following command.
sudo apt install php8.2-common php8.2-mysql php8.2-xml php8.2-xmlrpc php8.2-curl php8.2-gd php8.2-imagick php8.2-cli php8.2-dev php8.2-imap php8.2-mbstring php8.2-opcache php8.2-soap php8.2-zip php8.2-redis php8.2-intl -y
Verify PHP Version
You can easily verify the PHP version using the below command.
php -v
Output
PHP 8.2.0 (cli) (built: Jan 04 2023 09:13:42) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.0, Copyright (c) Zend Technologies
with Zend OPcache v8.2.0, Copyright (c), by Zend Technologies
Configure PHP 8.2
Now we configure PHP for Web Applications by changing some values in php.ini
file.
For PHP 8.2 with Apache, the php.ini
location will be in the following directory.
sudo nano /etc/php/8.2/apache2/php.ini
For PHP 8.2 FPM with Nginx the php.ini
location will be in the following directory.
sudo nano /etc/php/8.2/fpm/php.ini
Hit F6
for search inside the editor and update the following values for better performance.
upload_max_filesize = 32M
post_max_size = 48M
memory_limit = 256M
max_execution_time = 600
max_input_vars = 3000
max_input_time = 1000
Once you have modified your PHP settings you need to restart your Apache for the changes to take effect.
For users with Nginx who use PHP-FPM, you need to restart PHP-FPM.
sudo service php8.2-fpm restart
Configure PHP 8.2 FPM Pools
PHP-FPM allows you to configure the user
and group
that the service will run under. You can modify these with these commands.
sudo nano /etc/php/8.2/fpm/pool.d/www.conf
If you want to change the username name you can change the following lines by replacing the www-data with your username
.
user = username
group = username
listen.owner = username
listen.group = username
Hit CTRL+X
and Y
to save the configuration and check if the configuration is correct and restart PHP.
Restart PHP 8.2 FPM
Once you have updated your PHP FPM settings you need to restart it to apply the changes.
sudo php-fpm8.2 -t
sudo service php8.2-fpm restart
Now you are having PHP 8.2 Installed and configured.
Upgrade/Downgrade PHP in CLI
As you are switching the PHP version for your web applications you will also need to change the PHP version in your CLI (Command Line Interface).
Execute the below command to change the PHP version on your CLI. You will be prompted with an interactive mode to choose your PHP version.
sudo update-alternatives --config php
There are 3 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/php8.2 82 auto mode
1 /usr/bin/php8.1 81 manual mode
2 /usr/bin/php7.4 74 manual mode
2 /usr/bin/php8.0 80 manual mode
4 /usr/bin/php8.2 82 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Enter your choice and hit ENTER.
Upgrade/Downgrade PHP for Apache
If you want to use a different PHP version you can upgrade or downgrade to the other PHP version as shown below..
You need to tell Apache to use the correct PHP version you installed right now. Disable the old PHP module (below I have mentioned php 8.2, you need to use your other php version used by Apache) and enable the new PHP module using the following command.
Replace the current enabled PHP version with your version.
sudo a2dismod php7.4
sudo a2enmod php8.2
Restart Apache for the changes to take effect.
sudo service apache2 restart
Upgrade/Downgrade PHP for Nginx
For Nginx, you need to update or downgrade the PHP-FPM socket in your Nginx configuration located inside the sites-available
directory. This will be located inside the location
block location ~ .php$
Edit your configuration…
sudo nano /etc/nginx/sites-available/your.conf
The line you need to modify will look like this…
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
You need to replace the old PHP version with the new version.
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
Test your configuration.
sudo nginx -t
Save the file and exit the editor and restart Nginx for the changes to take effect.
sudo service nginx restart
Conclusion
You now know how to install PHP 8.2 for Apache and Nginx on your Ubuntu 22.04 server, as well as how to upgrade or downgrade to a different PHP version. I hope that by reading this article, you get a complete guide on how to install PHP 8.2 on Ubuntu 22.02.
I appreciate your time. Please leave a comment below if you run into any issues or have any suggestions.