首先如果你是用Centos7的话,systemctl这个神器可以用上了:
//nginx开机自启动
systemctl enable nginx.service
//php开机自启动
systemctl enable php-fpm
//mysql开机自启动
systemctl enable mysqld.service 或者systemctl enable mysql.service
刚刚配置的服务需要让systemctl能识别,就必须刷新配置
systemctl daemon-reload
如果没有权限可以使用sudo
sudo systemctl daemon-reload
systemctl常用命令如下:
//启动一个服务:
systemctl start nginx.service
//关闭一个服务:
systemctl stop postfix.service
//重启一个服务:
systemctl restart nginx.service
//显示一个服务的状态:
systemctl status postfix.service
//在开机时启用一个服务:
systemctl enable nginx.service
//在开机时禁用一个服务:
systemctl disable nginx.service
//查看服务是否开机启动:
systemctl is-enabled nginx.service
//查看已启动的服务列表:
systemctl list-unit-files|grep enabled
如果非CenOS7又不想贸然改系统的话,按下方走:
ONE:nginx
一.进入编辑文本:
vim /etc/init.id/nginx
按i进入编辑模式并添加如下代码:
#!/bin/bash
# chkconfig: 2345 10 90
# description: myservice ....
nginxd=/usr/local/bin/nginx
nginx_config=/etc/nginx.conf
nginx_pid=/var/run/nginx.pid
nginx_lock=/var/lock/subsys/nginx
RETVAL=0
prog=\"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 1
fi
echo -n $\"Starting $prog: \"
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${nginx_lock}
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $\"Stopping $prog: \"
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${nginx_lock} ${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\" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $\"Usage: $prog {start|stop|restart|reload|status|help}\"
exit 1
esac
exit $RETVAL
二.按左上角esc键退出编辑模式并输入:wq保存退出
三. 追加文本执行权限(a+x参数表示 ==> all user can execute 所有用户可执行)
chmod a+x /etc/init.d/nginx
四. 加入服务进程(保存后可以通过service nginx start ,service nginx stop开启关闭)
chkconfig –add /etc/init.d/nginx
五.设置终端模式开机启动:
chkconfig nginx on
六.附:从第四步开始也可以换成 vim /etc/rc.local命令
七.启动开机自启动命令
/etc/init.d/nginx start
TWO:mysql自启动
一.将mysql安装目录下 support-files目录下的mysql.server文件拷贝到/etc/init.d/目录下并改名为mysqld,并更改权限
cp /usr/local/mysql/support-files/mysql.service /etc/init.d/mysqld
二.添加权限:
chmod 775 /etc/init.d/mysqld
三.设置开机启动:
chkconfig mysqld on
THREE:PHP自启动
vim /etc/init.d/php-fpm
#!/bin/sh
#
# php-fpm - this script starts and stops the php-fpm daemin
#
# chkconfig: - 85 15
# processname: php-fpm
# config: /usr/local/php/etc/php-fpm.conf
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="php-fpm daemon"
NAME=php-fpm
DAEMON=/usr/local/php/sbin/$NAME //这里设成自己的目录
CONFIGFILE=/usr/local/php/etc/php-fpm.conf //这里设成自己的目录
PIDFILE=/usr/local/php/var/run/$NAME.pid //这里设成自己的目录
SCRIPTNAME=/etc/init.d/$NAME //这里设成自己的目录
# If the daemon file is not found, terminate the script.
test -x $DAEMON || exit 0
d_start(){
$DAEMON -y $CONFIGFILE || echo -n " already running"
}
d_stop(){
kill -QUIT `cat $PIDFILE` || echo -n " no running"
}
d_reload(){
kill -HUP `cat $PIDFILE` || echo -n " could not reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "Reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
# Sleep for two seconds before starting again, this should give the nginx daemon some time to perform a graceful stop
sleep 2
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload)" >&2
exit 3
;;
esac
exit 0
3.添加权限:(a+x参数表示 ==> all user can execute 所有用户可执行)
chmod a+x /etc/init.d/php-fpm
4.追加自启动
chkconfig php-fpm on
555
555
555
555
555