Setup Nginx PHP FPM Percona Mysql

From Brian Nelson Ramblings
Jump to: navigation, search
Install Nginx - PHP+FPM - Percona MySQL

Setup Nginx + php-fpm + Percona Mysql

LEMP stack is a group of open source software to get web servers up and running. The acronym stands for Linux, nginx (pronounced Engine x), MySQL, and PHP. Since the server is already running Ubuntu, the linux part is taken care of. Here is how to install the rest.

First we will want to enable the epel repo and percona repos

rpm -Uhv http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x86_64.rpm
rpm -Uhv http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

1) Install Percona Mysql

Lets install Percona56 Server

yum install Percona-Server-server-56 Percona-Server-client-56

To increase performance of INNODB tables lets create separate namespaces

vim /etc/my.cnf

Now add the following line under [mysqld]

[mysqld]
innodb_file_per_table

Lets make sure Percona starts on boot

chkconfig mysql on

Lets Start Percona Server

/etc/init.d/mysql restart

2) Install Nginx

yum install nginx -y

Let nginx to start on boot

chkconfig nginx on

Now lets start nginx

/etc/init.d/nginx start

Lets open up port 80 in iptables

/sbin/iptables -I INPUT -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT

Save and close the file. Please note that under Red Hat enterprise Linux (RHEL) / CentOS / Fedora Linux you can use following commands to save and restore firewall rules.

/etc/init.d/iptables save

3) Install PHP-FPM/PHP-CLI

Lets install php-fpm with Yum

yum install php-fpm php-mysql php-mssql

Start php-fpm at boot up

chkconfig php-fpm on
/etc/init.d/php-fpm start

Lets check and make sure there is no issues with php-fpm

php-fpm -v
PHP 5.3.3 (fpm-fcgi) (built: Dec 11 2013 03:17:57)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

If you want the default php -v via the command line we can create a symlink

ln -s /usr/sbin/php-fpm /usr/sbin/php

4) Configure Nginx to use PHP+FPM

First thing we need to do is change the cgi.fix_pathinfo

sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php.ini

If this number is kept as 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path—a much safer alternative.

Now restart php-fpm so it takes

/etc/init.d/php-fpm restart

PHP+FPM TCP

By default php-fpm configuration is setup to tcp port 9000, you can verify this by going to /etc/php-fpm.d/www.conf and looking at the line that starts with listen

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

listen = 127.0.0.1:9000

Now you will need to edit the conf.d file that you wish to setup php-fpm on

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

Locate where it says location ~ \.php$ and add the following

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

Exit out and restart nginx

/etc/init.d/nginx restart

Now you can test to see if php is working, create a phpinfo file.

vim /usr/share/nginx/html/myphpinfo.php

Add the following line

<?php phpinfo(); ?>

Load it from your browser, you should see the standard phpinfo page.

PHP+FPM Socket

By default php-fpm configuration is setup to tcp port 9000, we are going to setup php-fpm to use a socket (/dev/shm/php-fpm.sock)

vim /etc/php-fpm/www.conf

listen = /dev/shm/php-fpm.sock

Now you will need to edit the conf.d file that you wish to setup php-fpm on

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

Locate where it says location ~ \.php$ and add the following

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

Exit out and restart nginx

/etc/init.d/nginx restart

Now you can test to see if php is working, create a phpinfo file.

vim /usr/share/nginx/html/myphpinfo.php

Add the following line

<?php phpinfo(); ?>

Load it from your browser, you should see the standard phpinfo page.