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
- Install Symfony CLI (optional, but recommended):
Terminal window curl -sS https://get.symfony.com/cli/installer | bashexport PATH="$HOME/.symfony/bin:$PATH" - Create a new Symfony app:
Terminal window symfony new my-symfony-app --webappcd my-symfony-app - Start the development server:
Your app should be running at http://localhost:8000.
Terminal window symfony serve
Sample Code (src/Controller/DefaultController.php)
Add a simple controller to your Symfony project:
<?phpnamespace 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
- Push your Symfony app to a GitHub repository.
- Log in to Klutch.sh.
- Create a new project and give it a name.
- 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
- Add a start command in your app settings:
(Klutch.sh will provide the
Terminal window php -S 0.0.0.0:$PORT -t publicPORT
environment variable.) - Click “Create” to deploy. Klutch.sh will build and deploy your app automatically.
Deploying With a Dockerfile
- Add a
Dockerfile
to your project root. Example:# Use official PHP imageFROM php:8.1-cli# Install ComposerCOPY --from=composer:2 /usr/bin/composer /usr/bin/composer# Set working directoryWORKDIR /app# Copy app sourceCOPY . .# Install dependenciesRUN composer install --no-interaction --optimize-autoloader# Expose port (match your Symfony app)EXPOSE 8000# Start the appCMD ["php", "-S", "0.0.0.0:$PORT", "-t", "public"] - Push your code (with Dockerfile) to GitHub.
- In Klutch.sh, follow the same steps to create a project and app, but select the Dockerfile option when prompted.
- Set the service details and environment variables as needed.
- 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.