知乎专栏 |
config php fastcgi
sudo vim /etc/nginx/sites-available/default location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; }
Spawn-fcgi
We still need a script to start our fast cgi processes. We will extract one from Lighttpd. and then disable start script of lighttpd
$ sudo apt-get install lighttpd $ sudo chmod -x /etc/init.d/lighttpd
$ sudo touch /usr/bin/php-fastcgi $ sudo vim /usr/bin/php-fastcgi #!/bin/sh /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -f /usr/bin/php5-cgi
fastcgi daemon
$ sudo touch /etc/init.d/nginx-fastcgi $ sudo chmod +x /usr/bin/php-fastcgi $ sudo vim /etc/init.d/nginx-fastcgi This is also a new empty file, add the following and save: #!/bin/bash PHP_SCRIPT=/usr/bin/php-fastcgi RETVAL=0 case "$1" in start) $PHP_SCRIPT RETVAL=$? ;; stop) killall -9 php RETVAL=$? ;; restart) killall -9 php $PHP_SCRIPT RETVAL=$? ;; *) echo "Usage: nginx-fastcgi {start|stop|restart}" exit 1 ;; esac exit $RETVAL We need to change some permissions to make this all work. $ sudo chmod +x /etc/init.d/nginx-fastcgi
create a test file
sudo vim /var/www/nginx-default/index.php <?php echo phpinfo(); ?>
./configure --prefix=/srv/php-5.3.8 \ --with-config-file-path=/srv/php-5.3.8/etc \ --with-config-file-scan-dir=/srv/php-5.3.8/etc/conf.d \ --enable-fpm \ --with-fpm-user=www \ --with-fpm-group=www \ --with-pear \ --with-curl \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --with-xpm-dir \ --with-iconv \ --with-mcrypt \ --with-mhash \ --with-zlib \ --with-xmlrpc \ --with-xsl \ --with-openssl \ --with-mysql=/srv/mysql-5.5.16-linux2.6-i686 \ --with-mysqli=/srv/mysql-5.5.16-linux2.6-i686/bin/mysql_config \ --with-pdo-mysql=/srv/mysql-5.5.16-linux2.6-i686 \ --with-sqlite=shared \ --with-pdo-sqlite=shared \ --disable-debug \ --enable-zip \ --enable-sockets \ --enable-soap \ --enable-mbstring \ --enable-magic-quotes \ --enable-inline-optimization \ --enable-gd-native-ttf \ --enable-xml \ --enable-ftp \ --enable-exif \ --enable-wddx \ --enable-bcmath \ --enable-calendar \ --enable-sqlite-utf8 \ --enable-shmop \ --enable-dba \ --enable-sysvsem \ --enable-sysvshm \ --enable-sysvmsg make && make install
如果出现 fpm 编译错误,取消--with-mcrypt 可以编译成功。
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm # chmod 755 /etc/init.d/php-fpm # ln -s /srv/php-5.3.5 /srv/php # cp /srv/php/etc/php-fpm.conf.default /srv/php/etc/php-fpm.conf # cp php.ini-production /srv/php/etc/php.ini
groupadd -g 80 www adduser -o --home /www --uid 80 --gid 80 -c "Web User" www
php-fpm.conf
# grep -v ';' /srv/php-5.3.5/etc/php-fpm.conf | grep -v "^$" [global] pid = run/php-fpm.pid error_log = log/php-fpm.log [www] listen = 127.0.0.1:9000 user = www group = www pm = dynamic pm.max_children = 2048 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500
chkconfig --add php-fpm
location ~ ^(.+\.php)(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; }
Unix Socket
location ~ .*\.(php|php5)?$ { #fastcgi_pass 127.0.0.1:9000; fastcgi_pass unix:/dev/shm/php-fpm.sock; fastcgi_index index.php; include fastcgi.conf; }
server { listen 80; listen 443 ssl http2; server_name cms.netkiller.cn; ssl_certificate ssl/netkiller.cn.crt; ssl_certificate_key ssl/netkiller.cn.key; ssl_session_cache shared:SSL:20m; ssl_session_timeout 60m; charset utf-8; access_log /var/log/nginx/cms.netkiller.cn.access.log main; error_page 497 https://$host$uri?$args; if ($scheme = http) { return 301 https://$server_name$request_uri; } location ~ ^/wp-content/uploads/.*\.php$ { deny all; } location / { root /opt/netkiller.cn/cms.netkiller.cn; index index.html index.php; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /opt/netkiller.cn/cms.netkiller.cn; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /opt/netkiller.cn/cms.netkiller.cn$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }