Install PHP 8.2 on Ubuntu 20.04 with Efficient Steps

Posted on

Install PHP 8.2 on Ubuntu 20.04 with Efficient Steps

Install PHP 8.2 on Ubuntu 20.04 with Efficient Steps

In this tutorial, we will guide you through the process of Install PHP 8.2 on Ubuntu 20.04. PHP is a widely used open-source server-side scripting language favored by developers for web development. Its versatility extends beyond web applications, enabling the creation of various projects, including Graphical User Interfaces (GUIs).

PHP 8.2 introduces significant improvements, including enhanced type-system features for more expressive and precise type safety, readonly classes, support for sensitive parameter redaction, a new random extension, and numerous other features designed to modernize and streamline the PHP language.

To follow this guide, you need access to an Ubuntu 20.04 server and a non-root user with sudo privileges. If you haven’t already set this up, you can refer to our guide on Initial Server Setup with Ubuntu 20.04.

This article will walk you through installing PHP 8.2 with both Apache and Nginx web servers.

1. Import Ondřej Surý PHP PPA on Ubuntu 20.04

To begin the installation of PHP 8.2, you’ll need to import the PPA (Personal Package Archive) maintained by Ondřej Surý. He is a key figure in the PHP and Debian communities, known for maintaining Ubuntu and Debian packages. His PPA provides updated PHP packages for Ubuntu.

Execute the following command to install the Ondřej Surý PPA and any required dependencies on Ubuntu 20.04:

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

After adding the PPA, it’s essential to update your local package index to recognize the new packages available. Run the following commands:

sudo apt update && sudo apt upgrade

2. Set up PHP 8.2 on Ubuntu 20.04

Now that the PPA is configured, you can proceed with installing PHP 8.2. Depending on whether you’re using Apache or Nginx as your web server, the installation process will vary slightly.

Installing PHP 8.2 for Apache on Ubuntu 20.04

If you’re using Apache HTTP server, you have the option of running PHP as an Apache module or using PHP-FPM (FastCGI Process Manager).

To install PHP 8.2 as an Apache module, execute the following command:

sudo apt install php8.2 libapache2-mod-php8.2 -y

Once the installation is complete, restart Apache to load the newly installed PHP module:

sudo systemctl restart apache2

Alternatively, you can install Apache with PHP-FPM. Use the following command:

sudo apt install php8.2-fpm libapache2-mod-fcgid
Enable PHP-FPM for Apache on Ubuntu 20.04

Note: PHP-FPM is not enabled by default for Apache. You need to explicitly enable it using the following command:

sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php8.2-fpm

After enabling PHP-FPM, restart Apache to apply the changes:

sudo systemctl restart apache2

To verify that your PHP-FPM service is active and running, use the following command:

sudo systemctl status php8.2-fpm
(The original article's image showing the output of the above command is displayed here)

You can also verify the PHP installation using the command:

php --version
(The original article's image showing the output of the above command is displayed here)

Installing PHP 8.2 for Nginx on Ubuntu 20.04

Nginx doesn’t have native PHP processing capabilities like Apache. To process PHP files, you need to install PHP-FPM, which acts as a fastCGI process manager.

Install PHP 8.2 and PHP 8.2-FPM on Ubuntu 20.04 using the following command:

sudo apt install php8.2 php8.2-fpm php8.2-cli -y

Verify that the PHP-FPM service is active and running:

sudo systemctl status php8.2-fpm
(The original article's image showing the output of the above command is displayed here)

Next, you need to configure your Nginx server block to process PHP files. Add the following example to your Nginx server block configuration:

server {
    # ... some other code
    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    }
}

Test your Nginx configuration to ensure there are no syntax errors:

sudo nginx -t
**Output**
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Finally, restart Nginx to apply the changes:

sudo systemctl restart nginx

You can verify the PHP 8.2 installation with:

php --version
**Output**
PHP 8.2.0 (cli) (built: Dec 10 2022 10:52:42) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.0, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.0, Copyright (c), by Zend Technologies

Conclusion

This guide covered the steps to Install PHP 8.2 on Ubuntu 20.04 using both Apache and Nginx. We hope you found this tutorial helpful. Please subscribe to our social media channels on Facebook, YouTube, and Twitter.

You may also like these articles:

Alternative Solutions for Installing PHP 8.2 on Ubuntu 20.04

While the Ondřej Surý PPA is a reliable method for installing specific PHP versions, alternative approaches exist. Here are two alternative solutions for installing PHP 8.2 on Ubuntu 20.04:

1. Using Docker

Docker provides a containerization platform that allows you to run applications in isolated environments. This is an excellent option if you want to avoid modifying your system’s PHP version or need to run multiple PHP versions concurrently.

Explanation:

Docker allows you to create a containerized environment specifically for your PHP application. You can define the exact PHP version, extensions, and dependencies required in a Dockerfile. This ensures that your application runs consistently across different environments, regardless of the PHP version installed on the host system.

Steps:

  1. Install Docker: If you don’t have Docker installed, follow the instructions on the official Docker website: https://docs.docker.com/engine/install/ubuntu/

  2. Create a Dockerfile: Create a file named Dockerfile in your project directory. This file will contain instructions for building your PHP container.

    FROM php:8.2-apache
    
    # Install necessary extensions
    RUN docker-php-ext-install mysqli pdo pdo_mysql
    
    # Copy your application code
    COPY . /var/www/html/
    
    # Set document root (optional)
    # ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
    
    < /var/www/html/public>
    # Configure Apache (optional)
    # COPY apache2.conf /etc/apache2/sites-available/000-default.conf

    Explanation of Dockerfile:

    • FROM php:8.2-apache: Specifies the base image. This uses the official PHP 8.2 image with Apache pre-installed. Other variants are available, such as php:8.2-fpm for PHP-FPM.
    • RUN docker-php-ext-install mysqli pdo pdo_mysql: Installs the mysqli, pdo, and pdo_mysql extensions. Add any other extensions your application requires.
    • COPY . /var/www/html/: Copies your application’s source code to the /var/www/html/ directory inside the container.
    • ENV APACHE_DOCUMENT_ROOT=/var/www/html/public: Sets the Apache document root to the public directory (if your application uses one). Remove this line if you’re not using a public directory.
    • COPY apache2.conf /etc/apache2/sites-available/000-default.conf: Copies a custom Apache configuration file. This is optional and only needed if you require specific Apache settings.
  3. Build the Docker Image: In your project directory (where the Dockerfile is located), run the following command to build the Docker image:

    docker build -t my-php-app .

    This command builds an image named my-php-app using the instructions in the Dockerfile.

  4. Run the Docker Container: Run the following command to start a container from the image:

    docker run -d -p 8080:80 my-php-app
    • -d: Runs the container in detached mode (in the background).
    • -p 8080:80: Maps port 8080 on your host machine to port 80 inside the container (where Apache is running). You can change 8080 to any available port on your host.
  5. Access Your Application: Open your web browser and navigate to http://localhost:8080 (or the port you specified) to access your application.

2. Using a Local Development Environment (e.g., Laragon, XAMPP)

Local development environments like Laragon (for Windows) and XAMPP (cross-platform) provide pre-configured stacks that include Apache or Nginx, MySQL, and PHP. These are ideal for development and testing, providing an easy way to switch between PHP versions.

Explanation:

These tools bundle everything you need for PHP development into a single package. They handle the installation and configuration of Apache/Nginx, MySQL, and PHP, allowing you to focus on coding. Many offer convenient interfaces for managing PHP versions and extensions.

Steps (Example using Laragon):

  1. Download and Install Laragon: Download Laragon from https://laragon.org/download/ and install it.

  2. Add PHP 8.2: Laragon typically comes with a default PHP version. To add PHP 8.2:

    • Stop all Laragon services (if running).
    • Download the PHP 8.2 thread-safe (TS) version from https://windows.php.net/download
    • Extract the contents of the downloaded ZIP file to a new folder named php-8.2.x-nts-Win32-vs16-x64 (or similar) inside the laragon/bin/php/ directory. Rename the directory to simply php-8.2.
    • Start Laragon.
  3. Switch to PHP 8.2:

    • Right-click on the Laragon tray icon.
    • Go to PHP -> Version and select 8.2.
    • Laragon will automatically restart Apache and MySQL to use the new PHP version.
  4. Create a Project: Create a new project in Laragon (e.g., by placing your PHP files in the laragon/www/myproject directory).

  5. Access Your Application: Open your web browser and navigate to http://myproject.test (or the domain name Laragon assigns to your project) to access your application.

XAMPP follows a similar process, allowing you to download different PHP versions as modules and switch between them through the XAMPP Control Panel.

These alternative solutions offer flexibility and control over your PHP environment, especially when dealing with multiple projects or different PHP version requirements. They provide a more isolated and manageable environment compared to directly installing PHP on your system.

Leave a Reply

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