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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
#!/bin/sh
# 编译安装管理PHP
App=php
AppName=PHP
AppBase=
/App
AppDir=$AppBase/$App
AppProg=$AppDir
/sbin/php-fpm
AppIni=$AppDir
/etc/php
.ini
AppConf=$AppDir
/etc/php-fpm
.conf
ExtensionDir=$($AppDir
/bin/php-config
--extension-
dir
)
AppSrcBase=
/App/src
AppSrcFile=$App-*.
tar
.*
AppSrcDir=$(
find
$AppSrcBase -maxdepth 1 -name
"$AppSrcFile"
-
type
f 2>
/dev/null
|
sed
-e
's/.tar.*$//'
-e
's/^.\///'
)
AppUser=$(
grep
"^[[:space:]]*user"
$AppConf 2>
/dev/null
|
awk
-F=
'{print $2}'
|
sed
-e
's/[[:space:]]//g'
-e
's/"//g'
-e "s/'
//g
")
AppGroup=$(
grep
"^[[:space:]]*group"
$AppConf 2>
/dev/null
|
awk
-F=
'{print $2}'
|
sed
-e
's/[[:space:]]//g'
-e
's/"//g'
-e "s/'
//g
")
AppPidDir=$(
dirname
$(
grep
"^[[:space:]]*pid"
$AppConf 2>
/dev/null
|
awk
-F=
'{print $2}'
|
sed
-e
's/[[:space:]]//g'
-e
's/"//g'
-e "s/'
//g
") 2>
/dev/null
)
AppErrorLogDir=$(
dirname
$(
grep
"^[[:space:]]*error_log"
$AppConf 2>
/dev/null
|
awk
-F=
'{print $2}'
|
sed
-e
's/[[:space:]]//g'
-e
's/"//g'
-e "s/'
//g
") 2>
/dev/null
)
AppSlowLogDir=$(
dirname
$(
grep
"^[[:space:]]*slowlog"
$AppConf 2>
/dev/null
|
awk
-F=
'{print $2}'
|
sed
-e
's/[[:space:]]//g'
-e
's/"//g'
-e "s/'
//g
") 2>
/dev/null
)
UploadTmpDir=$(
grep
"^[[:space:]]*upload_tmp_dir"
$AppIni 2>
/dev/null
|
awk
-F=
'{print $2}'
|
sed
-e
's/[[:space:]]//g'
-e
's/"//g'
-e "s/'
//g
")
grep
"^session.save_handler"
$AppIni 2>
/dev/null
|
grep
-q
"files"
[ $? -
eq
0 ] && SessionDir=$(
grep
"^[[:space:]]*session.save_path"
$AppIni 2>
/dev/null
|
awk
-F=
'{print $2}'
|
sed
-e
's/[[:space:]]//g'
-e
's/"//g'
-e "s/'
//g
")
CacheDir=$(
grep
"^[[:space:]]*eaccelerator.cache_dir"
$AppIni 2>
/dev/null
|
awk
-F=
'{print $2}'
|
sed
-e
's/[[:space:]]//g'
-e
's/"//g'
-e "s/'
//g
")
AppUser=${AppUser:-nobody}
AppGroup=${AppGroup:-nobody}
AppPidDir=${AppPidDir:=$AppDir
/var/run
}
AppErrorLogDir=${AppErrorLogDir:-$AppDir
/var/log
}
AppSlowLogDir=${AppSlowLogDir:-$AppDir
/var/log
}
RemoveFlag=0
InstallFlag=0
# 获取PID
fpid()
{
AppMasterPid=$(
ps
ax |
grep
"php-fpm: master process"
|
grep
-
v
"grep"
|
awk
'{print $1}'
2>
/dev/null
)
AppWorkerPid=$(
ps
ax |
grep
"php-fpm: pool"
|
grep
-
v
"grep"
|
awk
'{print $1}'
2>
/dev/null
)
}
# 查询状态
fstatus()
{
fpid
if
[ ! -f
"$AppProg"
];
then
echo
"$AppName 未安装"
else
echo
"$AppName 已安装"
if
[ -z
"$AppMasterPid"
];
then
echo
"$AppName 未启动"
else
echo
"$AppName 正在运行"
fi
fi
}
# 删除
fremove()
{
fpid
RemoveFlag=1
if
[ -z
"$AppMasterPid"
];
then
if
[ -d
"$AppDir"
];
then
rm
-rf $AppDir &&
echo
"删除 $AppName"
else
echo
"$AppName 未安装"
fi
else
echo
"$AppName 正在运行"
&&
exit
fi
}
# 备份
fbackup()
{
Day=$(
date
+%Y-%m-%d)
BackupFile=$App.$Day.tgz
if
[ -f
"$AppProg"
];
then
cd
$AppBase
tar
zcvf $BackupFile --exclude=var
/log/
* --exclude=var
/run/
* $App --backup=numbered
[ $? -
eq
0 ] &&
echo
"$AppName 备份成功"
||
echo
"$AppName 备份失败"
else
echo
"$AppName 未安装"
fi
}
# 安装
finstall()
{
fpid
InstallFlag=1
if
[ -z
"$AppMasterPid"
];
then
test
-f
"$AppProg"
&&
echo
"$AppName 已安装"
[ $? -
ne
0 ] && fupdate && fcpconf
else
echo
"$AppName 正在运行"
fi
}
# 拷贝配置
fcpconf()
{
cp
-vf --backup=numbered $ScriptDir
/php
.ini $AppIni
cp
-vf --backup=numbered $ScriptDir
/php-fpm
.conf $AppConf
}
# 更新
fupdate()
{
Operate=
"更新"
[ $InstallFlag -
eq
1 ] && Operate=
"安装"
[ $RemoveFlag -
ne
1 ] && fbackup
cd
$AppSrcBase
test
-d
"$AppSrcDir"
&&
rm
-rf $AppSrcDir
tar
Jxf $AppSrcFile ||
tar
jxf $AppSrcFile ||
tar
zxf $AppSrcFile
cd
$AppSrcDir
.
/configure
\
"--prefix=$AppDir"
\
"--disable-all"
\
"--enable-fpm"
\
"--enable-opcache"
\
"--enable-pdo"
\
"--enable-session"
\
"--with-pcre-dir"
\
"--with-pdo-mysql=mysqlnd"
[ $? -
eq
0 ] &&
make
&&
make
install
if
[ $? -
eq
0 ];
then
echo
"$AppName $Operate成功"
else
echo
"$AppName $Operate失败"
exit
1
fi
}
# 初始化
finit()
{
echo
"初始化 $AppName"
id
-gn $AppGroup &>
/dev/null
if
[ $? -
ne
0 ];
then
groupadd $AppGroup &&
echo
"新建 $AppName 运行组:$AppGroup"
else
echo
"$AppName 运行组:$AppGroup 已存在"
fi
id
-un $AppUser &>
/dev/null
if
[ $? -
ne
0 ];
then
useradd
-s
/bin/false
-g $AppGroup -M $AppUser
if
[ $? -
eq
0 ];
then
echo
"新建 $AppName 运行用户:$AppUser"
echo
"S0nGPhb693$"
|
passwd
--stdin $AppUser &>
/dev/null
fi
else
echo
"$AppName 运行用户:$AppUser 已存在"
fi
echo
$AppPidDir |
grep
-q
"^/"
if
[ $? -
eq
1 ];
then
AppPidDir=$AppDir
/var/
$AppPidDir
fi
if
[ ! -e
"$AppPidDir"
];
then
mkdir
-p $AppPidDir &&
echo
"新建 $AppName PID文件存放目录:$AppPidDir"
else
echo
"$AppName PID文件存放目录:$AppPidDir 已存在"
fi
echo
$AppErrorLogDir |
grep
-q
"^/"
if
[ $? -
eq
1 ];
then
AppErrorLogDir=$AppDir
/var/
$AppErrorLogDir
fi
if
[ ! -e
"$AppErrorLogDir"
];
then
mkdir
-p $AppErrorLogDir &&
echo
"新建 $AppName 错误日志目录:$AppErrorLogDir"
else
echo
"$AppErrorLogDir 错误日志目录:$AppErrorLogDir 已存在"
fi
echo
$AppSlowLogDir |
grep
-q
"^/"
if
[ $? -
eq
1 ];
then
AppSlowLogDir=$AppDir/$AppSlowLogDir
fi
if
[ ! -e
"$AppSlowLogDir"
];
then
mkdir
-p $AppSlowLogDir &&
echo
"新建 $AppName 慢日志目录:$AppSlowLogDir"
else
echo
"$AppSlowLogDir 慢日志目录:$AppSlowLogDir 已存在"
fi
printf
"\n"
if
[ -n
"$UploadTmpDir"
];
then
echo
$UploadTmpDir |
grep
-q
"^/"
if
[ $? -
eq
0 ];
then
if
[ ! -e
"$UploadTmpDir"
];
then
mkdir
-p $UploadTmpDir &&
echo
"新建 $AppName 文件上传临时存储目录:$UploadTmpDir"
else
echo
"$AppName 文件上传临时存储目录:$UploadTmpDir 已存在"
fi
chown
-R $AppUser:$AppGroup $UploadTmpDir &&
echo
"修改 $AppName 文件上传临时存储目录拥有者为 $AppUser,属组为 $AppGroup"
printf
"\n"
fi
fi
if
[ -n
"$SessionDir"
];
then
echo
$SessionDir |
grep
-q
"^/"
if
[ $? -
eq
0 ];
then
if
[ ! -e
"$SessionDir"
];
then
mkdir
-p $SessionDir &&
echo
"新建 $AppName 会话存储目录:$SessionDir"
else
echo
"$AppName 会话存储目录:$SessionDir 已存在"
fi
chown
-R $AppUser:$AppGroup $SessionDir &&
echo
"修改 $AppName 会话存储目录拥有者为 $AppUser,属组为 $AppGroup"
printf
"\n"
fi
fi
if
[ -n
"$CacheDir"
];
then
echo
$CacheDir |
grep
-q
"^/"
if
[ $? -
eq
0 ];
then
if
[ ! -e
"$CacheDir"
];
then
mkdir
-p $CacheDir &&
echo
"新建 eAccelerator 缓存目录:$CacheDir"
else
echo
"eAccelerator 缓存目录:$CacheDir 已存在"
fi
chown
-R $AppUser:$AppGroup $CacheDir &&
echo
"修改 eAccelerator 缓存目录拥有者为 $AppUser,属组为 $AppGroup"
fi
fi
sed
-i
"s|extension_dir.*$|extension_dir = \"$ExtensionDir\"|"
$AppIni
}
# 检查配置
ftest()
{
$AppProg -t &&
echo
"$AppName 配置正确"
||
echo
"$AppName 配置错误"
}
# 启动
fstart()
{
fpid
if
[ -n
"$AppMasterPid"
];
then
echo
"$AppName 正在运行"
else
$AppProg -c $AppIni &&
echo
"启动 $AppName"
||
echo
"$AppName 启动失败"
fi
}
# 停止
fstop()
{
fpid
if
[ -n
"$AppMasterPid"
];
then
kill
-INT $AppMasterPid &&
echo
"停止 $AppName"
||
echo
"$AppName 停止失败"
else
echo
"$AppName 未启动"
fi
}
# 重载配置
freload()
{
fpid
if
[ -n
"$AppMasterPid"
];
then
kill
-USR2 $AppMasterPid &&
echo
"重载 $AppName 配置"
||
echo
"$AppName 重载配置失败"
else
echo
"$AppName 未启动"
fi
}
# 重启
frestart()
{
fpid
[ -n
"$AppMasterPid"
] && fstop &&
sleep
1
fstart
}
# 终止进程
fkill()
{
fpid
if
[ -n
"$AppMasterPid"
];
then
echo
"$AppMasterPid"
|
xargs
kill
-9
if
[ $? -
eq
0 ];
then
echo
"终止 $AppName 主进程"
else
echo
"终止 $AppName 主进程失败"
fi
else
echo
"$AppName 主进程未运行"
fi
if
[ -n
"$AppWorkerPid"
];
then
echo
"$AppWorkerPid"
|
xargs
kill
-9
if
[ $? -
eq
0 ];
then
echo
"终止 $AppName 工作进程"
else
echo
"终止 $AppName 工作进程失败"
fi
else
echo
"$AppName 工作进程未运行"
fi
}
ScriptDir=$(
cd
$(
dirname
$0);
pwd
)
ScriptFile=$(
basename
$0)
case
"$1"
in
"install"
) finstall;;
"update"
) fupdate;;
"reinstall"
) fremove && finstall;;
"remove"
) fremove;;
"backup"
) fbackup;;
"init"
) finit;;
"start"
) fstart;;
"stop"
) fstop;;
"restart"
) frestart;;
"status"
) fstatus;;
"cpconf"
) fcpconf;;
"test"
) ftest;;
"reload"
) freload;;
"kill"
) fkill;;
* )
echo
"$ScriptFile install 安装 $AppName"
echo
"$ScriptFile update 更新 $AppName"
echo
"$ScriptFile reinstall 重装 $AppName"
echo
"$ScriptFile remove 删除 $AppName"
echo
"$ScriptFile backup 备份 $AppName"
echo
"$ScriptFile init 初始化 $AppName"
echo
"$ScriptFile start 启动 $AppName"
echo
"$ScriptFile stop 停止 $AppName"
echo
"$ScriptFile restart 重启 $AppName"
echo
"$ScriptFile status 查询 $AppName 状态"
echo
"$ScriptFile cpconf 拷贝 $AppName 配置"
echo
"$ScriptFile test 检查 $AppName 配置"
echo
"$ScriptFile reload 重载 $AppName 配置"
echo
"$ScriptFile kill 终止 $AppName 进程"
;;
esac
|
本文转自dongsong1117 51CTO博客,原文链接:http://blog.51cto.com/dongsong/1638464,如需转载请自行联系原作者