PHP is one of the most popular and powerful scripting languages for web development. It is used by millions of websites, including WordPress, Facebook, and Wikipedia. PHP allows you to create dynamic and interactive web pages, as well as connect to databases, send emails, process forms, and more.
The latest version of PHP is 8.3, which was released in December 2023. PHP 8.3 offers many features and benefits, such as:
- Improved performance and memory usage
- New syntax and operators, such as the nullsafe operator, the match expression, and the named arguments
- New built-in functions and classes, such as the str_contains, str_starts_with, str_ends_with, and WeakMap functions and classes and many more
In this article, we will guide you on how to install PHP 8.3 on Ubuntu 22.04, a popular Linux distribution that is widely used for web servers and cloud computing. We will also show you how to install and configure some common PHP extensions and settings, as well as how to test the PHP functionality and performance.
Prerequisites
Before you start installing PHP 8.3 on Ubuntu 22.04, you need to have the following requirements:
- A system running Ubuntu 22.04.
- A user account with sudo privileges.
- A stable internet connection.
Initial Server Setup
The first step to install PHP 8.3 on Ubuntu 22.04 is to update the system packages and repositories. This will ensure that your system is up to date and secure, as well as compatible with the latest PHP version.
To update the system, run the following commands in the terminal:
sudo apt update
sudo apt upgrade
The sudo apt update
command will update the list of available packages and repositories from the internet. The sudo apt upgrade
command will upgrade the installed packages to their latest versions.
Add the Ondřej Surý PPA Repository
The next step to install PHP 8.3 on Ubuntu 22.04 is to add the Ondřej Surý PPA repository. A PPA (Personal Package Archive) is a third-party repository that provides additional packages for Ubuntu users. The Ondřej Surý PPA repository is a trusted and reliable source of PHP packages for Ubuntu, and it supports multiple PHP versions, including PHP 8.3.
To add the Ondřej Surý PPA repository, run the following command in the terminal:
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
You may be asked to enter your user password or confirm the addition of the repository. Enter your password or type Y
and press Enter
when prompted.
After adding the Ondřej Surý PPA repository, you need to update the system again to refresh the list of available packages and repositories. Run the following command in the terminal:
sudo apt update
Install PHP 8.3
The next step to install PHP 8.3 on Ubuntu 22.04 is to install PHP 8.3 and its common modules. A module is a component of PHP that provides specific functionality or compatibility with various web applications and frameworks. Some of the common modules that you may need are:
- php8.3-common: This module provides the basic PHP functionality, such as the core PHP functions, classes, and interfaces.
- php8.3-cli: This module provides the command-line interface for PHP, which allows you to run PHP scripts from the terminal.
- php8.3-fpm: This module provides the FastCGI Process Manager for PHP, which is a service that handles the communication between PHP and the web server, such as Apache or Nginx.
To install PHP 8.3 and its common modules for Apache or Nginx, run the following command in the terminal:
sudo apt install php8.3 php8.3-common php8.3-cli php8.3-fpm
To verify the installation and check the PHP version, run the following command in the terminal:
php -v
You should see a message that shows the PHP version, such as:
Output
PHP 8.3.0 (cli) (built: Jan 22 2024 16:12:52) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.0, Copyright (c) Zend Technologies
with Zend OPcache v8.3.0, Copyright (c), by Zend Technologies
Install PHP 8.3 Extensions
The next step to install PHP 8.3 on Ubuntu 22.04 is to install additional PHP extensions. PHP has many extensions that can enhance its functionality and compatibility with various web applications and frameworks. Some of the most popular and useful PHP extensions are installed as follows:
sudo apt install php8.3-common php8.3-mysql php8.3-xml php8.3-xmlrpc php8.3-curl php8.3-gd php8.3-imagick php8.3-cli php8.3-dev php8.3-imap php8.3-mbstring php8.3-opcache php8.3-soap php8.3-zip php8.3-redis php8.3-intl -y
Configure PHP 8.2
PHP has many settings that can be customized to suit the needs and preferences of the user and the web application. Some of the common and important PHP settings are:
memory_limit
: sets the maximum amount of memory that a PHP script can useupload_max_filesize
: sets the maximum size of an uploaded filemax_execution_time
: sets the maximum time that a PHP script can rundisplay_errors
: determines whether to display errors on the screen or noterror_reporting
: determines the level of error reporting
Now we configure PHP for Web Applications by changing some of these values in php.ini
file.
For PHP 8.3 with Apache the php.ini
location will be in following directory.
sudo nano /etc/php/8.3/apache2/php.ini
For PHP 8.3 FPM with Nginx the php.ini
location will be in following directory.
sudo nano /etc/php/8.3/fpm/php.ini
Hit F6
for search inside the editor and update the following values for better PHP 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 to who use PHP-FPM, you need to restart PHP-FPM.
sudo service php8.3-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.3/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.
Learn how to optimize PHP-FPM further for better reliability.
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.3 -t
sudo service php8.3-fpm restart
Now you are having PHP 8.3 Installed and configured.
Upgrade/Downgrade PHP in CLI
As you are switching 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 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 4 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/php8.3 83 auto mode
1 /usr/bin/php8.1 81 manual mode
2 /usr/bin/php7.4 74 manual mode
3 /usr/bin/php8.0 80 manual mode
4 /usr/bin/php8.2 82 manual mode
5 /usr/bin/php8.3 83 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Enter you 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.3, 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 php8.1
sudo a2enmod php8.3
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/php8.1-fpm.sock;
You need to replace the old PHP version with the new version.
fastcgi_pass unix:/run/php/php8.3-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
Now you have learnt how to install PHP 8.3 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.3 on Ubuntu 22.02.
I appreciate your time. Please leave a comment below if you run into any issues or have any suggestions.
Leave your Reply