linux(centos)上配置nginx、mysql、php-fpm、redis开机启动<转>
原文 http://levi.cg.am/archives/2925
I、nginx开机启动
- 在/etc/init.d/目录下创建脚本
1
vi/etc/init.d/nginx - 更改脚本权限
1
chmod775/etc/init.d/nginx - 编写脚本内容
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
#!/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/webserver/nginx/sbin/nginxnginx_config=/usr/local/webserver/nginx/conf/nginx.confnginx_pid=/usr/local/webserver/nginx/logs/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"] &&exit0[ -x $nginxd ] ||exit0# Start nginx daemons functions.start() {if[ -e $nginx_pid ];thenecho"nginx already running...."exit1fiecho-n $"Starting $prog: "daemon $nginxd -c ${nginx_config}RETVAL=$?echo[ $RETVAL = 0 ] &&touch/var/lock/subsys/nginxreturn$RETVAL}# Stop nginx daemons functions.stop() {echo-n $"Stopping $prog: "killproc $nginxdRETVAL=$?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 -HUPRETVAL=$?echo}# See how we were called.case"$1"instart)start;;stop)stop;;reload)reload;;restart)stopstart;;status)status $progRETVAL=$?;;*)echo$"Usage: $prog {start|stop|restart|reload|status|help}"exit1esacexit$RETVAL - 设置开机启动
1
chkconfig nginxd on
II、设置mysql开机启动
- 将mysql安装目录下 support-files目录下的mysql.server文件拷贝到/etc/init.d/目录下并改名为mysqld,并更改权限
1
chmod775/etc/init.d/mysqld - 设置开机启动
1
chkconfig mysqld on
III、php-fpm开机启动
- 在/etc/init.d/目录下创建脚本
1
vi/etc/init.d/php-fpm - 更改脚本权限
1
chmod775/etc/init.d/php-fpm - 编写脚本内容
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
#!/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.confset-ePATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binDESC="php-fpm daemon"NAME=php-fpmDAEMON=/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 ||exit0d_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"instart)echo-n"Starting $DESC: $NAME"d_startecho".";;stop)echo-n"Stopping $DESC: $NAME"d_stopecho".";;reload)echo-n"Reloading $DESC configuration..."d_reloadecho"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 stopsleep2d_startecho".";;*)echo"Usage: $SCRIPTNAME {start|stop|restart|force-reload)">&2exit3;;esacexit0 - 设置开机启动
1
chkconfig php-fpm on
Ⅳ、设置redis开机启动
- 在/etc/init.d/目录下创建脚本
1
vi/etc/init.d/redis - 更改脚本权限
1
chmod775/etc/init.d/redis - 编写脚本内容
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
###########################PATH=/usr/local/bin:/sbin:/usr/bin:/binREDISPORT=6379EXEC=/usr/local/bin/redis-serverREDIS_CLI=/usr/local/bin/redis-cliPIDFILE=/var/run/redis.pidCONF="/etc/redis.conf"case"$1"instart)if[ -f $PIDFILE ]thenecho"$PIDFILE exists, process is already running or crashed"elseecho"Starting Redis server..."$EXEC $CONFfiif["$?"="0"]thenecho"Redis is running..."fi;;stop)if[ ! -f $PIDFILE ]thenecho"$PIDFILE does not exist, process is not running"elsePID=$(cat$PIDFILE)echo"Stopping ..."$REDIS_CLI -p $REDISPORT SHUTDOWNwhile[ -x ${PIDFILE} ]doecho"Waiting for Redis to shutdown ..."sleep1doneecho"Redis stopped"fi;;restart|force-reload)${0} stop${0} start;;*)echo"Usage: /etc/init.d/redis {start|stop|restart|force-reload}">&2exit1esac############################## - 设置开机启动
1
chkconfig redis on
至此,大功告成。可以用命令 chkconfig 查看开机启动服务列表
|
1
|
chkconfig --list |
附录:
1、nigx重启错误
bind() to 0.0.0.0:80 failed (98: Address already in use)
这个是nginx重启是 经常遇到的。 网上找了很多信息 都是没有啥用。说的乱七八糟的。 发现原来是nginx重复重启。自己占用了端口。 解决方法
|
1
|
killall -9 nginx |
杀掉nginx 进程 然后重启就行了。
|
1
|
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 关闭:
1
kill-SIGINT `cat/usr/local/php/var/run/php-fpm.pid` - php-fpm 重启:
1
kill-SIGUSR2 `cat/usr/local/php/var/run/php-fpm.pid`
其次配置文件不再使用的xml 格式,改为了INI,但是配置参数几乎和以前一样,可参照xml格式的格式配置。
3、nginx 启动 关闭
- nginx的启动(nginx.conf文件基本上位于nginx主目录中的conf目录中)
1
nginx -c nginx.conf - nginx的停止(nginx.pid文件基本上位于nginx主目录中的logs目录中)
1
ps-ef |grepnginx可发现数个nginx进程,其中标有master的为主进程,其它为子进程, 停止nginx主要就是对主进程进行信号控制.
- 从容停止
1
kill-QUIT `catnginx.pid` - 快速停止
1
kill-TERM `catnginx.pid`or
1kill-INT `catnginx.pid` - 强制停止
1
kill-9 `catnginx.pid`
- 从容停止
- nginx的平滑重启
首先要验证新的配置文件是否正确:1nginc -t -c nginx.conf成功后向主进程发送HUP信号即可: [/shell]kill -HUP `cat nginx.pid`[/shell]
4、nginx的平滑升级
- 备份好旧的可执行文件,使用新版本替换旧版本
- kill -USR2 旧版本的主进程PID 进行平滑升级, 此时新老版本共存
- kill -WINCH 旧版本的主进程PID 逐步关闭旧主进程的工作进程
- 当旧主进程产生的工作进程全部关闭后, 可以决定是否使用新版本还是旧版本.(需要使用kill命令来杀死新或旧主进程)
1234567891011121314151617
#!/bin/shBASE_DIR='/usr/local/'${BASE_DIR}nginx/sbin/nginx-t -c ${BASE_DIR}nginx/conf/nginx.conf >& ${BASE_DIR}nginx/logs/nginx.startinfo=`cat${BASE_DIR}nginx/logs/nginx.start`if[ `echo$info |grep-c"syntax is ok"` -eq1 ];thenif[ `psaux|grep"nginx"|grep-c"master"` == 1 ];thenkill-HUP `cat${BASE_DIR}nginx/logs/nginx.pid`echo"ok"elsekillall -9 nginxsleep1${BASE_DIR}nginx/sbin/nginxfielseecho"######## error: ########"cat${BASE_DIR}nginx/logs/nginx.startfi
5、CentOS修改系统环境变量
我这里拿php作为一个例子,我的php安装在/usr/local/webserver/php下,没有把php加入环境变量时,你在命令行执行
|
1
2
|
#查看当前php的版本信息[root@CentOS ~]# php -v |
会提示你此命令不存在。
下面详细说说linux下修改环境变量的方法
方法一:
在/etc/profile文件中添加变量【对所有用户生效(永久的)】
用VI在文件/etc/profile文件中增加变量,该变量将会对Linux下所有用户有效,并且是“永久的”。
|
1
|
[root@CentOS ~]# vi /etc/profile |
在文件末尾加上如下两行代码
|
1
2
|
PATH=/usr/local/webserver/php/bin:$PATHexport PATH |
如:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# /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 manipulationif [ "$EUID" = "0" ]; then pathmunge /sbin pathmunge /usr/sbin pathmunge /usr/local/sbinelse pathmunge /usr/local/sbin after pathmunge /usr/sbin after pathmunge /sbin afterfiHOSTNAME=`/bin/hostname 2>/dev/null`HISTSIZE=1000if [ "$HISTCONTROL" = "ignorespace" ] ; then export HISTCONTROL=ignorebothelse export HISTCONTROL=ignoredupsfiexport 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 fileif [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then umask 002else umask 022fifor i in /etc/profile.d/*.sh ; do if [ -r "$i" ]; then if [ "${-#*i}" != "$-" ]; then . "$i" else . "$i" >/dev/null 2>&1 fi fidoneunset iunset pathmungePATH=/usr/local/webserver/php/bin:$PATHexport PATH |
要是刚才的修改马上生效,需要执行以下代码
|
1
|
[root@CentOS ~]# source /etc/profile |
这时再查看系统环境变量,就能看见刚才加的东西已经生效了
|
1
2
|
[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的版本
|
1
2
3
4
|
[root@CentOS ~]# php -vPHP 5.3.8 (cli) (built: Jun 27 2012 14:28:20)Copyright (c) 1997-2011 The PHP GroupZend 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时就没有这个变量,需要使用的话还需要重新定义。例如
|
1
|
export PATH=/usr/local/webserver/php/bin:$PATH
|
linux(centos)上配置nginx、mysql、php-fpm、redis开机启动<转>的更多相关文章
- Linux centos 6 配置php环境,扩展redis
1.首先安装一个虚拟机(我自己版本:VM 10.0.4) yum -y install openssl psmisc openssl-devel php-devel pcre-devel gcc gc ...
- Centos上配置nginx+uwsgi+负载均衡配置
负载均衡在服务端开发中算是一个比较重要的特性.因为Nginx除了作为常规的Web服务器外,还会被大规模的用于反向代理后端,Nginx的异步框架可以处理很大的并发请求,把这些并发请求hold住之后就可以 ...
- Linux2 在Linux(CentOS)上配置SSH免登陆
前言: 本文主要是我在安装hadoop之前,需要先配置SSH免登陆.通过网上搜索,发现不少类似的资料,但多少都有些小问题,所以结合自己的实践,记录在此,作为参考.如果能帮助到其他人,自然是更 ...
- 阿里云服务器Linux CentOS安装配置(八)nginx安装、配置、域名绑定
阿里云服务器Linux CentOS安装配置(八)nginx安装.配置.域名绑定 1.安装nginx yum -y install nginx 2.启动nginx service nginx star ...
- Linux CentOS上安装 MySQL 8.0.16
前言: 因为我需要在我新安装的Linux CentOS系统服务器中安装和配置MySQL服务器,然而对于我们这种Linux使用小白而言在Linux系统中下载,解压,配置MySQL等一系列的操作还是有些耗 ...
- 阿里云服务器Linux CentOS安装配置(三)yum安装mysql
阿里云服务器Linux CentOS安装配置(三)yum安装mysql 1.执行yum安装mysql命令:yum -y install mysql-server mysql-devel 2.启动mys ...
- 阿里云服务器Linux CentOS安装配置(一)购买阿里云服务器
阿里云服务器Linux CentOS安装配置(一)购买阿里云服务器 我在阿里云购买的服务器配置 CPU:1核 内存:2G 系统盘:40G 公共镜像:CentOS 6.5 64位 公网带宽:1Mbps ...
- 阿里云服务器Linux CentOS安装配置(零)目录
阿里云服务器Linux CentOS安装配置(零)目录 阿里云服务器Linux CentOS安装配置(一)购买阿里云服务器 阿里云服务器Linux CentOS安装配置(二)yum安装svn 阿里云服 ...
- 阿里云服务器Linux CentOS安装配置(七)域名解析
阿里云服务器Linux CentOS安装配置(七)域名解析 1.购买域名 登录阿里云,左侧菜单点击[域名],然后[域名注册],完成域名购买.(一般首年45元) 2.添加域名解析 在域名列表里点击你的域 ...
随机推荐
- webapp开发——‘手机屏幕分辨率’与‘浏览器分辨率’不要混淆
关于webApp响应式设计遇到的问题,分享给大家,最近在做一个手机webApp,因为我手机是”米3“,屏幕截图大小是1080宽,所以css样式用@media screen and(min-width: ...
- NandFlash驱动框架
1.首先和前面的几个驱动程序相似,需要分配一个nand_chip结构体 s3c_nand = kzalloc(sizeof(struct nand_chip), GFP_KERNEL); 然后填充该结 ...
- Cordova VS React Native 谁是未来? - b
对于原生native还是倍加推崇的,极佳的用户体验和性能让我学的如痴如醉,可是互联网这个世界可以用一句话可以总结:没有什么是不可能的.自从阿里淘宝天猫横空出世,它们教会了人们如何在网上购物,然后仿佛一 ...
- Java实现Qt的SIGNAL-SLOT机制
SIGNAL-SLOT是Qt的一大特色,使用起来十分方便.在传统的AWT和Swing编程中,我们都是为要在 监听的对象上添加Listener监听器.被监听对象中保存有Listener的列表,当相关事件 ...
- c#,if 分支语句,条件运算符
//输入整数a和b, //若a²+b²大于100,则输出a²+b²百位以上数字, //否则输出两数之和 /*Console.Write("请输入整数a:"); int a = in ...
- Alignment ( 最长上升(下降)子序列 )
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11397 Accepted: 3630 Description In t ...
- 【转】JAVA程序中Float和Double精度丢失问题
原文网址:http://blog.sina.com.cn/s/blog_827d041701017ctm.html 问题提出:12.0f-11.9f=0.10000038,"减不尽" ...
- linux下的webserver BOA及CGIC库的使用指南(转帖)
我把网页挂载到nfs 下面的文件中(需要新建一个文件www ),不过这样很方便! 安装过程 ====================================================== ...
- 数据结构(动态树):COGS 27. [WC 2006] 水管局长
27. [WC 2006] 水管局长 ★★★☆ 输入文件:tube.in 输出文件:tube.out 简单对比时间限制:3 s 内存限制:128 MB [问题描述 ] SC 省 MY ...
- selenuim ide回放时出现的问题
[error] Unexpected Exception: fileName -> chrome://selenium-ide/content/selenium-core/scripts/htm ...