vhost.sh 33 KB

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