how to install php 8.1 on ubuntu 22.04

22

Jun

How to Install PHP 8.1 on Ubuntu 22.04

PHP 8.1 is the latest major release of PHP, bringing several new features, improvements, and optimizations. If you’re looking to upgrade or install PHP 8.1 on Ubuntu 22.04 server or desktop, follow these step-by-step instructions.

Update Package List

First, update the package list to ensure you have the latest information about available packages.

sudo apt update

Add PHP PPA

Ondřej Surý maintains a PPA (Personal Package Archive) that includes the latest PHP versions for Ubuntu. Add this PPA to your system.

sudo add-apt-repository ppa:ondrej/php

You may need to press Enter to confirm adding the repository.

Update Package List Again

After adding the PPA, update the package list again to include packages from the newly added repository.

sudo apt update

Install PHP 8.1 on Ubuntu and Required Extensions

Now, install PHP 8.1 along with commonly used extensions. Adjust the list of extensions based on your specific requirements.

sudo apt install -y php8.1 php8.1-mcrypt php8.1-gd php8.1-curl php8.1-mysql php8.1-zip php8.1-xml php8.1-soap php8.1-intl php8.1-mbstring php8.1-bcmath

Configure PHP Settings

Edit the PHP configuration file to customize PHP settings according to your needs. Here, we use nano, but you can use any text editor you prefer.

sudo nano /etc/php/8.1/apache2/php.ini

Make sure to adjust the following settings or add them if they are not present. These settings are commonly adjusted for web applications:

memory_limit = 2048M
max_input_vars = 10000
max_execution_time = 3600
max_input_time = 3600
display_errors = On
error_reporting = E_ALL
post_max_size = 2024M

Save and close the file (Ctrl + X, then Y to confirm and Enter to save).

Restart Apache

After making changes to the PHP configuration, restart the Apache web server for the changes to take effect.

sudo service apache2 restart

Verification

To verify that PHP 8.1 is installed correctly, you can create a PHP info page. Create a file named info.php in your web server’s root directory (/var/www/html/ by default) with the following content:

<?php
phpinfo();
?>

Save the file and access it through your web browser (http://localhost/info.php). You should see a page displaying PHP information, confirming the installation.

Conclusion

You have successfully installed PHP 8.1 on your Ubuntu 22.04 system and configured it for basic web application needs. Remember to adjust additional PHP settings or install other extensions as per your project requirements. PHP 8.1 brings performance improvements and new features, ensuring your applications run efficiently and securely. Enjoy coding with PHP 8.1!

Leave a Reply

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

RELATED

Posts