先安装必要项:
yum install gcc gcc-c++ pcre-devel zlib-devel openssl-devel automake autoconf libtool make -y
我们要安装1.19.0版本,访问NGINX官网,找到指定版本地址:http://nginx.org/
#我们要安装的目录为 /var/nginx ,先进入/var,下载安装文件到该目录下:
cd /var/
wget http://nginx.org/download/nginx-1.19.0.tar.gz
#解压:
tar -zxvf nginx-1.19.0.tar.gz
#进入nginx-1.19.0目录:
cd nginx-1.19.0
#配置安装信息(--prefix=/var/nginx 指定安装目录):
./configure --prefix=/var/nginx
#编译:
make
#安装:
make install
相关命令:
进入NGINX目录下的sbin文件夹:
启动:
./nginx
检测配置文件:
./nginx -t
从容停止服务(等待进程完成当前工作再停止):
./nginx -s quit
立即停止服务(无论进程是否在工作,都直接停止):
./nginx -s stop
重启服务:
./nginx -s reload
查询进程号:
ps -ef | grep nginx
#从容停止:
kill -QUIT 进程号
#快速停止:
kill -TERM 进程号
#强制停止:
kill -9 nginx
调整防火墙规则,以便其他电脑可以访问:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
以下是重头戏,将NGINX添加服务,自启动:
vim /etc/init.d/nginx
输入以下内容,注意NGINX的安装路径要改一下,输完后按ESC,输入:wq,回车保存并退出:
#! /bin/bash
# chkconfig: - 85 15
PATH=/var/nginx
DESC="nginx"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
scriptNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $scriptNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
然后:
cd /etc/init.d/
#设置所有人都有对这个启动脚本nginx文件的执行权限:
chmod a+x nginx
chmod a+x nginx
#把nginx加入系统服务中:
chkconfig --add nginx
#把服务设置为开机启动:
chkconfig nginx on
#注:reboot重启系统生效!
重启后,就可以执行台下命令进行管理了:
systemctl start nginx.service # 启动nginx
systemctl stop nginx.service # 结束nginx
systemctl restart nginx.service # 重启nginx
systemctl enable nginx.service # 开机启动