vhost.sh 33 KB

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