vhost.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/bin/bash
  2. echo -e '
  3. _ _
  4. __ _| |_ ___ __| |_
  5. \ V / ` \/ _ (_-< _|
  6. \_/|_||_\___/__/\__|
  7. '
  8. echo -e "Nginx VHOST install "
  9. . bin/checkroot.sh
  10. # get the current position
  11. _cwd="$(pwd)"
  12. # check for assets forlder
  13. _assets="$_cwd/assets"
  14. if [ ! -d "$_assets" ]; then
  15. _assets="$_cwd/../assets"
  16. if [ ! -d "$_assets" ]; then
  17. echo "!! can't find assets directory !!"
  18. exit
  19. fi
  20. fi
  21. while [ "$_domain" = "" ]
  22. do
  23. read -p "enter a domain name ? " _domain
  24. if [ "$_domain" != "" ]; then
  25. read -p "is domain $_domain correcte [y|n] " validated
  26. if [ "$validated" = "y" ]; then
  27. break
  28. else
  29. _domain=""
  30. fi
  31. fi
  32. done
  33. # ask for simple php conf or drupal conf
  34. while [ "$_drupal" != "yes" ] && [ "$_drupal" != "no" ]
  35. do
  36. echo -n "Is your site is a drupal one? [yes|no] "
  37. read _drupal
  38. done
  39. # ask for let's encrypt
  40. while [ "$_letsencrypt" != "yes" ] && [ "$_letsencrypt" != "no" ]
  41. do
  42. echo -e "Let's encrypt"
  43. echo -e "Let's encrypt needs a public registered domain name with proper DNS records ( A records or CNAME records for subdomains pointing to your server)."
  44. echo -n "Should we install let's encrypt certificate with $_domain? [yes|no] "
  45. read _letsencrypt
  46. done
  47. service nginx stop
  48. # lets'encrypt
  49. # https://certbot.eff.org/lets-encrypt/debianstretch-nginx
  50. if [ "$_letsencrypt" = "yes" ]; then
  51. apk add certbot
  52. certbot certonly --standalone -d "$_domain" --cert-name "$_domain"
  53. # TODO stop the whole process if letsencrypt faile
  54. mkdir -p /etc/nginx/ssl/certs/"$_domain"
  55. openssl dhparam -out /etc/nginx/ssl/certs/"$_domain"/dhparam.pem 2048
  56. # renewing
  57. touch /var/spool/cron/crontabs/root
  58. crontab -l > mycron
  59. echo -e "0 3 * * * certbot renew --pre-hook 'service nginx stop' --post-hook 'service nginx start' --cert-name $_domain" >> mycron
  60. crontab mycron
  61. rm -f mycron
  62. fi
  63. if [ "$_drupal" = "yes" ]; then
  64. if [ "$_letsencrypt" = "yes" ]; then
  65. _conffile="drupal-ssl.nginxconf"
  66. else
  67. _conffile="drupal.nginxconf"
  68. fi
  69. else
  70. if [ "$_letsencrypt" = "yes" ]; then
  71. _conffile="simple-phpfpm-ssl.nginxconf"
  72. else
  73. _conffile="simple-phpfpm.nginxconf"
  74. fi
  75. fi
  76. cp "$_assets/vhosts/$_conffile" /etc/nginx/conf.d/"$_domain".conf
  77. sed -i -r "s/DOMAIN\.LTD/$_domain/g" /etc/nginx/conf.d/"$_domain".conf
  78. mkdir -p /var/www/"$_domain"/app/public_html
  79. mkdir /var/www/"$_domain"/log
  80. cp "$_assets/vhosts/index.php" /var/www/"$_domain"/app/public_html/
  81. sed -i -r "s/DOMAIN\.LTD/$_domain/g" /var/www/"$_domain"/app/public_html/index.php
  82. #set proper right to user will handle the app
  83. chown -R www:www /var/www/"$_domain"/
  84. # chmod -R g+w /var/www/"$_domain"/
  85. # chmod -R g+r /var/www/"$_domain"/
  86. # create a shortcut to the site
  87. echo -n "Should we install a shortcut for a user? [Y|n] "
  88. read yn
  89. yn=${yn:-y}
  90. if [ "$yn" = "Y" ] || [ "$yn" = "y" ]; then
  91. # if $user var does not exists (vhost.sh ran directly) ask for it
  92. if [ -z ${user+x} ]; then
  93. while [ "$user" = "" ]
  94. do
  95. read -p "enter an existing user name ? " user
  96. if [ "$user" != "" ]; then
  97. # check if user already exists
  98. if id "$user" >/dev/null 2>&1; then
  99. read -p "is user name $user correcte [y|n] " validated
  100. if [ "$validated" = "y" ]; then
  101. break
  102. else
  103. user=""
  104. fi
  105. else
  106. echo -e "user $user doesn't exists, you must provide an existing user"
  107. user=""
  108. fi
  109. fi
  110. done
  111. fi
  112. echo -e "installing shortcut for '$user'";
  113. mkdir /home/"$user"/www/
  114. chown "$user":"$user" /home/"$user"/www/
  115. ln -s /var/www/"$_domain" /home/"$user"/www/"$_domain"
  116. chown "$user":"$user" /home/"$user"/www/"$_domain"
  117. chown -R www:"$user" /home/"$user"/www/"$_domain"/app
  118. chmod -R g+rw /home/"$user"/www/"$_domain"/app
  119. . bin/gitdeploy.sh
  120. else
  121. echo -e 'no shortcut installed'
  122. fi
  123. # activate the vhost
  124. # ln -s /etc/nginx/sites-available/"$_domain".conf /etc/nginx/sites-enabled/"$_domain".conf
  125. nginx -t
  126. # restart nginx
  127. service nginx start
  128. echo -e "vhost $_domain configured"