Deploy Laravel lên Production
# .env production settings
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com
# Cache everything
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
# Optimize autoloader
composer install --optimize-autoloader --no-dev
# Run migrations
php artisan migrate --force
# Clear & optimize
php artisan optimize
# Dockerfile
FROM php:8.3-fpm-alpine
# Install extensions
RUN docker-php-ext-install pdo pdo_mysql opcache
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/html
# Copy application
COPY . .
# Install dependencies
RUN composer install --optimize-autoloader --no-dev
# Set permissions
RUN chown -R www-data:www-data storage bootstrap/cache
# Optimize
RUN php artisan config:cache && \
php artisan route:cache && \
php artisan view:cache
EXPOSE 9000
CMD ["php-fpm"]
# docker-compose.yml
version: '3.8'
services:
app:
build: .
volumes:
- ./storage:/var/www/html/storage
depends_on:
- mysql
- redis
nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- app
mysql:
image: mysql:8.0
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: secret
volumes:
- mysql_data:/var/lib/mysql
redis:
image: redis:alpine
volumes:
mysql_data:
# .github/workflows/deploy.yml
name: Deploy
on:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: mbstring, pdo, pdo_mysql
- name: Install Dependencies
run: composer install --prefer-dist --no-progress
- name: Run Tests
run: php artisan test
deploy:
needs: test
runs-on: ubuntu-latest
steps:
- name: Deploy to Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
cd /var/www/laravel
git pull origin main
composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan optimize
php artisan queue:restart