发布于 2015-11-07 02:52:57 | 432 次阅读 | 评论: 0 | 来源: PHPERZ
Nginx WEB服务器
Nginx 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。
下载最新稳定版nginx-1.8.0
http://nginx.org/en/download.html
Centos 6.5(Final)
yum-y install pcre-devel openssl-devel
(a) 解压然后编译
./configure--prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx--group=nginx --error-log-path=/var/log/nginx/error.log--http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid--lock-path=/var/lock/nginx.lock --with-http_ssl_module--with-http_stub_status_module --with-http_gzip_static_module--with-http_flv_module --with-http_mp4_module--http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi make && make install
(b) 创建上面指定的目录,不然启动时会报错
mkdir -pv/var/tmp/nginx/{client,proxy,fastcgi,uwsgi}
根据目前不同的系统下载相应脚本
https://www.nginx.com/resources/wiki/start/topics/examples/initscripts/
对应Redhat的脚本如下:
#!/bin/sh # # nginx - this script starts and stops thenginx daemon # # chkconfig: - 85 15 # description: NGINX is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no"] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && ./etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { #make required directories user=`$nginx -V 2>&1 | grep "configure arguments:" |sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` if[ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating"$value mkdir -p $value && chown-R $user $value fi fi done } start() { [-x $nginx ] || exit 5 [-f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [$retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [$retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
其中两句修改为实际对应的路径:
nginx="/usr/local/nginx/sbin/nginx"
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
然后保存为/etc/init.d/nginx 并添加执行权限
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chmod +x /etc/init.d/nginx
(a) 修改nginx.conf 文件
如果php和nginx不在同一台机器,php配置文件中要改一下client监听端口,允许nginx访问
打开vim/etc/nginx/nginx.conf,最后面有php配置的案例,去掉注释启用即可
# pass the PHP scripts to FastCGI serverlistening on 127.0.0.1:9000 location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; #传到哪个服务器 fastcgi_index index.php; #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; #这句是系统默认,有可能会出现错误,修改为下面这句即可 fastcgi_paramSCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; #使用扩展的fastcgi参数 }
(b) /etc/nginx/下还应该有fastcgi_params文件,它的作用是把当前服务器的信息存放到变量中,传
递给php服务器,让其了解情况
例:其中一行内容:
fastcgi_paramQUERY_STRING $query_string;
最右边$query_string是nginx的变量,赋值给QUERY_STRING
注意:这个文件默认的内容是不适合nginx使用的,会启用不了php的,要做修改
编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容:
fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; 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 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;
并在/etc/nginx/nginx.conf所支持的主页面格式中添加php格式的主页,类似如下:
location/ { root html; index index.php index.html index.htm; }
在nginx的网站根目录下新建index.php测试,文件内容如下:
里面附带了测试数据库连接的语句
<?php $conn =mysql_connect('127.0.0.1','root','123456'); if($conn) echo succ; else echo fail; mysql_close(); phpinfo(); ?>
注意:如果访问http://localhost/index.php出现 File not found 的提示
并且/var/log/nginx/error.log出现一下错误:
2015/11/05 23:23:07[error] 86596#0: *13 FastCGI sent in stderr: "Primary script unknown"while reading response header from upstream, client: 127.0.0.1, server:localhost, request: "GET /index.php HTTP/1.1", upstream:"fastcgi://127.0.0.1:9000", host: "localhost"
故障原因:
fastcgi_param SCRIPT_FILENAME 告诉fastcgi进程到哪里找到这个文件,当这个param 为/scripts$fastcgi_script_name时,访问index.php 会到 /scripts/index.php 查找这个文件,此时当然找不到了
解决方法:
只要把Nginx配置文件中
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
修改成如下方式($document_root)即可:
fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name;
$document_root:当前请求映射到的root配置