vhost.sh 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # http://oneinstack.com
  9. # https://github.com/lj2007331/oneinstack
  10. export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  11. clear
  12. printf "
  13. #######################################################################
  14. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  15. # For more information please visit http://oneinstack.com #
  16. #######################################################################
  17. "
  18. . ./options.conf
  19. . ./include/color.sh
  20. . ./include/check_web.sh
  21. . ./include/get_char.sh
  22. # Check if user is root
  23. [ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
  24. Usage() {
  25. printf "
  26. Usage: $0 [ ${CMSG}add${CEND} | ${CMSG}del${CEND} ]
  27. ${CMSG}add${CEND} --->Add Virtualhost
  28. ${CMSG}del${CEND} --->Delete Virtualhost
  29. "
  30. }
  31. Choose_env()
  32. {
  33. if [ -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
  34. Number=111
  35. while :
  36. do
  37. echo
  38. echo 'Please choose to use environment:'
  39. echo -e "\t${CMSG}1${CEND}. Use php"
  40. echo -e "\t${CMSG}2${CEND}. Use java"
  41. echo -e "\t${CMSG}3${CEND}. Use hhvm"
  42. read -p "Please input a number:(Default 1 press Enter) " Choose_number
  43. [ -z "$Choose_number" ] && Choose_number=1
  44. if [ $Choose_number != 1 -a $Choose_number != 2 -a $Choose_number != 3 ];then
  45. echo "${CWARNING}input error! Please only input number 1,2,3${CEND}"
  46. else
  47. break
  48. fi
  49. done
  50. [ "$Choose_number" == '1' ] && NGX_FLAG=php
  51. [ "$Choose_number" == '2' ] && NGX_FLAG=java
  52. [ "$Choose_number" == '3' ] && NGX_FLAG=hhvm
  53. elif [ -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a ! -e "/usr/bin/hhvm" ];then
  54. Number=110
  55. while :
  56. do
  57. echo
  58. echo 'Please choose to use environment:'
  59. echo -e "\t${CMSG}1${CEND}. Use php"
  60. echo -e "\t${CMSG}2${CEND}. Use java"
  61. read -p "Please input a number:(Default 1 press Enter) " Choose_number
  62. [ -z "$Choose_number" ] && Choose_number=1
  63. if [ $Choose_number != 1 -a $Choose_number != 2 ];then
  64. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  65. else
  66. break
  67. fi
  68. done
  69. [ "$Choose_number" == '1' ] && NGX_FLAG=php
  70. [ "$Choose_number" == '2' ] && NGX_FLAG=java
  71. elif [ -e "$php_install_dir/bin/phpize" -a ! -e "$tomcat_install_dir/conf/server.xml" -a ! -e "/usr/bin/hhvm" ];then
  72. Number=100
  73. NGX_FLAG=php
  74. elif [ -e "$php_install_dir/bin/phpize" -a ! -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
  75. Number=101
  76. while :
  77. do
  78. echo
  79. echo 'Please choose to use environment:'
  80. echo -e "\t${CMSG}1${CEND}. Use php"
  81. echo -e "\t${CMSG}2${CEND}. Use hhvm"
  82. read -p "Please input a number:(Default 1 press Enter) " Choose_number
  83. [ -z "$Choose_number" ] && Choose_number=1
  84. if [ $Choose_number != 1 -a $Choose_number != 2 ];then
  85. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  86. else
  87. break
  88. fi
  89. done
  90. [ "$Choose_number" == '1' ] && NGX_FLAG=php
  91. [ "$Choose_number" == '2' ] && NGX_FLAG=hhvm
  92. elif [ ! -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
  93. Number=011
  94. while :
  95. do
  96. echo
  97. echo 'Please choose to use environment:'
  98. echo -e "\t${CMSG}1${CEND}. Use java"
  99. echo -e "\t${CMSG}2${CEND}. Use hhvm"
  100. read -p "Please input a number:(Default 1 press Enter) " Choose_number
  101. [ -z "$Choose_number" ] && Choose_number=1
  102. if [ $Choose_number != 1 -a $Choose_number != 2 ];then
  103. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  104. else
  105. break
  106. fi
  107. done
  108. [ "$Choose_number" == '1' ] && NGX_FLAG=java
  109. [ "$Choose_number" == '2' ] && NGX_FLAG=hhvm
  110. elif [ ! -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a ! -e "/usr/bin/hhvm" ];then
  111. Number=010
  112. NGX_FLAG=java
  113. elif [ ! -e "$php_install_dir/bin/phpize" -a ! -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
  114. Number=001
  115. NGX_FLAG=hhvm
  116. else
  117. Number=000
  118. exit
  119. fi
  120. if [ "$NGX_FLAG" == 'php' ];then
  121. NGX_CONF=$(echo -e "location ~ .*\.(php|php5)?$ {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php-cgi.sock;\n fastcgi_index index.php;\n include fastcgi.conf;\n }")
  122. elif [ "$NGX_FLAG" == 'java' ];then
  123. NGX_CONF=$(echo -e "location ~ {\n proxy_pass http://127.0.0.1:8080;\n include proxy.conf;\n }")
  124. elif [ "$NGX_FLAG" == 'hhvm' ];then
  125. NGX_CONF=$(echo -e "location ~ .*\.(php|php5)?$ {\n fastcgi_pass unix:/var/log/hhvm/sock;\n fastcgi_index index.php;\n fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n include fastcgi_params;\n }")
  126. fi
  127. }
  128. Input_Add_domain()
  129. {
  130. while :
  131. do
  132. echo
  133. read -p "Please input domain(example: www.linuxeye.com): " domain
  134. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  135. echo "${CWARNING}input error! ${CEND}"
  136. else
  137. break
  138. fi
  139. done
  140. if [ -e "$web_install_dir/conf/vhost/$domain.conf" -o -e "$apache_install_dir/conf/vhost/$domain.conf" ]; then
  141. [ -e "$web_install_dir/conf/vhost/$domain.conf" ] && echo -e "$domain in the Nginx/Tengine already exist! \nYou can delete \033[32m$web_install_dir/conf/vhost/$domain.conf\033[0m and re-create"
  142. [ -e "$apache_install_dir/conf/vhost/$domain.conf" ] && echo -e "$domain in the Apache already exist! \nYou can delete \033[32m$apache_install_dir/conf/vhost/$domain.conf\033[0m and re-create"
  143. exit
  144. else
  145. echo "domain=$domain"
  146. fi
  147. while :
  148. do
  149. echo
  150. read -p "Do you want to add more domain name? [y/n]: " moredomainame_yn
  151. if [ "$moredomainame_yn" != 'y' ] && [ "$moredomainame_yn" != 'n' ];then
  152. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  153. else
  154. break
  155. fi
  156. done
  157. if [ "$moredomainame_yn" == 'y' ]; then
  158. while :
  159. do
  160. echo
  161. read -p "Type domainname,example(linuxeye.com www.example.com): " moredomain
  162. if [ -z "`echo $moredomain | grep '.*\..*'`" ]; then
  163. echo "${CWARNING}input error! ${CEND}"
  164. else
  165. [ "$moredomain" == "$domain" ] && echo "${CWARNING}Domain name already exists! ${CND}" && continue
  166. echo domain list="$moredomain"
  167. moredomainame=" $moredomain"
  168. break
  169. fi
  170. done
  171. Domain_alias=ServerAlias$moredomainame
  172. fi
  173. echo
  174. echo "Please input the directory for the domain:$domain :"
  175. read -p "(Default directory: $wwwroot_dir/$domain): " vhostdir
  176. if [ -z "$vhostdir" ]; then
  177. vhostdir="$wwwroot_dir/$domain"
  178. echo "Virtual Host Directory=${CMSG}$vhostdir${CEND}"
  179. fi
  180. echo
  181. echo "Create Virtul Host directory......"
  182. mkdir -p $vhostdir
  183. echo "set permissions of Virtual Host directory......"
  184. chown -R ${run_user}.$run_user $vhostdir
  185. }
  186. Nginx_anti_hotlinking()
  187. {
  188. while :
  189. do
  190. echo
  191. read -p "Do you want to add hotlink protection? [y/n]: " anti_hotlinking_yn
  192. if [ "$anti_hotlinking_yn" != 'y' ] && [ "$anti_hotlinking_yn" != 'n' ];then
  193. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  194. else
  195. break
  196. fi
  197. done
  198. if [ -n "`echo $domain | grep '.*\..*\..*'`" ];then
  199. domain_allow="*.${domain#*.} $domain"
  200. else
  201. domain_allow="*.$domain $domain"
  202. fi
  203. if [ "$anti_hotlinking_yn" == 'y' ];then
  204. if [ "$moredomainame_yn" == 'y' ]; then
  205. domain_allow_all=$domain_allow$moredomainame
  206. else
  207. domain_allow_all=$domain_allow
  208. fi
  209. anti_hotlinking=$(echo -e "location ~ .*\.(wma|wmv|asf|mp3|mmf|zip|rar|jpg|gif|png|swf|flv)$ {\n valid_referers none blocked $domain_allow_all;\n if (\$invalid_referer) {\n #rewrite ^/ http://www.linuxeye.com/403.html;\n return 403;\n }\n }")
  210. else
  211. anti_hotlinking=
  212. fi
  213. }
  214. Nginx_rewrite()
  215. {
  216. while :
  217. do
  218. echo
  219. read -p "Allow Rewrite rule? [y/n]: " rewrite_yn
  220. if [ "$rewrite_yn" != 'y' ] && [ "$rewrite_yn" != 'n' ];then
  221. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  222. else
  223. break
  224. fi
  225. done
  226. if [ "$rewrite_yn" == 'n' ];then
  227. rewrite="none"
  228. touch "$web_install_dir/conf/$rewrite.conf"
  229. else
  230. echo
  231. echo "Please input the rewrite of programme :"
  232. echo "${CMSG}wordpress${CEND},${CMSG}discuz${CEND},${CMSG}opencart${CEND},${CMSG}laravel${CEND},${CMSG}typecho${CEND},${CMSG}ecshop${CEND},${CMSG}drupal${CEND},${CMSG}joomla${CEND} rewrite was exist."
  233. read -p "(Default rewrite: other):" rewrite
  234. if [ "$rewrite" == "" ]; then
  235. rewrite="other"
  236. fi
  237. echo "You choose rewrite=${CMSG}$rewrite${CEND}"
  238. if [ -e "config/$rewrite.conf" ];then
  239. /bin/cp config/$rewrite.conf $web_install_dir/conf/$rewrite.conf
  240. else
  241. touch "$web_install_dir/conf/$rewrite.conf"
  242. fi
  243. fi
  244. }
  245. Nginx_log()
  246. {
  247. while :
  248. do
  249. echo
  250. read -p "Allow Nginx/Tengine access_log? [y/n]: " access_yn
  251. if [ "$access_yn" != 'y' ] && [ "$access_yn" != 'n' ];then
  252. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  253. else
  254. break
  255. fi
  256. done
  257. if [ "$access_yn" == 'n' ]; then
  258. N_log="access_log off;"
  259. else
  260. N_log="access_log $wwwlogs_dir/${domain}_nginx.log combined;"
  261. echo "You access log file=${CMSG}$wwwlogs_dir/${domain}_nginx.log${CEND}"
  262. fi
  263. }
  264. Create_nginx_tomcat_conf()
  265. {
  266. [ -n "`grep $vhostdir $tomcat_install_dir/conf/server.xml`" ] && { echo -e "\n$vhostdir in the tomcat already exist! \nYou must manually modify the file=${MSG}$tomcat_install_dir/conf/server.xml${CEND}"; exit; }
  267. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  268. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  269. server {
  270. listen 80;
  271. server_name $domain$moredomainame;
  272. $N_log
  273. index index.html index.htm index.jsp index.php;
  274. root $vhostdir;
  275. $anti_hotlinking
  276. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  277. expires 30d;
  278. access_log off;
  279. }
  280. location ~ .*\.(js|css)?$ {
  281. expires 7d;
  282. access_log off;
  283. }
  284. $NGX_CONF
  285. }
  286. EOF
  287. sed -i "s@autoDeploy=\"true\">@autoDeploy=\"true\">\n\t<Context path=\"\" docBase=\"$vhostdir\" debug=\"0\" reloadable=\"true\" crossContext=\"true\"/>@" $tomcat_install_dir/conf/server.xml
  288. echo
  289. $web_install_dir/sbin/nginx -t
  290. if [ $? == 0 ];then
  291. echo "Restart Nginx......"
  292. $web_install_dir/sbin/nginx -s reload
  293. else
  294. rm -rf $web_install_dir/conf/vhost/$domain.conf
  295. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  296. exit 1
  297. fi
  298. printf "
  299. #######################################################################
  300. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  301. # For more information please visit http://oneinstack.com #
  302. #######################################################################
  303. "
  304. echo "`printf "%-32s" "Your domain:"`${CMSG}$domain${CEND}"
  305. echo "`printf "%-32s" "Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  306. echo "`printf "%-32s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  307. }
  308. Create_nginx_php-fpm_hhvm_conf()
  309. {
  310. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  311. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  312. server {
  313. listen 80;
  314. server_name $domain$moredomainame;
  315. $N_log
  316. index index.html index.htm index.jsp index.php;
  317. include $rewrite.conf;
  318. root $vhostdir;
  319. $anti_hotlinking
  320. $NGX_CONF
  321. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  322. expires 30d;
  323. access_log off;
  324. }
  325. location ~ .*\.(js|css)?$ {
  326. expires 7d;
  327. access_log off;
  328. }
  329. }
  330. EOF
  331. echo
  332. $web_install_dir/sbin/nginx -t
  333. if [ $? == 0 ];then
  334. echo "Restart Nginx......"
  335. $web_install_dir/sbin/nginx -s reload
  336. else
  337. rm -rf $web_install_dir/conf/vhost/$domain.conf
  338. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  339. exit 1
  340. fi
  341. printf "
  342. #######################################################################
  343. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  344. # For more information please visit http://oneinstack.com #
  345. #######################################################################
  346. "
  347. echo "`printf "%-32s" "Your domain:"`${CMSG}$domain${CEND}"
  348. echo "`printf "%-32s" "Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  349. echo "`printf "%-32s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  350. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-32s" "Rewrite rule:"`${CMSG}$rewrite${CEND}"
  351. }
  352. Apache_log()
  353. {
  354. while :
  355. do
  356. echo
  357. read -p "Allow Apache access_log? [y/n]: " access_yn
  358. if [ "$access_yn" != 'y' ] && [ "$access_yn" != 'n' ];then
  359. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  360. else
  361. break
  362. fi
  363. done
  364. if [ "$access_yn" == 'n' ]; then
  365. A_log='CustomLog "/dev/null" common'
  366. else
  367. A_log="CustomLog \"$wwwlogs_dir/${domain}_apache.log\" common"
  368. echo "You access log file=$wwwlogs_dir/${domain}_apache.log"
  369. fi
  370. }
  371. Create_apache_conf()
  372. {
  373. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  374. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  375. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  376. <VirtualHost *:80>
  377. ServerAdmin admin@linuxeye.com
  378. DocumentRoot "$vhostdir"
  379. ServerName $domain
  380. $Domain_alias
  381. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  382. $A_log
  383. <Directory "$vhostdir">
  384. SetOutputFilter DEFLATE
  385. Options FollowSymLinks
  386. $R_TMP
  387. AllowOverride All
  388. Order allow,deny
  389. Allow from all
  390. DirectoryIndex index.html index.php
  391. </Directory>
  392. </VirtualHost>
  393. EOF
  394. echo
  395. $apache_install_dir/bin/apachectl -t
  396. if [ $? == 0 ];then
  397. echo "Restart Apache......"
  398. /etc/init.d/httpd restart
  399. else
  400. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  401. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  402. exit 1
  403. fi
  404. printf "
  405. #######################################################################
  406. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  407. # For more information please visit http://oneinstack.com #
  408. #######################################################################
  409. "
  410. echo "`printf "%-32s" "Your domain:"`${CMSG}$domain${CEND}"
  411. echo "`printf "%-32s" "Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  412. echo "`printf "%-32s" "Directory of $domain:"`${CMSG}$vhostdir${CEND}"
  413. }
  414. Create_nginx_apache_mod-php_conf()
  415. {
  416. # Nginx/Tengine
  417. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  418. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  419. server {
  420. listen 80;
  421. server_name $domain$moredomainame;
  422. $N_log
  423. index index.html index.htm index.jsp index.php;
  424. root $vhostdir;
  425. $anti_hotlinking
  426. location / {
  427. try_files \$uri @apache;
  428. }
  429. location @apache {
  430. proxy_pass http://127.0.0.1:88;
  431. include proxy.conf;
  432. }
  433. location ~ .*\.(php|php5)?$ {
  434. proxy_pass http://127.0.0.1:88;
  435. include proxy.conf;
  436. }
  437. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  438. expires 30d;
  439. access_log off;
  440. }
  441. location ~ .*\.(js|css)?$ {
  442. expires 7d;
  443. access_log off;
  444. }
  445. }
  446. EOF
  447. echo
  448. $web_install_dir/sbin/nginx -t
  449. if [ $? == 0 ];then
  450. echo "Restart Nginx......"
  451. $web_install_dir/sbin/nginx -s reload
  452. else
  453. rm -rf $web_install_dir/conf/vhost/$domain.conf
  454. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  455. fi
  456. # Apache
  457. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  458. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  459. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  460. <VirtualHost *:88>
  461. ServerAdmin admin@linuxeye.com
  462. DocumentRoot "$vhostdir"
  463. ServerName $domain
  464. $Domain_alias
  465. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  466. $A_log
  467. <Directory "$vhostdir">
  468. SetOutputFilter DEFLATE
  469. Options FollowSymLinks
  470. $R_TMP
  471. AllowOverride All
  472. Order allow,deny
  473. Allow from all
  474. DirectoryIndex index.html index.php
  475. </Directory>
  476. </VirtualHost>
  477. EOF
  478. echo
  479. $apache_install_dir/bin/apachectl -t
  480. if [ $? == 0 ];then
  481. echo "Restart Apache......"
  482. /etc/init.d/httpd restart
  483. else
  484. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  485. exit 1
  486. fi
  487. printf "
  488. #######################################################################
  489. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  490. # For more information please visit http://oneinstack.com #
  491. #######################################################################
  492. "
  493. echo "`printf "%-32s" "Your domain:"`${CMSG}$domain${CEND}"
  494. echo "`printf "%-32s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  495. echo "`printf "%-32s" "Apache Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  496. echo "`printf "%-32s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  497. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-32s" "Rewrite rule:"`${CMSG}$rewrite${CEND}"
  498. }
  499. Add_Vhost() {
  500. if [ -e "$web_install_dir/sbin/nginx" -a ! -e "$apache_install_dir/modules/libphp5.so" ];then
  501. Choose_env
  502. Input_Add_domain
  503. Nginx_anti_hotlinking
  504. if [ "$NGX_FLAG" == 'java' ];then
  505. Nginx_log
  506. Create_nginx_tomcat_conf
  507. else
  508. Nginx_rewrite
  509. Nginx_log
  510. Create_nginx_php-fpm_hhvm_conf
  511. fi
  512. elif [ ! -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/modules/libphp5.so" ];then
  513. Choose_env
  514. Input_Add_domain
  515. Apache_log
  516. Create_apache_conf
  517. elif [ -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/modules/libphp5.so" ];then
  518. Choose_env
  519. Input_Add_domain
  520. Nginx_anti_hotlinking
  521. if [ "$NGX_FLAG" == 'java' ];then
  522. Nginx_log
  523. Create_nginx_tomcat_conf
  524. elif [ "$NGX_FLAG" == 'hhvm' ];then
  525. Nginx_rewrite
  526. Nginx_log
  527. Create_nginx_php-fpm_hhvm_conf
  528. elif [ "$NGX_FLAG" == 'php' ];then
  529. #Nginx_rewrite
  530. Nginx_log
  531. Apache_log
  532. Create_nginx_apache_mod-php_conf
  533. fi
  534. fi
  535. }
  536. Del_NGX_Vhost() {
  537. if [ -e "$web_install_dir/sbin/nginx" ];then
  538. [ -d "$web_install_dir/conf/vhost" ] && Domain_List=`ls $web_install_dir/conf/vhost | sed "s@.conf@@g"`
  539. if [ -n "$Domain_List" ];then
  540. echo
  541. echo "Virtualhost list:"
  542. echo ${CMSG}$Domain_List${CEND}
  543. while :
  544. do
  545. echo
  546. read -p "Please input a domain you want to delete: " domain
  547. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  548. echo "${CWARNING}input error! ${CEND}"
  549. else
  550. if [ -e "$web_install_dir/conf/vhost/${domain}.conf" ];then
  551. Directory=`grep ^root $web_install_dir/conf/vhost/${domain}.conf | awk -F'[ ;]' '{print $2}'`
  552. rm -rf $web_install_dir/conf/vhost/${domain}.conf
  553. $web_install_dir/sbin/nginx -s reload
  554. while :
  555. do
  556. echo
  557. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_NGX_wwwroot_yn
  558. if [ "$Del_NGX_wwwroot_yn" != 'y' ] && [ "$Del_NGX_wwwroot_yn" != 'n' ];then
  559. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  560. else
  561. break
  562. fi
  563. done
  564. if [ "$Del_NGX_wwwroot_yn" == 'y' ];then
  565. echo "Press Ctrl+c to cancel or Press any key to continue..."
  566. char=`get_char`
  567. rm -rf $Directory
  568. fi
  569. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  570. else
  571. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  572. fi
  573. break
  574. fi
  575. done
  576. else
  577. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  578. fi
  579. fi
  580. }
  581. Del_Apache_Vhost() {
  582. if [ -e "$apache_install_dir/conf/httpd.conf" ];then
  583. if [ -e "$web_install_dir/sbin/nginx" ];then
  584. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  585. /etc/init.d/httpd restart
  586. else
  587. Domain_List=`ls $apache_install_dir/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g"`
  588. if [ -n "$Domain_List" ];then
  589. echo
  590. echo "Virtualhost list:"
  591. echo ${CMSG}$Domain_List${CEND}
  592. while :
  593. do
  594. echo
  595. read -p "Please input a domain you want to delete: " domain
  596. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  597. echo "${CWARNING}input error! ${CEND}"
  598. else
  599. if [ -e "$apache_install_dir/conf/vhost/${domain}.conf" ];then
  600. Directory=`grep '^<Directory' $apache_install_dir/conf/vhost/${domain}.conf | awk -F'"' '{print $2}'`
  601. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  602. /etc/init.d/httpd restart
  603. while :
  604. do
  605. echo
  606. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Apache_wwwroot_yn
  607. if [ "$Del_Apache_wwwroot_yn" != 'y' ] && [ "$Del_Apache_wwwroot_yn" != 'n' ];then
  608. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  609. else
  610. break
  611. fi
  612. done
  613. if [ "$Del_Apache_wwwroot_yn" == 'y' ];then
  614. echo "Press Ctrl+c to cancel or Press any key to continue..."
  615. char=`get_char`
  616. rm -rf $Directory
  617. fi
  618. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  619. else
  620. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  621. fi
  622. break
  623. fi
  624. done
  625. else
  626. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  627. fi
  628. fi
  629. fi
  630. }
  631. Del_Tomcat_Vhost() {
  632. if [ -e "$tomcat_install_dir/conf/server.xml" ];then
  633. Num=`grep -n "docBase=\"$Directory\"" $tomcat_install_dir/conf/server.xml | awk -F: '{print $1}'`
  634. [ -n "$Num" ] && { sed -i ${Num}d $tomcat_install_dir/conf/server.xml; /etc/init.d/tomcat restart; }
  635. fi
  636. }
  637. if [ $# == 0 ];then
  638. Add_Vhost
  639. elif [ $# == 1 ];then
  640. case $1 in
  641. add)
  642. Add_Vhost
  643. ;;
  644. del)
  645. Del_NGX_Vhost
  646. Del_Apache_Vhost
  647. Del_Tomcat_Vhost
  648. ;;
  649. *)
  650. Usage
  651. ;;
  652. esac
  653. else
  654. Usage
  655. fi