vhost.sh 4.2 KB

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