vhost.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. echo -e '\033[35m
  2. __ __
  3. _ __/ /_ ____ _____/ /_
  4. | | / / __ \/ __ \/ ___/ __/
  5. | |/ / / / / /_/ (__ ) /_
  6. |___/_/ /_/\____/____/\__/
  7. \033[0m'
  8. echo -e "\033[35;1mNginx VHOST install \033[0m"
  9. while [ "$vh" != "y" ] && [ "$vh" != "n" ]
  10. do
  11. echo -n "Should we install a vhost? [y|n] "
  12. read vh
  13. # vh=${vh:-y}
  14. done
  15. if [ "$vh" = "y" ]; then
  16. # get the current position
  17. _cwd="$(pwd)"
  18. # check for assets forlder
  19. _assets="$_cwd/assets"
  20. if [ ! -d "$_assets" ]; then
  21. _assets="$_cwd/../assets"
  22. if [ ! -d "$_assets" ]; then
  23. echo "!! can't find assets directory !!"
  24. exit
  25. fi
  26. fi
  27. while [ "$_domain" = "" ]
  28. do
  29. read -p "enter a hostname ? " _domain
  30. if [ "$_domain" != "" ]; then
  31. read -p "is hostname $_domain correcte [y|n] " validated
  32. if [ "$validated" = "y" ]; then
  33. break
  34. else
  35. _domain=""
  36. fi
  37. fi
  38. done
  39. # ask for simple php conf or drupal conf
  40. while [ "$_drupal" != "yes" ] && [ "$_drupal" != "no" ]
  41. do
  42. echo -n "Is your site is a drupal one? [yes|no] "
  43. read _drupal
  44. done
  45. # ask for let's encrypt
  46. while [ "$_letsencrypt" != "yes" ] && [ "$_letsencrypt" != "no" ]
  47. do
  48. echo -e "Let's encrypt"
  49. 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)."
  50. echo -n "Should we install let's encrypt certificate with $_domain? [yes|no] "
  51. read _letsencrypt
  52. done
  53. # lets'encrypt
  54. # https://certbot.eff.org/lets-encrypt/debianstretch-nginx
  55. if [ "$_letsencrypt" = "yes" ]; then
  56. apt-get --yes --force-yes install certbot
  57. certbot certonly --cert-name "$_domain" --standalone –d "$_domain"
  58. openssl dhparam –out /etc/nginx/dhparam.pem 2048
  59. # TODO renewing
  60. touch /var/spool/crontab/root
  61. crontab -l > mycron
  62. echo -e "0 3 * * * certbot renew --pre-hook 'systemctl stop nginx' --post-hook 'systemctl start nginx' --cert-name $_domain" >> mycron
  63. crontab mycron
  64. rm mycron
  65. fi
  66. if [ "$_drupal" = "yes" ]; then
  67. if [ "$_letsencrypt" = "yes" ]; then
  68. _conffile = "drupal-ssl.nginxconf"
  69. else
  70. _conffile = "drupal.nginxconf"
  71. fi
  72. else
  73. if [ "$_letsencrypt" = "yes" ]; then
  74. _conffile = "simple-phpfpm-ssl.nginxconf"
  75. else
  76. _conffile = "simple-phpfpm.nginxconf"
  77. fi
  78. fi
  79. cp "$_assets/$_conffile" /etc/nginx/sites-available/"$_domain".conf
  80. sed -ir "s/DOMAIN\.LTD/$_domain/g" /etc/nginx/sites-available/"$_domain".conf
  81. mkdir -p /var/www/"$_domain"/public_html
  82. mkdir /var/www/"$_domain"/logs
  83. #set proper right to user will handle the app
  84. chown -R root:admin /var/www/"$_domain"/
  85. chmod -R g+w /var/www/"$_domain"/
  86. chmod -R g+r /var/www/"$_domain"/
  87. # create a shortcut to the site
  88. # TODO ask for $user name if not existing
  89. echo -n "Should we install a shortcut for a user? [Y|n] "
  90. read yn
  91. yn=${yn:-y}
  92. if [ "$yn" = "y" ]; then
  93. if [ -z ${user+x} ]; then
  94. echo -n "Enter an existing user name: "
  95. read user
  96. while [ "$user" = "" ]
  97. do
  98. read -p "enter a user name ? " user
  99. if [ "$user" != "" ]; then
  100. check if user already exists
  101. if id "$user" >/dev/null 2>&1; then
  102. read -p "is user name $user correcte [y|n] " validated
  103. if [ "$validated" = "y" ]; then
  104. break
  105. else
  106. user=""
  107. fi
  108. else
  109. echo -e "user $user doesn't exists, you must provide an existing user"
  110. user=""
  111. fi
  112. fi
  113. done
  114. fi
  115. echo -e "shortcut will be installed for '$user'";
  116. sleep 3
  117. mkdir /home/"$user"/www/
  118. chown "$user":admin /home/"$user"/www/
  119. ln -s /var/www/"$_domain" /home/"$user"/www/"$_domain"
  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. # restart nginx
  126. systemctl restart nginx
  127. echo -e "\033[92;1mvhost $_domain configured \033[Om"
  128. else
  129. echo -e "Vhost installation aborted"
  130. fi