nginx支持LNMP
安装php-fpm
yum install php-fpm -y
使用/etc/php-fpm.d/www.conf配置文件默认配置即可
打开php配置:
vim /etc/nginx/conf.d/defaults
location ~ \.php$ { root /usr/share/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; }
vim /etc/nginx/server.conf
location / { root html; index index.php index.html index.htm; //支持index.php页面 }
修改fastcgi_parms
vim /etc/nginx/fastcgi_params
fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REQUEST_SCHEME $scheme; fastcgi_param HTTPS $https if_not_empty; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200;
上面内容中添加如下一行: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
php支持mysql
# yum install php-mysql -y
安装mysql
# yum install mysql-server -y
测试页
<?php $conn = mysql_connect('127.0.0.1','root',''); if ($conn) echo succ; else echo failure; mysql_close(); phpinfo(); ?>
为动态内容fastcgi协议的集群设置缓存
http上下文中添加:
upstream upservers { server 10.0.0.102 max_fails=5 fail_timeout=1s weight=1; server 10.0.0.103 max_fails=5 fail_timeout=1s weight=1; } proxy_cache_path /cache/nginx/ levels=1:1 keys_zone=mycache:32m; fastcgi_cache_path /cache/fastcgi/ levels=1:1 keys_zone=fcgicache:32m inactive=3m max_size=1g;
server.conf配置文件location中定义:
location ~ \.php$ { 46 fastcgi_cache fcgicache; 47 fastcgi_cache_valid 200 10m; 48 fastcgi_cache_valid 302 3m; 49 fastcgi_cache_valid any 1m; 50 51 root html; 52 fastcgi_pass 127.0.0.1:9000; 53 fastcgi_index index.php; 54 fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 55 include fastcgi_params; 56 }
其他参数介绍:
fastcgi_limit_rate: 限定从上游服务器收内容的速率
Syntax: fastcgi_limit_rate rate; Default: fastcgi_limit_rate 0; Context: http, server, location This directive appeared in version 1.7.7.
fastcgi_param: 向后端传递参数
Syntax: fastcgi_param parameter value [if_not_empty]; Default: — Context: http, server, location
fastcgi_store: 将内容存到磁盘上,不建议开启,如果要开启说明性能是下降的
Syntax: fastcgi_store on | off | string; Default: fastcgi_store off; Context: http, server, location
location /images/ { root /data/www; error_page 404 = /fetch$uri; } location /fetch/ { internal; fastcgi_pass backend:9000; ... fastcgi_store on; fastcgi_store_access user:rw group:rw all:r; fastcgi_temp_path /data/temp; //不建议启用,应该加大内存空间buffer的大小 alias /data/www/; }
关闭缓存
fastcgi_cache off;