Wednesday 23 May 2012

nginx php5-fpm on debian 6





First add the dotdeb repo to your sources.list file:
Code:
nano /etc/apt/sources.list
add this to the bottom of the file:
Code:
deb http://packages.dotdeb.org squeeze all
deb-src http://packages.dotdeb.org squeeze all
Next, add the GnuPG key to your distribution:
Code:
wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | apt-key add -
rm dotdeb.gpg
Update APT:
Code:
apt-get update
Install php and php-fpm plus some common addons:
Code:
apt-get install php5 php5-fpm php-pear php5-common php5-mcrypt php5-mysql php5-cli php5-gd
Install nginx:
Code:
apt-get install nginx
Tweak the php-fpm configuration:
Code:
nano /etc/php5/fpm/pool.d/www.conf 
The following tweaks have been customized for a 512MB-1GB VPS. You can use the same numbers here or come up with your own. This is just what I have found to be the most resource friendly for a lightly used VPS:
Code:
pm.max_children = 25
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 10
pm.max_requests = 500
This line is optional but I highly suggest you use it. Basically it's saying that if a php-fpm process hangs it will terminate it if it continues to hang for 30 seconds. This will add stability and reliability to your php application in the event there is a problem:
Code:
request_terminate_timeout = 30s
restart php-fpm:
Code:
/etc/init.d/php5-fpm restart
Tweak your nginx configuration:
Code:
nano /etc/nginx/nginx.conf
The client_max_body_size option changes the max from the 1MB default (which is a must for most php apps):
Code:
client_max_body_size 20M;
client_body_buffer_size 128k;
Remove the default vhost symlink:
Code:
cd /etc/nginx/sites-enabled
rm default
Create a fresh and clean vhost file:
Code:
nano /etc/nginx/sites-available/www.website.com
Code:
server {
                listen 80;
                server_name website.com www.website.com;

                access_log /var/log/nginx/website.access_log;
                error_log /var/log/nginx/website.error_log;

                root /var/www/www.website.com;
                index index.php index.htm index.html;

                location ~ .php$ {
                  fastcgi_pass   127.0.0.1:9000;
                  fastcgi_index  index.php;
                  fastcgi_param  SCRIPT_FILENAME /var/www/www.website.com$fastcgi_script_name;
                  include fastcgi_params;
                }
       }
Create a new symlink for the new vhost under sites-enabled:
Code:
ln -s /etc/nginx/sites-available/www.website.com /etc/nginx/sites-enabled/www.website.com
Restart nginx:
Code:
/etc/init.d/nginx restart
That's it. You should now have a clean installation of nginx and php-fpm with a single vhost running on port 80. If you want to have the same vhost running with ssl on port 443, copy and paste the entire vhost code into the bottom of the vhost file, change 'listen' to 443:
Code:
                listen 443;
Then add these lines that will point to your ssl certs:
Code:
                ssl on;
                ssl_certificate /path/to/certificate/www.website.com.crt;
                ssl_certificate_key /path/to/certificate_key/www.website.com.key;
Please let me know if there is anything I missed or anything that might need to be changed.

Reply With Quote

No comments:

Post a Comment