-1

I installed Nginx & PHP using this guide here:

Nginx install guide

yum install php php-mysql php-fpm

edited /etc/php-fpm.d/www.conf

listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nobody
listen.group = nobody
listen.owner = nobody
listen.group = nobody

ran:

systemctl start php-fpm

/etc/nginx/conf.d/default.conf

server {
listen       80;
server_name  <my servers IP here - removed>;

# note that these lines are originally from the "location /" block
root   /usr/share/nginx/html;
index index.php index.html index.htm;

location / {
    try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
    root /usr/share/nginx/html;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
}

I then tested

http://<my server ip>/phpinfo.php

and it worked!

I then installed pear

yum install php-pear

but now php files don't work, the browser tries to download them instead of running them.

I've tried:

  1. rebooting the server
  2. restarting nginx
  3. restarting php-fpm
  4. checking all the config files to make sure they as the same as above.

I'm completely stuck. I don't know what to check to get php work again. This is the first time I've installed Nginx. I've looked around on the net for answers and on here.

I'm running Centos 7

help :)

UPDATE:

I've tried a much more shortened config file:

server {
listen       80;
server_name  <my servers IP here - removed>;

# note that these lines are originally from the "location /" block
root   /usr/share/nginx/html;
index index.php index.html index.htm;

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}
}

but that didn't work either.

I also tried:

fastcgi_pass 127.0.0.1:9000;

instead of:

fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

but that didn't work too.

UPDATE 2

i found out that if I go to:

http://my ip/phpinfo.php - it works!

but if I go to:

http://domainname/phpinfo.php - it tries to download the PHP file instead of running it.

How do I make php files run while using the domain name instead of the IP?

user1398287
  • 5,245
  • 5
  • 21
  • 25
  • I tried to uninstall pear - yum uninstall php-pear but it didn't fix the issue – user1398287 May 02 '17 at 21:09
  • I tried uninstalling a re-installing nginx & php but it still doesn't process php files – user1398287 May 04 '17 at 10:54
  • What does the log say? – darckcrystale May 04 '17 at 20:54
  • If I remember well, I faced this issue some times ago. I don't remember how I resolved it but I think logs helped me. Try to do a `tail -f` on your log(s) file(s) (nginx + php) to see exactly what is happening when you call your page: it may be a problem coming from php-fpm. – darckcrystale May 04 '17 at 21:26
  • I found out that php files run when browsing to the phpinfo file using the server's IP but not the domain name. Any ideas on how I can get this working using the domain name instead of the IP? – user1398287 May 08 '17 at 10:14
  • Are you trying to do this on your computer or on a remote server? – darckcrystale May 09 '17 at 09:45
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Jun 30 '17 at 21:57

4 Answers4

3

According your second update, you have problem with Listen directive. nginx listen only IP-address or Domain name that mentioned in Listen directive. Maximillian gives you right answer. It you put listen 80 in your configuration file, you'll solve your problem, but there would be wotk only one site, with this listen. If you would like to configure PHP on your domain you could configure listen yourdomain.com:80.

2

Have you tried to edit

/etc/nginx/sites-enabled/default ?

Then you need to edit it as follows.

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

afterward, you have to restart php-fpm and nginx

service nginx restart && service php-fpm restart
Max
  • 1,203
  • 1
  • 14
  • 28
1

Try with below config for location,

location ~ .php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/$fastcgi_script_name;
    include        fastcgi_params;
}

And do clear cache beforehand.

For your 2nd update try below in server block,

listen 127.0.0.1:8000; #your ip with port
server_name  example.com; #domain name
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
0

Recently I also need to setup linux and php environment, so I strictly follow your step to setup it.

I encounter this two issue when I setup the LEMP environment,

a. nginx issue, post and answer

b. php-fpm issue, post and answer

I don't encounter the issue you face, so I show the environment for you, you may compare with yours.

Centos Version

vi /etc/centos-release

CentOS Linux release 7.3.1611 (Core)

Mysql version

mysql -u root -p
//enter password

Server version: 5.5.52-MariaDB MariaDB Server

Php version

php -v

PHP 5.4.16 (cli) (built: Nov 6 2016 00:29:02)

Pear info

pear list // refer to this post

Installed packages, channel pear.php.net:
========================================= 
Package          Version State
Archive_Tar      1.3.11  stable
Console_Getopt   1.3.1   stable
PEAR             1.9.4   stable
Structures_Graph 1.0.4   stable
XML_Util         1.2.1   stable

Nginx config file vi /etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  192.168.236.129;

    # note that these lines are originally from the "location /" block
    root   /usr/share/nginx/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

info.php vi /usr/share/nginx/html/info.php

<?php phpinfo(); ?>
Community
  • 1
  • 1
LF00
  • 27,015
  • 29
  • 156
  • 295
  • thanks for this, everything was the same apart from I didn't have mysql installed. and my php version was slightly older. I copied over my nginx config file with yours and restarted everything. I found out that php files run when browsing to the phpinfo file using the server's IP but not the domain name. – user1398287 May 08 '17 at 10:11
  • check your hosts file to see you domain name bind to which ip, here may be your DNS issue. – LF00 May 08 '17 at 10:16