To set up a local Laravel application to mimic a production environment with automated deployment and startup, you can follow these steps:
1. Local Development Environment
-
WSL 2 Setup: Ensure you have WSL 2 installed and configured on your Windows machine. Install a Linux distribution (like Ubuntu) and set up your development environment with PHP, Composer, and Laravel.
-
Local Server: Use a tool like Laravel Valet for WSL or Homestead to serve your application locally. This will give you a more production-like environment.
2. Version Control with Git and GitHub
-
Git Setup: Initialize a Git repository in your Laravel project and commit your changes regularly.
-
GitHub Repository: Push your local repository to GitHub. This will serve as the central repository for your code.
3. Automated Deployment
-
Deployment Script: Write a deployment script that pulls the latest changes from GitHub and runs necessary commands like
composer install,php artisan migrate, andnpm run production. -
GitHub Actions: Set up a GitHub Actions workflow to automate the deployment process. You can trigger this workflow on every push to the main branch.
name: Deploy to Local Server on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Set up PHP uses: shivammathur/setup-php@v2 with: php-version: '8.0' - name: Install dependencies run: composer install --no-dev --optimize-autoloader - name: Run migrations run: php artisan migrate --force - name: Build assets run: npm ci && npm run production - name: Deploy to server run: | ssh user@your-local-server 'cd /path/to/your/app && git pull origin main && php artisan migrate --force && npm run production'
4. Running the App on Startup
-
Systemd Service: Create a systemd service to start your Laravel application automatically on boot. This is applicable if you're using a Linux environment.
Create a service file, e.g.,
/etc/systemd/system/laravel-app.service:[Unit] Description=Laravel Application [Service] ExecStart=/usr/bin/php /path/to/your/app/artisan serve --host=0.0.0.0 --port=8000 Restart=always User=your-username Group=www-data Environment=PATH=/usr/bin:/usr/local/bin Environment=NODE_ENV=production WorkingDirectory=/path/to/your/app [Install] WantedBy=multi-user.targetEnable and start the service:
sudo systemctl enable laravel-app sudo systemctl start laravel-app
5. Accessing the Application
- Local Domain: Use a tool like Valet to map your application to a local domain, e.g.,
http://myapp.test.
By following these steps, you can create a local environment that closely resembles a production setup, with automated deployment and startup processes. This will help you practice real-world deployment workflows while keeping everything on your local machine.