nginx开机启动
1.首先,在linux系统的/etc/init.d/目录下创建nginx文件
vim /etc/init.d/nginx
2.加入脚本
1 #!/bin/bash 2 # nginx Startup script for the Nginx HTTP Server 3 # it is v.0.0.2 version. 4 # chkconfig: - 85 15 5 # description: Nginx is a high-performance web and proxy server. 6 # It has a lot of features, but it's not for everyone. 7 # processname: nginx 8 # pidfile: /var/run/nginx.pid 9 # config: /usr/local/nginx/conf/nginx.conf10 nginxd=/usr/local/nginx/sbin/nginx11 nginx_config=/usr/local/nginx/conf/nginx.conf12 nginx_pid=/var/run/nginx.pid13 RETVAL=014 prog="nginx"15 # Source function library.16 . /etc/rc.d/init.d/functions17 # Source networking configuration.18 . /etc/sysconfig/network19 # Check that networking is up.20 [ ${NETWORKING} = "no" ] && exit 021 [ -x $nginxd ] || exit 022 # Start nginx daemons functions.23 start() {24 if [ -e $nginx_pid ];then25 echo "nginx already running...."26 exit 127 fi28 echo -n $"Starting $prog: "29 daemon $nginxd -c ${nginx_config}30 RETVAL=$?31 echo32 [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx33 return $RETVAL34 }35 # Stop nginx daemons functions.36 stop() {37 echo -n $"Stopping $prog: "38 killproc $nginxd39 RETVAL=$?40 echo41 [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid42 }43 # reload nginx service functions.44 reload() {45 echo -n $"Reloading $prog: "46 #kill -HUP `cat ${nginx_pid}`47 killproc $nginxd -HUP48 RETVAL=$?49 echo50 }51 # See how we were called.52 case "$1" in53 start)54 start55 ;;56 stop)57 stop58 ;;59 reload)60 reload61 ;;62 restart)63 stop64 start65 ;;66 status)67 status $prog68 RETVAL=$?69 ;;70 *)71 echo $"Usage: $prog {start|stop|restart|reload|status|help}"72 exit 173 esac74 exit $RETVAL
或者脚本:
1 #!/bin/bash 2 # nginx Startup script for the Nginx HTTP Server 3 # it is v.0.0.2 version. 4 # chkconfig: - 85 15 5 # description: Nginx is a high-performance web and proxy server. 6 # It has a lot of features, but it's not for everyone. 7 # processname: nginx 8 # pidfile: /var/run/nginx.pid 9 # config: /usr/local/nginx/conf/nginx.conf10 nginxd=/usr/local/nginx/sbin/nginx11 nginx_config=/usr/local/nginx/conf/nginx.conf12 nginx_pid=/var/run/nginx.pid13 RETVAL=014 prog="nginx"15 # Source function library.16 . /etc/rc.d/init.d/functions17 # Source networking configuration.18 . /etc/sysconfig/network19 # Check that networking is up.20 [ ${NETWORKING} = "no" ] && exit 021 [ -x $nginxd ] || exit 022 # Start nginx daemons functions.23 start() {24 if [ -e $nginx_pid ];then25 echo "nginx already running...."26 exit 127 fi28 echo -n $"Starting $prog: "29 daemon $nginxd -c ${nginx_config}30 RETVAL=$?31 echo32 [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx33 return $RETVAL34 }35 # Stop nginx daemons functions.36 stop() {37 echo -n $"Stopping $prog: "38 killproc $nginxd39 RETVAL=$?40 echo41 [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid42 }43 # reload nginx service functions.44 reload() {45 echo -n $"Reloading $prog: "46 #kill -HUP `cat ${nginx_pid}`47 killproc $nginxd -HUP48 RETVAL=$?49 echo50 }51 # See how we were called.52 case "$1" in53 start)54 start55 ;;56 stop)57 stop58 ;;59 reload)60 reload61 ;;62 restart)63 stop64 start65 ;;66 status)67 status $prog68 RETVAL=$?69 ;;70 *)71 echo $"Usage: $prog {start|stop|restart|reload|status|help}"72 exit 173 esac
源码来源于:https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
注意其中
nginx=”/usr/sbin/nginx” 修改成nginx执行程序的路径。
NGINX_CONF_FILE=”/etc/nginx/nginx.conf” 修改成配置文件的路径。
3.设置文件权限
chmod a+x /etc/init.d/nginx
4.使用chkconfig进行管理
先将nginx服务加入chkconfig管理列表:
chkconfig --add /etc/init.d/nginx
加完这个之后,就可以使用service对nginx进行启动,重启等操作了。
service nginx startservice nginx stop
注意:如果是刚配置好chkconfig管理,要多敲几次service nginx stop 命令(刚加入有可能稍等下,命令才有效)
5.设置终端模式开机启动:
chkconfig nginx on
php开机启动
前提需要正确配置php-fpm
vim /etc/init.d/php-fpm
内容如下
#!/bin/sh # chkconfig: 2345 15 95# description: PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation \# with some additional features useful for sites of any size, especially busier sites.# DateTime: 2016-09-20# Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 phpfpm="/usr/local/php/sbin/php-fpm" prog=$(basename ${phpfpm}) lockfile=/var/lock/subsys/phpfpmstart() { [ -x ${phpfpm} ] || exit 5 echo -n $"Starting $prog: " daemon ${phpfpm} retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc ${phpfpm} -HUP RETVAL=$? echo } force_reload() { restart } configtest() { ${phpfpm} -t} 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 ;; status) rh_status ;; *) echo $"Usage: $0 {start|stop|status|restart|reload|configtest}" exit 2 esac
再就是加到开机启动项里面去
chkconfig --add php-fpm
然后启动
service php-fpm start // 启动service php-fpm stop // 停止
故障一:权限被拒绝
env: /etc/init.d/php-fpm: Permission denied
解决办法:
把/etc/rc.d/init.d/php-fpm 权限改为0755 或者 chmod a+x /etc/init.d/php-fpm
故障二:php-fpm出现乱码(而且文件变的很大二十几兆)
解决办法:
1.备份当前php-fpm
2. 新建php-fpm
ssh > vim /etc/rc.d/init.d/php-fpm
3.输入代码:
#!/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.confnginxd=/usr/local/nginx/sbin/nginxnginx_config=/usr/local/nginx/conf/nginx.confnginx_pid=/var/run/nginx.pidRETVAL=0prog="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 1fi 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" instart) start ;;stop) stop ;;reload) reload ;;restart) stop start ;;status) status $prog RETVAL=$? ;;*) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1esacexit $RETVAL
其他参考:
https://blog.csdn.net/u013870094/article/details/52463026
https://www.cnblogs.com/jimisun/p/8057156.html