$ sudo apt install nginx php8.2-fpm<\/code><\/pre>\n\n\n\nAs you can see above PHP-FPM comes as a PHP extension.<\/p>\n\n\n\n
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:<\/p>\n\n\n\n
$ sudo nano \/etc\/nginx\/nginx.conf\n\nFile:\n\nserver {\n listen 80;\n server_name mysite.com;\n root \/var\/www\/mysite.com;\n index index.php index.html;\n\n location ~ \\.php$ {\n include snippets\/fastcgi-php.conf;\n fastcgi_pass unix:\/var\/run\/php\/php8.2-fpm.sock;\n }\n}<\/code><\/pre>\n\n\n\nSafe and exit the file then verify Nginx configuration using this command:<\/p>\n\n\n\n
$ sudo nginx -t\n\nOutput\nnginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok\nnginx: configuration file \/etc\/nginx\/nginx.conf test is successful<\/code><\/pre>\n\n\n\nUsing PHP 8.2 with Apache<\/h2>\n\n\n\n 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.<\/p>\n\n\n\n
Option 1: Using mod_php<\/h2>\n\n\n\n Install both Apache and PHP, and any other necessary PHP extensions including the Apache PHP module like so:<\/p>\n\n\n\n
$ sudo apt install apache2 libapache2-mod-php8.2<\/code><\/pre>\n\n\n\nNext enable mod_php module:<\/p>\n\n\n\n
$ sudo a2enmod php8.2<\/code><\/pre>\n\n\n\nRestart Apache web server.<\/p>\n\n\n\n
$ sudo systemctl restart apache2<\/code><\/pre>\n\n\n\nOption 2: Using PHP 8.2 – FPM<\/h2>\n\n\n\n Install Apache and PHP-FPM.<\/p>\n\n\n\n
$ sudo apt install apache2 php8.2-fpm<\/code><\/pre>\n\n\n\nOnce the installation is complete, you can start the PHP-FPM service by running the following command:<\/p>\n\n\n\n
$ sudo systemctl start php8.2-fpm<\/code><\/pre>\n\n\n\nYou can also enable PHP-FPM to start at boot time by running the following command:<\/p>\n\n\n\n
$ sudo systemctl enable php8.2-fpm<\/code><\/pre>\n\n\n\nConfigure Apache to use PHP8.2-FPM<\/h2>\n\n\n\n To configure Apache to use PHP-FPM, you need to enable the \u201cproxy_fcgi\u201d and \u201cproxy\u201d modules. You can do this by running the following command:<\/p>\n\n\n\n
$ sudo a2enmod proxy_fcgi proxy <\/code><\/pre>\n\n\n\nCreate a new configuration file for your virtual host using the following command:<\/p>\n\n\n\n
$ sudo nano \/etc\/apache2\/sites-available\/mysite.com.conf<\/code><\/pre>\n\n\n\nInside the new configuration file, add the following configuration with PHP-FPM:<\/p>\n\n\n\n
<VirtualHost *:80>\n ServerName mysite.com\n DocumentRoot \/var\/www\/mysite.com\n \n <Directory \/var\/www\/mysite.com>\n Options -Indexes +FollowSymLinks +MultiViews\n AllowOverride All\n Require all granted\n <\/Directory>\n \n <FilesMatch \\.php$>\n SetHandler "proxy:unix:\/run\/php\/php8.2-fpm.sock|fcgi:\/\/localhost\/"\n <\/FilesMatch>\n \n ErrorLog ${APACHE_LOG_DIR}\/error.log\n CustomLog ${APACHE_LOG_DIR}\/access.log combined\n<\/VirtualHost><\/code><\/pre>\n\n\n\nEnable the new virtual host by running the following command:<\/p>\n\n\n\n
$ sudo a2ensite example.com.conf<\/code><\/pre>\n\n\n\nReload the Apache web server to apply the changes:<\/p>\n\n\n\n
$ sudo systemctl reload apache2 <\/code><\/pre>\n\n\n\nYou can omit the FilesMatch block in the configuration block if your main PHP version is PHP 8.2<\/p>\n\n\n\n
Bonus: Removing Old PHP and extensions<\/h2>\n\n\n\n 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.<\/p>\n\n\n\n
Enable PHP 8.2 as the main PHP version:<\/p>\n\n\n\n
$ sudo a2enconf php8.2-fpm<\/code><\/pre>\n\n\n\nDisable PHP 8.1 FPM extension completely.<\/p>\n\n\n\n
$ sudo a2disconf php8.1-fpm<\/code><\/pre>\n\n\n\nTo remove all PHP 8.1 packages after upgrading to PHP 8.2 run the commands below.<\/p>\n\n\n\n
$ sudo apt purge php8.1*<\/code><\/pre>\n\n\n\nConclusion<\/h2>\n\n\n\n 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.<\/p>\n","protected":false},"excerpt":{"rendered":"
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…<\/p>\n","protected":false},"author":1,"featured_media":13102,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,9,25,16,18],"tags":[58,121,350,400,424,433,449,452,471,531,544,591,638],"yoast_head":"\n
How to Use PHP 8.2 with Nginx | Apache web Servers<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n\t \n\t \n\t \n