Nginx实践01-ngnix编译安装

来源:互联网 发布:淘宝上架宝贝数量 编辑:程序博客网 时间:2024/06/15 09:12

1.下载nginx安装包

下载地址:http://nginx.org/en/download.html(里面有nginx各个版本)
解压到指定目录:
1
tar -xvf nginx-1.10.3.tar.gz
解压出来的目录简单介绍:
src:软件的所有源代码
man:man文档
auto:脚本文件,和configure脚本程序有关
conf:配置文件
html:存放了两个后缀为.html的静态页面文件

2.编译安装常用参数

--prefix=<path>:指定nginx软件的安装路径
--prefix=<path>:指定nginx可执行文件的安装路径,默认为/sbin/nginx/目录
--conf-path=<path>:在未给定-c选项下,指定默认的nignx.conf的路径
--pid-path=<path>:在nginx.conf未指定pid指令的情况下,指定默认的pid文件路径
--lock-path=<path>:在nginx.conf未指定nginx.lock的情况下,指定默认的lock文件路径,lock是nginx的锁文件
--error-log-path=<path>:在nginx.conf未指定error_log路径的情况下,指定默认的错误日志文件路径
--http-log-path=<path>:在nginx.conf未指定access.log的情况下,指定默认的访问日志文件路径
--user=<user>:在nginx.conf未指定用户的情况下,指定默认的nginx运行的属主
--group=<group>:在nginx.conf未指定用户组的情况下,指定默认的属组

--with-debug:启用nginx的调试日志
--add-module=<path>:指定第三方模块的路径,用于编译到nginx服务器中
--with-poll_module:声明启用poll模块。
--with-select_module:声明启用select处理模式
--with-http_ssl_module:启用ssl模块
--with-http_stub_status_module:启用server status页,默认不启用
--with-http_perl_module:启用perl模块,使得nginx支持perl脚本的运行
--with-perl_modules_path=<path>:指定perl模块的路径
--with-perl=<path>:指定perl执行文件的路径

--with-mail:声明启用IMAP4/POP3/SMTP模块,该模块负责mail代理服务的处理
--with-client-body-temp-path=<path>:指定存放http访问客户端请求报文的临时文件的路径
--http-proxy-temp-path=<path>:启用http的proxy模块之后,指定存放http代理临时文件的路径
--http-fastcgi-temp-path=<path>:启用http的fastcgi模块后,指定存放fastcgi模块临时文件的路径

其他的gzip、access、auth_basic、rewrite、proxy、fastcgi模块默认启用

3.编译安装

安装之前,先安装可能的依赖包:
新建nginx用户和组
1
useradd nginx -s /sbin/nologin -M  #不创建家目录,不能登录
我们编译安装时需要定义各种配置需要的目录进行,所以需要新建一些目录:
编译安装:
1
 ./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 --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
注意:如果编译安装过程中出现错误,屏幕上会出现报错信息。大部分的编译报错是由于缺少某些库的支持引起的。
另外,在没有改动源代码的情况下,如果需要重新编译安装nginx,就不必再使用configure脚本自动生成makefile了,可以像删除上次安装的nginx路径,解压源码包重新按上面的编译安装步骤来一遍。

4.检查安装:

1
查看nginx的版本:
2
[root@node002 nginx]# nginx -v
3
nginx version: nginx/1.10.3
4
测试nginx的配置文件:
5
[root@node002 nginx]# nginx -t
6
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
7
nginx: configuration file /etc/nginx/nginx.conf test is successful

5.nginx服务

提供nginx服务的脚本,方便nginx管理
1
#!/bin/bash
2
#
3
# nginx - this script starts and stops the nginx daemon
4
#
5
# chkconfig:  - 85 15 
6
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
7
#              proxy and IMAP/POP3 proxy server
8
# processname: nginx
9
# config:      /etc/nginx/nginx.conf
10
# config:      /etc/sysconfig/nginx
11
# pidfile:    /var/run/nginx.pid
12
  
13
# Source function library.
14
. /etc/rc.d/init.d/functions
15
  
16
# Source networking configuration.
17
. /etc/sysconfig/network
18
  
19
# Check that networking is up.
20
[ "$NETWORKING" = "no" ] && exit 0
21
  
22
nginx="/usr/sbin/nginx"
23
#注意:这个路径一定要跟nginx程序路径保持一致,否则nginx服务不能启动
24
prog=$(basename $nginx)
25
  
26
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
27
  
28
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
29
  
30
lockfile=/var/lock/subsys/nginx
31
  
32
make_dirs() {
33
  # make required directories
34
  user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
35
  options=`$nginx -V 2>&1 | grep 'configure arguments:'`
36
  for opt in $options; do
37
      if [ `echo $opt | grep '.*-temp-path'` ]; then
38
          value=`echo $opt | cut -d "=" -f 2`
39
          if [ ! -d "$value" ]; then
40
              # echo "creating" $value
41
              mkdir -p $value && chown -R $user $value
42
          fi
43
      fi
44
  done
45
}
46
  
47
start() {
48
    [ -x $nginx ] || exit 5
49
    [ -f $NGINX_CONF_FILE ] || exit 6
50
    make_dirs
51
    echo -n $"Starting $prog: "
52
    daemon $nginx -c $NGINX_CONF_FILE
53
    retval=$?
54
    echo
55
    [ $retval -eq 0 ] && touch $lockfile
56
    return $retval
57
}
58
  
59
stop() {
60
    echo -n $"Stopping $prog: "
61
    killproc $prog -QUIT
62
    retval=$?
63
    echo
64
    [ $retval -eq 0 ] && rm -f $lockfile
65
    return $retval
66
}
67
  
68
restart() {
69
    configtest || return $?
70
    stop
71
    sleep 1
72
    start
73
}
74
  
75
reload() {
76
    configtest || return $?
77
    echo -n $"Reloading $prog: "
78
    killproc $nginx -HUP
79
    RETVAL=$?
80
    echo
81
}
82
  
83
force_reload() {
84
    restart
85
}
86
  
87
configtest() {
88
  $nginx -t -c $NGINX_CONF_FILE
89
}
90
  
91
rh_status() {
92
    status $prog
93
}
94
  
95
rh_status_q() {
96
    rh_status >/dev/null 2>&1
97
}
98
  
99
case "$1" in
100
    start)
101
        rh_status_q && exit 0
102
        $1
103
        ;;
104
    stop)
105
        rh_status_q || exit 0
106
        $1
107
        ;;
108
    restart|configtest)
109
        $1
110
        ;;
111
    reload)
112
        rh_status_q || exit 7
113
        $1
114
        ;;
115
    force-reload)
116
        force_reload
117
        ;;
118
    status)
119
        rh_status
120
        ;;
121
    condrestart|try-restart)
122
        rh_status_q || exit 0
123
            ;;
124
    *)
125
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
126
        exit 2
127
esac
将nginx加入开机启动
检查:
1
service nginx start
2
netstat -tnulp|grep nginx

6.nginx常用命令

-v:显示nginx版本
-t:测试配置文件时候有问题
-s:stop, quit, reopen, reload这4种状态
-c:要加载的配置文件路径





原创粉丝点击