Enable PHP Integration with NginX Web Server AFTER Installing the Required Packages!!!

From TheBestLinux.com
Jump to navigationJump to search

If you've decided to use Nginx instead of the "Old Tried & True" Apache HTTPD web server for whatever reason, such as my main reason when running a web server on a low 1MB memory server, such as an Amazon AWS "Micro Instance",
getting PHP to work with Nginx can be a little tricky, especially if you've installed all the required packages, but are still having

issues with the web browser trying to download and save any .php files you try to point to, such as the one-liner

<?php phpinfo(); ?>


the issue could be you need to still "configure" Nginx to use PHP properly, as has been my case when in a hurry and not bothering to read the README file, LOL~!

There are two files which MUST be in place in order for PHP files & scripts to execute correctly!
The files both need to be copied from the "/usr/share/doc/php-fpm-X.X.XX/" directory, using the actual version number of the php-fpm package you have installed. In my case, it's "/usr/share/doc/php-fpm-7.4.23/.

Here are the actual commands I typed as the ROOT superuser in order to configure and get Nginx to support PHP.
First, I become the root user which is required. I could use sudo for each command, but to save time and avoid confusion, I will just use sudo to become the root user and then proceed from there:

[jamie@mail:~]$ sudo su -
Last login: Mon Aug 30 21:32:07 PDT 2021 on pts/0
[root@mail:~]#



Then, I change to the appropriate directory as discussed above, which is dependent on the version of php-fpm you have installed. If you don't know what that is, type this command to find the version of php-fpm you currently have installed:

[root@mail:~]# rpm -q php-fpm
php-fpm-7.4.23-1.el7.remi.x86_64
[root@mail:~]#



We can expand the above command to get the EXACT directory and file names as follows:

[root@mail:~]# rpm -ql php-fpm | grep doc
/usr/share/doc/php-fpm-7.4.23
/usr/share/doc/php-fpm-7.4.23/README
/usr/share/doc/php-fpm-7.4.23/httpd-php.conf
/usr/share/doc/php-fpm-7.4.23/nginx-fpm.conf
/usr/share/doc/php-fpm-7.4.23/nginx-php.conf
/usr/share/doc/php-fpm-7.4.23/php-fpm.conf.default
/usr/share/doc/php-fpm-7.4.23/www.conf.default
[root@mail:~]#



So now we know where to go. Type this command to change to the required directory:

[root@mail:~]# cd /usr/share/doc/php-fpm-7.4.23/



You should now be in the "/usr/share/doc/php-fpm-7.4.23/" directory, if you have the same exact version of php-fpm I have installed. Adjust your commands to suite your installed version.

From here, copy the two required files into their required destination location while renaming them to the required file names, as seen here as I do it on one of my Email servers I lease in Los Angeles:

[root@mail:/usr/share/doc/php-fpm-7.4.23]# cp nginx-fpm.conf /etc/nginx/conf.d/php-fpm.conf
[root@mail:/usr/share/doc/php-fpm-7.4.23]# 
[root@mail:/usr/share/doc/php-fpm-7.4.23]# cp nginx-php.conf /etc/nginx/default.d/php.conf
[root@mail:/usr/share/doc/php-fpm-7.4.23]#



Then, restart the php-fpm and nginx services, or just reboot the server and you should be good to go! Test your setup using the phpinfo script, which you can create in "/usr/share/nginx/html/" directory, with any name but MUST end with the ".php" filename extension, such as "phpinfo.php", which is what I always use. This file should contain these three lines(or you can do it as a one-liner, but it makes it harder to read):

<?php
phpinfo();
?>

Save the file, and then point to it using a web browser to test it out! It should give you a nice looking PHP Version 7.4.23, as in my case!

Here is a screenshot showing one of my server's PHP info page:

PHP Info Page

If you have any issues, double check your typing and look for any minor mistakes, as anything as small as a missing or misplaced comma or semi-colon can easily break things, though never anything that can't be fixed by editing nothing more than text files, as is the case most of the time when administering a Linux server! Let me know if you have any issues or would like any future clarification, and I'll do my best to help you out!