create.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/bash
  2. set -e
  3. if [ "$#" -ne 1 ]
  4. then
  5. echo "Usage: $0 USER"
  6. exit 1
  7. fi
  8. pacman -S --needed 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 /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. export WORKSPACE=/srv/http/$1
  21. if [ ! -d $WORKSPACE/htdocs ]
  22. then
  23. mkdir -p $WORKSPACE/htdocs
  24. chown $1:$1 $WORKSPACE/htdocs
  25. fi
  26. if [ ! -d $WORKSPACE/logs ]
  27. then
  28. mkdir -p $WORKSPACE/logs
  29. chown http:http $WORKSPACE/logs
  30. fi
  31. if [ ! -d $WORKSPACE/tmp ]
  32. then
  33. mkdir -p /workspace/tmp/$1
  34. chown $1:$1 /workspace/tmp/$1
  35. fi
  36. if [ ! -d $WORKSPACE/dashboard ]
  37. then
  38. mkdir -p /workspace/dashboard/$1
  39. chown $1:$1 /workspace/dashboard/$1
  40. fi
  41. if [ ! -f $WORKSPACE/nginx.conf ]
  42. then
  43. # https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
  44. cat > $WORKSPACE/nginx.conf <<EOF
  45. # https://laravel.com/docs/9.x/deployment#nginx
  46. server {
  47. listen 60080;
  48. server_name ${1//_/-}.spring.wikipali.org;
  49. access_log $WORKSPACE/logs/access.org;
  50. error_log $WORKSPACE/logs/error.log;
  51. add_header X-Frame-Options "SAMEORIGIN";
  52. add_header X-Content-Type-Options "nosniff";
  53. root $WORKSPACE/htdocs/public;
  54. index index.html index.php;
  55. charset utf-8;
  56. gzip on;
  57. client_max_body_size 16M;
  58. location / {
  59. try_files \$uri \$uri/ /index.php?\$query_string;
  60. }
  61. location /my/ {
  62. alias $WORKSPACE/dashboard/;
  63. try_files \$uri \$uri/ /my/index.html;
  64. location ~* \.(css|js|png|jpg|jpeg|gif|gz|svg|mp4|ogg|ogv|webm|htc|xml|woff)$ {
  65. access_log off;
  66. expires max;
  67. }
  68. }
  69. location = /favicon.ico { access_log off; log_not_found off; }
  70. location = /robots.txt { access_log off; log_not_found off; }
  71. error_page 404 /index.php;
  72. location ~ \.php\$ {
  73. fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
  74. fastcgi_param SCRIPT_FILENAME \$realpath_root\$fastcgi_script_name;
  75. include fastcgi_params;
  76. }
  77. location ~ /\.(?!well-known).* {
  78. deny all;
  79. }
  80. }
  81. EOF
  82. ln -sf $WORKSPACE/nginx.conf /etc/nginx/sites-enabled/$1-spring.conf
  83. fi
  84. echo "done($1)."
  85. exit 0