Wednesday, October 22, 2014

Installing Nginx With PHP5 (And PHP-FPM) And MySQL Support (LEMP) On Debian Wheezy

Installing MySQL
apt-get install mysql-server mysql-client
Installing Nginx
apt-get install nginx
/etc/init.d/nginx start
Installing PHP5 - PHP-FPM (FastCGI Process Manager)
apt-get install php5-fpm
apt-cache search php5
apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
apt-get install php-apc
/etc/init.d/php5-fpm restart
Configuring Nginx
vi /etc/nginx/nginx.conf
  • First (this is optional) adjust the number of worker processes and set the keepalive_timeout to a reasonable value:
  • keepalive_timeout 65; => keepalive_timeout 2;
[...]
worker_processes  4;
[...]
keepalive_timeout   2;
[...]
Configuring Virtual Hosts Nginx (default)
vi /etc/nginx/sites-available/default
  • uncomment at line abou:
listen   80; ## listen for ipv4; this line is default and implied
  • add default php file by add index.php to this line:
index index.php index.html index.htm;
  • uncomment and add "try_files $uri =404;" at box "location ~ \.php$":
  • line "try_files $uri =404;" to prevent zero-day exploits (see http://wiki.nginx.org/Pitfalls#Passing_Uncontrolled_Requests_to_PHP and http://forum.nginx.org/read.php?2,88845,page=3).
location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

                # With php5-cgi alone:
                #fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
  • Now save the file and restart nginx:
/etc/init.d/nginx restart
  • test nginx http://localhost

Configuring PHP5
vi /etc/php5/fpm/php.ini
  • and set ";cgi.fix_pathinfo=1" => "cgi.fix_pathinfo=0"
[...]
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]
  • and restart PHP-FPM
/etc/init.d/php5-fpm restart
Test Nginx + PHP5 + MySQL
vi /usr/share/nginx/www/info.php
  • and add this content
<?php phpinfo(); ?>
  • test open browser and type http://localhost/info.php


Making PHP-FPM Use A TCP Connection 
Note: ผู้เขียนไม่แนะนำให้คอนฟิก TCP ใช้ Socket แบบเดิมดีกว่า เพราะผู้เขียนลองใช้แบบ TCP ไปสักพักแล้วพบว่ามัน 502 Bad Gateway บ่อยมากเลย
อางอิง : https://rtcamp.com/tutorials/php/fpm-sysctl-tweaking/
  • By default PHP-FPM is listening on the socket /var/run/php5-fpm.sock. It is also possible to make PHP-FPM use a TCP connection. To do this, open /etc/php5/fpm/pool.d/www.conf...
vi /etc/php5/fpm/pool.d/www.conf
  • ... and make the listen line look as follows:
[...]
;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000
[...]
  • and config vhost nginx
vi /etc/nginx/sites-available/default
  • comment and uncomment follow this:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
  • Finally restart php5 and nginx:
/etc/init.d/php5-fpm restart 
/etc/init.d/nginx reload
Configuring CGI/Perl Scripts
  • If you want to serve CGI/Perl scripts with nginx, please read this tutorial: Serving CGI Scripts With Nginx On Debian Squeeze/Ubuntu 11.04
  • http://www.howtoforge.com/serving-cgi-scripts-with-nginx-on-debian-squeeze-ubuntu-11.04
ที่มา : 
  • http://www.howtoforge.com/installing-nginx-with-php5-and-php-fpm-and-mysql-support-lemp-on-debian-wheezy
    https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-12-04
    http://www.sitepoint.com/setting-up-php-behind-nginx-with-fastcgi/

No comments:

Post a Comment