vhost.sh 4.3 KB

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