centos7安装nginx
centos7安装nginx
centos7安装nginx有两种方式,这里重点介绍源码包安装
yum安装
添加nginx到yum源中
1
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
安装nginx
1
yum install -y nginx
启动nginx
1
systemctl start nginx
设置开机自启
1
systemctl enable nginx
源码包安装
首先安装依赖包
安装gcc
1
yum install -y gcc-c++
安装pcre
1
yum install -y pcre pcre-devel
安装zlib
1
yum install -y zlib zlib-devel
安装openssl
1
yum install -y opsenssl openssl-devel
下载安装nginx
首先到官网:nginx下载链接
下载安装包上传至/usr/local/nginx.这个目录是软件默认安装路径,然后解压缩
1
tar -zxvf nginx-1.20.2.tar.gz
或者直接在线下载
1
2
3
4
mkdir /usr/local/nginx
cd /usr/local/nginx
yum install -y wget
wget http://nginx.org/download/nginx-1.20.2.tar.gz
解压缩
1
tar zxvf nginx-1.20.2.tar.gz
进入nginx目录下
1
cd nginx-1.20.2
检查平台安装环境
1
./configure --prefix=/usr/local/nginx
编译并安装
1
2
make
make install
启动nginx
1
/usr/local/nginx/sbin/nginx
防火墙开放80端口
1
firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙
1
firewall-cmd --reload
看到下面的界面就说明成功了
设置开机启动
因为是用源码编译安装的,所以首先需要手动将nginx添加至服务
1
vi /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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/sh
# nginx - this script starts and stops the nginx daemin
#
# 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
# 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)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
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 $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
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
进入此目录
1
cd /etc/init.d
1
chmod 755 /etc/init.d/nginx
注册成服务
1
chkconfig -add nginx
设置nginx开机自启
1
systemctl enable nginx
nginx常用命令
1
2
3
4
5
6
cd /usr/local/nginx/sbin/
./nginx 启动
./nginx -s stop 停止
./nginx -s quit 安全退出
./nginx -s reload 重新加载配置文件
ps aux|grep nginx 查看nginx进程
License:
CC BY 4.0