Install PHP 8.1 with extensions in ubuntu. In this tutorial, you will learn how to install php 8.1 with it’s extensions in ubuntu 20.04 & 22.04 apache using the terminal or command line.
Prerequisites
If you want to install PHP in your Ubuntu system, then for this you must first have Apache server installed in your ubuntu system. By clicking on this link, you can learn how to install Apache server in Ubuntu.
How to Install PHP 8.1 on Ubuntu 20.04 | 22.04 Apache Terminal
Steps and install configure php 8.1 with it’s extensions on ubuntu 20.04 & 22.04 Apache using terminal or command line:
- Step 1 – Ubuntu Update System Packages
- Step 2 – Install Software Properties Common
- Step 3 – Add PHP Repository
- Step 4 – Install PHP 8.1 and Extensions Ubuntu
- Step 5 – Configure Apache with PHP-FPM
- Step 6 – Test Installed PHP Version
Step 1 – Ubuntu Update System Packages
Before installing any software, it’s a good practice to update your system’s package list and upgrade existing packages:
sudo apt update sudo apt upgrade
Step 2 – Install Software Properties Common
You’ll need the software-properties-common
package to easily manage software repositories:
sudo apt install software-properties-common
Step 3 – Add PHP Repository
PHP 8.1 might not be available in the default Ubuntu repositories, so you’ll need to add the repository containing PHP 8.1:
sudo add-apt-repository ppa:ondrej/php
After adding the repository, update the package list again:
sudo apt update
Step 4 – Install PHP 8.1 and Extensions Ubuntu
Now you can install PHP 8.1 along with the extensions you need. For example, if you want to install common extensions like mbstring
, curl
, json
, and mysql
:
sudo apt install php8.1 php8.1-common php8.1-cli php8.1-fpm php8.1-mbstring php8.1-curl php8.1-json php8.1-mysql
Step 5 – Configure Apache with PHP-FPM
In this step, you need to configure Php-FPM with apache by executing the following command to install the necessary packages:
sudo apt update
By default PHP-FPM is not enabled in Apache. To enable it, run:
sudo a2enmod proxy_fcgi setenvif sudo a2enconf php8.0-fpm
To activate the changes, restart Apache:
systemctl restart apache2
Step 6 – Test Installed PHP Version
To verify that PHP 8.1 is installed and configured correctly, create a simple PHP script and run it. Use the following command to create a file named info.php
in your web server’s root directory:
sudo nano /var/www/html/info.php
Add the following content to the info.php
file:
<?php phpinfo(); ?>
Save the file and access it from your web browser by navigating to http://your_server_ip/info.php
. You should see a page displaying PHP information, including the version.
Conclusion
You’ve successfully installed PHP 8.1 on Ubuntu 20.04 | 22.04 and added some common PHP extensions. You can now start developing and deploying your PHP applications using the latest version of PHP. Remember to adjust the extensions and configurations according to your project’s needs.
Recommended Ubuntu Posts
If you have any questions or thoughts to share, use the comment form below to reach us.