Repository navigation
Expand file tree
/
Copy pathcppcms_run
More file actions
executable file
·513 lines (428 loc) · 12.6 KB
/
Copy pathcppcms_run
File metadata and controls
executable file
·513 lines (428 loc) · 12.6 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#!/usr/bin/env bash
locate_server()
{
NAME=$1
if [ "$WEB_SERVER" != "" ] ; then
return
fi
WEB_SERVER="`which $NAME 2>/dev/null`"
if [ "$WEB_SERVER" != "" ] ; then
return
fi
for p in /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin \
/sbin /bin /opt/sbin /opt/bin /usr/$NAME/bin /usr/$NAME/sbin /usr/local/$NAME/sbin /usr/local/$NAME/bin ;
do
if [ -x "$p/$NAME" ] ; then
WEB_SERVER="$p/$NAME"
return
elif [ -x "$p/$NAME.exe" ] ; then
WEB_SERVER="$p/$NAME.exe"
return
fi
done
if [ "$2" == "exit" ] ; then
echo "Failed to locate server $NAME, exitting"
echo "Try providing PATH valiable"
exit 1
fi
}
lighttpd() {
locate_server lighttpd $1
[ "$WEB_SERVER" == "" ] && return
CONFIG_FILE=$DIR/lighty.conf
case $API in
fastcgi) ;;
scgi) ;;
*)
if [ "$1" == "exit" ]; then
echo "Mod $API is not supported by lightppd" ; exit 1
else
WEB_SERVER=""
return
fi
;;
esac
cat > $CONFIG_FILE << EOF
server.modules = ("mod_$API")
server.document-root = "$ROOT"
server.pid-file = "$PID_FILE"
# mimetype mapping
mimetype.assign = (
".pdf" => "application/pdf",
".sig" => "application/pgp-signature",
".spl" => "application/futuresplash",
".class" => "application/octet-stream",
".ps" => "application/postscript",
".torrent" => "application/x-bittorrent",
".dvi" => "application/x-dvi",
".gz" => "application/x-gzip",
".pac" => "application/x-ns-proxy-autoconfig",
".swf" => "application/x-shockwave-flash",
".tar.gz" => "application/x-tgz",
".tgz" => "application/x-tgz",
".tar" => "application/x-tar",
".zip" => "application/zip",
".mp3" => "audio/mpeg",
".m3u" => "audio/x-mpegurl",
".wma" => "audio/x-ms-wma",
".wax" => "audio/x-ms-wax",
".ogg" => "application/ogg",
".wav" => "audio/x-wav",
".gif" => "image/gif",
".jpg" => "image/jpeg",
".jpeg" => "image/jpeg",
".png" => "image/png",
".xbm" => "image/x-xbitmap",
".xpm" => "image/x-xpixmap",
".xwd" => "image/x-xwindowdump",
".css" => "text/css",
".html" => "text/html",
".htm" => "text/html",
".js" => "text/javascript",
".asc" => "text/plain",
".c" => "text/plain",
".cpp" => "text/plain",
".log" => "text/plain",
".conf" => "text/plain",
".text" => "text/plain",
".txt" => "text/plain",
".dtd" => "text/xml",
".xml" => "text/xml",
".mpeg" => "video/mpeg",
".mpg" => "video/mpeg",
".mov" => "video/quicktime",
".qt" => "video/quicktime",
".avi" => "video/x-msvideo",
".asf" => "video/x-ms-asf",
".asx" => "video/x-ms-asf",
".wmv" => "video/x-ms-wmv",
".bz2" => "application/x-bzip",
".tbz" => "application/x-bzip-compressed-tar",
".tar.bz2" => "application/x-bzip-compressed-tar",
# default mime type
"" => "application/octet-stream",
)
server.port = $PORT
server.bind = "$HOST"
$API.server = ( "$SCRIPT" => ( "localhost" => (
"check-local" => "disable",
"socket" => "$SOCKET"
)))
EOF
WEB_SERVER="$WEB_SERVER -f $CONFIG_FILE"
}
nginx()
{
locate_server nginx $1
[ "$WEB_SERVER" == "" ] && return
if [ "$API" != "fastcgi" ] ; then
if [ "$1" == "exit" ] ; then
echo "Nginx supports fastcgi only"
exit 1
else
WEB_SERVER=""
return
fi
fi
CONFIG_FILE=$DIR/cppcms-nginx.conf
# This is fix for cygwin specific issue
# of select
case `uname` in
*CYGWIN*) EVENT="use poll;" ;;
esac
cat >$CONFIG_FILE <<EOF
worker_processes 1;
pid $PID_FILE;
error_log $DIR/nginx.log;
events {
worker_connections 1024;
$EVENT
}
http {
access_log off;
types {
text/html html htm shtml;
text/css css;
text/xml xml rss;
image/gif gif;
image/jpeg jpeg jpg;
application/x-javascript js;
text/plain txt;
text/x-component htc;
text/mathml mml;
image/png png;
image/x-icon ico;
image/x-jng jng;
image/vnd.wap.wbmp wbmp;
application/pdf pdf;
application/zip zip;
audio/mpeg mp3;
audio/x-realaudio ra;
video/mpeg mpeg mpg;
video/quicktime mov;
video/x-flv flv;
video/x-msvideo avi;
video/x-ms-wmv wmv;
video/x-ms-asf asx asf;
video/x-mng mng;
}
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
tcp_nodelay on;
gzip off;
server {
listen $HOST:$PORT;
server_name localhost;
root $ROOT;
location / {
index index.html index.htm;
}
set \$path_info "";
if ( \$fastcgi_script_name ~ ^$SCRIPT(.*)\$ ) {
set \$path_info \$1;
}
location ~ ^$SCRIPT.*$ {
fastcgi_pass unix:$SOCKET;
fastcgi_param QUERY_STRING \$query_string;
fastcgi_param REQUEST_METHOD \$request_method;
fastcgi_param CONTENT_TYPE \$content_type;
fastcgi_param CONTENT_LENGTH \$content_length;
fastcgi_param SCRIPT_NAME $SCRIPT;
fastcgi_param PATH_INFO \$path_info;
fastcgi_param REQUEST_URI \$request_uri;
fastcgi_param DOCUMENT_URI \$document_uri;
fastcgi_param DOCUMENT_ROOT \$document_root;
fastcgi_param SERVER_PROTOCOL \$server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param REMOTE_ADDR \$remote_addr;
fastcgi_param REMOTE_PORT \$remote_port;
fastcgi_param SERVER_ADDR \$server_addr;
fastcgi_param SERVER_PORT \$server_port;
fastcgi_param SERVER_NAME \$server_name;
}
}
}
EOF
WEB_SERVER="$WEB_SERVER -c $CONFIG_FILE"
}
apache()
{
locate_server apache2
locate_server httpd2
locate_server apache
locate_server httpd $1
[ "$WEB_SERVER" == "" ] && return
if [ "$APACHE_MOD_DIR" == "" ] ; then
for p in /usr/lib/apache2/modules \
/usr/local/lib/apache2/modules \
/opt/lib/apache2/modules \
/usr/lib/httpd2/modules \
/usr/local/lib/httpd2/modules \
/opt/lib/httpd2/modules \
/usr/lib/apache/modules \
/usr/local/lib/apache/modules \
/opt/lib/apache/modules \
/usr/lib/httpd/modules \
/usr/local/lib/httpd/modules \
/opt/lib/httpd/modules
do
if [ -f $p/mod_fastcgi.* ] ; then
APACHE_MOD_DIR="$p"
break;
fi
done
fi
if [ "$APACHE_MOD_DIR" == "" ] ; then
if [ "$1" == "exit" ] ; then
echo "Can't find "mod_fastcgi" try specifing APACHE_MOD_DIR"
exit 1;
else
WEB_SERVER=""
return
fi
fi
if [ "$API" != "fastcgi" ] ; then
if [ "$1" == "exit" ] ; then
echo "Apache2 supports fastcgi only"
exit 1
else
WEB_SERVER=""
return
fi
fi
cat >$DIR/mime.types <<EOF
text/html html htm shtml
text/css css
text/xml xml rss
image/gif gif
image/jpeg jpeg jpg
application/x-javascript js
text/plain txt
text/x-component htc
text/mathml mml
image/png png
image/x-icon ico
image/x-jng jng
image/vnd.wap.wbmp wbmp
application/pdf pdf
application/zip zip
audio/mpeg mp3
audio/x-realaudio ra
video/mpeg mpeg mpg
video/quicktime mov
video/x-flv flv
video/x-msvideo avi
video/x-ms-wmv wmv
video/x-ms-asf asx asf
video/x-mng mng
EOF
cat >$DIR/apache.conf << EOF
Listen $HOST:$PORT
PidFile $PID_FILE
ErrorLog $DIR/apache.log
TypesConfig $DIR/mime.types
LoadModule alias_module $APACHE_MOD_DIR/mod_alias.so
LoadModule fastcgi_module $APACHE_MOD_DIR/mod_fastcgi.so
LoadModule mime_module $APACHE_MOD_DIR/mod_mime.so
FastCgiIpcDir $DIR/ipc
FastCgiExternalServer $DIR/myapp.fcgi -socket $SOCKET
ScriptAliasMatch ^$SCRIPT(.*)$ $DIR/myapp.fcgi\$1
AddHandler fastcgi-script .fcgi
<VirtualHost *>
DocumentRoot $ROOT
</VirtualHost>
EOF
WEB_SERVER="$WEB_SERVER -f $DIR/apache.conf"
}
find_param()
{
perl -e "while(<>) { print \"\$1\" if /^\s*$2\s*=\s*\"([^\"]+)\"\s*(#.*)?/}" <$1
}
help()
{
echo "Usage cppcms_run [-e] [ -S server ] [-p port] [-h host] \\"
echo " [-r /document/root ] [-s /script ]"
echo " program -c config.txt [ additional parameters ]"
echo " -c configuration file of cppcms executable"
echo " -p port to start the server on, default 8080"
echo " -s fastcgi script name, default '/'+your program name"
echo " -h host to bind, default 127.0.0.1 "
echo " -r document root (default .)"
echo " -S (lighttpd|nginx|apache2) - web server you want to run"
echo " -e Do not start application, started externaly (for debugging)"
exit 1
}
## Inital configuration
# Try to find what sever exists
# Default parameters
HOST=127.0.0.1
PORT=8080
ROOT=`pwd`
while ! [ -e "$1" ] ; do
if [ "$1" == "-s" ] ; then
SCRIPT="$2"
shift
elif [ "$1" == "-S" ] ; then
TRY_SETUP="$2"
shift
elif [ "$1" == "-h" ]; then
HOST="$2"
shift
elif [ "$1" == "-p" ]; then
PORT="$2";
shift
elif [ "$1" == "-e" ]; then
NO_APP=yes
elif [ "$1" == "-r" ]; then
ROOT="$2"
shift
else
help
fi
shift
done
PROGRAM="$1"
if [ "$2" != "-c" ] || [ "$3" == "" ] ; then
help
fi
APP_CONFIG_FILE="$3"
if [ "$SCRIPT" == "" ] ; then
SCRIPT="/$PROGRAM"
fi
# Setup runtime parameters
SOCKET=`find_param $APP_CONFIG_FILE server.socket`
API=`find_param $APP_CONFIG_FILE server.api`
if [ "$SOCKET" == "" ] || [ "$API" == "" ] ; then
echo "Can't find server.api and server.socket if $APP_CONFIG_FILE"
echo "Aborting"
exit 1
fi
DIR="`pwd`/cppcms_rundir"
PID_FILE="$DIR/srv.pid"
mkdir -p $DIR
echo "Web Server Host:$HOST"
echo "Web Server Port:$PORT"
echo "Document Root:$ROOT"
echo "Script:$SCRIPT"
echo "Api:$API"
echo "Socket:$SOCKET"
# Configure web server
if [ "$TRY_SETUP" != "" ]; then
case "$TRY_SETUP" in
lighttpd) lighttpd exit ;;
nginx) nginx exit ;;
apache2) apache exit ;;
apache) apache exit ;;
*) help
esac
else
lighttpd
[ "$WEB_SERVER" == "" ] && nginx
[ "$WEB_SERVER" == "" ] && apache
if [ "$WEB_SERVER" == "" ]; then
echo "Can't find sutiable web server (nginx/lighttpd/apache)"
echo "You may try to change PATH variable. In case of Apache2"
echo "You may want to define APACHE_MOD_DIR variable"
exit 1
fi
fi
echo "Server:$WEB_SERVER"
if [ "$NO_APP" != "yes" ]; then
echo "Starting Application Server:/$PROGRAM"
shift
./$PROGRAM $* &
FASTCGI_SERVER=$!
echo "Application Server PID:$FASTCGI_SERVER"
fi
echo "Starting Web Server"
$WEB_SERVER
sleep 1
if ! [ -f "$PID_FILE" ]; then
echo "No web server pid... sleeping 2 more seconds"
sleep 2
fi
if [ -f "$PID_FILE" ] ; then
echo "Web Server PID:`cat $PID_FILE`"
trap "echo peress any key to stop" SIGHUP SIGINT SIGTERM
echo
echo "------------------------------------"
echo "- Press Any Key To Stop The Server -"
echo "------------------------------------"
read
else
echo "Seems that web server had not started"
if [ "$NO_APP" != "yes" ]; then
echo "Stopping FastCGI application"
kill $FASTCGI_SERVER
fi
echo "Aborting"
exit 1
fi
if [ "$NO_APP" != "yes" ]; then
echo Killing Application pid=$FASTCGI_SERVER
kill $FASTCGI_SERVER
fi
echo Killing Web Server pid=`cat $PID_FILE`
kill `cat $PID_FILE`