Home  »  ArticlesGuidesHow ToLibrariesTechnologyTools   »   How to Use PHP 8.2 with Nginx | Apache web Servers

How to Use PHP 8.2 with Nginx | Apache web Servers

The most likely use case for PHP is in conjuction with a web server. In this guide we will be showing you how to use PHP 8.2 with Nginx and Apache web Servers.

If you do not have PHP 8.2 on your Linux systems, we have already published a series of guides showing how you can install it on difference platforms as follows:

You can use PHP with either Nginx or Apache web server to create your dynamic and interactive web pages using the following instructions depending on your choice of web server. In the examples below will assume you are using Debian based distros

Using PHP 8.2 with Nginx

With Nginx, PHP code is typically executed by a separate process called FastCGI Process Manager (PHP-FPM). PHP-FPM is a daemon that listens for incoming PHP requests and runs them in a separate process. Nginx acts as a reverse proxy, forwarding incoming requests to PHP-FPM to be executed.

If you haven’t installed Nginx you will need to install it together PHP-FPM by running the following command:

$ sudo apt install nginx php8.2-fpm

As you can see above PHP-FPM comes as a PHP extension.

Once Nginx and FPM extension are installed, you will need to configure Nginx to forward incoming requests to PHP-FPM. This can be done by editing the Nginx configuration and add the following block inside the http block to configure Nginx to forward PHP requests to PHP-FPM:

$ sudo nano /etc/nginx/nginx.conf


server {
    listen 80;
    server_name mysite.com;
    root /var/www/mysite.com;
    index index.php index.html;

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

Safe and exit the file then verify Nginx configuration using this command:

$ sudo nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Using PHP 8.2 with Apache

Apache web server can use PHP as an Apache PHP module or as a FastCGI Process Manager (PHP-FPM). The Apache mod_php module, which is built into Apache. It is responsible for processing PHP code directly within the Apache process.

Option 1: Using mod_php

Install both Apache and PHP, and any other necessary PHP extensions including the Apache PHP module like so:

$ sudo apt install apache2 libapache2-mod-php8.2

Next enable mod_php module:

$ sudo a2enmod php8.2

Restart Apache web server.

$ sudo systemctl restart apache2

Option 2: Using PHP 8.2 – FPM

Install Apache and PHP-FPM.

$ sudo apt install apache2  php8.2-fpm

Once the installation is complete, you can start the PHP-FPM service by running the following command:

$ sudo systemctl start php8.2-fpm

You can also enable PHP-FPM to start at boot time by running the following command:

$ sudo systemctl enable php8.2-fpm

Configure Apache to use PHP8.2-FPM

To configure Apache to use PHP-FPM, you need to enable the “proxy_fcgi” and “proxy” modules. You can do this by running the following command:

$ sudo a2enmod proxy_fcgi proxy 

Create a new configuration file for your virtual host using the following command:

$ sudo nano /etc/apache2/sites-available/mysite.com.conf

Inside the new configuration file, add the following configuration with PHP-FPM:

<VirtualHost *:80>
    ServerName mysite.com
    DocumentRoot /var/www/mysite.com
    <Directory /var/www/mysite.com>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    <FilesMatch \.php$>
        SetHandler "proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost/"
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Enable the new virtual host by running the following command:

$ sudo a2ensite example.com.conf

Reload the Apache web server to apply the changes:

$ sudo systemctl reload apache2 

You can omit the FilesMatch block in the configuration block if your main PHP version is PHP 8.2

Bonus: Removing Old PHP and extensions

If you have more than one or older PHP versions running you can disable the old module and enable the new one using the following.

Enable PHP 8.2 as the main PHP version:

$ sudo a2enconf php8.2-fpm

Disable PHP 8.1 FPM extension completely.

$ sudo a2disconf php8.1-fpm

To remove all PHP 8.1 packages after upgrading to PHP 8.2 run the commands below.

$ sudo apt purge php8.1*


The methods above can be applied to any supported PHP versions. If running on CentOS or RHEL based distros you can replace instances of sudo apt with sudo yum. Also replace apache2 with httpd. Happy PHP 8.2 coding.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.