Symfony on fortrabbit

Published

Updated

Run Symfony on fortrabbit with Git deployment. The Symfony software template sets the root path, APP_ENV, APP_SECRET and DATABASE_URL, runs Composer, and clears the cache and migrates the database after each deployment.

Install Symfony locally

Set up Symfony in a local development environment, see the Symfony setup docs. fortrabbit serves PHP with Apache, so add the .htaccess rules of the Apache pack, and confirm the Flex recipe with y:

composer require symfony/apache-pack
shell

Push the code to a GitHub repository for Git deployment, see the Git intro if Git is new.

Create an app

Create an app in the dashboard, choose Symfony as the software, and connect the GitHub repository.

Software template

The Symfony software template sets these values:

Symfony 8, 7, 6, 5

DeploymentValue
Root pathpublic
Deploy strategyreplace
Excluded paths/public/uploads/node_modules
Build commandscomposer install --no-dev --prefer-dist --no-interaction --no-ansi --no-progress
Post-deploy commandsphp bin/console cache:clearphp bin/console assets:install publicphp bin/console doctrine:migrations:migrate
Environment variablesValue
APP_DEBUGfalse
APP_ENVprod
APP_SECRET!RANDOM_KEY!
Database environment variablesValue
DATABASE_URLmysql://${FORTRABBIT_DB_USER}:${FORTRABBIT_DB_PASSWORD}@${FORTRABBIT_DB_HOST}:3306/${FORTRABBIT_DB_NAME}

In short:

  • The root path public
  • APP_ENV=prod, APP_DEBUG=false and a random APP_SECRET
  • DATABASE_URL from the MySQL component
  • Post deploy commands that clear the cache, install the assets and run the Doctrine migrations
  • /public/uploads as an exclude pattern, so uploads survive deployments

With a package.json in the repository, the stack detector adds npm install and npm run build to the build commands. Apps created without the template need these settings by hand: ENV vars, root path and the deployment settings.

Commands over SSH

Run console commands over SSH, for example to load fixtures:

$ ssh {{app-env-id}}@ssh.{{region}}.frbit.app 'php bin/console doctrine:fixtures:load'
$ ssh {{app-env-id}}@ssh.{{region}}.frbit.app 'php bin/console cache:clear'
shell

Tailwind

The TailwindBundle downloads the standalone Tailwind CLI, a binary the build cannot run, see limits of the build. Install Tailwind through npm instead, for example with Webpack Encore:

composer require symfony/webpack-encore-bundle
npm install -D tailwindcss postcss postcss-loader autoprefixer @tailwindcss/postcss
shell

Enable PostCSS in webpack.config.js:

    .enablePostCssLoader()
js

Create postcss.config.js in the project root:

module.exports = {
  plugins: {
    '@tailwindcss/postcss': {},
    autoprefixer: {},
  },
};
js

And import Tailwind in assets/styles/app.css:

@import 'tailwindcss';
@source '../../templates';
css

The build commands then compile it with each deployment.

Logging

Symfony logs with Monolog. Its default configuration for prod writes to php://stderr, which shows up in the logs of the dashboard. Keep that in config/packages/monolog.yaml.

Sending e-mail

fortrabbit has no sendmail. Configure the Symfony Mailer with the MAILER_DSN ENV var, pointing to SMTP or a transactional mail provider, see the Symfony Mailer docs.

Written by humans and AI in collaboration.

AI use & editorial processEdit on GitHub ↗