1. nginx的安装和使用
nginx的安装
tar zxf nginx-1.17.8.tar.gz
cd nginx-1.17.8/
yum install gcc openssl-devel pcre-devel zlib -y
./configure --prefix=/usr/local/nginx
make && make install
# 安装成功后,可以在/usr/local/nginx下看到安装好的文件
[root@localhost nginx]# pwd
/usr/local/nginx
[root@localhost nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
nginx的启动和关闭
/usr/local/nginx/sbin/nginx #启动
/usr/local/nginx/sbin/nginx -s stop #关闭
netstat -antlpe | grep nginx |wc -l #查看端口状态(0表示关闭,1表示开启)
[root@localhost nginx-1.17.8]# netstat -antlupe|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 29041 4811/nginx: master
2. nginx服务开启和停止脚本
#!/bin/bash
. /etc/init.d/functions # 加载系统函数可以
path=/usr/local/nginx/sbin # 设定nginx启动命令路径
function start(){
if [ `netstat -antlpe|grep nginx|wc -l` -eq 0 ];then
$path/nginx # 打开nginx
RETVAL=$?
if [ $RETVAL -eq 0 ];then
action "nginx is started" /bin/true
return $RETVAL
else
action "nginx is started" /bin/false
return $RETVAL
fi
else
echo "nginx is running"
return $RETVAL
fi
}
function stop(){
if [ `netstat -antlupe|grep nginx|wc -l` -ne 0 ]
then
$path/nginx -s stop
RETVAL=$?
if [ $REVAL -eq 0 ]
then
action "nginx is stoped" /bin/true
else
action "nginx is toped" /bin/false
fi
else
echo "nginx is stoped"
return 0
fi
}
case "$1" in # 通过特殊参数$1接受脚本参数的字符串
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 1
start
;;
*)
echo $"usage: $0 {start|stop|restart}"
exit 1
esac
exit $RETVAL
使用脚本需要加上执行命令
chmod +x /etc/init.d/nginxd
测试:
[root@localhost init.d]# /etc/init.d/nginxd start
Reloading systemd: [ OK ]
Starting nginxd (via systemctl): [ OK ]
[root@localhost init.d]# /etc/init.d/nginxd stop
Stopping nginxd (via systemctl): [ OK ]
[root@localhost init.d]# /etc/init.d/nginxd restart
Restarting nginxd (via systemctl): [ OK ]
[root@localhost init.d]# /etc/init.d/nginxd haha
usage: /etc/init.d/nginxd{start|stop|restart}