How to Install and Run PHP 8.x on Ubuntu 20.04 – Step-by-Step Guide

PHP is one of the most popular programming languages for web development, powering millions of websites worldwide. With the release of PHP 8.x, developers gain access to numerous new features, enhancements, and performance improvements. If you’re an Ubuntu 20.04 user interested in harnessing the power of PHP 8.x for your projects, this guide will walk you through the installation process step by step.

Step 1: Update your system

Before installing PHP 8.x, it is very important to make sure your system is up-to-date. Open a terminal and run the following commands:

sudo apt update
sudo apt upgrade

This will update the package list and upgrade the existing packages to their latest version.

Step 2: Install PHP 8.x

The Ubuntu 20.04 repository may not have the latest PHP version available. Therefore, we will use the Ondřej Surý repository, which provides up-to-date PHP packages for Ubuntu.

Add repository:

sudo add-apt-repository ppa:ondrej/php

Press Enter when prompted to confirm adding the repository.

Next, update the package list again.

sudo apt update

sudo apt update

Finally, install PHP 8.x with the usual extensions:

sudo apt php8.0 install php8.0-common php8.0-cli

This command installs PHP 8.0 core with a command-line interface and common modules. You can install additional PHP extensions as per your requirements.

Step 3: Verify the PHP installation

To ensure that PHP 8.x is installed correctly, you can run the following command to check the installed version:

php -v

This command will display the PHP version installed on your system.

Step 4: Configure Apache or Nginx

If you use Apache or Nginx as your web server, you need to configure them to work with PHP. For Apache, you can install the PHP module with the following command:

sudo apt install libapache2-mod-php8.0

After installing the module, restart Apache.

sudo systemctl restart apache2

For Nginx, you need to install the PHP FastCGI Process Manager (PHP-FPM) with the following command:

sudo apt-install php8.0-fpm

Then, you need to configure your Nginx server blocks to use PHP-FPM. This configuration may vary depending on your specific setup.

Step 5: Test the PHP

To ensure that PHP is running correctly on . your web server, you can create a simple PHP script and access it through a web browser. For example, create a file named `info.php` in the document root directory of your web server (e.g., `/var/www/html/`) with the following content:

<?php
phpinfo();
?>

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

Conclusion

By following these steps, you can easily install and run PHP 8.x on Ubuntu 20.04. Whether you are an experienced developer or just starting out with PHP development, using the latest version brings many benefits in terms of performance, security, and new features. Keeping your PHP environment up-to-date ensures compatibility with modern web applications and frameworks, enhancing the overall development experience.

Tags:
Leave A Comment