vhost.sh 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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-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-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-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-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. NGX_FLAG=php
  118. fi
  119. if [ "$NGX_FLAG" == 'php' ];then
  120. NGX_CONF=$(echo -e "location ~ [^/]\.php(/|$) {\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. Nginx_ssl() {
  128. printf "
  129. You are about to be asked to enter information that will be incorporated
  130. into your certificate request.
  131. What you are about to enter is what is called a Distinguished Name or a DN.
  132. There are quite a few fields but you can leave some blank
  133. For some fields there will be a default value,
  134. If you enter '.', the field will be left blank.
  135. "
  136. echo
  137. read -p "Country Name (2 letter code) [CN]: " SELFSIGNEDSSL_C
  138. [ -z "$SELFSIGNEDSSL_C" ] && SELFSIGNEDSSL_C=CN
  139. echo
  140. read -p "State or Province Name (full name) [Shanghai]: " SELFSIGNEDSSL_ST
  141. [ -z "$SELFSIGNEDSSL_ST" ] && SELFSIGNEDSSL_ST=Shanghai
  142. echo
  143. read -p "Locality Name (eg, city) [Shanghai]: " SELFSIGNEDSSL_L
  144. [ -z "$SELFSIGNEDSSL_L" ] && SELFSIGNEDSSL_L=Shanghai
  145. echo
  146. read -p "Organization Name (eg, company) [LinuxEye Inc.]: " SELFSIGNEDSSL_O
  147. [ -z "$SELFSIGNEDSSL_O" ] && SELFSIGNEDSSL_O='LinuxEye Inc.'
  148. echo
  149. read -p "Organizational Unit Name (eg, section) [IT Dept.]: " SELFSIGNEDSSL_OU
  150. [ -z "$SELFSIGNEDSSL_OU" ] && SELFSIGNEDSSL_OU='IT Dept.'
  151. if [[ "$($web_install_dir/sbin/nginx -V 2>&1 | grep -Eo 'with-http_v2_module')" = 'with-http_v2_module' ]]; then
  152. LISTENOPT='443 ssl http2'
  153. else
  154. LISTENOPT='443 ssl spdy'
  155. fi
  156. [ ! -d "$web_install_dir/conf/ssl" ] && mkdir $web_install_dir/conf/ssl
  157. openssl req -new -newkey rsa:2048 -sha256 -nodes -out $web_install_dir/conf/ssl/${domain}.csr -keyout $web_install_dir/conf/ssl/${domain}.key -subj "/C=${SELFSIGNEDSSL_C}/ST=${SELFSIGNEDSSL_ST}/L=${SELFSIGNEDSSL_L}/O=${SELFSIGNEDSSL_O}/OU=${SELFSIGNEDSSL_OU}/CN=${domain}" > /dev/null 2>&1
  158. /bin/cp $web_install_dir/conf/ssl/${domain}.csr{,_bk.`date +%Y-%m-%d_%H%M`}
  159. /bin/cp $web_install_dir/conf/ssl/${domain}.key{,_bk.`date +%Y-%m-%d_%H%M`}
  160. openssl x509 -req -days 36500 -sha256 -in $web_install_dir/conf/ssl/${domain}.csr -signkey $web_install_dir/conf/ssl/${domain}.key -out $web_install_dir/conf/ssl/${domain}.crt > /dev/null 2>&1
  161. }
  162. Print_ssl() {
  163. echo "`printf "%-30s" "Self-signed SSL Certificate:"`${CMSG}$web_install_dir/conf/ssl/${domain}.crt${CEND}"
  164. echo "`printf "%-30s" "SSL Private Key:"`${CMSG}$web_install_dir/conf/ssl/${domain}.key${CEND}"
  165. echo "`printf "%-30s" "SSL CSR File:"`${CMSG}$web_install_dir/conf/ssl/${domain}.csr${CEND}"
  166. }
  167. Input_Add_domain() {
  168. if [ -e "$web_install_dir/sbin/nginx" ];then
  169. while :
  170. do
  171. echo
  172. read -p "Do you want to setup SSL under Nginx? [y/n]: " nginx_ssl_yn
  173. if [[ ! $nginx_ssl_yn =~ ^[y,n]$ ]];then
  174. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  175. else
  176. break
  177. fi
  178. done
  179. fi
  180. while :
  181. do
  182. echo
  183. read -p "Please input domain(example: www.linuxeye.com): " domain
  184. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  185. echo "${CWARNING}input error! ${CEND}"
  186. else
  187. break
  188. fi
  189. done
  190. 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
  191. [ -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"
  192. [ -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"
  193. [ -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"
  194. exit
  195. else
  196. echo "domain=$domain"
  197. fi
  198. while :
  199. do
  200. echo
  201. read -p "Do you want to add more domain name? [y/n]: " moredomainame_yn
  202. if [[ ! $moredomainame_yn =~ ^[y,n]$ ]];then
  203. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  204. else
  205. break
  206. fi
  207. done
  208. if [ "$moredomainame_yn" == 'y' ]; then
  209. while :
  210. do
  211. echo
  212. read -p "Type domainname or IP(example: linuxeye.com 121.43.8.8): " moredomain
  213. if [ -z "`echo $moredomain | grep '.*\..*'`" ]; then
  214. echo "${CWARNING}input error! ${CEND}"
  215. else
  216. [ "$moredomain" == "$domain" ] && echo "${CWARNING}Domain name already exists! ${CND}" && continue
  217. echo domain list="$moredomain"
  218. moredomainame=" $moredomain"
  219. break
  220. fi
  221. done
  222. Apache_Domain_alias=ServerAlias$moredomainame
  223. Tomcat_Domain_alias=$(for D in `echo $moredomainame`; do echo "<Alias>$D</Alias>"; done)
  224. if [ -e "$web_install_dir/sbin/nginx" ];then
  225. while :
  226. do
  227. echo
  228. read -p "Do you want to redirect from $moredomain to $domain? [y/n]: " redirect_yn
  229. if [[ ! $redirect_yn =~ ^[y,n]$ ]];then
  230. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  231. else
  232. break
  233. fi
  234. done
  235. [ "$nginx_ssl_yn" == 'y' ] && HTTP_flag=https || HTTP_flag=http
  236. [ "$redirect_yn" == 'y' ] && Nginx_redirect=$(echo -e "if (\$host != $domain) {\n\trewrite ^/(.*)\$ \$scheme://$domain/\$1 permanent;\n\t}")
  237. fi
  238. fi
  239. if [ "$nginx_ssl_yn" == 'y' ]; then
  240. Nginx_ssl
  241. Nginx_conf=$(echo -e "listen $LISTENOPT;\nssl_certificate $web_install_dir/conf/ssl/$domain.crt;\nssl_certificate_key $web_install_dir/conf/ssl/$domain.key;\nssl_session_timeout 10m;\nssl_protocols TLSv1 TLSv1.1 TLSv1.2;\nssl_prefer_server_ciphers on;\nssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-RC4-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:RC4-SHA:\!aNULL:\!eNULL:\!EXPORT:\!DES:\!3DES:\!MD5:\!DSS:\!PKS";\nssl_session_cache builtin:1000 shared:SSL:10m;\nresolver 8.8.8.8 8.8.4.4 valid=300s;\nresolver_timeout 5s;")
  242. if [ "$moredomainame_yn" == 'y' ]; then
  243. Nginx_http_to_https=$(echo -e "server {\nlisten 80;\nserver_name $domain$moredomainame;\nrewrite ^/(.*) https://$domain/\$1 permanent;\n}")
  244. else
  245. Nginx_http_to_https=$(echo -e "server {\nlisten 80;\nserver_name $domain;\nrewrite ^/(.*) https://$domain/\$1 permanent;\n}")
  246. fi
  247. else
  248. Nginx_conf='listen 80;'
  249. fi
  250. while :
  251. do
  252. echo
  253. echo "Please input the directory for the domain:$domain :"
  254. read -p "(Default directory: $wwwroot_dir/$domain): " vhostdir
  255. if [ -n "$vhostdir" -a -z "`echo $vhostdir | grep '^/'`" ];then
  256. echo "${CWARNING}input error! Press Enter to continue...${CEND}"
  257. else
  258. if [ -z "$vhostdir" ]; then
  259. vhostdir="$wwwroot_dir/$domain"
  260. echo "Virtual Host Directory=${CMSG}$vhostdir${CEND}"
  261. fi
  262. echo
  263. echo "Create Virtul Host directory......"
  264. mkdir -p $vhostdir
  265. echo "set permissions of Virtual Host directory......"
  266. chown -R ${run_user}.$run_user $vhostdir
  267. break
  268. fi
  269. done
  270. }
  271. Nginx_anti_hotlinking() {
  272. while :
  273. do
  274. echo
  275. read -p "Do you want to add hotlink protection? [y/n]: " anti_hotlinking_yn
  276. if [[ ! $anti_hotlinking_yn =~ ^[y,n]$ ]];then
  277. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  278. else
  279. break
  280. fi
  281. done
  282. if [ -n "`echo $domain | grep '.*\..*\..*'`" ];then
  283. domain_allow="*.${domain#*.} $domain"
  284. else
  285. domain_allow="*.$domain $domain"
  286. fi
  287. if [ "$anti_hotlinking_yn" == 'y' ];then
  288. if [ "$moredomainame_yn" == 'y' ]; then
  289. domain_allow_all=$domain_allow$moredomainame
  290. else
  291. domain_allow_all=$domain_allow
  292. fi
  293. 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 }")
  294. else
  295. anti_hotlinking=
  296. fi
  297. }
  298. Nginx_rewrite() {
  299. [ ! -d "$web_install_dir/conf/rewrite" ] && mkdir $web_install_dir/conf/rewrite
  300. while :
  301. do
  302. echo
  303. read -p "Allow Rewrite rule? [y/n]: " rewrite_yn
  304. if [[ ! $rewrite_yn =~ ^[y,n]$ ]];then
  305. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  306. else
  307. break
  308. fi
  309. done
  310. if [ "$rewrite_yn" == 'n' ];then
  311. rewrite="none"
  312. touch "$web_install_dir/conf/rewrite/$rewrite.conf"
  313. else
  314. echo
  315. echo "Please input the rewrite of programme :"
  316. echo "${CMSG}wordpress${CEND},${CMSG}discuz${CEND},${CMSG}opencart${CEND},${CMSG}thinkphp${CEND},${CMSG}laravel${CEND},${CMSG}typecho${CEND},${CMSG}ecshop${CEND},${CMSG}drupal${CEND},${CMSG}joomla${CEND} rewrite was exist."
  317. read -p "(Default rewrite: other):" rewrite
  318. if [ "$rewrite" == "" ]; then
  319. rewrite="other"
  320. fi
  321. echo "You choose rewrite=${CMSG}$rewrite${CEND}"
  322. [ "$NGX_FLAG" == 'php' -a "$rewrite" == "thinkphp" ] && NGX_CONF=$(echo -e "location ~ \.php {\n #fastcgi_pass remote_php_ip:9000;\n fastcgi_pass unix:/dev/shm/php-cgi.sock;\n fastcgi_index index.php;\n include fastcgi_params;\n set \$real_script_name \$fastcgi_script_name;\n if (\$fastcgi_script_name ~ \"^(.+?\.php)(/.+)\$\") {\n set \$real_script_name \$1;\n #set \$path_info \$2;\n }\n fastcgi_param SCRIPT_FILENAME \$document_root\$real_script_name;\n fastcgi_param SCRIPT_NAME \$real_script_name;\n #fastcgi_param PATH_INFO \$path_info;\n }")
  323. if [ -e "config/$rewrite.conf" ];then
  324. /bin/cp config/$rewrite.conf $web_install_dir/conf/rewrite/$rewrite.conf
  325. else
  326. touch "$web_install_dir/conf/rewrite/$rewrite.conf"
  327. fi
  328. fi
  329. }
  330. Nginx_log() {
  331. while :
  332. do
  333. echo
  334. read -p "Allow Nginx/Tengine access_log? [y/n]: " access_yn
  335. if [[ ! $access_yn =~ ^[y,n]$ ]];then
  336. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  337. else
  338. break
  339. fi
  340. done
  341. if [ "$access_yn" == 'n' ]; then
  342. N_log="access_log off;"
  343. else
  344. N_log="access_log $wwwlogs_dir/${domain}_nginx.log combined;"
  345. echo "You access log file=${CMSG}$wwwlogs_dir/${domain}_nginx.log${CEND}"
  346. fi
  347. }
  348. Create_nginx_tomcat_conf() {
  349. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  350. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  351. server {
  352. $Nginx_conf
  353. server_name $domain$moredomainame;
  354. $N_log
  355. index index.html index.htm index.jsp;
  356. root $vhostdir;
  357. $Nginx_redirect
  358. $anti_hotlinking
  359. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  360. expires 30d;
  361. access_log off;
  362. }
  363. location ~ .*\.(js|css)?$ {
  364. expires 7d;
  365. access_log off;
  366. }
  367. $NGX_CONF
  368. }
  369. $Nginx_http_to_https
  370. EOF
  371. cat > $tomcat_install_dir/conf/vhost/$domain.xml << EOF
  372. <Host name="$domain" appBase="webapps" unpackWARs="true" autoDeploy="true"> $Tomcat_Domain_alias
  373. <Context path="" docBase="$vhostdir" debug="0" reloadable="false" crossContext="true"/>
  374. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  375. prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  376. </Host>
  377. EOF
  378. [ -z "`grep -o "vhost-${domain} SYSTEM" $tomcat_install_dir/conf/server.xml`" ] && sed -i "/vhost-localhost SYSTEM/a<\!ENTITY vhost-${domain} SYSTEM \"file://$tomcat_install_dir/conf/vhost/$domain.xml\">" $tomcat_install_dir/conf/server.xml
  379. [ -z "`grep -o "vhost-${domain};" $tomcat_install_dir/conf/server.xml`" ] && sed -i "s@vhost-localhost;@&\n \&vhost-${domain};@" $tomcat_install_dir/conf/server.xml
  380. echo
  381. $web_install_dir/sbin/nginx -t
  382. if [ $? == 0 ];then
  383. echo "Reload Nginx......"
  384. $web_install_dir/sbin/nginx -s reload
  385. /etc/init.d/tomcat restart
  386. else
  387. rm -rf $web_install_dir/conf/vhost/$domain.conf
  388. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  389. exit 1
  390. fi
  391. printf "
  392. #######################################################################
  393. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  394. # For more information please visit http://oneinstack.com #
  395. #######################################################################
  396. "
  397. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  398. echo "`printf "%-30s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  399. echo "`printf "%-30s" "Tomcat Virtualhost conf:"`${CMSG}$tomcat_install_dir/conf/vhost/$domain.xml${CEND}"
  400. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  401. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  402. }
  403. Create_tomcat_conf() {
  404. cat > $tomcat_install_dir/conf/vhost/$domain.xml << EOF
  405. <Host name="$domain" appBase="webapps" unpackWARs="true" autoDeploy="true"> $Tomcat_Domain_alias
  406. <Context path="" docBase="$vhostdir" debug="0" reloadable="false" crossContext="true"/>
  407. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  408. prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  409. </Host>
  410. EOF
  411. [ -z "`grep -o "vhost-${domain} SYSTEM" $tomcat_install_dir/conf/server.xml`" ] && sed -i "/vhost-localhost SYSTEM/a<\!ENTITY vhost-${domain} SYSTEM \"file://$tomcat_install_dir/conf/vhost/$domain.xml\">" $tomcat_install_dir/conf/server.xml
  412. [ -z "`grep -o "vhost-${domain};" $tomcat_install_dir/conf/server.xml`" ] && sed -i "s@vhost-localhost;@&\n \&vhost-${domain};@" $tomcat_install_dir/conf/server.xml
  413. echo
  414. /etc/init.d/tomcat restart
  415. printf "
  416. #######################################################################
  417. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  418. # For more information please visit http://oneinstack.com #
  419. #######################################################################
  420. "
  421. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  422. echo "`printf "%-30s" "Tomcat Virtualhost conf:"`${CMSG}$tomcat_install_dir/conf/vhost/$domain.xml${CEND}"
  423. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  424. echo "`printf "%-30s" "index url:"`${CMSG}http://${domain}:8080/${CEND}"
  425. }
  426. Create_nginx_php-fpm_hhvm_conf() {
  427. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  428. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  429. server {
  430. $Nginx_conf
  431. server_name $domain$moredomainame;
  432. $N_log
  433. index index.html index.htm index.php;
  434. include $web_install_dir/conf/rewrite/$rewrite.conf;
  435. root $vhostdir;
  436. $Nginx_redirect
  437. $anti_hotlinking
  438. $NGX_CONF
  439. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  440. expires 30d;
  441. access_log off;
  442. }
  443. location ~ .*\.(js|css)?$ {
  444. expires 7d;
  445. access_log off;
  446. }
  447. }
  448. $Nginx_http_to_https
  449. EOF
  450. echo
  451. $web_install_dir/sbin/nginx -t
  452. if [ $? == 0 ];then
  453. echo "Reload Nginx......"
  454. $web_install_dir/sbin/nginx -s reload
  455. else
  456. rm -rf $web_install_dir/conf/vhost/$domain.conf
  457. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  458. exit 1
  459. fi
  460. printf "
  461. #######################################################################
  462. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  463. # For more information please visit http://oneinstack.com #
  464. #######################################################################
  465. "
  466. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  467. echo "`printf "%-30s" "Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  468. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  469. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-30s" "Rewrite rule:"`${CMSG}$web_install_dir/conf/rewrite/$rewrite.conf${CEND}"
  470. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  471. }
  472. Apache_log() {
  473. while :
  474. do
  475. echo
  476. read -p "Allow Apache access_log? [y/n]: " access_yn
  477. if [[ ! $access_yn =~ ^[y,n]$ ]];then
  478. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  479. else
  480. break
  481. fi
  482. done
  483. if [ "$access_yn" == 'n' ]; then
  484. A_log='CustomLog "/dev/null" common'
  485. else
  486. A_log="CustomLog \"$wwwlogs_dir/${domain}_apache.log\" common"
  487. echo "You access log file=$wwwlogs_dir/${domain}_apache.log"
  488. fi
  489. }
  490. Create_apache_conf() {
  491. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  492. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  493. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  494. <VirtualHost *:80>
  495. ServerAdmin admin@linuxeye.com
  496. DocumentRoot "$vhostdir"
  497. ServerName $domain
  498. $Apache_Domain_alias
  499. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  500. $A_log
  501. <Directory "$vhostdir">
  502. SetOutputFilter DEFLATE
  503. Options FollowSymLinks ExecCGI
  504. $R_TMP
  505. AllowOverride All
  506. Order allow,deny
  507. Allow from all
  508. DirectoryIndex index.html index.php
  509. </Directory>
  510. </VirtualHost>
  511. EOF
  512. echo
  513. $apache_install_dir/bin/apachectl -t
  514. if [ $? == 0 ];then
  515. echo "Restart Apache......"
  516. /etc/init.d/httpd restart
  517. else
  518. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  519. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  520. exit 1
  521. fi
  522. printf "
  523. #######################################################################
  524. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  525. # For more information please visit http://oneinstack.com #
  526. #######################################################################
  527. "
  528. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  529. echo "`printf "%-30s" "Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  530. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  531. }
  532. Create_nginx_apache_mod-php_conf() {
  533. # Nginx/Tengine
  534. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  535. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  536. server {
  537. $Nginx_conf
  538. server_name $domain$moredomainame;
  539. $N_log
  540. index index.html index.htm index.php;
  541. root $vhostdir;
  542. $Nginx_redirect
  543. $anti_hotlinking
  544. location / {
  545. try_files \$uri @apache;
  546. }
  547. location @apache {
  548. proxy_pass http://127.0.0.1:88;
  549. include proxy.conf;
  550. }
  551. location ~ .*\.(php|php5|cgi|pl)?$ {
  552. proxy_pass http://127.0.0.1:88;
  553. include proxy.conf;
  554. }
  555. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  556. expires 30d;
  557. access_log off;
  558. }
  559. location ~ .*\.(js|css)?$ {
  560. expires 7d;
  561. access_log off;
  562. }
  563. }
  564. $Nginx_http_to_https
  565. EOF
  566. echo
  567. $web_install_dir/sbin/nginx -t
  568. if [ $? == 0 ];then
  569. echo "Reload Nginx......"
  570. $web_install_dir/sbin/nginx -s reload
  571. else
  572. rm -rf $web_install_dir/conf/vhost/$domain.conf
  573. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  574. fi
  575. # Apache
  576. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  577. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  578. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  579. <VirtualHost *:88>
  580. ServerAdmin admin@linuxeye.com
  581. DocumentRoot "$vhostdir"
  582. ServerName $domain
  583. $Apache_Domain_alias
  584. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  585. $A_log
  586. <Directory "$vhostdir">
  587. SetOutputFilter DEFLATE
  588. Options FollowSymLinks ExecCGI
  589. $R_TMP
  590. AllowOverride All
  591. Order allow,deny
  592. Allow from all
  593. DirectoryIndex index.html index.php
  594. </Directory>
  595. </VirtualHost>
  596. EOF
  597. echo
  598. $apache_install_dir/bin/apachectl -t
  599. if [ $? == 0 ];then
  600. echo "Restart Apache......"
  601. /etc/init.d/httpd restart
  602. else
  603. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  604. exit 1
  605. fi
  606. printf "
  607. #######################################################################
  608. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  609. # For more information please visit http://oneinstack.com #
  610. #######################################################################
  611. "
  612. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  613. echo "`printf "%-30s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  614. echo "`printf "%-30s" "Apache Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  615. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  616. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-28s" "Rewrite rule:"`${CMSG}$web_install_dir/conf/rewrite/$rewrite.conf${CEND}"
  617. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  618. }
  619. Add_Vhost() {
  620. if [ -e "$web_install_dir/sbin/nginx" -a ! -e "`ls $apache_install_dir/modules/libphp?.so 2>/dev/null`" ];then
  621. Choose_env
  622. Input_Add_domain
  623. Nginx_anti_hotlinking
  624. if [ "$NGX_FLAG" == 'java' ];then
  625. Nginx_log
  626. Create_nginx_tomcat_conf
  627. else
  628. Nginx_rewrite
  629. Nginx_log
  630. Create_nginx_php-fpm_hhvm_conf
  631. fi
  632. elif [ ! -e "$web_install_dir/sbin/nginx" -a -e "`ls $apache_install_dir/modules/libphp?.so 2>/dev/null`" ];then
  633. Choose_env
  634. Input_Add_domain
  635. Apache_log
  636. Create_apache_conf
  637. elif [ ! -e "$web_install_dir/sbin/nginx" -a ! -e "$apache_install_dir/conf/httpd.conf" -a -e "$tomcat_install_dir/conf/server.xml" ];then
  638. Choose_env
  639. Input_Add_domain
  640. Create_tomcat_conf
  641. elif [ -e "$web_install_dir/sbin/nginx" -a -e "`ls $apache_install_dir/modules/libphp?.so 2>/dev/null`" ];then
  642. Choose_env
  643. Input_Add_domain
  644. Nginx_anti_hotlinking
  645. if [ "$NGX_FLAG" == 'java' ];then
  646. Nginx_log
  647. Create_nginx_tomcat_conf
  648. elif [ "$NGX_FLAG" == 'hhvm' ];then
  649. Nginx_rewrite
  650. Nginx_log
  651. Create_nginx_php-fpm_hhvm_conf
  652. elif [ "$NGX_FLAG" == 'php' ];then
  653. #Nginx_rewrite
  654. Nginx_log
  655. Apache_log
  656. Create_nginx_apache_mod-php_conf
  657. fi
  658. fi
  659. }
  660. Del_NGX_Vhost() {
  661. if [ -e "$web_install_dir/sbin/nginx" ];then
  662. [ -d "$web_install_dir/conf/vhost" ] && Domain_List=`ls $web_install_dir/conf/vhost | sed "s@.conf@@g"`
  663. if [ -n "$Domain_List" ];then
  664. echo
  665. echo "Virtualhost list:"
  666. echo ${CMSG}$Domain_List${CEND}
  667. while :
  668. do
  669. echo
  670. read -p "Please input a domain you want to delete: " domain
  671. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  672. echo "${CWARNING}input error! ${CEND}"
  673. else
  674. if [ -e "$web_install_dir/conf/vhost/${domain}.conf" ];then
  675. Directory=`grep ^root $web_install_dir/conf/vhost/${domain}.conf | awk -F'[ ;]' '{print $2}'`
  676. rm -rf $web_install_dir/conf/vhost/${domain}.conf
  677. $web_install_dir/sbin/nginx -s reload
  678. while :
  679. do
  680. echo
  681. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  682. if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
  683. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  684. else
  685. break
  686. fi
  687. done
  688. if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
  689. echo "Press Ctrl+c to cancel or Press any key to continue..."
  690. char=`get_char`
  691. rm -rf $Directory
  692. fi
  693. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  694. else
  695. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  696. fi
  697. break
  698. fi
  699. done
  700. else
  701. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  702. fi
  703. fi
  704. }
  705. Del_Apache_Vhost() {
  706. if [ -e "$apache_install_dir/conf/httpd.conf" ];then
  707. if [ -e "$web_install_dir/sbin/nginx" ];then
  708. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  709. /etc/init.d/httpd restart
  710. else
  711. Domain_List=`ls $apache_install_dir/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g"`
  712. if [ -n "$Domain_List" ];then
  713. echo
  714. echo "Virtualhost list:"
  715. echo ${CMSG}$Domain_List${CEND}
  716. while :
  717. do
  718. echo
  719. read -p "Please input a domain you want to delete: " domain
  720. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  721. echo "${CWARNING}input error! ${CEND}"
  722. else
  723. if [ -e "$apache_install_dir/conf/vhost/${domain}.conf" ];then
  724. Directory=`grep '^<Directory' $apache_install_dir/conf/vhost/${domain}.conf | awk -F'"' '{print $2}'`
  725. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  726. /etc/init.d/httpd restart
  727. while :
  728. do
  729. echo
  730. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  731. if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
  732. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  733. else
  734. break
  735. fi
  736. done
  737. if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
  738. echo "Press Ctrl+c to cancel or Press any key to continue..."
  739. char=`get_char`
  740. rm -rf $Directory
  741. fi
  742. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  743. else
  744. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  745. fi
  746. break
  747. fi
  748. done
  749. else
  750. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  751. fi
  752. fi
  753. fi
  754. }
  755. Del_Tomcat_Vhost() {
  756. if [ -e "$tomcat_install_dir/conf/server.xml" ];then
  757. if [ -e "$web_install_dir/sbin/nginx" ];then
  758. if [ -n "`grep vhost-${domain} $tomcat_install_dir/conf/server.xml`" ];then
  759. sed -i /vhost-${domain}/d $tomcat_install_dir/conf/server.xml
  760. rm -rf $tomcat_install_dir/conf/vhost/${domain}.xml
  761. /etc/init.d/tomcat restart
  762. fi
  763. else
  764. Domain_List=`ls $tomcat_install_dir/conf/vhost | grep -v 'localhost.xml' | sed "s@.xml@@g"`
  765. if [ -n "$Domain_List" ];then
  766. echo
  767. echo "Virtualhost list:"
  768. echo ${CMSG}$Domain_List${CEND}
  769. while :
  770. do
  771. echo
  772. read -p "Please input a domain you want to delete: " domain
  773. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  774. echo "${CWARNING}input error! ${CEND}"
  775. else
  776. if [ -n "`grep vhost-${domain} $tomcat_install_dir/conf/server.xml`" ];then
  777. sed -i /vhost-${domain}/d $tomcat_install_dir/conf/server.xml
  778. rm -rf $tomcat_install_dir/conf/vhost/${domain}.xml
  779. /etc/init.d/tomcat restart
  780. while :
  781. do
  782. echo
  783. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Vhost_wwwroot_yn
  784. if [[ ! $Del_Vhost_wwwroot_yn =~ ^[y,n]$ ]];then
  785. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  786. else
  787. break
  788. fi
  789. done
  790. if [ "$Del_Vhost_wwwroot_yn" == 'y' ];then
  791. echo "Press Ctrl+c to cancel or Press any key to continue..."
  792. char=`get_char`
  793. rm -rf $Directory
  794. fi
  795. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  796. else
  797. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  798. fi
  799. break
  800. fi
  801. done
  802. else
  803. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  804. fi
  805. fi
  806. fi
  807. }
  808. if [ $# == 0 ];then
  809. Add_Vhost
  810. elif [ $# == 1 ];then
  811. case $1 in
  812. add)
  813. Add_Vhost
  814. ;;
  815. del)
  816. Del_NGX_Vhost
  817. Del_Apache_Vhost
  818. Del_Tomcat_Vhost
  819. ;;
  820. *)
  821. Usage
  822. ;;
  823. esac
  824. else
  825. Usage
  826. fi