Skip to content

Deploying a Symfony App

Symfony is a powerful and flexible PHP framework for building web applications and APIs. It is known for its reusable components, strong architecture, and large ecosystem, making it a top choice for scalable and maintainable PHP projects.

This guide explains how to deploy a Symfony 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 Symfony

  1. Install Symfony CLI (optional, but recommended):
    Terminal window
    curl -sS https://get.symfony.com/cli/installer | bash
    export PATH="$HOME/.symfony/bin:$PATH"
  2. Create a new Symfony app:
    Terminal window
    symfony new my-symfony-app --webapp
    cd my-symfony-app
  3. Start the development server:
    Terminal window
    symfony serve
    Your app should be running at http://localhost:8000.

Sample Code (src/Controller/DefaultController.php)

Add a simple controller to your Symfony project:

<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
#[Route('/', name: 'home')]
public function index(): Response
{
return new Response('Hello from Symfony on Klutch.sh!');
}
}

Deploying Without a Dockerfile

  1. Push your Symfony 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 Symfony GitHub repository and branch
    • Set the port to route traffic (usually 8000 for Symfony)
    • Choose region, compute, number of instances, and add any environment variables
  5. Add a start command in your app settings:
    Terminal window
    php -S 0.0.0.0:$PORT -t public
    (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 Symfony app)
    EXPOSE 8000
    # Start the app
    CMD ["php", "-S", "0.0.0.0:$PORT", "-t", "public"]
  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 Symfony 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.