vhost.sh 4.1 KB

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