vhost.sh 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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="location ~ .*\.(php|php5)?$ {\n\t#fastcgi_pass remote_php_ip:9000;\n\tfastcgi_pass unix:/dev/shm/php-cgi.sock;\n\tfastcgi_index index.php;\n\tinclude fastcgi.conf;\n\t}"
  122. elif [ "$NGX_FLAG" == 'java' ];then
  123. NGX_CONF="location ~ {\n\tproxy_set_header Host \$host;\n\tproxy_set_header X-Real-IP \$remote_addr;\n\tproxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;\n\tproxy_pass http://127.0.0.1:8080;\n\t}"
  124. elif [ "$NGX_FLAG" == 'hhvm' ];then
  125. NGX_CONF="location ~ .*\.(php|php5)?$ {\n\tfastcgi_pass unix:/var/log/hhvm/sock;\n\tfastcgi_index index.php;\n\tfastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n\tinclude fastcgi_params;\n\t}"
  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\tvalid_referers none blocked $domain_allow_all;\n\tif (\$invalid_referer) {\n\t\t#rewrite ^/ http://www.linuxeye.com/403.html;\n\t\treturn 403;\n\t\t}\n\t}")
  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. }
  279. location ~ .*\.(js|css)?$ {
  280. expires 7d;
  281. }
  282. `echo -e $NGX_CONF`
  283. }
  284. EOF
  285. 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
  286. echo
  287. $web_install_dir/sbin/nginx -t
  288. if [ $? == 0 ];then
  289. echo "Restart Nginx......"
  290. $web_install_dir/sbin/nginx -s reload
  291. else
  292. rm -rf $web_install_dir/conf/vhost/$domain.conf
  293. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  294. exit 1
  295. fi
  296. printf "
  297. #######################################################################
  298. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  299. # For more information please visit http://oneinstack.com #
  300. #######################################################################
  301. "
  302. echo "`printf "%-32s" "Your domain:"`${CMSG}$domain${CEND}"
  303. echo "`printf "%-32s" "Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  304. echo "`printf "%-32s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  305. }
  306. Create_nginx_php-fpm_hhvm_conf()
  307. {
  308. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  309. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  310. server {
  311. listen 80;
  312. server_name $domain$moredomainame;
  313. $N_log
  314. index index.html index.htm index.jsp index.php;
  315. include $rewrite.conf;
  316. root $vhostdir;
  317. $anti_hotlinking
  318. `echo -e $NGX_CONF`
  319. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  320. expires 30d;
  321. }
  322. location ~ .*\.(js|css)?$ {
  323. expires 7d;
  324. }
  325. }
  326. EOF
  327. echo
  328. $web_install_dir/sbin/nginx -t
  329. if [ $? == 0 ];then
  330. echo "Restart Nginx......"
  331. $web_install_dir/sbin/nginx -s reload
  332. else
  333. rm -rf $web_install_dir/conf/vhost/$domain.conf
  334. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  335. exit 1
  336. fi
  337. printf "
  338. #######################################################################
  339. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  340. # For more information please visit http://oneinstack.com #
  341. #######################################################################
  342. "
  343. echo "`printf "%-32s" "Your domain:"`${CMSG}$domain${CEND}"
  344. echo "`printf "%-32s" "Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  345. echo "`printf "%-32s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  346. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-32s" "Rewrite rule:"`${CMSG}$rewrite${CEND}"
  347. }
  348. Apache_log()
  349. {
  350. while :
  351. do
  352. echo
  353. read -p "Allow Apache access_log? [y/n]: " access_yn
  354. if [ "$access_yn" != 'y' ] && [ "$access_yn" != 'n' ];then
  355. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  356. else
  357. break
  358. fi
  359. done
  360. if [ "$access_yn" == 'n' ]; then
  361. A_log='CustomLog "/dev/null" common'
  362. else
  363. A_log="CustomLog \"$wwwlogs_dir/${domain}_apache.log\" common"
  364. echo "You access log file=$wwwlogs_dir/${domain}_apache.log"
  365. fi
  366. }
  367. Create_apache_conf()
  368. {
  369. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  370. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  371. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  372. <VirtualHost *:80>
  373. ServerAdmin admin@linuxeye.com
  374. DocumentRoot "$vhostdir"
  375. ServerName $domain
  376. $Domain_alias
  377. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  378. $A_log
  379. <Directory "$vhostdir">
  380. SetOutputFilter DEFLATE
  381. Options FollowSymLinks
  382. $R_TMP
  383. AllowOverride All
  384. Order allow,deny
  385. Allow from all
  386. DirectoryIndex index.html index.php
  387. </Directory>
  388. </VirtualHost>
  389. EOF
  390. echo
  391. $apache_install_dir/bin/apachectl -t
  392. if [ $? == 0 ];then
  393. echo "Restart Apache......"
  394. /etc/init.d/httpd restart
  395. else
  396. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  397. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  398. exit 1
  399. fi
  400. printf "
  401. #######################################################################
  402. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  403. # For more information please visit http://oneinstack.com #
  404. #######################################################################
  405. "
  406. echo "`printf "%-32s" "Your domain:"`${CMSG}$domain${CEND}"
  407. echo "`printf "%-32s" "Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  408. echo "`printf "%-32s" "Directory of $domain:"`${CMSG}$vhostdir${CEND}"
  409. }
  410. Create_nginx_apache_mod-php_conf()
  411. {
  412. # Nginx/Tengine
  413. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  414. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  415. server {
  416. listen 80;
  417. server_name $domain$moredomainame;
  418. $N_log
  419. index index.html index.htm index.jsp index.php;
  420. root $vhostdir;
  421. $anti_hotlinking
  422. location / {
  423. try_files \$uri @apache;
  424. }
  425. location @apache {
  426. internal;
  427. proxy_pass http://127.0.0.1:9090;
  428. }
  429. location ~ .*\.(php|php5)?$ {
  430. proxy_pass http://127.0.0.1:9090;
  431. }
  432. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  433. expires 30d;
  434. }
  435. location ~ .*\.(js|css)?$ {
  436. expires 7d;
  437. }
  438. }
  439. EOF
  440. echo
  441. $web_install_dir/sbin/nginx -t
  442. if [ $? == 0 ];then
  443. echo "Restart Nginx......"
  444. $web_install_dir/sbin/nginx -s reload
  445. else
  446. rm -rf $web_install_dir/conf/vhost/$domain.conf
  447. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  448. fi
  449. # Apache
  450. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  451. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  452. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  453. <VirtualHost *:9090>
  454. ServerAdmin admin@linuxeye.com
  455. DocumentRoot "$vhostdir"
  456. ServerName $domain
  457. $Domain_alias
  458. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  459. $A_log
  460. <Directory "$vhostdir">
  461. SetOutputFilter DEFLATE
  462. Options FollowSymLinks
  463. $R_TMP
  464. AllowOverride All
  465. Order allow,deny
  466. Allow from all
  467. DirectoryIndex index.html index.php
  468. </Directory>
  469. </VirtualHost>
  470. EOF
  471. echo
  472. $apache_install_dir/bin/apachectl -t
  473. if [ $? == 0 ];then
  474. echo "Restart Apache......"
  475. /etc/init.d/httpd restart
  476. else
  477. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  478. exit 1
  479. fi
  480. printf "
  481. #######################################################################
  482. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  483. # For more information please visit http://oneinstack.com #
  484. #######################################################################
  485. "
  486. echo "`printf "%-32s" "Your domain:"`${CMSG}$domain${CEND}"
  487. echo "`printf "%-32s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  488. echo "`printf "%-32s" "Apache Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  489. echo "`printf "%-32s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  490. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-32s" "Rewrite rule:"`${CMSG}$rewrite${CEND}"
  491. }
  492. Add_Vhost() {
  493. if [ -e "$web_install_dir/sbin/nginx" -a ! -e "$apache_install_dir/modules/libphp5.so" ];then
  494. Choose_env
  495. Input_Add_domain
  496. Nginx_anti_hotlinking
  497. if [ "$NGX_FLAG" == 'java' ];then
  498. Nginx_log
  499. Create_nginx_tomcat_conf
  500. else
  501. Nginx_rewrite
  502. Nginx_log
  503. Create_nginx_php-fpm_hhvm_conf
  504. fi
  505. elif [ ! -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/modules/libphp5.so" ];then
  506. Choose_env
  507. Input_Add_domain
  508. Apache_log
  509. Create_apache_conf
  510. elif [ -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/modules/libphp5.so" ];then
  511. Choose_env
  512. Input_Add_domain
  513. Nginx_anti_hotlinking
  514. if [ "$NGX_FLAG" == 'java' ];then
  515. Nginx_log
  516. Create_nginx_tomcat_conf
  517. elif [ "$NGX_FLAG" == 'hhvm' ];then
  518. Nginx_rewrite
  519. Nginx_log
  520. Create_nginx_php-fpm_hhvm_conf
  521. elif [ "$NGX_FLAG" == 'php' ];then
  522. #Nginx_rewrite
  523. Nginx_log
  524. Apache_log
  525. Create_nginx_apache_mod-php_conf
  526. fi
  527. fi
  528. }
  529. Del_NGX_Vhost() {
  530. if [ -e "$web_install_dir/sbin/nginx" ];then
  531. [ -d "$web_install_dir/conf/vhost" ] && Domain_List=`ls $web_install_dir/conf/vhost | sed "s@.conf@@g"`
  532. if [ -n "$Domain_List" ];then
  533. echo
  534. echo "Virtualhost list:"
  535. echo ${CMSG}$Domain_List${CEND}
  536. while :
  537. do
  538. echo
  539. read -p "Please input a domain you want to delete: " domain
  540. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  541. echo "${CWARNING}input error! ${CEND}"
  542. else
  543. if [ -e "$web_install_dir/conf/vhost/${domain}.conf" ];then
  544. Directory=`grep ^root $web_install_dir/conf/vhost/${domain}.conf | awk -F'[ ;]' '{print $2}'`
  545. rm -rf $web_install_dir/conf/vhost/${domain}.conf
  546. $web_install_dir/sbin/nginx -s reload
  547. while :
  548. do
  549. echo ''
  550. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_NGX_wwwroot_yn
  551. if [ "$Del_NGX_wwwroot_yn" != 'y' ] && [ "$Del_NGX_wwwroot_yn" != 'n' ];then
  552. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  553. else
  554. break
  555. fi
  556. done
  557. if [ "$Del_NGX_wwwroot_yn" == 'y' ];then
  558. echo "Press Ctrl+c to cancel or Press any key to continue..."
  559. char=`get_char`
  560. rm -rf $Directory
  561. fi
  562. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  563. else
  564. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  565. fi
  566. break
  567. fi
  568. done
  569. else
  570. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  571. fi
  572. fi
  573. }
  574. Del_Apache_Vhost() {
  575. if [ -e "$apache_install_dir/conf/httpd.conf" ];then
  576. if [ -e "$web_install_dir/sbin/nginx" ];then
  577. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  578. /etc/init.d/httpd restart
  579. else
  580. Domain_List=`ls $apache_install_dir/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g"`
  581. if [ -n "$Domain_List" ];then
  582. echo
  583. echo "Virtualhost list:"
  584. echo ${CMSG}$Domain_List${CEND}
  585. while :
  586. do
  587. echo
  588. read -p "Please input a domain you want to delete: " domain
  589. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  590. echo "${CWARNING}input error! ${CEND}"
  591. else
  592. if [ -e "$apache_install_dir/conf/vhost/${domain}.conf" ];then
  593. Directory=`grep '^<Directory' $apache_install_dir/conf/vhost/${domain}.conf | awk -F'"' '{print $2}'`
  594. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  595. /etc/init.d/httpd restart
  596. while :
  597. do
  598. echo ''
  599. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Apache_wwwroot_yn
  600. if [ "$Del_Apache_wwwroot_yn" != 'y' ] && [ "$Del_Apache_wwwroot_yn" != 'n' ];then
  601. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  602. else
  603. break
  604. fi
  605. done
  606. if [ "$Del_Apache_wwwroot_yn" == 'y' ];then
  607. echo "Press Ctrl+c to cancel or Press any key to continue..."
  608. char=`get_char`
  609. rm -rf $Directory
  610. fi
  611. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  612. else
  613. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  614. fi
  615. break
  616. fi
  617. done
  618. else
  619. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  620. fi
  621. fi
  622. fi
  623. }
  624. Del_Tomcat_Vhost() {
  625. if [ -e "$tomcat_install_dir/conf/server.xml" ];then
  626. Num=`grep -n "docBase=\"$Directory\"" $tomcat_install_dir/conf/server.xml | awk -F: '{print $1}'`
  627. [ -n "$Num" ] && { sed -i ${Num}d $tomcat_install_dir/conf/server.xml; /etc/init.d/tomcat restart; }
  628. fi
  629. }
  630. if [ $# == 0 ];then
  631. Add_Vhost
  632. elif [ $# == 1 ];then
  633. case $1 in
  634. add)
  635. Add_Vhost
  636. ;;
  637. del)
  638. Del_NGX_Vhost
  639. Del_Apache_Vhost
  640. Del_Tomcat_Vhost
  641. ;;
  642. *)
  643. Usage
  644. ;;
  645. esac
  646. else
  647. Usage
  648. fi