user.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/bash
  2. set -e
  3. if [ "$#" -ne 1 ]
  4. then
  5. echo "Usage: $0 USER"
  6. exit 1
  7. fi
  8. apt -y install zsh git zip unzip bzip2 curl wget vim pwgen
  9. if id "$1" &>/dev/null
  10. then
  11. echo "user $1 found"
  12. else
  13. echo "create user $1"
  14. useradd -m -d /workspace/home/$1 -s /bin/zsh $1
  15. fi
  16. echo 'reset password'
  17. passwd -l $1
  18. echo "$1:$(pwgen 32 1)" | chpasswd
  19. echo 'setup nginx'
  20. if [ ! -d /workspace/www/$1/htdocs ]
  21. then
  22. mkdir -p /workspace/www/$1/htdocs
  23. chown $1:$1 /workspace/www/$1/htdocs
  24. fi
  25. if [ ! -d /workspace/www/$1/logs ]
  26. then
  27. mkdir -p /workspace/www/$1/logs
  28. chown www-data:www-data /workspace/www/$1/logs
  29. fi
  30. if [ ! -f /workspace/www/$1/nginx.conf ]
  31. then
  32. # https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
  33. cat > /workspace/www/$1/nginx.conf <<EOF
  34. server {
  35. listen 80;
  36. root /workspace/www/$1/htdocs;
  37. index index.html index.php;
  38. server_name ${1//_/-}.spring.wikipali.org;
  39. access_log /workspace/www/$1/logs/access.org;
  40. error_log /workspace/www/$1/logs/error.log;
  41. location / {
  42. try_files \$uri \$uri/ =404;
  43. }
  44. location ~ \.php$ {
  45. include snippets/fastcgi-php.conf;
  46. fastcgi_pass unix:/run/php/php-fpm.sock;
  47. }
  48. }
  49. EOF
  50. ln -sf /workspace/www/$1/nginx.conf /etc/nginx/sites-enabled/$1.spring.wikipali.org.conf
  51. fi
  52. echo "done($1)."
  53. exit 0