james1232's avatar

Running A local Laravel application like one in production?

I’m building a personal Laravel task manager app, and I want it to follow a typical deployment pipeline: local development -> staging -> Git -> GitHub -> deployed to a local server as a production app. The idea is to have a setup where I can work locally in WSL 2, push my changes to GitHub, and automatically deploy them to a server running on my machine.

Since I’m the only one using this app, my goal is to practice setting up proper deployment workflows while also getting to use a tool that’s tailored to my needs. I’d like the app to start automatically whenever I boot my computer, so I don’t have to manually start the server each time.

Basically, I want it to behave like a 'real' production app but run locally with minimal hassle. What’s the best way to set this up, including automating deployment and making the app available on startup?

0 likes
2 replies
LaryAI's avatar
Level 58

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, and npm 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.

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.target
    

    Enable 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.

hyouhoan's avatar

if me I could follow the "keep it simple" strategy.

  1. dockerizing your project and set docker with "restart: alway" and starting docker on OS startup
  2. run a cronjob in your local pc to querying github to check whenever you have new release tag. then trigger the "deploy" script which will pull the latest code, rebuild docker image and up. by this way I don't have to expose my local server to internet. the "deploy" script is just shell script.
1 like

Please or to participate in this conversation.