一、方法一(推荐)
1、创建脚本
1)centos
vim /usr/lib/systemd/system/nginx.service
[Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid # Nginx will fail to start if /run/nginx.pid already exists but has the wrong # SELinux context. This might happen when running `nginx -t` from the cmdline. # https://bugzilla.redhat.com/show_bug.cgi?id=1268621 ExecStartPre=/usr/bin/rm -f /run/nginx.pid ExecStartPre=/usr/local/sbin/nginx -t ExecStart=/usr/local/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true [Install] WantedBy=multi-user.target
根据nginx编译情况修改下面三项配置
ExecStartPre=/usr/bin/rm -f /run/nginx.pid ExecStartPre=/usr/local/sbin/nginx -t ExecStart=/usr/local/sbin/nginx
2)ubuntu
vim /lib/systemd/system/nginx.service
[Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid # Nginx will fail to start if /run/nginx.pid already exists but has the wrong # SELinux context. This might happen when running `nginx -t` from the cmdline. # https://bugzilla.redhat.com/show_bug.cgi?id=1268621 ExecStartPre=/bin/rm -f /run/nginx.pid ExecStartPre=/usr/local/sbin/nginx -t ExecStart=/usr/local/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true [Install] WantedBy=multi-user.target
根据nginx编译情况修改下面三项配置
ExecStartPre=/bin/rm -f /run/nginx.pid ExecStartPre=/usr/local/sbin/nginx -t ExecStart=/usr/local/sbin/nginx
2、修改权限
1)centos
sudo chmod +x /usr/lib/systemd/system/nginx.service
2)ubuntu
sudo chmod +x /lib/systemd/system/nginx.service
3、开机启动
sudo systemctl enable nginx.service
4、更多控制命令
systemctl start nginx.service #启动nginx服务 systemctl enable nginx.service #设置开机自启动 systemctl disable nginx.service #停止开机自启动 systemctl status nginx.service #查看服务当前状态 systemctl restart nginx.service #重新启动服务 systemctl list-units --type=service #查看所有已启动的服务
二、方法二
1、关闭nginx
2、创建启动脚本
vim /etc/init.d/nginx
3、写入以下代码
#!/bin/sh # Name:nginx service # nginx - this script starts and stops the nginx daemon # # 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_DAEMON_PATH="/usr/local/sbin/nginx" NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" NGINX_LOCK_FILE="/var/lock/subsys/nginx" prog=$(basename $NGINX_DAEMON_PATH) start() { [ -x $NGINX_DAEMON_PATH ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $NGINX_DAEMON_PATH -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $NGINX_LOCK_FILE return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $NGINX_LOCK_FILE return $retval } restart() { configtest || return $? stop start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $NGINX_DAEMON_PATH -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $NGINX_DAEMON_PATH -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
4、给启动脚本增加执行权限
chmod +x /etc/init.d/nginx