Nginx是一个高性能、轻量级的HTTP服务器,占用内存少,稳定性高,也可做为反向代理服务器。Nginx采用事件驱动,在Linux操作系统下,Nginx使用epoll事件模型,充分使用异步逻辑,削减了上下文调度开销,并发服务能力更强。
PHP-FPM是PHP的FastCGI 进程管理器,具有一些高级特性,将PHP进程管理与Web服务器分开,可以分开部署在不同机器上,监听不同的端口和使用不同的 php.ini 配置文件。
PHP-FPM特性之一:fastcgi_finish_request能够在请求完成和刷新数据后,继续在后台执行耗时的工作(录入视频转换、统计处理等),达到分离执行后台任务而又不阻塞PHP快速响应,可作为PHP性能优化点。
PHP-FPM另一特性:可以记录请求当中慢响应的日志,类似Mysql满日志,包括文件名,函数名,行号等,可用于分析PHP的性能问题。
在Apache + mod_php下,每一个请求,Apache便会启动一个进程并加载php解释器来处理。在Nginx + PHP-FPM下,Nginx将PHP请求和环境变量通过socket传递给FastCGI进程;将静态资源请求则缓存到本地并返回。FastCGI 进程管理器采用固定静态(或动态)的子进程来处理请求,占用内存少,具有更好的扩展能力。
在CentOs 6.4上安装Nginx和PHP-FPM:
yum search nginx sudo yum install nginx sudo chkconfig --levels 235 nginx on sudo service nginx start yum search fpm sudo yum install php54-fpm
编辑php.ini
sudo vim /etc/php.ini
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's ; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok ; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting ; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting ; of zero causes PHP to behave as before. Default is 1. You should fix your scripts ; to use SCRIPT_FILENAME rather than PATH_TRANSLATED. ; http://www.php.net/manual/en/ini.core.php#ini.cgi.fix-pathinfo cgi.fix_pathinfo=0
启动PHP-FPM
chkconfig --levels 235 php-fpm on service php-fpm start
更改Nginx worker_processes配置
sudo vim /etc/nginx/nginx.conf
worker_processes 4;
更改Nginx 配置以便处理php请求
sudo vim /etc/nginx/conf.d/default.conf
server { listen 80 default_server; server_name _; #charset koi8-r; #access_log logs/host.access.log main; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/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 /usr/share/nginx/html; #fastcgi_pass 127.0.0.1:9000; fastcgi_pass unix:/tmp/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi.conf; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
编辑PHP-FPM
sudo vim /etc/php-fpm.d/www.conf
; List of ipv4 addresses of FastCGI clients which are allowed to connect. ; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original ; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address ; must be separated by a comma. If this value is left blank, connections will be ; accepted from any ip address. ; Default Value: any ;listen.allowed_clients = 127.0.0.1 listen = /tmp/php5-fpm.sock ; Set permissions for unix socket, if one is used. In Linux, read/write ; permissions must be set in order to allow connections from a web server. Many ; BSD-derived systems allow connections regardless of permissions. ; Default Values: user and group are set as the running user ; mode is set to 0660 listen.owner = nginx listen.group = nginx listen.mode = 0660 ; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. ; RPM: apache Choosed to be able to access some dir as httpd user = nginx ; RPM: Keep a group allowed to write in log dir. group = nginx
重启PHP-FPM
sudo service php-fpm restart
创建PHP页面
sudo vim /usr/share/nginx/html/info.php
<?php phpinfo();
重启Nginx
sudo service nginx restart
参考链接:
Nginx and PHP-FPM Configuration and Optimizing Tips and Tricks
nginx
FastCGI 进程管理器(FPM)
Apache + mod_php compared to Nginx + php-fpm
Which PHP mode? Apache vs CGI vs FastCGI
PHP: What are the advantages of FastCGI over mod_php?
What are the advantages of using PHP-FPM + Nginx over Apache and mod_php?
How To Install Nginx With PHP5 (And PHP-FPM) And MySQL Support On CentOS 6.5
How To Install Linux, nginx, MySQL, PHP (LEMP) stack on CentOS 6
Linux上配置Nginx+PHP5(FastCGI)
使用fastcgi_finish_request提高页面响应速度
善用php-fpm的慢执行日志slow log,分析php性能问题
swoole之代码热更新实现
Pingback引用通告: PHP C扩展框架Phalcon | 勇气