1
0

vhost.sh 32 KB

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