ララベルアプリ本番公開完全マニュアル

【1】前提条件

  • サーバーに以下がインストール済みであること

    • nginx

    • PHP 8.1以上

    • MySQL 5.7以上 or MariaDB

    • Composer

    • Node.js 16以上

    • certbot (Let’s Encrypt)

  • Laravelプロジェクトはgit管理されていること

  • SSH秘密鍵でサーバーに接続できること

  • DNS設定が済んでいること


【2】基本のデプロイ手順

2-1. Git clone

cd /var/www/
git clone git@github.com:yourname/your-laravel-project.git shanai-blog
cd shanai-blog

2-2. .env作成、編集

cp .env.example .env
vim .env

編集ポイント:

2-3. 権限設定

sudo chown -R nginx:nginx storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache

2-4. Composer install

composer install --no-dev --optimize-autoloader

2-5. APP_KEY生成

php artisan key:generate

2-6. DBマイグレーション

php artisan migrate --force

2-7. vite本番ビルド

npm install
npm run build

2-8. nginx設定作成

sudo vi /etc/nginx/conf.d/app1.conf

例:

server {
    listen 80;
    server_name app1.test-app-first-service.online;

    root /var/www/shanai-blog/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }
}

2-9. nginx再起動

sudo nginx -t
sudo systemctl reload nginx

2-10. SSL化 (Let’s Encrypt)

sudo yum install certbot python3-certbot-nginx
sudo certbot --nginx -d app1.test-app-first-service.online

2-11. 権限再確認

sudo chown -R nginx:nginx storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache

2-12. ブラウザ確認

例:

https://app1.test-app-first-service.online

【3】よくあるトラブルシューティング集

3-1. 500 Internal Server Error

  • storage/bootstrap/cacheの権限誤り

  • APP_KEY未生成

  • DB接続エラー

対処:

sudo chown -R nginx:nginx storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
php artisan key:generate
php artisan config:clear
php artisan config:cache

3-2. Vite manifest not found

対処:

npm install
npm run build

3-3. CSS/JSが読み込まれない

  • .envのAPP_URL/ASSET_URLがhttpのまま

  • viteビルドが古い

対処:

  • .envをhttpsに直してnpm run build

3-4. リダイレクト不実装

対処: nginxで以下を追加

server {
    listen 80;
    server_name your-domain.com;

    return 301 https://$server_name$request_uri;
}

3-5. GitHub Actionsデプロイ失敗

  • Secrets設定欠如

  • deploy.ymlパス誤り

  • サーバー側の権限誤り

対処:

  • Secrets再確認

  • deploy.yml読み直し

  • storageパーミッション再設定


【4】GitHub Actions 自動デプロイテンプレート

.github/workflows/deploy.yml

name: Deploy Laravel to Server

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Setup SSH
        uses: webfactory/ssh-agent@v0.7.0
        with:
          ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}

      - name: Deploy to Server
        run: |
          ssh -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }} "
            cd /var/www/shanai-blog &&
            git pull origin main &&
            composer install --no-dev --optimize-autoloader &&
            php artisan migrate --force &&
            npm install &&
            npm run build &&
            sudo chown -R nginx:nginx storage bootstrap/cache &&
            sudo chmod -R 775 storage bootstrap/cache
          "

Secretsで必要なもの:

  • HOST

  • USERNAME

  • SSH_PRIVATE_KEY

デプロイの設定でうまくいかない時

nginxとポート解放

  • ubuntu debian系は yum をaptに読み替える
  • nginxの設定ファルの置き場所と内容が少し変わる。
    • ubuntu では/etc/nginx/conf.d ではなくて、sites-enabled に confを作成
    • nginx のphp設定を変更
      location ~ \.php$ {
      include snippets/fastcgi-php.conf;
      fastcgi_pass unix:/run/php/php8.3-fpm.sock;
      }
sudo ufw allow 443
sudo ufw allow 443/tcp
sudo ufw reload

sudo ufw status

php8.3-fpm インストール

sudo apt update
sudo apt install php8.3-fpm

sudo systemctl status php8.3-fpm

権限設定

  • ubuntu debian はnginx ユーザーがいない->www-data
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
  • 所有者の確認 3列目がユーザー4列目がグループ
ls -ld storage
drwxrwxr-x 5 www-data www-data 4096 Apr 29 13:48 storage