PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/soar/1.0/web/build.sh

http://arronwork.googlecode.com/
Shell | 512 lines | 382 code | 55 blank | 75 comment | 48 complexity | 8b220d63568ce3e33cdc0ea8c5d7ccd1 MD5 | raw file
  1. #!/bin/bash
  2. # vim: fdm=marker fmr=[[[,]]] cms=\ #%s#
  3. build_apache()
  4. { #[[[#
  5. local web_dir=$1
  6. local server_dir=${web_dir}/apache
  7. local pkg_dir=$2
  8. if [[ -e $server_dir ]]
  9. then
  10. if [[ ! -d $server_dir || ! -w $server_dir ]]
  11. then
  12. echo "Error: $server_dir not directory or writeable"
  13. return 1
  14. fi
  15. rm -fr $server_dir/*
  16. else
  17. mkdir $server_dir
  18. if (( $? != 0 ))
  19. then
  20. echo "Error: create $server_dir fail"
  21. return 1
  22. fi
  23. fi
  24. # local limit_tar=$pkg_dir/$(grep mod_limitipconn $pkg_dir/list)
  25. # local cronolog_tar=$pkg_dir/$(grep cronolog $pkg_dir/list)
  26. local httpd_tar=$pkg_dir/$(grep httpd $pkg_dir/list)
  27. local php_tar=$pkg_dir/$(grep php $pkg_dir/list)
  28. # if [[ ! -f "$limit_tar" || ! -f "$cronolog_tar" || \
  29. # ! -f "$httpd_tar" || ! -f "$php_tar" ]]
  30. if [[ ! -f "$httpd_tar" || ! -f "$php_tar" ]]
  31. then
  32. echo "Error: pkg in $pkg_dir not found"
  33. return 1
  34. fi
  35. mkdir -p _pkg
  36. cd _pkg # /_pkg
  37. rm -fr *
  38. #=== Check pkg ===#
  39. # Check httpd
  40. local httpd_dir
  41. httpd_dir=$(basename $httpd_tar)
  42. httpd_dir=${httpd_dir%.tar.gz}
  43. tar -zxf $httpd_tar
  44. echo "APACHE: $httpd_dir"
  45. # Check mod_limitipconn
  46. # local limit_dir
  47. # limit_dir=$(basename $limit_tar)
  48. # limit_dir=${limit_dir%.tar.gz}
  49. # tar -zxf $limit_tar
  50. # echo "MOD_LIMITIPCONN: $limit_dir"
  51. # Check cronolog
  52. # local cronolog_dir
  53. # cronolog_dir=$(basename $cronolog_tar)
  54. # cronolog_dir=${cronolog_dir%.tar.gz}
  55. # tar -zxf $cronolog_tar
  56. # echo "CRONOLOG: $cronolog_dir"
  57. # Check php
  58. local php_dir
  59. php_dir=$(basename $php_tar)
  60. php_dir=${php_dir%.tar.gz}
  61. tar -zxf $php_tar
  62. echo "PHP: $php_dir"
  63. #=== Compile apache ===#
  64. cd $httpd_dir # /_pkg/httpd_dir
  65. ./configure --silent --prefix=$server_dir --enable-so --enable-mods-shared=most --with-mpm=worker --enable-ssl --enable-rewrite
  66. # --with-expat=builtin
  67. if (( $? != 0))
  68. then
  69. echo "Error: apache configure fail"
  70. return 1
  71. fi
  72. make -s
  73. if (( $? != 0))
  74. then
  75. echo "Error: apache make fail"
  76. return 1
  77. fi
  78. make -s install
  79. if (( $? != 0))
  80. then
  81. echo "Error: apache install fail"
  82. return 1
  83. fi
  84. # ??limitipconn
  85. # cd ../$limit_dir # /_pkg/limit_dir
  86. # PATH=$PATH:$server_dir/bin
  87. # make -s
  88. # make -s install
  89. # ??cronolog
  90. # cd ../$cronolog_dir # /_pkg/cronolog_dir
  91. # ./configure --silent
  92. # make -s
  93. # cp src/cronolog $server_dir/bin
  94. # ?? httpd.conf
  95. awk '
  96. /^ *ServerAdmin/ {
  97. printf("ServerAdmin webmaster@site.com\n");
  98. next;
  99. }
  100. /^ *# *ServerName/ {
  101. printf("ServerName site.com\n");
  102. next;
  103. }
  104. /^ *DocumentRoot/ {
  105. printf("DocumentRoot \"%s/htdocs\"\n", web_dir);
  106. next;
  107. }
  108. /^ *<Directory.*htdocs.*>/ {
  109. printf("<Directory \"%s/htdocs\">\n", web_dir);
  110. next;
  111. }
  112. / *DirectoryIndex index\.html/ {
  113. printf("DirectoryIndex index.html index.htm index.php\n");
  114. next;
  115. }
  116. / *AddType application\/x-gzip/ {
  117. print;
  118. printf(" AddType application/x-httpd-php .php .inc .api\n");
  119. next;
  120. }
  121. #/^ *ErrorLog/ {
  122. # printf("ErrorLog \"|%s/bin/cronolog %s/logs/errorlog/%%Y-%%m-%%d-error.log\"\n", server_dir, server_dir);
  123. # next;
  124. #}
  125. #/^ *CustomLog/ {
  126. # printf("CustomLog \"|%s/bin/cronolog %s/logs/customlog/%%Y-%%m-%%d-access.log\" combined\n", server_dir, server_dir);
  127. # next;
  128. #}
  129. /^ *#Include conf\/extra\/httpd-mpm\.conf/ {
  130. printf("Include conf/extra/httpd-mpm.conf\n");
  131. next;
  132. }
  133. /^ *#Include conf\/extra\/httpd-default\.conf/ {
  134. printf("Include conf/extra/httpd-default.conf\n");
  135. next;
  136. }
  137. #/^ *#Include conf\/extra\/httpd-ssl\.conf/ {
  138. # printf("Include conf/extra/httpd-ssl.conf\n");
  139. # next;
  140. #}
  141. {
  142. gsub("Indexes", "");
  143. print;
  144. }
  145. ' web_dir=$web_dir server_dir=$server_dir $server_dir/conf/httpd.conf >httpd.conf.tmp
  146. echo "
  147. AddDefaultCharset UTF-8
  148. #<IfModule mod_limitipconn>
  149. #<Location />
  150. # MaxConnPerIP 5
  151. #</Location>
  152. #</IfModule>
  153. " >>httpd.conf.tmp
  154. mv -f httpd.conf.tmp $server_dir/conf/httpd.conf
  155. # extra/httpd-default.conf
  156. awk '
  157. /^ *Timeout / {
  158. printf("Timeout 60\n");
  159. next;
  160. }
  161. #/^ *KeepAlive / {
  162. # printf("KeepAlive Off\n");
  163. # next;
  164. #}
  165. { print; }
  166. ' $server_dir/conf/extra/httpd-default.conf >httpd-default.conf.tmp
  167. mv -f httpd-default.conf.tmp $server_dir/conf/extra/httpd-default.conf
  168. # extra/httpd-autoindex.conf
  169. # awk '
  170. #{
  171. # gsub("Indexes", "");
  172. # print;
  173. #}
  174. #' $server_dir/conf/extra/httpd-autoindex.conf >httpd-autoindex.conf.tmp
  175. # mv -f httpd-autoindex.conf.tmp $server_dir/conf/extra/httpd-autoindex.conf
  176. # extra/httpd-ssl.conf
  177. awk '
  178. /^ *ServerName/ {
  179. printf("ServerName site.com\n");
  180. next;
  181. }
  182. /^ *ServerAdmin/ {
  183. printf("ServerAdmin webmaster@site.com\n");
  184. next;
  185. }
  186. /^ *DocumentRoot/ {
  187. printf("DocumentRoot \"%s/htdocs\"\n", web_dir);
  188. next;
  189. }
  190. /^ *SSLCertificateFile/ {
  191. printf("SSLCertificateFile %s/conf/ssl.crt/site.crt\n", server_dir);
  192. next;
  193. }
  194. /^ *SSLCertificateKeyFile/ {
  195. printf("SSLCertificateKeyFile %s/conf/ssl.key/site.key\n", server_dir);
  196. next;
  197. }
  198. { print; }
  199. ' web_dir=$web_dir server_dir=$server_dir $server_dir/conf/extra/httpd-ssl.conf > httpd-ssl.conf.tmp
  200. mv -f httpd-ssl.conf.tmp $server_dir/conf/extra/httpd-ssl.conf
  201. #=== ??PHP ===#
  202. cd ../$php_dir # /_pkg/php_dir
  203. ./buildconf --force
  204. local libdir
  205. case $CPU in
  206. i386|i686) libdir=lib ;;
  207. x86_64) libdir=lib64 ;;
  208. esac
  209. ./configure --silent --with-libdir=$libdir --prefix=$server_dir/php --with-config-file-path=$server_dir/conf --disable-debug --with-apxs2=$server_dir/bin/apxs --enable-cli --enable-sockets --enable-sysvmsg --enable-soap --enable-mbstring --with-zlib --with-openssl --with-iconv --with-mysql --without-sqlite --enable-gd-native-ttf --with-gd --with-png-dir --with-jpeg-dir --with-ttf --with-freetype-dir
  210. if (( $? != 0 ))
  211. then
  212. echo "Error: PHP configure fail"
  213. return 1
  214. fi
  215. make -s clean
  216. make -s
  217. if (( $? != 0 ))
  218. then
  219. echo "Error: PHP make fail"
  220. return 1
  221. fi
  222. make -s install
  223. if (( $? != 0 ))
  224. then
  225. echo "Error: PHP install fail"
  226. return 1
  227. fi
  228. # ??php.ini
  229. awk '
  230. /^ display_errors *=/ {
  231. printf("display_errors = Off\n");
  232. next;
  233. }
  234. /log_errors *=/ {
  235. printf("log_errors = On\n");
  236. next;
  237. }
  238. /error_log *= *filename/ {
  239. printf("error_log = %s/logs/php.error.log\n", server_dir);
  240. next;
  241. }
  242. { print; }
  243. ' server_dir=$server_dir php.ini-dist > $server_dir/conf/php.ini
  244. cd .. # /_pkg
  245. cd .. # /
  246. #=== Configure ===#
  247. cd apache # /apache
  248. # Append to php.ini
  249. cat php.ini >>$server_dir/conf/php.ini
  250. # svrctl
  251. cp svrctl_tmpl.sh $server_dir/bin/svrctl
  252. chmod 755 $server_dir/bin/svrctl
  253. # site crt & key
  254. mkdir -p $server_dir/conf/ssl.crt
  255. cp site.crt $server_dir/conf/ssl.crt
  256. mkdir -p $server_dir/conf/ssl.key
  257. cp site.key $server_dir/conf/ssl.key
  258. chmod 777 $server_dir/logs
  259. mkdir $server_dir/logs/errorlog $server_dir/logs/customlog
  260. chmod 777 $server_dir/logs/*
  261. cd .. # /
  262. } #]]]#
  263. build_nginx()
  264. { #[[[#
  265. local web_dir=$1
  266. local server_dir=${web_dir}/nginx
  267. local pkg_dir=$2
  268. if [[ -e $server_dir ]]
  269. then
  270. if [[ ! -d $server_dir || ! -w $server_dir ]]
  271. then
  272. echo "Error: $server_dir not directory or writeable"
  273. return 1
  274. fi
  275. rm -fr $server_dir/*
  276. else
  277. mkdir $server_dir
  278. if (( $? != 0 ))
  279. then
  280. echo "Error: create $server_dir fail"
  281. return 1
  282. fi
  283. fi
  284. local nginx_tar=$pkg_dir/$(grep nginx $pkg_dir/list)
  285. local php_tar=$pkg_dir/$(grep php $pkg_dir/list)
  286. if [[ ! -f "$nginx_tar" || ! -f "$php_tar" ]]
  287. then
  288. echo "Error: pkg in $pkg_dir not found"
  289. return 1
  290. fi
  291. mkdir -p _pkg
  292. cd _pkg # /_pkg
  293. rm -fr *
  294. #=== Check pkg ===#
  295. # Check nginx
  296. local nginx_dir
  297. nginx_dir=$(basename $nginx_tar)
  298. nginx_dir=${nginx_dir%.tar.gz}
  299. tar -zxf $nginx_tar
  300. echo "nginx: $nginx_dir"
  301. # Check php
  302. local php_dir
  303. php_dir=$(basename $php_tar)
  304. php_dir=${php_dir%.tar.gz}
  305. tar -zxf $php_tar
  306. echo "php: $php_dir"
  307. #=== Compile nginx ===#
  308. cd $nginx_dir # /_pkg/nginx_dir
  309. ./configure --prefix=$server_dir --with-http_stub_status_module --with-http_ssl_module
  310. if (( $? != 0))
  311. then
  312. echo "Error: nginx configure fail"
  313. return 1
  314. fi
  315. make -s
  316. if (( $? != 0))
  317. then
  318. echo "Error: nginx make fail"
  319. return 1
  320. fi
  321. make -s install
  322. if (( $? != 0))
  323. then
  324. echo "Error: nginx install fail"
  325. return 1
  326. fi
  327. cd .. # /_pkg
  328. #=== Compile PHP ===#
  329. cd $php_dir # /_pkg/php_dir
  330. ./buildconf --force
  331. local libdir
  332. case $CPU in
  333. i386|i686) libdir=lib ;;
  334. x86_64) libdir=lib64 ;;
  335. esac
  336. ./configure --silent --with-libdir=$libdir --prefix=$server_dir/php --with-config-file-path=$server_dir/conf --disable-debug --enable-fastcgi --enable-cli --enable-sockets --enable-soap --enable-mbstring --with-zlib --with-openssl --with-iconv --with-mysql --without-sqlite --enable-gd-native-ttf --with-gd --with-png-dir --with-jpeg-dir --with-ttf --with-freetype-dir
  337. if (( $? != 0 ))
  338. then
  339. echo "Error: PHP configure fail"
  340. return 1
  341. fi
  342. make -s clean
  343. make -s
  344. if (( $? != 0 ))
  345. then
  346. echo "Error: PHP make fail"
  347. return 1
  348. fi
  349. make -s install
  350. if (( $? != 0 ))
  351. then
  352. echo "Error: PHP install fail"
  353. return 1
  354. fi
  355. # ??php.ini
  356. awk '
  357. /^ display_errors *=/ {
  358. printf("display_errors = Off\n");
  359. next;
  360. }
  361. /log_errors *=/ {
  362. printf("log_errors = On\n");
  363. next;
  364. }
  365. /error_log *= *filename/ {
  366. printf("error_log = %s/logs/php.error.log\n", server_dir);
  367. next;
  368. }
  369. { print; }
  370. ' server_dir=$server_dir php.ini-dist > $server_dir/conf/php.ini
  371. cd .. # /_pkg
  372. cd .. # /
  373. #=== Configure ===#
  374. cd nginx # /nginx
  375. # nginx.conf
  376. cp -f nginx.conf $server_dir/conf
  377. # Append to php.ini
  378. cat php.ini >>$server_dir/conf/php.ini
  379. # svrctl
  380. mkdir $server_dir/bin
  381. cp svrctl_tmpl.sh $server_dir/bin/svrctl
  382. chmod 755 $server_dir/bin/svrctl
  383. # # site crt & key
  384. # mkdir -p $server_dir/conf/ssl.crt
  385. # cp site.crt $server_dir/conf/ssl.crt
  386. # mkdir -p $server_dir/conf/ssl.key
  387. # cp site.key $server_dir/conf/ssl.key
  388. chmod 777 $server_dir/logs
  389. # mkdir $server_dir/logs/errorlog $server_dir/logs/customlog
  390. # chmod 777 $server_dir/logs/*
  391. cd .. # /
  392. } #]]]#
  393. copy_program()
  394. { #[[[#
  395. local web_dir=$1
  396. cd phpinc # /phpinc
  397. mkdir -p $web_dir/phpinc/soar
  398. cp -f soar/*.inc $web_dir/phpinc/soar
  399. cd .. # /
  400. cd htdocs/test # /htdocs/test
  401. mkdir -p $web_dir/htdocs/test
  402. cp -f *.php $web_dir/htdocs/test
  403. cd ../.. # /
  404. } #]]]#
  405. main()
  406. { #[[[#
  407. if [[ ! -f "${PWD}/$(basename $0)" ]]
  408. then
  409. echo
  410. echo "Error: must execute under pwd of $(basename $0)"
  411. echo
  412. return 1
  413. fi
  414. local web_dir=/opt/soar/web
  415. local pkg_dir=${PWD}/../build/pkg
  416. if [[ -e $web_dir ]]
  417. then
  418. if [[ ! -d $web_dir || ! -w $web_dir ]]
  419. then
  420. echo "Error: $web_dir not directory or writeable"
  421. return 1
  422. fi
  423. else
  424. mkdir $web_dir
  425. if (( $? != 0 ))
  426. then
  427. echo "Error: create $web_dir fail"
  428. return 1
  429. fi
  430. fi
  431. # apache + php shared module
  432. build_apache "$web_dir" "$pkg_dir"
  433. if (( $? != 0 ))
  434. then
  435. echo
  436. echo "Error: compile apache fail"
  437. echo
  438. return 1
  439. fi
  440. # nginx + php fast-cgi
  441. build_nginx "$web_dir" "$pkg_dir"
  442. if (( $? != 0 ))
  443. then
  444. echo
  445. echo "Error: compile nginx fail"
  446. echo
  447. return 1
  448. fi
  449. # program
  450. copy_program "$web_dir"
  451. echo
  452. echo "Succeed"
  453. echo
  454. } #]]]#
  455. CPU=$(uname -m | tr '[:upper:]' '[:lower:]')
  456. main
  457. exit $?