create.sh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 [ ! -f $WORKSPACE/nginx.conf ]
  37. then
  38. # https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
  39. cat > $WORKSPACE/nginx.conf <<EOF
  40. # https://laravel.com/docs/9.x/deployment#nginx
  41. server {
  42. listen 60080;
  43. server_name ${1//_/-}.spring.wikipali.org;
  44. access_log $WORKSPACE/logs/access.org;
  45. error_log $WORKSPACE/logs/error.log;
  46. add_header X-Frame-Options "SAMEORIGIN";
  47. add_header X-Content-Type-Options "nosniff";
  48. root $WORKSPACE/htdocs/public;
  49. index index.html index.php;
  50. charset utf-8;
  51. gzip on;
  52. client_max_body_size 16M;
  53. location / {
  54. try_files \$uri \$uri/ /index.php?\$query_string;
  55. }
  56. location = /favicon.ico { access_log off; log_not_found off; }
  57. location = /robots.txt { access_log off; log_not_found off; }
  58. error_page 404 /index.php;
  59. location ~ \.php\$ {
  60. fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
  61. fastcgi_param SCRIPT_FILENAME \$realpath_root\$fastcgi_script_name;
  62. include fastcgi_params;
  63. }
  64. location ~ /\.(?!well-known).* {
  65. deny all;
  66. }
  67. }
  68. EOF
  69. ln -sf $WORKSPACE/nginx.conf /etc/nginx/sites-enabled/$1-spring.conf
  70. fi
  71. echo "done($1)."
  72. exit 0