Are you looking to install PHP on Ubuntu 22.04/24.04 LTS but don’t know where to start? 🤔 Don’t worry! This guide will walk you through setting up your PHP environment quickly and easily, whether you’re a beginner or an experienced developer. We’ll also share some tips on optimizing PHP 8 performance so you can get the best results.

Installing PHP 8.1/8.2/8.3
The PHP PPA repository for Ubuntu allows you to install multiple PHP versions, including 7.4, 8.0, 8.1, 8.2, 8.3, and 8.4, on Ubuntu-based systems like 22.04 and 24.04. In this guide, we will install PHP 8.2 and PHP 8.2-FPM. Simply specify the version you want to install and follow the steps.
Step 1: Install Required Tools
PHP 8.1 is available through a third-party PPA repository, which needs to be added to your system. Start by installing the necessary tools and adding the repository:
sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common -y
Step 2: Add the PHP PPA Repository
sudo add-apt-repository ppa:ondrej/php
Step 3: Update Package Lists
sudo apt update
Step 4: Install PHP
To install PHP, specify the version number as shown below:
sudo apt install php8.3 -y
To check the installed PHP version, run:
php -v
Để kiểm tra trạng thái hoạt động PHP:
sudo systemctl status php8.3-fpm
If multiple PHP versions are installed, you can set a default version using:
sudo update-alternatives --config php
This command allows you to set a default PHP version on your system. The available versions will be listed in numerical order, and you simply need to enter the corresponding number to select one, as shown in the screenshot.

Installing PHP-FPM
PHP-FPM versions are available in the same PPA repository (Step 2), so you only need to specify the desired version:
sudo apt install php8.3-fpm
Enable PHP-FPM to start with the system:
sudo systemctl enable php8.3-fpm
Installing PHP Extensions
PHP extensions are additional modules that enhance PHP’s functionality, allowing it to interact with databases, process images, handle encryption, and more. Depending on your web application, certain extensions may be required to ensure proper functionality.
php8.3-amqp php8.3-common php8.3-gd php8.3-ldap php8.3-apcu php8.3-curl php8.3-gmp php8.3-mailparse php8.3-ast php8.3-dba php8.3-igbinary php8.3-mbstring php8.3-bcmath php8.3-dev php8.3-imagick php8.3-memcached php8.3-bz2 php8.3-ds php8.3-imap php8.3-msgpack php8.3-cgi php8.3-enchant php8.3-interbase php8.3-mysql php8.3-readline php8.3-sqlite3 php8.3-xsl php8.3-xml php8.3-redis php8.3-sybase php8.3-yac php8.3-raphf php8.3-rrd php8.3-tidy php8.3-yaml php8.3-solr php8.3-smbclient php8.3-uuid php8.3-zip php8.3-psr php8.3-snmp php8.3-xdebug php8.3-zmq php8.3-cli php8.3-odbc php8.3-opcache php8.3-pgsql php8.3-phpdbg php8.3-pspell php8.3-soap php8.3-oauth php8.3-psr php8.3-fpm php8.3-intl php8.3-xhprof
To install a specific PHP 8.x extension, use:
sudo apt install php8.3-<extension>
To install multiple extensions at once:
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-intl php8.3-fpm php8.3-zip php8.3-bcmath php8.3-mcrypt
Configuring PHP-FPM (Optional)
By default, PHP-FPM runs under the www-data
user. However, if you are managing your projects under a different user, it’s recommended to update this setting for better security and permission manager.
Step 1: Create a Backup
sudo cp /etc/php/8.3/fpm/pool.d/www.conf /etc/php/8.3/fpm/pool.d/www.conf.disbaled
Step 2: Edit the PHP-FPM Configuration File
sudo nano /etc/php/8.3/fpm/pool.d/www.conf
Tìm và thay đổi user = www-data
thành user = your-username
. (Replace your-username
with your actual Ubuntu username.)

For additional PHP optimization tips, visit: 👉 PHP.ini Optimization Guide
Step 3: Restart PHP-FPM to Apply Changes
sudo service php8.3-fpm restart
Common Issues and Fixes
PHP-FPM Fails to Start
If PHP-FPM does not start due to configuration changes, check the error log with:
sudo journalctl -u php8.3-fpm
Or revert to the original configuration file and restart PHP-FPM.
You have successfully installed PHP on Ubuntu 22.04/24.04 LTS. 🚀 Now you can start developing web applications, testing PHP scripts, and optimizing performance as needed. If you run into any issues, double-check your PHP version, required modules, and error logs to troubleshoot quickly. Happy coding, and may your server always run smoothly! 🎯