When developing or deploying web applications, you often need a specific version of PHP to ensure compatibility and performance. In this guide, we’ll show you how to install a particular PHP version on Debian/Ubuntu using the popular Ondrej repository. Additionally, we’ll share tips for installing extensions and configuring your php.ini
file for optimal performance.
Step 1: Add the Ondrej Repository
Ondrej Surý maintains a well-regarded PPA (Personal Package Archive) that provides multiple PHP versions for Debian and Ubuntu systems. Here’s how to add it:
- Update your package list:
sudo apt update
- Install the required dependencies:
sudo apt install -y software-properties-common ca-certificates lsb-release apt-transport-https
- Add the Ondrej PPA:
sudo add-apt-repository ppa:ondrej/php
- Update your package list again to include packages from the new repository:
sudo apt update
Step 2: Install the Desired PHP Version
Once the Ondrej PPA is set up, you can install your preferred PHP version. For example, to install PHP 8.1:
sudo apt install -y php8.1
You can verify the installed version with:
php -v
To install a different version, replace php8.1
with the desired version, such as php7.4
or php8.2
.
Step 3: Manage PHP Versions
If you have multiple PHP versions installed, you can switch between them using the update-alternatives
command:
sudo update-alternatives --config php
This command will display a list of installed PHP versions. Enter the number corresponding to the version you want to use.
To switch PHP for Apache or Nginx, use these commands:
For Apache:
sudo a2dismod php8.1
sudo a2enmod php7.4
sudo systemctl restart apache2
For Nginx, update the PHP-FPM socket in your server block configuration, then restart Nginx:
sudo systemctl restart nginx
Step 4: Install PHP Extensions
PHP extensions add functionality to your PHP installation. Common extensions include pdo
, mbstring
, and xml
. To install extensions, use the following command:
sudo apt install -y php8.1-<extension_name>
For example, to install pdo
and xml
:
sudo apt install -y php8.1-pdo php8.1-xml
To list available extensions:
apt-cache search php8.1-
Replace php8.1
with your installed version if necessary.
Step 5: Configure PHP Settings
To configure PHP, edit the php.ini
file. The location depends on your setup:
- For CLI:
/etc/php/8.1/cli/php.ini
- For Apache:
/etc/php/8.1/apache2/php.ini
- For Nginx with PHP-FPM:
/etc/php/8.1/fpm/php.ini
Common Tweaks
- Increase Memory Limit
memory_limit = 256M
- Increase Maximum File Upload Size
upload_max_filesize = 50M post_max_size = 50M
- Enable Error Reporting (for development)
display_errors = On error_reporting = E_ALL
- Set Timezone
date.timezone = Europe/Paris
After editing, restart the relevant service to apply the changes:
For Apache:
sudo systemctl restart apache2
For PHP-FPM (Nginx):
sudo systemctl restart php8.1-fpm
Conclusion
Installing and configuring PHP with the Ondrej repository on Debian/Ubuntu is a straightforward process. By following these steps, you’ll be equipped to run any PHP version your application requires, alongside essential extensions and configuration tweaks. For more guides on optimizing your PHP environment, stay tuned to our blog!
Do you have any favorite PHP tips or tricks? Share them in the comments below!
Founder of ToolsLib, Designer, Web and Cybersecurity Expert.
Passionate about software development and crafting elegant, user-friendly designs.
Stay Updated with ToolsLib! 🚀
Join our community to receive the latest cybersecurity tips, software updates, and exclusive insights straight to your inbox!