vhost.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 domain name ? " _domain
  30. if [ "$_domain" != "" ]; then
  31. read -p "is domain $_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 "\033[35;1mLet's encrypt \033[0m"
  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. systemctl stop nginx
  54. # lets'encrypt
  55. # https://certbot.eff.org/lets-encrypt/debianstretch-nginx
  56. if [ "$_letsencrypt" = "yes" ]; then
  57. apt-get --yes --force-yes install certbot
  58. certbot certonly --standalone -d "$_domain" --cert-name "$_domain"
  59. # TODO stop the whole process if letsencrypt faile
  60. mkdir -p /etc/nginx/ssl/certs/"$_domain"
  61. openssl dhparam -out /etc/nginx/ssl/certs/"$_domain"/dhparam.pem 2048
  62. # renewing
  63. touch /var/spool/cron/crontabs/root
  64. crontab -l > mycron
  65. echo -e "0 3 * * * certbot renew --pre-hook 'systemctl stop nginx' --post-hook 'systemctl start nginx' --cert-name $_domain" >> mycron
  66. crontab mycron
  67. rm mycron
  68. fi
  69. if [ "$_drupal" = "yes" ]; then
  70. if [ "$_letsencrypt" = "yes" ]; then
  71. _conffile="drupal-ssl.nginxconf"
  72. else
  73. _conffile="drupal.nginxconf"
  74. fi
  75. else
  76. if [ "$_letsencrypt" = "yes" ]; then
  77. _conffile="simple-phpfpm-ssl.nginxconf"
  78. else
  79. _conffile="simple-phpfpm.nginxconf"
  80. fi
  81. fi
  82. cp "$_assets/$_conffile" /etc/nginx/sites-available/"$_domain".conf
  83. sed -i -r "s/DOMAIN\.LTD/$_domain/g" /etc/nginx/sites-available/"$_domain".conf
  84. mkdir -p /var/www/"$_domain"/public_html
  85. mkdir /var/www/"$_domain"/log
  86. cp "$_assets/index.php" /var/www/"$_domain"/public_html/
  87. sed -i -r "s/DOMAIN\.LTD/$_domain/g" /var/www/"$_domain"/public_html/index.php
  88. #set proper right to user will handle the app
  89. chown -R root:admin /var/www/"$_domain"/
  90. chmod -R g+w /var/www/"$_domain"/
  91. chmod -R g+r /var/www/"$_domain"/
  92. # create a shortcut to the site
  93. echo -n "Should we install a shortcut for a user? [Y|n] "
  94. read yn
  95. yn=${yn:-y}
  96. if [ "$yn" = "Y" ] || [ "$yn" = "y" ]; then
  97. # if $user var does not exists (vhost.sh ran directly) ask for it
  98. if [ -z ${user+x} ]; then
  99. while [ "$user" = "" ]
  100. do
  101. read -p "enter an existing user name ? " user
  102. if [ "$user" != "" ]; then
  103. # check if user already exists
  104. if id "$user" >/dev/null 2>&1; then
  105. read -p "is user name $user correcte [y|n] " validated
  106. if [ "$validated" = "y" ]; then
  107. break
  108. else
  109. user=""
  110. fi
  111. else
  112. echo -e "user $user doesn't exists, you must provide an existing user"
  113. user=""
  114. fi
  115. fi
  116. done
  117. fi
  118. echo -e "shortcut will be installed for '$user'";
  119. sleep 3
  120. mkdir /home/"$user"/www/
  121. chown "$user":admin /home/"$user"/www/
  122. ln -s /var/www/"$_domain" /home/"$user"/www/"$_domain"
  123. chown "$user":admin /home/"$user"/www/"$_domain"
  124. else
  125. echo -e 'no shortcut installed'
  126. fi
  127. # activate the vhost
  128. ln -s /etc/nginx/sites-available/"$_domain".conf /etc/nginx/sites-enabled/"$_domain".conf
  129. # restart nginx
  130. systemctl start nginx
  131. echo -e "\033[92;1mvhost $_domain configured \033[Om"
  132. else
  133. echo -e "Vhost installation aborted"
  134. fi