nginx命令
wang 2021-07-21 nginx
nginx下载地址:下载 nginx-1.21.0.tar.gz 安装包
# docker安装
# 安装
docker pull nginx
1
# 启动
# 注意:nginx配置文件的监控地址要是容器地址/usr/share/nginx/html
docker run --privileged=true --name nginx -p 80:80 \
-v /usr/local/docker/nginx/html:/usr/share/nginx/html:rw \
-v /usr/local/docker/nginx/logs:/var/log/nginx \
-v /usr/local/docker/nginx/conf:/etc/nginx \
-d nginx
1
2
3
4
5
6
2
3
4
5
6
# 源码安装
# 安装nginx
1.安装前置环境
# 安装依赖和一些组件
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
1
2
2
2.解压并设置设置目录权限
# 解压
tar -zxvf nginx-1.21.0.tar.gz
# 进入nginx
cd nginx-1.21.0
# 带ssl证书功能
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
# 不带ssl证书功能
./configure
# 编译安装
make && make install
# 进入sbin目录
cd /usr/local/nginx/sbin
# 启动
./nginx
# 停止
./nginx stop
# 检查配置文件
./nginx -t
# 重新记载配置文件
./nginx -s reload
# 查看已启动服务的端口和进程id
netstat -nlpt
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
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
# 开机自启
1.创建自启文件“nginx”
vi /etc/init.d/nginx
# 添加执行权限
chmod +x /etc/init.d/nginx
# 添加nginx自启任务
chkconfig --add nginx
# 查看所有的自启任务
chkconfig --list
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
nginx自启脚本内容
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx # nginx安装路劲
nginx_config=/usr/local/nginx/conf/nginx.conf # nginx配置文件
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
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
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
3.配置启动权限
chmod a+x /etc/init.d/nginx # (a+x ==> all user can execute 所有用户可执行)
1