编写开机启动脚本:
I、nginx开机启动
在/etc/init.d/目录下创建脚本
vi� /etc/init.d/nginx
更改脚本权限
chmod 775 /etc/init.d/nginx
编写脚本内容
#!/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.conf
nginxd=/usr/local/webserver/nginx/sbin/nginx
nginx_config=/usr/local/webserver/nginx/conf/nginx.conf
nginx_pid=/usr/local/webserver/nginx/logs/nginx.pid
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 /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 /usr/local/webserver/nginx/logs/nginx.pid
}
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
设置开机启动
chkconfig nginxd on
II、设置mysql开机启动
将mysql安装目录下 support-files目录下的mysql.server文件拷贝到/etc/init.d/目录下并改名为mysqld,并更改权限
chmod 775 /etc/init.d/mysqld
设置开机启动
chkconfig mysqld on
III、php-fpm开机启动
在/etc/init.d/目录下创建脚本
vi /etc/init.d/php-fpm
更改脚本权限
chmod 775 /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
设置开机启动
chkconfig php-fpm on
Ⅳ、设置redis开机启动
在/etc/init.d/目录下创建脚本
vi /etc/init.d/redis
更改脚本权限
chmod 775 /etc/init.d/redis
编写脚本内容
###########################
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/etc/redis.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
##############################
设置开机启动
chkconfig redis on
至此,大功告成。可以用命令 chkconfig 查看开机启动服务列表
chkconfig --list
附录:
1、nigx重启错误
bind() to 0.0.0.0:80 failed (98: Address already in use)
这个是nginx重启是 经常遇到的。 � 网上找了很多信息 都是没有啥用。说的乱七八糟的。 � 发现原来是nginx重复重启。自己占用了端口。 解决方法
killall -9 nginx
杀掉nginx 进程 �然后重启就行了。
service nginx restart
2、php-fpm 启动 关闭
php-fpm 不再支持 php-fpm 补丁具有的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,需要使用信号控制:
master进程可以理解以下信号
SIGINT, SIGTERM 立刻终止
SIGQUIT 平滑终止
SIGUSR1 重新打开日志文件
SIGUSR2 平滑重载所有worker进程并重新载入配置和二进制模块
示例:
php-fpm 关闭:
kill -SIGINT `cat /usr/local/php/var/run/php-fpm.pid`
php-fpm 重启:
kill -SIGUSR2 `cat /usr/local/php/var/run/php-fpm.pid`
其次配置文件不再使用的xml 格式,改为了INI,但是配置参数几乎和以前一样,可参照xml格式的格式配置。
3、nginx 启动 关闭
nginx的启动(nginx.conf文件基本上位于nginx主目录中的conf目录中)
nginx -c nginx.conf
nginx的停止(nginx.pid文件基本上位于nginx主目录中的logs目录中)
ps -ef | grep nginx
可发现数个nginx进程,其中标有master的为主进程,其它为子进程, 停止nginx主要就是对主进程进行信号控制.
从容停止
kill -QUIT `cat nginx.pid`
快速停止
kill -TERM `cat nginx.pid`
or
kill -INT `cat nginx.pid`
强制停止
kill -9 `cat nginx.pid`
nginx的平滑重启
首先要验证新的配置文件是否正确:
nginc -t -c nginx.conf
成功后向主进程发送HUP信号即可: [/shell]kill -HUP `cat nginx.pid`[/shell]
4、nginx的平滑升级
备份好旧的可执行文件,使用新版本替换旧版本
kill -USR2 旧版本的主进程PID 进行平滑升级, 此时新老版本共存
kill -WINCH 旧版本的主进程PID� 逐步关闭旧主进程的工作进程
当旧主进程产生的工作进程全部关闭后, 可以决定是否使用新版本还是旧版本.(需要使用kill命令来杀死新或旧主进程)
#!/bin/sh
BASE_DIR='/usr/local/'
${BASE_DIR}nginx/sbin/nginx -t -c ${BASE_DIR}nginx/conf/nginx.conf >& ${BASE_DIR}nginx/logs/nginx.start
info=`cat ${BASE_DIR}nginx/logs/nginx.start`
if [ `echo $info | grep -c "syntax is ok" ` -eq 1 ]; then
if [ `ps aux|grep "nginx"|grep -c "master"` == 1 ]; then
kill -HUP `cat ${BASE_DIR}nginx/logs/nginx.pid`
echo "ok"
else
killall -9 nginx
sleep 1
${BASE_DIR}nginx/sbin/nginx
fi
else
echo "######## error: ########"
cat ${BASE_DIR}nginx/logs/nginx.start
fi
5、CentOS修改系统环境变量
我这里拿php作为一个例子,我的php安装在/usr/local/webserver/php下,没有把php加入环境变量时,你在命令行执行
#查看当前php的版本信息
[root@CentOS ~]# php -v
会提示你此命令不存在。
下面详细说说linux下修改环境变量的方法
方法一:
在/etc/profile文件中添加变量【对所有用户生效(永久的)】
用VI在文件/etc/profile文件中增加变量,该变量将会对Linux下所有用户有效,并且是“永久的”。
[root@CentOS ~]# vi /etc/profile
在文件末尾加上如下两行代码
PATH=/usr/local/webserver/php/bin:$PATH
export PATH
如:
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
# ksh workaround
EUID=`id -u`
UID=`id -ru`
fi
USER="`id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi
# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
pathmunge /sbin after
fi
HOSTNAME=`/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
export HISTCONTROL=ignoreboth
else
export HISTCONTROL=ignoredups
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
umask 002
else
umask 022
fi
for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null 2>&1
fi
fi
done
unset i
unset pathmunge
PATH=/usr/local/webserver/php/bin:$PATH
export PATH
要是刚才的修改马上生效,需要执行以下代码
[root@CentOS ~]# source /etc/profile
这时再查看系统环境变量,就能看见刚才加的东西已经生效了
[root@CentOS ~]# echo $PATH
/usr/local/webserver/php/bin:/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
现在就能直接使用php命令了(而不是像之前写很长一串/usr/local/webserver/php/bin/php -v),例如查看当前php的版本
[root@CentOS ~]# php -v
PHP 5.3.8 (cli) (built: Jun 27 2012 14:28:20)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
方法二:
在用户目录下的.bash_profile文件中增加变量【对单一用户生效(永久的)】
用VI在用户目录下的.bash_profile文件中增加变量,改变量仅会对当前用户有效,并且是“永久的”。具体操作和方法1一样,这里就不在列举代码了。
方法三:
直接运行export命令定义变量【只对当前shell(BASH)有效(临时的)】
在shell的命令行下直接使用[export变量名=变量值]定义变量,该变量只在当前的shell(BASH)或其子shell(BASH)下是有效的,shell关闭了,变量也就失效了,再打开新shell时就没有这个变量,需要使用的话还需要重新定义。例如
export PATH=/usr/local/webserver/php/bin:$PATH
有话要说