设为首页 收藏本站
查看: 568|回复: 0

[经验分享] Nginx1.8.0 编译安装

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-11-9 09:40:07 | 显示全部楼层 |阅读模式

1.准备安装包
         下载最新稳定版nginx-1.8.0
         http://nginx.org/en/download.html

2.安装环境准备
         Centos 6.5(Final)
          yum-y install pcre-devel openssl-devel

3. 安装
         (a) 解压然后编译
         
1
2
3
./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) 创建上面指定的目录,不然启动时会报错
1
mkdir -pv/var/tmp/nginx/{client,proxy,fastcgi,uwsgi}






4. 启动服务配置
        根据目前不同的系统下载相应脚本
        [url]https://www.nginx.com/resources/wiki/start/topics/examples/initscripts/[/url]

        对应Redhat的脚本如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/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


5. php与Nginx结合

        (a) 修改nginx.conf 文件
    如果php和nginx不在同一台机器,php配置文件中要改一下client监听端口,允许nginx访问
              打开vim/etc/nginx/nginx.conf,最后面有php配置的案例,去掉注释启用即可
         
         
1
2
3
4
5
6
7
8
9
10
# 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,将其内容更改为如下内容:
         
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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格式的主页,类似如下:
        
1
2
3
4
location/ {
    root   html;
    index  index.php index.html index.htm;
}





6. 测试
    在nginx的网站根目录下新建index.php测试,文件内容如下:
               里面附带了测试数据库连接的语句
1
2
3
4
5
6
7
8
<?php $conn =mysql_connect('127.0.0.1','root','123456');
  if($conn)
       echo succ;
else
       echo fail;
mysql_close();
phpinfo();
?>






7. 安装过程可能遇到的问题
    注意:如果访问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配置





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-136858-1-1.html 上篇帖子: django+nginx+uwsgi 搭建环境 下篇帖子: Keeplived配置Nginx双机高可用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表