vhost.sh 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. exit
  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. Input_Add_domain() {
  128. while :
  129. do
  130. echo
  131. read -p "Please input domain(example: www.linuxeye.com): " domain
  132. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  133. echo "${CWARNING}input error! ${CEND}"
  134. else
  135. break
  136. fi
  137. done
  138. 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
  139. [ -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"
  140. [ -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"
  141. [ -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"
  142. exit
  143. else
  144. echo "domain=$domain"
  145. fi
  146. while :
  147. do
  148. echo
  149. read -p "Do you want to add more domain name? [y/n]: " moredomainame_yn
  150. if [ "$moredomainame_yn" != 'y' ] && [ "$moredomainame_yn" != 'n' ];then
  151. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  152. else
  153. break
  154. fi
  155. done
  156. if [ "$moredomainame_yn" == 'y' ]; then
  157. while :
  158. do
  159. echo
  160. read -p "Type domainname,example(linuxeye.com www.example.com): " moredomain
  161. if [ -z "`echo $moredomain | grep '.*\..*'`" ]; then
  162. echo "${CWARNING}input error! ${CEND}"
  163. else
  164. [ "$moredomain" == "$domain" ] && echo "${CWARNING}Domain name already exists! ${CND}" && continue
  165. echo domain list="$moredomain"
  166. moredomainame=" $moredomain"
  167. break
  168. fi
  169. done
  170. Apache_Domain_alias=ServerAlias$moredomainame
  171. Tomcat_Domain_alias=$(for D in `echo $moredomainame`; do echo "<Alias>$D</Alias>"; done)
  172. fi
  173. echo
  174. echo "Please input the directory for the domain:$domain :"
  175. read -p "(Default directory: $wwwroot_dir/$domain): " vhostdir
  176. if [ -z "$vhostdir" ]; then
  177. vhostdir="$wwwroot_dir/$domain"
  178. echo "Virtual Host Directory=${CMSG}$vhostdir${CEND}"
  179. fi
  180. echo
  181. echo "Create Virtul Host directory......"
  182. mkdir -p $vhostdir
  183. echo "set permissions of Virtual Host directory......"
  184. chown -R ${run_user}.$run_user $vhostdir
  185. }
  186. Nginx_anti_hotlinking() {
  187. while :
  188. do
  189. echo
  190. read -p "Do you want to add hotlink protection? [y/n]: " anti_hotlinking_yn
  191. if [ "$anti_hotlinking_yn" != 'y' ] && [ "$anti_hotlinking_yn" != 'n' ];then
  192. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  193. else
  194. break
  195. fi
  196. done
  197. if [ -n "`echo $domain | grep '.*\..*\..*'`" ];then
  198. domain_allow="*.${domain#*.} $domain"
  199. else
  200. domain_allow="*.$domain $domain"
  201. fi
  202. if [ "$anti_hotlinking_yn" == 'y' ];then
  203. if [ "$moredomainame_yn" == 'y' ]; then
  204. domain_allow_all=$domain_allow$moredomainame
  205. else
  206. domain_allow_all=$domain_allow
  207. fi
  208. 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 }")
  209. else
  210. anti_hotlinking=
  211. fi
  212. }
  213. Nginx_rewrite() {
  214. while :
  215. do
  216. echo
  217. read -p "Allow Rewrite rule? [y/n]: " rewrite_yn
  218. if [ "$rewrite_yn" != 'y' ] && [ "$rewrite_yn" != 'n' ];then
  219. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  220. else
  221. break
  222. fi
  223. done
  224. if [ "$rewrite_yn" == 'n' ];then
  225. rewrite="none"
  226. touch "$web_install_dir/conf/$rewrite.conf"
  227. else
  228. echo
  229. echo "Please input the rewrite of programme :"
  230. 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."
  231. read -p "(Default rewrite: other):" rewrite
  232. if [ "$rewrite" == "" ]; then
  233. rewrite="other"
  234. fi
  235. echo "You choose rewrite=${CMSG}$rewrite${CEND}"
  236. [ "$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 }")
  237. if [ -e "config/$rewrite.conf" ];then
  238. /bin/cp config/$rewrite.conf $web_install_dir/conf/$rewrite.conf
  239. else
  240. touch "$web_install_dir/conf/$rewrite.conf"
  241. fi
  242. fi
  243. }
  244. Nginx_log() {
  245. while :
  246. do
  247. echo
  248. read -p "Allow Nginx/Tengine access_log? [y/n]: " access_yn
  249. if [ "$access_yn" != 'y' ] && [ "$access_yn" != 'n' ];then
  250. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  251. else
  252. break
  253. fi
  254. done
  255. if [ "$access_yn" == 'n' ]; then
  256. N_log="access_log off;"
  257. else
  258. N_log="access_log $wwwlogs_dir/${domain}_nginx.log combined;"
  259. echo "You access log file=${CMSG}$wwwlogs_dir/${domain}_nginx.log${CEND}"
  260. fi
  261. }
  262. Create_nginx_tomcat_conf() {
  263. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  264. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  265. server {
  266. listen 80;
  267. server_name $domain$moredomainame;
  268. $N_log
  269. index index.html index.htm index.jsp;
  270. root $vhostdir;
  271. $anti_hotlinking
  272. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  273. expires 30d;
  274. access_log off;
  275. }
  276. location ~ .*\.(js|css)?$ {
  277. expires 7d;
  278. access_log off;
  279. }
  280. $NGX_CONF
  281. }
  282. EOF
  283. cat > $tomcat_install_dir/conf/vhost/$domain.xml << EOF
  284. <Host name="$domain" appBase="webapps" unpackWARs="true" autoDeploy="true"> $Tomcat_Domain_alias
  285. <Context path="" docBase="$vhostdir" debug="0" reloadable="false" crossContext="true"/>
  286. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  287. prefix="${domain}_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  288. </Host>
  289. EOF
  290. [ -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
  291. [ -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
  292. echo
  293. $web_install_dir/sbin/nginx -t
  294. if [ $? == 0 ];then
  295. echo "Reload Nginx......"
  296. $web_install_dir/sbin/nginx -s reload
  297. /etc/init.d/tomcat restart
  298. else
  299. rm -rf $web_install_dir/conf/vhost/$domain.conf
  300. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  301. exit 1
  302. fi
  303. printf "
  304. #######################################################################
  305. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  306. # For more information please visit http://oneinstack.com #
  307. #######################################################################
  308. "
  309. echo "`printf "%-28s" "Your domain:"`${CMSG}$domain${CEND}"
  310. echo "`printf "%-28s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  311. echo "`printf "%-28s" "Tomcat Virtualhost conf:"`${CMSG}$tomcat_install_dir/conf/vhost/$domain.xml${CEND}"
  312. echo "`printf "%-28s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  313. }
  314. Create_nginx_php-fpm_hhvm_conf() {
  315. [ ! -d $web_install_dir/conf/vhost ] && mkdir $web_install_dir/conf/vhost
  316. cat > $web_install_dir/conf/vhost/$domain.conf << EOF
  317. server {
  318. listen 80;
  319. server_name $domain$moredomainame;
  320. $N_log
  321. index index.html index.htm index.php;
  322. include $rewrite.conf;
  323. root $vhostdir;
  324. $anti_hotlinking
  325. $NGX_CONF
  326. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  327. expires 30d;
  328. access_log off;
  329. }
  330. location ~ .*\.(js|css)?$ {
  331. expires 7d;
  332. access_log off;
  333. }
  334. }
  335. EOF
  336. echo
  337. $web_install_dir/sbin/nginx -t
  338. if [ $? == 0 ];then
  339. echo "Reload Nginx......"
  340. $web_install_dir/sbin/nginx -s reload
  341. else
  342. rm -rf $web_install_dir/conf/vhost/$domain.conf
  343. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  344. exit 1
  345. fi
  346. printf "
  347. #######################################################################
  348. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  349. # For more information please visit http://oneinstack.com #
  350. #######################################################################
  351. "
  352. echo "`printf "%-20s" "Your domain:"`${CMSG}$domain${CEND}"
  353. echo "`printf "%-20s" "Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  354. echo "`printf "%-20s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  355. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-20s" "Rewrite rule:"`${CMSG}$rewrite${CEND}"
  356. }
  357. Apache_log() {
  358. while :
  359. do
  360. echo
  361. read -p "Allow Apache access_log? [y/n]: " access_yn
  362. if [ "$access_yn" != 'y' ] && [ "$access_yn" != 'n' ];then
  363. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  364. else
  365. break
  366. fi
  367. done
  368. if [ "$access_yn" == 'n' ]; then
  369. A_log='CustomLog "/dev/null" common'
  370. else
  371. A_log="CustomLog \"$wwwlogs_dir/${domain}_apache.log\" common"
  372. echo "You access log file=$wwwlogs_dir/${domain}_apache.log"
  373. fi
  374. }
  375. Create_apache_conf() {
  376. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  377. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  378. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  379. <VirtualHost *:80>
  380. ServerAdmin admin@linuxeye.com
  381. DocumentRoot "$vhostdir"
  382. ServerName $domain
  383. $Apache_Domain_alias
  384. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  385. $A_log
  386. <Directory "$vhostdir">
  387. SetOutputFilter DEFLATE
  388. Options FollowSymLinks
  389. $R_TMP
  390. AllowOverride All
  391. Order allow,deny
  392. Allow from all
  393. DirectoryIndex index.html index.php
  394. </Directory>
  395. </VirtualHost>
  396. EOF
  397. echo
  398. $apache_install_dir/bin/apachectl -t
  399. if [ $? == 0 ];then
  400. echo "Restart Apache......"
  401. /etc/init.d/httpd restart
  402. else
  403. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  404. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  405. exit 1
  406. fi
  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 "%-20s" "Your domain:"`${CMSG}$domain${CEND}"
  414. echo "`printf "%-20s" "Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  415. echo "`printf "%-20s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  416. }
  417. Create_nginx_apache_mod-php_conf() {
  418. # Nginx/Tengine
  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. listen 80;
  423. server_name $domain$moredomainame;
  424. $N_log
  425. index index.html index.htm index.php;
  426. root $vhostdir;
  427. $anti_hotlinking
  428. location / {
  429. try_files \$uri @apache;
  430. }
  431. location @apache {
  432. proxy_pass http://127.0.0.1:88;
  433. include proxy.conf;
  434. }
  435. location ~ .*\.(php|php5)?$ {
  436. proxy_pass http://127.0.0.1:88;
  437. include proxy.conf;
  438. }
  439. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  440. expires 30d;
  441. access_log off;
  442. }
  443. location ~ .*\.(js|css)?$ {
  444. expires 7d;
  445. access_log off;
  446. }
  447. }
  448. EOF
  449. echo
  450. $web_install_dir/sbin/nginx -t
  451. if [ $? == 0 ];then
  452. echo "Reload Nginx......"
  453. $web_install_dir/sbin/nginx -s reload
  454. else
  455. rm -rf $web_install_dir/conf/vhost/$domain.conf
  456. echo "Create virtualhost ... [${CFAILURE}FAILED${CEND}]"
  457. fi
  458. # Apache
  459. [ "`$apache_install_dir/bin/apachectl -v | awk -F'.' /version/'{print $2}'`" == '4' ] && R_TMP='Require all granted' || R_TMP=
  460. [ ! -d $apache_install_dir/conf/vhost ] && mkdir $apache_install_dir/conf/vhost
  461. cat > $apache_install_dir/conf/vhost/$domain.conf << EOF
  462. <VirtualHost *:88>
  463. ServerAdmin admin@linuxeye.com
  464. DocumentRoot "$vhostdir"
  465. ServerName $domain
  466. $Apache_Domain_alias
  467. ErrorLog "$wwwlogs_dir/${domain}_error_apache.log"
  468. $A_log
  469. <Directory "$vhostdir">
  470. SetOutputFilter DEFLATE
  471. Options FollowSymLinks
  472. $R_TMP
  473. AllowOverride All
  474. Order allow,deny
  475. Allow from all
  476. DirectoryIndex index.html index.php
  477. </Directory>
  478. </VirtualHost>
  479. EOF
  480. echo
  481. $apache_install_dir/bin/apachectl -t
  482. if [ $? == 0 ];then
  483. echo "Restart Apache......"
  484. /etc/init.d/httpd restart
  485. else
  486. rm -rf $apache_install_dir/conf/vhost/$domain.conf
  487. exit 1
  488. fi
  489. printf "
  490. #######################################################################
  491. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  492. # For more information please visit http://oneinstack.com #
  493. #######################################################################
  494. "
  495. echo "`printf "%-28s" "Your domain:"`${CMSG}$domain${CEND}"
  496. echo "`printf "%-28s" "Nginx Virtualhost conf:"`${CMSG}$web_install_dir/conf/vhost/$domain.conf${CEND}"
  497. echo "`printf "%-28s" "Apache Virtualhost conf:"`${CMSG}$apache_install_dir/conf/vhost/$domain.conf${CEND}"
  498. echo "`printf "%-28s" "Directory of:"`${CMSG}$vhostdir${CEND}"
  499. [ "$rewrite_yn" == 'y' ] && echo "`printf "%-28s" "Rewrite rule:"`${CMSG}$rewrite${CEND}"
  500. }
  501. Add_Vhost() {
  502. if [ -e "$web_install_dir/sbin/nginx" -a ! -e "$apache_install_dir/modules/libphp5.so" ];then
  503. Choose_env
  504. Input_Add_domain
  505. Nginx_anti_hotlinking
  506. if [ "$NGX_FLAG" == 'java' ];then
  507. Nginx_log
  508. Create_nginx_tomcat_conf
  509. else
  510. Nginx_rewrite
  511. Nginx_log
  512. Create_nginx_php-fpm_hhvm_conf
  513. fi
  514. elif [ ! -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/modules/libphp5.so" ];then
  515. Choose_env
  516. Input_Add_domain
  517. Apache_log
  518. Create_apache_conf
  519. elif [ -e "$web_install_dir/sbin/nginx" -a -e "$apache_install_dir/modules/libphp5.so" ];then
  520. Choose_env
  521. Input_Add_domain
  522. Nginx_anti_hotlinking
  523. if [ "$NGX_FLAG" == 'java' ];then
  524. Nginx_log
  525. Create_nginx_tomcat_conf
  526. elif [ "$NGX_FLAG" == 'hhvm' ];then
  527. Nginx_rewrite
  528. Nginx_log
  529. Create_nginx_php-fpm_hhvm_conf
  530. elif [ "$NGX_FLAG" == 'php' ];then
  531. #Nginx_rewrite
  532. Nginx_log
  533. Apache_log
  534. Create_nginx_apache_mod-php_conf
  535. fi
  536. fi
  537. }
  538. Del_NGX_Vhost() {
  539. if [ -e "$web_install_dir/sbin/nginx" ];then
  540. [ -d "$web_install_dir/conf/vhost" ] && Domain_List=`ls $web_install_dir/conf/vhost | sed "s@.conf@@g"`
  541. if [ -n "$Domain_List" ];then
  542. echo
  543. echo "Virtualhost list:"
  544. echo ${CMSG}$Domain_List${CEND}
  545. while :
  546. do
  547. echo
  548. read -p "Please input a domain you want to delete: " domain
  549. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  550. echo "${CWARNING}input error! ${CEND}"
  551. else
  552. if [ -e "$web_install_dir/conf/vhost/${domain}.conf" ];then
  553. Directory=`grep ^root $web_install_dir/conf/vhost/${domain}.conf | awk -F'[ ;]' '{print $2}'`
  554. rm -rf $web_install_dir/conf/vhost/${domain}.conf
  555. $web_install_dir/sbin/nginx -s reload
  556. while :
  557. do
  558. echo
  559. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_NGX_wwwroot_yn
  560. if [ "$Del_NGX_wwwroot_yn" != 'y' ] && [ "$Del_NGX_wwwroot_yn" != 'n' ];then
  561. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  562. else
  563. break
  564. fi
  565. done
  566. if [ "$Del_NGX_wwwroot_yn" == 'y' ];then
  567. echo "Press Ctrl+c to cancel or Press any key to continue..."
  568. char=`get_char`
  569. rm -rf $Directory
  570. fi
  571. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  572. else
  573. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  574. fi
  575. break
  576. fi
  577. done
  578. else
  579. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  580. fi
  581. fi
  582. }
  583. Del_Apache_Vhost() {
  584. if [ -e "$apache_install_dir/conf/httpd.conf" ];then
  585. if [ -e "$web_install_dir/sbin/nginx" ];then
  586. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  587. /etc/init.d/httpd restart
  588. else
  589. Domain_List=`ls $apache_install_dir/conf/vhost | grep -v '0.conf' | sed "s@.conf@@g"`
  590. if [ -n "$Domain_List" ];then
  591. echo
  592. echo "Virtualhost list:"
  593. echo ${CMSG}$Domain_List${CEND}
  594. while :
  595. do
  596. echo
  597. read -p "Please input a domain you want to delete: " domain
  598. if [ -z "`echo $domain | grep '.*\..*'`" ]; then
  599. echo "${CWARNING}input error! ${CEND}"
  600. else
  601. if [ -e "$apache_install_dir/conf/vhost/${domain}.conf" ];then
  602. Directory=`grep '^<Directory' $apache_install_dir/conf/vhost/${domain}.conf | awk -F'"' '{print $2}'`
  603. rm -rf $apache_install_dir/conf/vhost/${domain}.conf
  604. /etc/init.d/httpd restart
  605. while :
  606. do
  607. echo
  608. read -p "Do you want to delete Virtul Host directory? [y/n]: " Del_Apache_wwwroot_yn
  609. if [ "$Del_Apache_wwwroot_yn" != 'y' ] && [ "$Del_Apache_wwwroot_yn" != 'n' ];then
  610. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  611. else
  612. break
  613. fi
  614. done
  615. if [ "$Del_Apache_wwwroot_yn" == 'y' ];then
  616. echo "Press Ctrl+c to cancel or Press any key to continue..."
  617. char=`get_char`
  618. rm -rf $Directory
  619. fi
  620. echo "${CSUCCESS}Domain: ${domain} has been deleted.${CEND}"
  621. else
  622. echo "${CWARNING}Virtualhost: $domain was not exist! ${CEND}"
  623. fi
  624. break
  625. fi
  626. done
  627. else
  628. echo "${CWARNING}Virtualhost was not exist! ${CEND}"
  629. fi
  630. fi
  631. fi
  632. }
  633. Del_Tomcat_Vhost() {
  634. if [ -e "$tomcat_install_dir/conf/server.xml" ] && [ -n "`grep ${domain}-vhost $tomcat_install_dir/conf/server.xml`" ];then
  635. sed -i /${domain}-vhost/d $tomcat_install_dir/conf/server.xml
  636. rm -rf $tomcat_install_dir/conf/vhost/${domain}.xml
  637. /etc/init.d/tomcat restart
  638. fi
  639. }
  640. if [ $# == 0 ];then
  641. Add_Vhost
  642. elif [ $# == 1 ];then
  643. case $1 in
  644. add)
  645. Add_Vhost
  646. ;;
  647. del)
  648. Del_NGX_Vhost
  649. Del_Apache_Vhost
  650. Del_Tomcat_Vhost
  651. ;;
  652. *)
  653. Usage
  654. ;;
  655. esac
  656. else
  657. Usage
  658. fi