vhost.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. echo '\033[35m
  2. __ __
  3. _ __/ /_ ____ _____/ /_
  4. | | / / __ \/ __ \/ ___/ __/
  5. | |/ / / / / /_/ (__ ) /_
  6. |___/_/ /_/\____/____/\__/
  7. \033[0m'
  8. echo "\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. while [ "$_domain" = "" ]
  17. do
  18. read -p "enter a hostname ? " _domain
  19. if [ "$_domain" != "" ]; then
  20. read -p "is hostname $_domain correcte [y|n] " validated
  21. if [ "$validated" = "y" ]; then
  22. break
  23. else
  24. _domain=""
  25. fi
  26. fi
  27. done
  28. # ask for simple php conf or drupal conf
  29. while [ "$_drupal" != "yes" ] && [ "$_drupal" != "no" ]
  30. do
  31. echo -n "Is your site is a drupal one? [yes|no] "
  32. read _drupal
  33. done
  34. # ask for let's encrypt
  35. while [ "$_letsencrypt" != "yes" ] && [ "$_letsencrypt" != "no" ]
  36. do
  37. echo "Let's encrypt"
  38. 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)."
  39. echo -n "Should we install let's encrypt certificate with $_domain? [yes|no] "
  40. read _letsencrypt
  41. done
  42. # lets'encrypt
  43. # https://certbot.eff.org/lets-encrypt/debianstretch-nginx
  44. if [ "$_letsencrypt" = "yes" ]; then
  45. apt-get install certbot
  46. certbot certonly --cert-name "$_domain" --standalone –d "$_domain"
  47. openssl dhparam –out /etc/nginx/dhparam.pem 2048
  48. # TODO renewing
  49. touch /var/spool/crontab/root
  50. crontab -l > mycron
  51. echo "0 3 * * * certbot renew --pre-hook 'systemctl stop nginx' --post-hook 'systemctl start nginx' --cert-name $_domain" >> mycron
  52. crontab mycron
  53. rm mycron
  54. fi
  55. if [ "$_drupal" = "yes" ]; then
  56. if [ "$_letsencrypt" = "yes" ]; then
  57. _conffile = "drupal-ssl.nginxconf"
  58. else
  59. _conffile = "drupal.nginxconf"
  60. fi
  61. else
  62. if [ "$_letsencrypt" = "yes" ]; then
  63. _conffile = "simple-phpfpm-ssl.nginxconf"
  64. else
  65. _conffile = "simple-phpfpm.nginxconf"
  66. fi
  67. fi
  68. cp "$_cwd"/assets/"$_conffile" /etc/nginx/sites-available/"$_domain".conf
  69. sed -ir "s/DOMAIN\.LTD/$_domain/g" /etc/nginx/sites-available/"$_domain".conf
  70. mkdir -p /var/www/"$_domain"/public_html
  71. mkdir /var/www/"$_domain"/logs
  72. #set proper right to user will handle the app
  73. chown -R root:admin /var/www/"$_domain"/
  74. chmod -R g+w /var/www/"$_domain"/
  75. chmod -R g+r /var/www/"$_domain"/
  76. # create a shortcut to the site
  77. # TODO ask for $user name if not existing
  78. mkdir /home/"$user"/www/
  79. chown "$user":admin /home/"$user"/www/
  80. ln -s /var/www/"$_domain" /home/"$user"/www/"$_domain"
  81. # activate the vhost
  82. ln -s /etc/nginx/sites-available/"$_domain".conf /etc/nginx/sites-enabled/"$_domain".conf
  83. # restart nginx
  84. systemctl restart nginx
  85. echo "\033[92;1mvhost $_domain configured\033[Om"
  86. else
  87. echo "Vhost installation aborted"
  88. fi