为了做监控,就试着搭了下lnmp。碰到了一些问题,找了不少资料,终于解决。
系统 ubuntu 14.10
nginx,php5 mysql都是通过apt-get 安装的。网上教程很多,这里就略去安装介绍。
问题主要出在nginx以及php的配置上面
值得注意的是,apt-get 安装的nginx版本挺新 1.6,其中有些配置文件和老版本有略微出入。老版本配置文件为nginx.conf ,虚拟主机都定义在里面,新版的则定义到/etc/nginx/sites-available/default 文件中,nginx目录下还有一个/etc/nginx/sites-enabled/default 的配置文件,为软连接。虚拟主机都在该配置文件中定义。
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
上述代码为nginx.conf中定义。
按照网上教程搭好lnmp之后,用info.php测试
<?php
phpinfo();
?>
却出现了502 bad gatewayd的错误,好是一番折腾之后最终解决了,/etc/nginx/sites-available/default部分内容如下:
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# <span style="color:#FF0000;">fastcgi_pass 127.0.0.1:9000;</span>
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
<span style="color:#FF0000;">fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;</span>
include fastcgi_params;
}
网上教程基本上都是取消了 fastcgi_pass 127.0.0.1:9000; 的注释,然后将 fastcgi_pass unix:/var/run/php5-fpm.sock; 注释掉,这样以来nginx要解析php页面其实是将请求转发到127.0.0.1:9000 上来处理。但是通过netstat -ltpn | grep :9000 根本没有相关的端口开启
通过查看 /etc/php5/fpm/pool.d/www.conf
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses on a
; specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /var/run/php5-fpm.sock
可以看到默认的监听方式为sock方式,所以端口监听方式就不行了。而sock监听方式也相对效率高一些。
当然光是修改这个还不够,需要注意的还有 fastcgi_param 参数。如果不写的话,虽然没了502错误,但是php页面上也是一片空白,不能正常显示。
关于这个问题这篇文章分析的很到位:
http://www.xker.com/page/e2012/1209/122251.html
大概意思如下:
Below is an example of the minimally necessary parameters for PHP:
fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
Parameter SCRIPT_FILENAME is used by PHP for determining the name of script to execute, and QUERY_STRING contains the parameters of the request.
所以 我们在没有定义SCRIPT_FILENAME这个系统变量的时候 php是没法解释执行的
还有一个问题值得注意,新版的nginx提供了对IPv6的支持,所以在配置文件中有这么一行
server {
listen 80 default_server;
# listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm index.php;
# Make site accessible from http://localhost/
server_name localhost;
但是我在本地将IPV6给禁了,所以启动的时候会出现
nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)
只要将defaul中的有关ipv6部分注释掉就好了。(配好之后记得用nginx -t -c nginx.conf 进行检查)