How to install PHP for nginx in Linux

Home > Search > How-to
  by

Use apt-get or yum to install PHP FPM.

[root@server1 ~]# yum install php-fpm

 

Add the following inside of the server block of the /etc/nginx/nginx.conf file.

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
}

 

Enable PHP, start PHP, and ensure PHP is active and running.

[root@server1 ~]# systemctl enable php-fpm
[root@server1 ~]# systemctl start php-fpm
[root@server1 ~]# systemctl status php-fpm

 

Create the phpinfo.php file in the document root of your web server.

[root@server1 ~]# touch /var/www/html/phpinfo.php

 

Add the following to the phpinfo.php file.

<?php
   phpinfo();
?>

 

Navigate to www.example.com/phpinfo.php, and the PHP info page should be displayed.

 

To serve index.php as one of your default documents, add index.php to the server block of your /etc/nginx/nginx.conf file. If you only want to server index.php as your default document, remove index.html.

. . .
server {
  listen       80 default_server;
  listen       [::]:80 default_server;
  server_name  www.example.com;
  root         /usr/share/nginx/html;
  index        index.html index.php;
. . .

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments