目次
【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
編集ポイント:
APP_NAMEAPP_ENV=productionAPP_URL=https://your-domain.comASSET_URL=https://your-domain.comDB_DATABASE/DB_USERNAME/DB_PASSWORD
2-3. 権限設定
sudo chown -R www-data:www-data /var/www/shanai-blog
sudo chmod -R 775 /var/www/shanai-blog/storage
sudo chmod -R 775 /var/www/shanai-blog/bootstrap/cache
2-4. Composerインストール
composer install --optimize-autoloader --no-dev
2-5. アプリケーションキー生成
php artisan key:generate
2-6. マイグレーション実行
php artisan migrate --force
2-7. ストレージリンク作成
php artisan storage:link
2-8. フロントエンドビルド
npm ci
npm run build
2-9. 最適化キャッシュ
php artisan config:cache
php artisan route:cache
php artisan view:cache
【3】Nginx設定
/etc/nginx/sites-available/shanai-blog を作成
server {
listen 80;
server_name your-domain.com;
root /var/www/shanai-blog/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ .php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; # PHPのバージョンに合わせて変更
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.(?!well-known).* {
deny all;
}
}
シンボリックリンク作成とNginx再起動:
sudo ln -s /etc/nginx/sites-available/shanai-blog /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
【4】Supervisor設定(キューやWebSocketを使う場合)
/etc/supervisor/conf.d/shanai-blog-worker.conf
[program:shanai-blog-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/shanai-blog/artisan queue:work --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/var/www/shanai-blog/storage/logs/worker.log
stopwaitsecs=3600
Supervisorの反映:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start shanai-blog-worker:*
【5】SSL証明書設定(Let's Encrypt)
sudo certbot --nginx -d your-domain.com
【6】GitHub Actionsでの自動デプロイ例
.github/workflows/deploy.yml
name: Deploy to Production
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Deploy via SSH
uses: appleboy/ssh-action@v0.1.10
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
cd /var/www/shanai-blog
git pull origin main
composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev
php artisan migrate --force
npm ci
npm run build
php artisan config:cache
php artisan route:cache
php artisan view:cache
sudo systemctl reload nginx
sudo supervisorctl restart all