Install PHP 8.2 on Ubuntu 22.04: Easy Setup – OrcaCore

Posted on

Install PHP 8.2 on Ubuntu 22.04: Easy Setup - OrcaCore

Install PHP 8.2 on Ubuntu 22.04: Easy Setup – OrcaCore

This tutorial provides a comprehensive guide on How To Install PHP 8.2 on Ubuntu 22.04. PHP, an acronym for Hypertext Pre-processor (originally Personal Home Pages), is a widely-used server-side scripting language essential for developing static websites, dynamic websites, and web applications.

PHP scripts require a server with PHP installed for interpretation. Clients accessing these scripts only need a web browser. A PHP file consists of PHP tags and uses the ".php" extension.

Follow the step-by-step instructions below to set up Install PHP 8.2 on Ubuntu 22.04 on your OrcaCore server.

Prerequisites

Before proceeding, ensure you have the following:

This guide will cover installing PHP 8.2 with both Apache and Nginx modules. The goal of this article is to help you Install PHP 8.2 on Ubuntu 22.04.

Import Ondřej Surý PHP PPA on Ubuntu 22.04

To begin, you need to import the PHP PPA maintained by Ondřej Surý, a leading PHP and Debian developer. This PPA provides the necessary packages for PHP 8.2.

Execute the following command to install the PPA and required packages:

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

Next, update and upgrade your local package index:

sudo apt update && sudo apt upgrade

Set up PHP 8.2 on Ubuntu 22.04

With the PPA imported, you can now install PHP 8.2. The installation process differs slightly depending on whether you’re using Apache or Nginx.

Installing PHP 8.2 for Apache on Ubuntu 22.04

If you’re using the Apache HTTP server, you can run PHP as an Apache module or using PHP-FPM.

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

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

After the installation, restart Apache to load the new PHP module:

sudo systemctl restart apache2

Alternatively, you can install Apache with PHP-FPM using this command:

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

Note: PHP-FPM isn’t enabled by default for Apache. Enable it with:

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

Restart Apache to apply the changes:

sudo systemctl restart apache2

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

sudo systemctl status php8.2-fpm
Output:
● php8.2-fpm.service - The PHP 8.2 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.2-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-01-23 14:35:00 UTC; 5s ago
       Docs: man:php-fpm8.2(8)
   Main PID: 1234 (php-fpm8.2)
     Status: Ready to handle connections
      Tasks: 7 (limit: 2281)
     Memory: 21.5M
        CPU: 87ms
     CGroup: /system.slice/php8.2-fpm.service
             ├─1234 php-fpm: master process (/etc/php/8.2/fpm/php-fpm.conf)
             ├─1235 php-fpm: pool www
             ├─1236 php-fpm: pool www
             ├─1237 php-fpm: pool www
             ├─1238 php-fpm: pool www
             ├─1239 php-fpm: pool www
             └─1240 php-fpm: pool www

You can verify your PHP installation with the following command:

php --version
Output:
PHP 8.2.x (cli) (built: Jan 1 2023 hh:mm:ss) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.x, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.x, Copyright (c) Zend Technologies

Installing PHP 8.2 for Nginx on Ubuntu 22.04

Nginx doesn’t natively process PHP like Apache. You need to install PHP-FPM (fastCGI process manager) to handle PHP files.

Install PHP 8.2, PHP 8.2-FPM, and the command-line interface (CLI) with:

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

Verify the PHP-FPM service is active and running:

sudo systemctl status php8.2-fpm
Output:
● php8.2-fpm.service - The PHP 8.2 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.2-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-01-23 14:40:00 UTC; 5s ago
       Docs: man:php-fpm8.2(8)
   Main PID: 4567 (php-fpm8.2)
     Status: Ready to handle connections
      Tasks: 7 (limit: 2281)
     Memory: 22.1M
        CPU: 92ms
     CGroup: /system.slice/php8.2-fpm.service
             ├─4567 php-fpm: master process (/etc/php/8.2/fpm/php-fpm.conf)
             ├─4568 php-fpm: pool www
             ├─4569 php-fpm: pool www
             ├─4570 php-fpm: pool www
             ├─4571 php-fpm: pool www
             ├─4572 php-fpm: pool www
             └─4573 php-fpm: pool www

Edit your Nginx server block configuration to process PHP files. Add the following block within the server block:

server {
    # ... other configuration directives ...

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

    # ... other configuration directives ...
}

Test Nginx for 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

Restart Nginx to apply the changes:

sudo systemctl restart nginx

Verify the Install PHP 8.2 on Ubuntu 22.04 using the same command:

php --version
Output:
PHP 8.2.x (cli) (built: Jan 1 2023 hh:mm:ss) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.x, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.x, Copyright (c) Zend Technologies

Conclusion

You have now successfully learned Install PHP 8.2 on Ubuntu 22.04. PHP 8.2 is a versatile scripting language essential for modern web development.

Stay updated by following us on Facebook, YouTube, and Twitter.

You might also find these articles helpful:

Alternative Solutions for Installing PHP 8.2 on Ubuntu 22.04

While the Ondřej Surý PPA method is common and reliable, here are two alternative approaches to Install PHP 8.2 on Ubuntu 22.04:

1. Using Docker

Docker allows you to containerize applications, including PHP. This approach isolates PHP and its dependencies from the host system, preventing conflicts and simplifying deployment.

  • Explanation: Docker provides a consistent environment for your PHP application. You can define the PHP version, extensions, and other dependencies within a Dockerfile. When the container is built, it creates a self-contained unit that can be run on any system with Docker installed. This eliminates dependency issues and ensures consistent behavior across different environments (development, testing, production).
  • Steps:

    1. Install Docker: Follow the official Docker documentation to install Docker on Ubuntu 22.04 (https://docs.docker.com/engine/install/ubuntu/).

    2. Create a Dockerfile: Create a file named Dockerfile in your project directory with the following content (adjust as needed):

    FROM php:8.2-fpm-alpine
    
    # Install any necessary extensions
    RUN docker-php-ext-install pdo pdo_mysql mysqli
    
    # Set working directory
    WORKDIR /var/www/html
    
    # Copy your application code
    COPY . .
    
    # Expose port 9000 for PHP-FPM
    EXPOSE 9000
    
    # Start PHP-FPM
    CMD ["php-fpm"]
    1. Build the Docker image: Navigate to the directory containing the Dockerfile in your terminal and run:
    docker build -t my-php-app .
    1. Run the Docker container:
    docker run -d -p 9000:9000 my-php-app

    This command runs the container in detached mode (-d) and maps port 9000 on the host to port 9000 inside the container (-p 9000:9000).

    1. Configure your web server (Nginx or Apache) to proxy requests to the Docker container:

      • Nginx example: In your Nginx server block configuration, add the following:
      location ~ .php$ {
          fastcgi_pass 127.0.0.1:9000;
          fastcgi_index index.php;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }
      • Apache example: Enable the proxy_fcgi module:
      sudo a2enmod proxy_fcgi
      sudo systemctl restart apache2

      Then, in your virtual host configuration, add:

      <FilesMatch .php$>
          SetHandler "proxy:fcgi://127.0.0.1:9000/"
      </FilesMatch>

      Remember to restart your web server after making these changes.

2. Compiling from Source

Compiling PHP from source offers the most control over the installation process. However, it’s more complex and time-consuming.

  • Explanation: This method allows you to customize every aspect of the PHP installation, including compile-time options and extensions. It’s ideal if you need very specific configurations or want to stay on the bleeding edge of PHP development. However, it also requires more manual maintenance, including handling security updates and dependencies.
  • Steps:

    1. Install Dependencies: Install the necessary build tools and libraries:
    sudo apt update
    sudo apt install build-essential autoconf libtool libxml2-dev libbz2-dev libcurl4-openssl-dev libjpeg-dev libpng-dev libxpm-dev libfreetype6-dev libgmp-dev libxslt1-dev libzip-dev bison re2c
    1. Download PHP Source Code: Download the PHP 8.2 source code from the official PHP website (https://www.php.net/downloads). Choose the source code package (e.g., php-8.2.x.tar.gz).

    2. Extract the Archive:

    tar -xvzf php-8.2.x.tar.gz
    cd php-8.2.x
    1. Configure the Build:
    ./configure --with-apxs2=/usr/bin/apxs2 --with-mysqli --with-pdo-mysql --enable-mbstring --enable-zip --with-curl --with-gd --with-jpeg --with-png --with-freetype --with-xsl

    Adjust the `–with-and–enable-options based on the extensions you need. If you are using Nginx, you can omit the–with-apxs2` option.

    1. Compile and Install:
    make
    sudo make install
    1. Configure PHP:

      • Copy the php.ini-development or php.ini-production file to /usr/local/etc/php/:
      sudo cp php.ini-production /usr/local/etc/php/php.ini
      • Edit the php.ini file to adjust settings like memory_limit, upload_max_filesize, and enabled extensions.
    2. Configure Web Server:

      • Apache: The configure script should have automatically configured Apache to load the PHP module. Restart Apache:
      sudo systemctl restart apache2
      • Nginx: You’ll need to manually configure Nginx to use PHP-FPM. Create a PHP-FPM configuration file (e.g., /etc/php/8.2/fpm/pool.d/www.conf) if it doesn’t exist, and configure your Nginx server block as shown in the main guide.
    3. Verify Installation:

    php --version

These alternative methods offer different trade-offs between ease of use, control, and isolation. Choose the method that best suits your specific needs and technical expertise. The original method described at the beginning is still a great option to Install PHP 8.2 on Ubuntu 22.04.

Leave a Reply

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