選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

125 行
3.3KB

  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then
  4. echo "Usage: scripts/deploy-raspi.sh <ssh-host> <git-repo> [domain]"
  5. exit 1
  6. fi
  7. ssh_host="$1"
  8. repo_url="$2"
  9. domain="${3:-DietPi.ntp.kentcommunications.com}"
  10. deploy_path="${DEPLOY_PATH:-/var/www/warehouse}"
  11. deploy_ref="${DEPLOY_REF:-main}"
  12. php_version="${PHP_VERSION:-8.2}"
  13. ssh "$ssh_host" bash -s -- "$repo_url" "$domain" "$deploy_path" "$deploy_ref" "$php_version" <<'REMOTE'
  14. set -euo pipefail
  15. repo_url="$1"
  16. domain="$2"
  17. deploy_path="$3"
  18. deploy_ref="$4"
  19. php_version="$5"
  20. owner="$(id -un)"
  21. group="$(id -gn)"
  22. php_fpm_service="php${php_version}-fpm"
  23. php_fpm_socket="/run/php/${php_fpm_service}.sock"
  24. set_env() {
  25. file="$1"
  26. key="$2"
  27. value="$3"
  28. if grep -q "^${key}=" "$file"; then
  29. sed -i "s|^${key}=.*|${key}=${value}|" "$file"
  30. else
  31. printf '%s=%s\n' "$key" "$value" >> "$file"
  32. fi
  33. }
  34. sudo apt-get update
  35. sudo apt-get install -y git nginx composer \
  36. "php${php_version}" "php${php_version}-cli" "php${php_version}-fpm" \
  37. "php${php_version}-mbstring" "php${php_version}-xml" \
  38. "php${php_version}-curl" "php${php_version}-zip" \
  39. "php${php_version}-sqlite3" "php${php_version}-mysql"
  40. sudo mkdir -p "$(dirname "$deploy_path")"
  41. if [ ! -d "$deploy_path/.git" ]; then
  42. sudo git clone --branch "$deploy_ref" --single-branch "$repo_url" "$deploy_path"
  43. sudo chown -R "$owner:$group" "$deploy_path"
  44. else
  45. git -C "$deploy_path" fetch --prune origin
  46. git -C "$deploy_path" checkout "$deploy_ref"
  47. git -C "$deploy_path" reset --hard "origin/$deploy_ref"
  48. fi
  49. cd "$deploy_path"
  50. if [ ! -f .env ] && [ -f .env.example ]; then cp .env.example .env; fi
  51. set_env .env APP_ENV prod
  52. set_env .env DB_CONNECTION sqlite
  53. set_env .env DB_SQLITE_PATH "$deploy_path/database/database.sqlite"
  54. composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader --ignore-platform-reqs
  55. mkdir -p storage/framework/views database
  56. touch database/database.sqlite
  57. sudo chgrp -R www-data storage database
  58. sudo chmod -R g+rwX storage database
  59. sudo find storage database -type d -exec chmod 2775 {} \;
  60. sudo chmod 664 database/database.sqlite
  61. php command.php migrate
  62. sudo tee /etc/systemd/system/warehouse-queue.service >/dev/null <<EOF
  63. [Unit]
  64. Description=Warehouse queue worker
  65. After=network.target
  66. [Service]
  67. Type=simple
  68. User=${owner}
  69. Group=www-data
  70. WorkingDirectory=${deploy_path}
  71. ExecStart=/usr/bin/php ${deploy_path}/command.php queue:work
  72. Restart=always
  73. RestartSec=3
  74. [Install]
  75. WantedBy=multi-user.target
  76. EOF
  77. sudo tee /etc/nginx/sites-available/warehouse >/dev/null <<EOF
  78. server {
  79. listen 80;
  80. listen [::]:80;
  81. server_name ${domain};
  82. root ${deploy_path}/public;
  83. index index.php;
  84. location / {
  85. try_files \$uri \$uri/ /index.php?\$query_string;
  86. }
  87. location ~ \.php$ {
  88. include snippets/fastcgi-php.conf;
  89. fastcgi_pass unix:${php_fpm_socket};
  90. }
  91. location ~ /\. {
  92. deny all;
  93. }
  94. }
  95. EOF
  96. sudo ln -sfn /etc/nginx/sites-available/warehouse /etc/nginx/sites-enabled/warehouse
  97. sudo rm -f /etc/nginx/sites-enabled/default
  98. sudo systemctl daemon-reload
  99. sudo systemctl enable "$php_fpm_service" nginx warehouse-queue.service
  100. sudo systemctl restart "$php_fpm_service"
  101. sudo nginx -t
  102. sudo systemctl restart nginx
  103. sudo systemctl restart warehouse-queue.service
  104. echo "Deployment complete: http://${domain}"
  105. REMOTE

Powered by TurnKey Linux.