Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to consistently setup PHP-FPM 5.6 with nginx on Amazon EC2 AMI instance

I cannot find a way to setup php-fpm on nginx on Amazon AMI EC2 instance from scratch. I know this should not be that difficult, but finding different answers based on *nix versions is confusing.

Here are the condensed steps I've taken that I thought would work, but don't. Does anyone have a set of steps to reliably setup php-fpm with nginx in Amazon AMI EC2 instance?

I've intentionally left out nginx.conf, etc from this post since they are the "stock" installations from the default yum repositories.

nginx version: 1.6.2

Does anyone have reliable steps to setup php-fpm in nginx for Amazon AMI EC2 instances? I would prefer to setup myself instead of using the AMI in the Amazon marketplace that charges for this setup.

Thanks

# install packages
yum install -y nginx
yum install -y php56-fpm.x86_64

# enable php in nginx.conf
vi /etc/nginx/nginx.conf
# add index.php at the beginning of index
  index   index.php index.html index.htm;

# uncomment the php block in nginx.conf
    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include        fastcgi_params;
    }

# tell php-fpm to run as same account as nginx
vi /etc/php-fpm-5.6.d/www.conf
- change user and group apache to nginx

# allow nginx user to read website files since they are typically owned by root
cd /usr/share/nginx
chown -R nginx:nginx html

# check to see if php works - doesn't with these steps
echo "<?php phpinfo(); ?>" > /usr/share/nginx/info.php

# restart services since we changed things
service nginx restart
service php-fpm-5.6 restart

# verify root path exists and is owned by nginx as we said above
# ls -l /usr/share/nginx/html
-rw-r--r-- 1 nginx nginx 3696 Mar  6 03:53 404.html
-rw-r--r-- 1 nginx nginx 3738 Mar  6 03:53 50x.html
-rw-r--r-- 1 nginx nginx 3770 Mar  6 03:53 index.html
-rw-r--r-- 1 nginx nginx   20 Apr 14 14:01 index.php

# I also verified php-fpm is listening on port 9000 and nginx is setup that way in the nginx.conf
# port 9000 usage is the default and I left it as-is for this question, but I would prefer to use sock once I get this working.

Edit

This is what I see in the nginx error log

2015/04/14 17:08:25 [error] 916#0: *9 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, 
client: 12.34.56.78, server: localhost, request: "GET /index.php HTTP/1.1", 
upstream: "fastcgi://127.0.0.1:9000", host: "12.34.56.90" 
like image 443
Jose Leon Avatar asked Apr 14 '15 15:04

Jose Leon


1 Answers

What do you see in nginx error log (/var/log/nginx/errors.log)?

Added after additional info (logs) provided:

To me it looks root should be server section not location.

server {
 ...
    root   /usr/share/nginx/html;
    ...
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include        fastcgi_params;
    }
}
like image 70
Greg Szymanski Avatar answered Sep 19 '22 00:09

Greg Szymanski