Setup EC2 Instance with NGINX and PHP-FPM

Setup EC2 Instance with NGINX and PHP-FPM

Login into AWS Console and launch default Amazon Instance.

Security Group:

Create your security group and download your key.
Restrict the permissions on your key.

SSH into your Machine:

Update your box with following command:

Install nginx:

Install php-fpm:

Add it to the startup:

Install php and dependencies:

Start php-fpm and nginx service:

Do sanity check to make sure the static content renders:

You will see the default page

PHP-FPM Configuration:
Config files location:

Change user and group from apache to nginx.

NGINX Configuration:
sudo vi /etc/nginx/nginx.conf
Include index.php in this block so .php file gets executed as startup page:

Change fastcgi_param location from default /scripts location.
Change the root folder too.

Change fastcgi_param location from default /scripts location.
Change the root folder too.

Restart the service:
[ec2-user@ip-10-250-69-249 nginx]$ sudo service php-fpm restart
Stopping php-fpm: [ OK ]
Starting php-fpm: [ OK ]
[ec2-user@ip-10-250-69-249 nginx]$ sudo service nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]

Create phpinfo file:
[ec2-user@ip-10-250-69-249 html]$ sudo vi test.php
Put these contents into the test.php file.

[ec2-user@ip-10-250-69-249 nginx]$ curl localhost/test.php
You will get all the php info details

WordPress setup:
[ec2-user@ip-xx-xxx-x-xx nginx]$ cd /usr/share/nginx/
[ec2-user@ip-xx-xxx-x-xx nginx]$ sudo mkdir www
[ec2-user@ip-xx-xxx-x-xx nginx]$ cd www/
[ec2-user@ip-xx-xxx-x-xx www]$ ls
[ec2-user@ip-xx-xxx-x-xx www]$ sudo mkdir wordpress
[ec2-user@ip-xx-xxx-x-xx www]$ cd wordpress/
[ec2-user@ip-xx-xxx-x-xx wordpress]$ ls
[ec2-user@ip-xx-xxx-x-xx wordpress]$ pwd
/usr/share/nginx/www/wordpress

Troubleshooting:

BELC02J30BHDKQ4:~ gvenkatar$ scp -r -i Kaizen.pem /Users/gvenkatar/devlopment/wordpress/* xx-xxx-x-xx.us-west-2.compute.amazonaws.com:/usr/share/nginx/www/wordpres/
Permission denied (publickey).
lost connection
BELC02J30BHDKQ4:~ gvenkatar$ scp -r -i Kaizen.pem /Users/gvenkatar/devlopment/wordpress/* ec2-user@xx-xxx-x-xx.us-west-2.compute.amazonaws.com:/usr/share/nginx/www/wordpress/

BELC02J30BHDKQ4:~ gvenkatar$ scp -r -i Kaizen.pem /Users/gvenkatar/devlopment/wordpress/* ec2-user@xx-xxx-x-xx.us-west-2.compute.amazonaws.com:/usr/share/nginx/www/wordpress/
scp: /usr/share/nginx/www/wordpress//index.php: Permission denied
scp: /usr/share/nginx/www/wordpress//wp-signup.php: Permission denied
scp: /usr/share/nginx/www/wordpress//wp-trackback.php: Permission denied
scp: /usr/share/nginx/www/wordpress//xmlrpc.php: Permission denied

[ec2-user@ip-10-250-69-249 wordpress]$ sudo chmod -R 777 ../wordpress/
Error:
PHP message: PHP Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php/session) in Unknown on line 0″ while reading upstream
Fix:
sudo chmod 777 session  // This opens up permission for read/write/execute for everyone. Not secure .

you can provide the access the nginx or apache user.
$ sudo chown nginx /var/lib/php/session

If you see this error , that indicates your php scripts are not properly processed by php-fpm.

2013/09/04 20:12:28 [crit] 21253#0: *1016 open() ?/usr/share/nginx/html/50x.html? failed (24: Too many open files), client: 127.0.0.1, server: localhost, request: ?GET /index.php HTTP/1.0″, upstream: ?http://127.0.0.1:80/index.php?, host: ?127.0.0.1″

Make sure your nginx.conf config file is correct.

Contents of my working nginx.conf config file:

Leave a Reply

Your email address will not be published. Required fields are marked *

*