Skip to content

Deploying a Laravel App

Laravel is a modern PHP framework known for its elegant syntax, powerful features, and developer-friendly tools. It simplifies web application development with built-in authentication, routing, ORM, and more, making it a top choice for building robust PHP apps.

This guide explains how to deploy a Laravel application to Klutch.sh, both with and without a Dockerfile. It also covers installation and provides sample code to get started.

Prerequisites

  • PHP 8.1+
  • Composer installed
  • Git and GitHub account
  • Klutch.sh account

Getting Started: Install Laravel

  1. Install Laravel globally (optional):
    Terminal window
    composer global require laravel/installer
    export PATH="$HOME/.composer/vendor/bin:$PATH"
  2. Create a new Laravel app:
    Terminal window
    laravel new my-laravel-app
    cd my-laravel-app
    Or use Composer directly:
    Terminal window
    composer create-project laravel/laravel my-laravel-app
    cd my-laravel-app
  3. Start the development server:
    Terminal window
    php artisan serve
    Your app should be running at http://localhost:8000.

Sample Code (routes/web.php)

Add a simple route to your Laravel project:

use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return 'Hello from Laravel on Klutch.sh!';
});

Deploying Without a Dockerfile

  1. Push your Laravel app to a GitHub repository.
  2. Log in to Klutch.sh.
  3. Create a new project and give it a name.
  4. Create a new app:
    • Select your Laravel GitHub repository and branch
    • Set the port to route traffic (usually 8000 for Laravel)
    • Choose region, compute, number of instances, and add any environment variables
  5. Add a start command in your app settings:
    Terminal window
    php artisan serve --host=0.0.0.0 --port=$PORT
    (Klutch.sh will provide the PORT environment variable.)
  6. Click “Create” to deploy. Klutch.sh will build and deploy your app automatically.

Deploying With a Dockerfile

  1. Add a Dockerfile to your project root. Example:
    # Use official PHP image
    FROM php:8.1-cli
    # Install Composer
    COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
    # Set working directory
    WORKDIR /app
    # Copy app source
    COPY . .
    # Install dependencies
    RUN composer install --no-interaction --optimize-autoloader
    # Expose port (match your Laravel app)
    EXPOSE 8000
    # Start the app
    CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=$PORT"]
  2. Push your code (with Dockerfile) to GitHub.
  3. In Klutch.sh, follow the same steps to create a project and app, but select the Dockerfile option when prompted.
  4. Set the service details and environment variables as needed.
  5. Click “Create” to deploy. Klutch.sh will build your Docker image and deploy your app.

Note: Your Laravel app should always listen on the PORT environment variable as shown above.


Resources


Deploying to Klutch.sh is simple and flexible. Choose the method that best fits your workflow and project requirements.