Install PHP 8 on UBUNTU

How to Install PHP 8.0 on Ubuntu

Step 1: Update Your System

Before installing PHP 8.0, make sure your system is up-to-date. Open a terminal and run the following commands:

sudo apt update
sudo apt upgrade -y

Step 2: Add PHP Repository

PHP 8.0 might not be available in the default Ubuntu repositories, so add the ondrej/php repository by running these commands:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Step 3: Install PHP 8 and Extensions

Install PHP 8.0 along with commonly used extensions using the following command:

sudo apt update
sudo apt install php8.0 php8.0-cli php8.0-fpm php8.0-mysql php8.0-json php8.0-opcache php8.0-mbstring php8.0-xml php8.0-gd php8.0-curl php8.0-zip php8.0-pear php8.0-dev

Step 4: Verify PHP Installation

Verify that PHP 8.0 has been successfully installed by running the following command:

php8.0 -v

Step 5: Configure PHP (Optional)

Customize PHP settings by editing the PHP configuration file. Use your preferred text editor to open the php.ini file, for example:

sudo nano /etc/php/8.0/cli/php.ini

Edit the file as needed, save your changes, and exit the text editor. You may also need to configure PHP for web server integration (e.g., Apache or Nginx) by editing the corresponding php.ini file.

Step 6: Start and Enable PHP-FPM (Optional)

If you're using PHP with a web server like Nginx, start and enable the PHP-FPM service with these commands:

sudo systemctl start php8.0-fpm
sudo systemctl enable php8.0-fpm

Step 7: Test PHP

Create a PHP test file to ensure PHP is working correctly. Use your preferred text editor to create a file, for example, info.php, in your web server's document root directory (often /var/www/html/):

sudo nano /var/www/html/info.php

Add the following line to the info.php file:

Save the file and exit the text editor.

Step 8: Access the PHP Info Page

Open a web browser and navigate to:

http://your_server_ip/info.php

Replace your_server_ip with your server's IP address. You should see the PHP info page, which provides detailed information about your PHP installation.

Step 9: Secure the PHP Info Page

After confirming that PHP works correctly, it's crucial to remove or secure the info.php file to prevent unauthorized access to sensitive information.

sudo rm /var/www/html/info.php

Alternatively, restrict access to it using your web server's configuration.

That's it! You've successfully installed PHP 8.0 (or the available PHP version) on Ubuntu and verified its functionality.

Related Articles