linux离线安装nginx

nginx编译安装介绍

下载nginx源码包:

http://nginx.org/download/nginx-1.12.1.tar.gz

然后解压,tar zxvf nginx-1.12.1.tar.gz

安装依赖:

安装之前使用rpm -qa |grep "名称"查看是否安全了包(centos),或者dpkg --get-selections | grep gcc(Ubuntu或者Debian)

链接:https://pan.baidu.com/s/1ggKF0XX

密码:vwig

  1. 安装gcc,如果没有gcc没有装,去看我的上一篇文章,离线安装gcc。

  2. 安装pcre依赖包:

    1
    2
    3
    rpm -ivh pcre-7.8-7.el6.x86_64.rpm --force(由于机器上已经有低版本的pcre,所以强制安装)。

    rpm -ivh pcre-devel-7.8-7.el6.x86_64.rpm。
  3. 安装libstdc++-devel(gcc-c++依赖) 一般来说REDHAT 6.5上会有这个依赖

    1
    rpm -ivh libstdc++-devel-4.4.7-4.el6.x86_64.rpm
  4. 安装gcc-c++ 一般来说REDHAT 6.5上会有这个依赖

    1
    rpm -ivh gcc-c++-4.4.7-4.el6.x86_64.rpm
  5. 安装zlib-devel 一般来说REDHAT 6.5上会有这个依赖

    1
    rpm -ivh zlib-devel-1.2.3-29.el6.x86_64.rpm
  6. 安装nginx

    1
    2
    3
    4
    5
    6
    7
    cd nginx-1.12.1

    ./configure

    make

    make install
  7. 运行nginx

    1
    2
    3
    cd /usr/local/nginx/sbin

    ./nginx

加入nginx开机启动

将如下代码复制粘贴到/etc/init.d/nginx

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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /usr/local/nginx/logs/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)

sysconfig="/etc/sysconfig/$prog"
lockfile="/var/lock/subsys/nginx"
pidfile="/usr/local/nginx/logs/${prog}.pid"

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f $sysconfig ] && . $sysconfig


start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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 -p $pidfile $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest_q || return 6
stop
start
}

reload() {
configtest_q || return 6
echo -n $"Reloading $prog: "
killproc -p $pidfile $prog -HUP
echo
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

configtest_q() {
$nginx -t -q -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

# Upgrade the binary with no downtime.
upgrade() {
local oldbin_pidfile="${pidfile}.oldbin"

configtest_q || return 6
echo -n $"Upgrading $prog: "
killproc -p $pidfile $prog -USR2
retval=$?
sleep 1
if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]]; then
killproc -p $oldbin_pidfile $prog -QUIT
success $"$prog online upgrade"
echo
return 0
else
failure $"$prog online upgrade"
echo
return 1
fi
}

# Tell nginx to reopen logs
reopen_logs() {
configtest_q || return 6
echo -n $"Reopening $prog logs: "
killproc -p $pidfile $prog -USR1
retval=$?
echo
return $retval
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest|reopen_logs)
$1
;;
force-reload|upgrade)
rh_status_q || exit 7
upgrade
;;
reload)
rh_status_q || exit 7
$1
;;
status|status_q)
rh_$1
;;
condrestart|try-restart)
rh_status_q || exit 7
restart
;;
*)
echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
exit 2
esac

启动nginx:

1
2
# chmod +x /etc/init.d/nginx
# service nginx start

配置nginx开机启动

1
# chkconfig nginx on

设置nginx环境变量

1
# vim /etc/profile

在其文件末尾添加如下变量

1
export PATH=$PATH:/usr/local/nginx/sbin

或者用以下命令添加

1
# sed -i '/unset -f pathmunge/a\export PATH=$PATH:/usr/local/nginx/sbin' /etc/profile

运行如下命令使环境变量生效

1
# source /etc/profile