How to Install PHP 8.1 on Ubuntu 22.04 | 20.04

How to Install PHP 8.1 on Ubuntu 22.04 | 20.04

If you want to install a specific version like PHP 8.1 in your Ubuntu system; In this tutorial, we will show you detail guide on how to install PHP 8.1 with its extensions in ubuntu 20.04 and 22.04 using terminal or cmd.

How to Install PHP 8.1 on Ubuntu 20.04 or 22.04

Here are some steps to install a specific version of PHP 8.1 in Ubuntu 22.04 and Ubuntu 20.04:

  • 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.

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *