PHP is an acronym of “Hypertext Preprocessor” is a scripting language widely used for developing web applications. It is the server-side scripting language that is embedded in HTML. Which is used to manage dynamic content, databases, session tracking, even build entire e-commerce websites.

Advertisement

PHP 8.0 is the latest stable version available for production use. The debian packages for PHP 8 is available under ppa:ondrej/php PPA for Ubuntu systems.

In this tutorial, you will learn the following.

  1. Install PHP 8 on Ubuntu
  2. Install PHP 8 Extensions
  3. Setup PHP 8 with Apache
  4. Setup PHP 8 with Nginx
  5. Switch between multiple PHP versions

So, let’s begin with a tutorial to install and Use PHP 8.0 on Ubuntu 20.04 LTS Focal system.

Step 1 – Install PHP 8 on Ubuntu

The Ondrej PHP PPA is always up to date and contains the latest PHP versions. Use the following commands to add PPA and install PHP 8.0 on Ubuntu 20.04 system.

  1. Firstly, you need to install a required package on your system. Open a terminal and execute the following commands.
    sudo apt update && sudo apt install software-properties-common -y 
    
  2. Enable PPA – Next, Use the following command to enable PHP PPA and update apt cache
    sudo add-apt-repository ppa:ondrej/php 
    
  3. Install PHP 8 – Next, Type below command to install the PHP 8.0 on Ubuntu system.
    sudo apt install php8.0 
    

    Press ‘Y’ for any confirmation asked by the installer.

Once completed the above commands, you have successfully installed PHP 8.0 on the Ubuntu system.

Step 2 – Installing PHP Extensions

Now, install the required PHP modules for your application. Use the following command to search all available PHP 8.0 modules.

sudo apt search php8.0-* 

Then install the required PHP modules. The following command will install some frequently used PHP modules on your system.

sudo apt install php8.0-gd php8.0-xml php8.0-soap php8.0-mbstring php8.0-mysql 

You may also need to install other PHP modules recommended by the application.

Step 3 – Install PHP 8 with Apache

Apache is a popular web server widely used for deploying PHP web applications. On Debian-based systems this is available with the name “apache2”.

An Apache module is available to integrate PHP. For PHP 8.0 install the following Apache module

sudo apt install libapache2-mod-php8.0 

Then enable PHP 8 module

sudo a2enmod php8.0 

All done, now your can host a PHP application with Apache using PHP 8.

To test the integration, create a phpinfo() file and verify the version.

Step 4 – Install PHP 8 with Nginx

There is no official PHP module for the Nginx server. In that case, we will use PHP-FPM to deploy PHP applications over Nginx web servers.

So, first, install PHP fpm on your system.

sudo apt install php8.0-fpm 

Once the installation is finished, enable and start the service.

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

Next, update the Nginx server block configuration with the following code.

server {

   # server block code ...

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;
    }
}

The above code instructs the Nginx server to process all files with the “.php” extension using PHP-fpm.

Finally, restart the Nginx webserver to apply the changes.

sudo systemctl restart nginx 

Step 5 – Switch Default PHP Version

You can easily switch between multiple PHP version installed on any system. Execute the following command on terminal:

sudo update-alternatives --config php  

This system has PHP 8.0 and PHP 7.4 installed. Select a PHP version on our choice.

There are 2 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.0   80        auto mode
  1            /usr/bin/php7.4   74        manual mode
  2            /usr/bin/php8.0   80        manual mode

Press  to keep the current choice[*], or type selection number: [ENTER CHOICE HERE]

Conclusion

This tutorial helps you to install PHP 8.0 on Ubuntu 20.04 Linux system. You may also need to install MySQL on your Ubuntu system.

Share.

11 Comments

  1. Typo:

    sudo add update && sudo apt install software-properties-common -y

    should be:

    sudo apt update && sudo apt install software-properties-common -y

Leave A Reply

Exit mobile version