vhost.sh 24 KB

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