目录
1、源码编译安装nginx
2、分别编写基于RHEL6和RHEL7的脚本。
2.1 RHEL6的nginx系统服务脚本
2.2 RHEL7的nginx系统服务脚本
1、源码编译安装nginx
1.首先关闭防火墙和selinux
[root@node13 ~]# systemctl stop firewalld
[root@node13 ~]# setenforce 0
2.准备环境(C和C++的编译环境)
[root@node13 ~]# yum install -y gcc gcc-c++ make
3.下载nginx安装包(或者使用wget直接上传)
链接:http://nginx.org/download/
4.解压安装包
[root@node13 ~]# tar xf nginx-1.22.0.tar.gz -C /usr/local/src/
[root@node13 ~]# cd /usr/local/src/nginx-1.22.0/
5.安装依赖项,配置nginx
[root@node13 nginx-1.22.0]# yum install -y pcre-devel zlib-devel
[root@node13 nginx-1.22.0]# ./configure --prefix=/usr/local/nginx
6.进行编译安装
[root@node13 nginx-1.22.0]# make
[root@node13 nginx-1.22.0]# make install
2、分别编写基于RHEL6和RHEL7的脚本。
2.1 RHEL6的nginx系统服务脚本
【1】编写系统服务脚本(/etc/init.d/nginx)
[root@node13 ~]# vim /etc/init.d/nginx # 主要修改以下两句
nginx=${NGINX-/usr/local/nginx/sbin/nginx}
conffile=${CONFFILE-/usr/local/nginx/conf/nginx.conf}
#!/bin/sh
#
# nginx Startup script for nginx
#
# chkconfig: - 85 15
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# description: nginx is an HTTP and reverse proxy server
#
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop nginx
### END INIT INFO# Source function library.
. /etc/rc.d/init.d/functionsif [ -L $0 ]; theninitscript=`/bin/readlink -f $0`
elseinitscript=$0
fisysconfig=`/bin/basename $initscript`if [ -f /etc/sysconfig/$sysconfig ]; then. /etc/sysconfig/$sysconfig
finginx=${NGINX-/usr/local/nginx/sbin/nginx}
prog=`/bin/basename $nginx`
conffile=${CONFFILE-/usr/local/nginx/conf/nginx.conf}
lockfile=${LOCKFILE-/var/lock/subsys/nginx}
pidfile=${PIDFILE-/var/run/nginx.pid}
SLEEPMSEC=${SLEEPMSEC-200000}
UPGRADEWAITLOOPS=${UPGRADEWAITLOOPS-5}
RETVAL=0start() {echo -n $"Starting $prog: "daemon --pidfile=${pidfile} ${nginx} -c ${conffile}RETVAL=$?echo[ $RETVAL = 0 ] && touch ${lockfile}return $RETVAL
}stop() {echo -n $"Stopping $prog: "killproc -p ${pidfile} ${prog}RETVAL=$?echo[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}reload() {echo -n $"Reloading $prog: "killproc -p ${pidfile} ${prog} -HUPRETVAL=$?echo
}upgrade() {oldbinpidfile=${pidfile}.oldbinconfigtest -q || returnecho -n $"Starting new master $prog: "killproc -p ${pidfile} ${prog} -USR2echofor i in `/usr/bin/seq $UPGRADEWAITLOOPS`; do/bin/usleep $SLEEPMSECif [ -f ${oldbinpidfile} -a -f ${pidfile} ]; thenecho -n $"Graceful shutdown of old $prog: "killproc -p ${oldbinpidfile} ${prog} -QUITRETVAL=$?echoreturnfidoneecho $"Upgrade failed!"RETVAL=1
}configtest() {if [ "$#" -ne 0 ] ; thencase "$1" in-q)FLAG=$1;;*);;esacshiftfi${nginx} -t -c ${conffile} $FLAGRETVAL=$?return $RETVAL
}rh_status() {status -p ${pidfile} -b ${nginx} ${nginx}
}# See how we were called.
case "$1" instart)rh_status >/dev/null 2>&1 && exit 0start;;stop)stop;;status)rh_statusRETVAL=$?;;restart)configtest -q || exit $RETVALstopstart;;upgrade)rh_status >/dev/null 2>&1 || exit 0upgrade;;condrestart|try-restart)if rh_status >/dev/null 2>&1; thenstopstartfi;;force-reload|reload)reload;;configtest)configtest;;*)echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}"RETVAL=2
esacexit $RETVAL
【2】修改配置文件(/usr/local/nginx/conf/nginx.conf)
# 找到配置以下pid:
pid /var/run/nginx.pid;
【3】增加执行权限
[root@node13 init.d]# chmod +x nginx
【4】添加系统服务,设置开机自启动
[root@node13 init.d]# chkconfig --add nginx
[root@node13 init.d]# chkconfig nginx on
[root@node13 init.d]# chkconfig --list nginx # 查看服务列表Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
【5】测试脚本
[root@node13 init.d]# service nginx start
Starting nginx (via systemctl): [ OK ]
[root@node13 init.d]# netstat -lnupt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7026/nginx: master
在浏览器上输入自己的IP地址,看是否可以访问到nginx首页
[root@node13 init.d]# service nginx stop # 停止服务,看能否停止
Stopping nginx (via systemctl): [ OK ]
[root@node13 init.d]# netstat -lnupt | grep 80 # 查看端口
再次访问浏览器就访问不到了
2.2 RHEL7的nginx系统服务脚本
【1】编写系统服务脚本(/usr/lib/systemd/system/nginx.service)
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true[Install]
WantedBy=multi-user.target
【2】修改配置文件(/usr/local/nginx/conf/nginx.conf)
# 找到配置以下pid:
pid /var/run/nginx.pid;
【3】增加执行权限
[root@node13 system]# chmod +x nginx.service
【4】测试脚本
[root@node13 system]# systemctl daemon-reload
[root@node13 system]# systemctl start nginx
[root@node13 system]# netstat -lnupt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17196/nginx: master
[root@node13 system]# systemctl stop nginx
[root@node13 system]# netstat -lnupt | grep 80