vhost.sh 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # http://oneinstack.com
  9. # https://github.com/lj2007331/oneinstack
  10. export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  11. clear
  12. printf "
  13. #######################################################################
  14. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  15. # For more information please visit http://oneinstack.com #
  16. #######################################################################
  17. "
  18. . ./options.conf
  19. . ./include/color.sh
  20. . ./include/check_web.sh
  21. . ./include/get_char.sh
  22. # Check if user is root
  23. [ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
  24. Usage() {
  25. printf "
  26. Usage: $0 [ ${CMSG}add${CEND} | ${CMSG}del${CEND} ]
  27. ${CMSG}add${CEND} --->Add Virtualhost
  28. ${CMSG}del${CEND} --->Delete Virtualhost
  29. "
  30. }
  31. Choose_env() {
  32. if [ -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
  33. Number=111
  34. while :
  35. do
  36. echo
  37. echo 'Please choose to use environment:'
  38. echo -e "\t${CMSG}1${CEND}. Use php"
  39. echo -e "\t${CMSG}2${CEND}. Use java"
  40. echo -e "\t${CMSG}3${CEND}. Use hhvm"
  41. read -p "Please input a number:(Default 1 press Enter) " Choose_number
  42. [ -z "$Choose_number" ] && Choose_number=1
  43. if [ $Choose_number != 1 -a $Choose_number != 2 -a $Choose_number != 3 ];then
  44. echo "${CWARNING}input error! Please only input number 1,2,3${CEND}"
  45. else
  46. break
  47. fi
  48. done
  49. [ "$Choose_number" == '1' ] && NGX_FLAG=php
  50. [ "$Choose_number" == '2' ] && NGX_FLAG=java
  51. [ "$Choose_number" == '3' ] && NGX_FLAG=hhvm
  52. elif [ -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a ! -e "/usr/bin/hhvm" ];then
  53. Number=110
  54. while :
  55. do
  56. echo
  57. echo 'Please choose to use environment:'
  58. echo -e "\t${CMSG}1${CEND}. Use php"
  59. echo -e "\t${CMSG}2${CEND}. Use java"
  60. read -p "Please input a number:(Default 1 press Enter) " Choose_number
  61. [ -z "$Choose_number" ] && Choose_number=1
  62. if [ $Choose_number != 1 -a $Choose_number != 2 ];then
  63. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  64. else
  65. break
  66. fi
  67. done
  68. [ "$Choose_number" == '1' ] && NGX_FLAG=php
  69. [ "$Choose_number" == '2' ] && NGX_FLAG=java
  70. elif [ -e "$php_install_dir/bin/phpize" -a ! -e "$tomcat_install_dir/conf/server.xml" -a ! -e "/usr/bin/hhvm" ];then
  71. Number=100
  72. NGX_FLAG=php
  73. elif [ -e "$php_install_dir/bin/phpize" -a ! -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
  74. Number=101
  75. while :
  76. do
  77. echo
  78. echo 'Please choose to use environment:'
  79. echo -e "\t${CMSG}1${CEND}. Use php"
  80. echo -e "\t${CMSG}2${CEND}. Use hhvm"
  81. read -p "Please input a number:(Default 1 press Enter) " Choose_number
  82. [ -z "$Choose_number" ] && Choose_number=1
  83. if [ $Choose_number != 1 -a $Choose_number != 2 ];then
  84. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  85. else
  86. break
  87. fi
  88. done
  89. [ "$Choose_number" == '1' ] && NGX_FLAG=php
  90. [ "$Choose_number" == '2' ] && NGX_FLAG=hhvm
  91. elif [ ! -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
  92. Number=011
  93. while :
  94. do
  95. echo
  96. echo 'Please choose to use environment:'
  97. echo -e "\t${CMSG}1${CEND}. Use java"
  98. echo -e "\t${CMSG}2${CEND}. Use hhvm"
  99. read -p "Please input a number:(Default 1 press Enter) " Choose_number
  100. [ -z "$Choose_number" ] && Choose_number=1
  101. if [ $Choose_number != 1 -a $Choose_number != 2 ];then
  102. echo "${CWARNING}input error! Please only input number 1,2${CEND}"
  103. else
  104. break
  105. fi
  106. done
  107. [ "$Choose_number" == '1' ] && NGX_FLAG=java
  108. [ "$Choose_number" == '2' ] && NGX_FLAG=hhvm
  109. elif [ ! -e "$php_install_dir/bin/phpize" -a -e "$tomcat_install_dir/conf/server.xml" -a ! -e "/usr/bin/hhvm" ];then
  110. Number=010
  111. NGX_FLAG=java
  112. elif [ ! -e "$php_install_dir/bin/phpize" -a ! -e "$tomcat_install_dir/conf/server.xml" -a -e "/usr/bin/hhvm" ];then
  113. Number=001
  114. NGX_FLAG=hhvm
  115. else
  116. Number=000
  117. 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' ] && [ "$nginx_ssl_yn" != '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' ] && [ "$moredomainame_yn" != '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. while :
  230. do
  231. echo
  232. read -p "Do you want to redirect from $moredomain to $domain? [y/n]: " redirect_yn
  233. if [ "$redirect_yn" != 'y' ] && [ "$redirect_yn" != 'n' ];then
  234. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  235. else
  236. break
  237. fi
  238. done
  239. [ "$nginx_ssl_yn" == 'y' ] && HTTP_flag=https || HTTP_flag=http
  240. [ "$redirect_yn" == 'y' ] && Nginx_redirect=$(echo -e "if (\$host != $domain) {\n\trewrite ^/(.*)\$ $HTTP_flag://$domain/\$1 permanent;\n\t}")
  241. fi
  242. while :
  243. do
  244. echo
  245. echo "Please input the directory for the domain:$domain :"
  246. read -p "(Default directory: $wwwroot_dir/$domain): " vhostdir
  247. if [ -n "$vhostdir" -a -z "`echo $vhostdir | grep '^/'`" ];then
  248. echo "${CWARNING}input error! Press Enter to continue...${CEND}"
  249. else
  250. if [ -z "$vhostdir" ]; then
  251. vhostdir="$wwwroot_dir/$domain"
  252. echo "Virtual Host Directory=${CMSG}$vhostdir${CEND}"
  253. fi
  254. echo
  255. echo "Create Virtul Host directory......"
  256. mkdir -p $vhostdir
  257. echo "set permissions of Virtual Host directory......"
  258. chown -R ${run_user}.$run_user $vhostdir
  259. break
  260. fi
  261. done
  262. }
  263. Nginx_anti_hotlinking() {
  264. while :
  265. do
  266. echo
  267. read -p "Do you want to add hotlink protection? [y/n]: " anti_hotlinking_yn
  268. if [ "$anti_hotlinking_yn" != 'y' ] && [ "$anti_hotlinking_yn" != 'n' ];then
  269. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  270. else
  271. break
  272. fi
  273. done
  274. if [ -n "`echo $domain | grep '.*\..*\..*'`" ];then
  275. domain_allow="*.${domain#*.} $domain"
  276. else
  277. domain_allow="*.$domain $domain"
  278. fi
  279. if [ "$anti_hotlinking_yn" == 'y' ];then
  280. if [ "$moredomainame_yn" == 'y' ]; then
  281. domain_allow_all=$domain_allow$moredomainame
  282. else
  283. domain_allow_all=$domain_allow
  284. fi
  285. 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 }")
  286. else
  287. anti_hotlinking=
  288. fi
  289. }
  290. Nginx_rewrite() {
  291. while :
  292. do
  293. echo
  294. read -p "Allow Rewrite rule? [y/n]: " rewrite_yn
  295. if [ "$rewrite_yn" != 'y' ] && [ "$rewrite_yn" != 'n' ];then
  296. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  297. else
  298. break
  299. fi
  300. done
  301. if [ "$rewrite_yn" == 'n' ];then
  302. rewrite="none"
  303. touch "$web_install_dir/conf/$rewrite.conf"
  304. else
  305. echo
  306. echo "Please input the rewrite of programme :"
  307. 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."
  308. read -p "(Default rewrite: other):" rewrite
  309. if [ "$rewrite" == "" ]; then
  310. rewrite="other"
  311. fi
  312. echo "You choose rewrite=${CMSG}$rewrite${CEND}"
  313. [ "$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 }")
  314. if [ -e "config/$rewrite.conf" ];then
  315. /bin/cp config/$rewrite.conf $web_install_dir/conf/$rewrite.conf
  316. else
  317. touch "$web_install_dir/conf/$rewrite.conf"
  318. fi
  319. fi
  320. }
  321. Nginx_log() {
  322. while :
  323. do
  324. echo
  325. read -p "Allow Nginx/Tengine access_log? [y/n]: " access_yn
  326. if [ "$access_yn" != 'y' ] && [ "$access_yn" != 'n' ];then
  327. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  328. else
  329. break
  330. fi
  331. done
  332. if [ "$access_yn" == 'n' ]; then
  333. N_log="access_log off;"
  334. else
  335. N_log="access_log $wwwlogs_dir/${domain}_nginx.log combined;"
  336. echo "You access log file=${CMSG}$wwwlogs_dir/${domain}_nginx.log${CEND}"
  337. fi
  338. }
  339. Create_nginx_tomcat_conf() {
  340. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  341. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  342. server {
  343. $Nginx_conf
  344. server_name $domain$moredomainame;
  345. $N_log
  346. index index.html index.htm index.jsp;
  347. root $vhostdir;
  348. $Nginx_redirect
  349. $anti_hotlinking
  350. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  351. expires 30d;
  352. access_log off;
  353. }
  354. location ~ .*\.(js|css)?$ {
  355. expires 7d;
  356. access_log off;
  357. }
  358. $NGX_CONF
  359. }
  360. EOF
  361. cat > $tomcat_install_dir/conf/vhost/$domain.xml << EOF
  362. <Host name="$domain" appBase="webapps" unpackWARs="true" autoDeploy="true"> $Tomcat_Domain_alias
  363. <Context path="" docBase="$vhostdir" debug="0" reloadable="false" crossContext="true"/>
  364. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  365. prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  366. </Host>
  367. EOF
  368. [ -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
  369. [ -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
  370. echo
  371. $web_install_dir/sbin/nginx -t
  372. if [ $? == 0 ];then
  373. echo "Reload Nginx......"
  374. $web_install_dir/sbin/nginx -s reload
  375. /etc/init.d/tomcat restart
  376. else
  377. rm -rf $web_install_dir/conf/vhost/$domain.conf
  378. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  379. exit 1
  380. fi
  381. printf "
  382. #######################################################################
  383. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  384. # For more information please visit http://oneinstack.com #
  385. #######################################################################
  386. "
  387. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  388. echo "`printf "%-30s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  389. echo "`printf "%-30s" "Tomcat Virtualhost conf:"`${CMSG}$tomcat_install_dir/conf/vhost/$domain.xml${CEND}"
  390. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  391. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  392. }
  393. Create_nginx_php-fpm_hhvm_conf() {
  394. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  395. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  396. server {
  397. $Nginx_conf
  398. server_name $domain$moredomainame;
  399. $N_log
  400. index index.html index.htm index.php;
  401. include $web_install_dir/conf/$rewrite.conf;
  402. root $vhostdir;
  403. $Nginx_redirect
  404. $anti_hotlinking
  405. $NGX_CONF
  406. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  407. expires 30d;
  408. access_log off;
  409. }
  410. location ~ .*\.(js|css)?$ {
  411. expires 7d;
  412. access_log off;
  413. }
  414. }
  415. EOF
  416. echo
  417. $web_install_dir/sbin/nginx -t
  418. if [ $? == 0 ];then
  419. echo "Reload Nginx......"
  420. $web_install_dir/sbin/nginx -s reload
  421. else
  422. rm -rf $web_install_dir/conf/vhost/$domain.conf
  423. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  424. exit 1
  425. fi
  426. printf "
  427. #######################################################################
  428. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  429. # For more information please visit http://oneinstack.com #
  430. #######################################################################
  431. "
  432. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  433. echo "`printf "%-30s" "Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  434. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  435. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-20s" "Rewrite rule:"`${CMSG}$web_install_dir/conf/$rewrite.conf${CEND}"
  436. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  437. }
  438. Apache_log() {
  439. while :
  440. do
  441. echo
  442. read -p "Allow Apache access_log? [y/n]: " access_yn
  443. if [ "$access_yn" != 'y' ] && [ "$access_yn" != 'n' ];then
  444. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  445. else
  446. break
  447. fi
  448. done
  449. if [ "$access_yn" == 'n' ]; then
  450. A_log='CustomLog "/dev/null" common'
  451. else
  452. A_log="CustomLog \"$wwwlogs_dir/${domain}_apache.log\" common"
  453. echo "You access log file=$wwwlogs_dir/${domain}_apache.log"
  454. fi
  455. }
  456. Create_apache_conf() {
  457. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  458. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  459. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  460. <VirtualHost *:80>
  461. ServerAdmin admin@linuxeye.com
  462. DocumentRoot "$vhostdir"
  463. ServerName $domain
  464. $Apache_Domain_alias
  465. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  466. $A_log
  467. <Directory "$vhostdir">
  468. SetOutputFilter DEFLATE
  469. Options FollowSymLinks ExecCGI
  470. $R_TMP
  471. AllowOverride All
  472. Order allow,deny
  473. Allow from all
  474. DirectoryIndex index.html index.php
  475. </Directory>
  476. </VirtualHost>
  477. EOF
  478. echo
  479. $apache_install_dir/bin/apachectl -t
  480. if [ $? == 0 ];then
  481. echo "Restart Apache......"
  482. /etc/init.d/httpd restart
  483. else
  484. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  485. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  486. exit 1
  487. fi
  488. printf "
  489. #######################################################################
  490. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  491. # For more information please visit http://oneinstack.com #
  492. #######################################################################
  493. "
  494. echo "`printf "%-20s" "Your domain:"`${CMSG}$domain${CEND}"
  495. echo "`printf "%-20s" "Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  496. echo "`printf "%-20s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  497. }
  498. Create_nginx_apache_mod-php_conf() {
  499. # Nginx/Tengine
  500. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  501. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  502. server {
  503. $Nginx_conf
  504. server_name $domain$moredomainame;
  505. $N_log
  506. index index.html index.htm index.php;
  507. root $vhostdir;
  508. $Nginx_redirect
  509. $anti_hotlinking
  510. location / {
  511. try_files \$uri @apache;
  512. }
  513. location @apache {
  514. proxy_pass http://127.0.0.1:88;
  515. include proxy.conf;
  516. }
  517. location ~ .*\.(php|php5|cgi|pl)?$ {
  518. proxy_pass http://127.0.0.1:88;
  519. include proxy.conf;
  520. }
  521. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  522. expires 30d;
  523. access_log off;
  524. }
  525. location ~ .*\.(js|css)?$ {
  526. expires 7d;
  527. access_log off;
  528. }
  529. }
  530. EOF
  531. echo
  532. $web_install_dir/sbin/nginx -t
  533. if [ $? == 0 ];then
  534. echo "Reload Nginx......"
  535. $web_install_dir/sbin/nginx -s reload
  536. else
  537. rm -rf $web_install_dir/conf/vhost/$domain.conf
  538. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  539. fi
  540. # Apache
  541. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  542. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  543. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  544. <VirtualHost *:88>
  545. ServerAdmin admin@linuxeye.com
  546. DocumentRoot "$vhostdir"
  547. ServerName $domain
  548. $Apache_Domain_alias
  549. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  550. $A_log
  551. <Directory "$vhostdir">
  552. SetOutputFilter DEFLATE
  553. Options FollowSymLinks ExecCGI
  554. $R_TMP
  555. AllowOverride All
  556. Order allow,deny
  557. Allow from all
  558. DirectoryIndex index.html index.php
  559. </Directory>
  560. </VirtualHost>
  561. EOF
  562. echo
  563. $apache_install_dir/bin/apachectl -t
  564. if [ $? == 0 ];then
  565. echo "Restart Apache......"
  566. /etc/init.d/httpd restart
  567. else
  568. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  569. exit 1
  570. fi
  571. printf "
  572. #######################################################################
  573. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  574. # For more information please visit http://oneinstack.com #
  575. #######################################################################
  576. "
  577. echo "`printf "%-30s" "Your domain:"`${CMSG}$domain${CEND}"
  578. echo "`printf "%-30s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  579. echo "`printf "%-30s" "Apache Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  580. echo "`printf "%-30s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  581. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-28s" "Rewrite rule:"`${CMSG}$web_install_dir/conf/$rewrite.conf${CEND}"
  582. [ "$nginx_ssl_yn" == 'y' ] && Print_ssl
  583. }
  584. Add_Vhost() {
  585. if [ -e "$web_install_dir/sbin/nginx" -a ! -e "$apache_install_dir/modules/libphp5.so" ];then
  586. Choose_env
  587. Input_Add_domain
  588. Nginx_anti_hotlinking
  589. if [ "$NGX_FLAG" == 'java' ];then
  590. Nginx_log
  591. Create_nginx_tomcat_conf
  592. else
  593. Nginx_rewrite
  594. Nginx_log
  595. Create_nginx_php-fpm_hhvm_conf
  596. fi
  597. elif [ ! -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/modules/libphp5.so" ];then
  598. Choose_env
  599. Input_Add_domain
  600. Apache_log
  601. Create_apache_conf
  602. elif [ -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/modules/libphp5.so" ];then
  603. Choose_env
  604. Input_Add_domain
  605. Nginx_anti_hotlinking
  606. if [ "$NGX_FLAG" == 'java' ];then
  607. Nginx_log
  608. Create_nginx_tomcat_conf
  609. elif [ "$NGX_FLAG" == 'hhvm' ];then
  610. Nginx_rewrite
  611. Nginx_log
  612. Create_nginx_php-fpm_hhvm_conf
  613. elif [ "$NGX_FLAG" == 'php' ];then
  614. #Nginx_rewrite
  615. Nginx_log
  616. Apache_log
  617. Create_nginx_apache_mod-php_conf
  618. fi
  619. fi
  620. }
  621. Del_NGX_Vhost() {
  622. if [ -e "$web_install_dir/sbin/nginx" ];then
  623. [ -d "$web_install_dir/conf/vhost" ] && Domain_List=`ls $web_install_dir/conf/vhost | sed "s@.conf@@g"`
  624. if [ -n "$Domain_List" ];then
  625. echo
  626. echo "Virtualhost list:"
  627. echo ${CMSG}$Domain_List${CEND}
  628. while :
  629. do
  630. echo
  631. read -p "Please input a domain you want to delete: " domain
  632. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  633. echo "${CWARNING}input error! ${CEND}"
  634. else
  635. if [ -e "$web_install_dir/conf/vhost/${domain}.conf" ];then
  636. Directory=`grep ^root $web_install_dir/conf/vhost/${domain}.conf | awk -F'[ ;]' '{print $2}'`
  637. rm -rf $web_install_dir/conf/vhost/${domain}.conf
  638. $web_install_dir/sbin/nginx -s reload
  639. while :
  640. do
  641. echo
  642. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_NGX_wwwroot_yn
  643. if [ "$Del_NGX_wwwroot_yn" != 'y' ] && [ "$Del_NGX_wwwroot_yn" != 'n' ];then
  644. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  645. else
  646. break
  647. fi
  648. done
  649. if [ "$Del_NGX_wwwroot_yn" == 'y' ];then
  650. echo "Press Ctrl+c to cancel or Press any key to continue..."
  651. char=`get_char`
  652. rm -rf $Directory
  653. fi
  654. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  655. else
  656. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  657. fi
  658. break
  659. fi
  660. done
  661. else
  662. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  663. fi
  664. fi
  665. }
  666. Del_Apache_Vhost() {
  667. if [ -e "$apache_install_dir/conf/httpd.conf" ];then
  668. if [ -e "$web_install_dir/sbin/nginx" ];then
  669. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  670. /etc/init.d/httpd restart
  671. else
  672. Domain_List=`ls $apache_install_dir/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g"`
  673. if [ -n "$Domain_List" ];then
  674. echo
  675. echo "Virtualhost list:"
  676. echo ${CMSG}$Domain_List${CEND}
  677. while :
  678. do
  679. echo
  680. read -p "Please input a domain you want to delete: " domain
  681. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  682. echo "${CWARNING}input error! ${CEND}"
  683. else
  684. if [ -e "$apache_install_dir/conf/vhost/${domain}.conf" ];then
  685. Directory=`grep '^<Directory' $apache_install_dir/conf/vhost/${domain}.conf | awk -F'"' '{print $2}'`
  686. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  687. /etc/init.d/httpd restart
  688. while :
  689. do
  690. echo
  691. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Apache_wwwroot_yn
  692. if [ "$Del_Apache_wwwroot_yn" != 'y' ] && [ "$Del_Apache_wwwroot_yn" != 'n' ];then
  693. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  694. else
  695. break
  696. fi
  697. done
  698. if [ "$Del_Apache_wwwroot_yn" == 'y' ];then
  699. echo "Press Ctrl+c to cancel or Press any key to continue..."
  700. char=`get_char`
  701. rm -rf $Directory
  702. fi
  703. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  704. else
  705. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  706. fi
  707. break
  708. fi
  709. done
  710. else
  711. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  712. fi
  713. fi
  714. fi
  715. }
  716. Del_Tomcat_Vhost() {
  717. if [ -e "$tomcat_install_dir/conf/server.xml" ] && [ -n "`grep ${domain}-vhost $tomcat_install_dir/conf/server.xml`" ];then
  718. sed -i /${domain}-vhost/d $tomcat_install_dir/conf/server.xml
  719. rm -rf $tomcat_install_dir/conf/vhost/${domain}.xml
  720. /etc/init.d/tomcat restart
  721. fi
  722. }
  723. if [ $# == 0 ];then
  724. Add_Vhost
  725. elif [ $# == 1 ];then
  726. case $1 in
  727. add)
  728. Add_Vhost
  729. ;;
  730. del)
  731. Del_NGX_Vhost
  732. Del_Apache_Vhost
  733. Del_Tomcat_Vhost
  734. ;;
  735. *)
  736. Usage
  737. ;;
  738. esac
  739. else
  740. Usage
  741. fi