Symfony API

Deploy an API built with Symfony #

The following describes the steps necessary to deploy a Symfony-based API. If you want to skip the project setup, head over to our Github repository to find the code.

Project setup #

Create the project using Composer:

$ composer create-project symfony/skeleton:"6.1.*" example-symfony-api
Creating a "symfony/skeleton:6.1.*" project at "./example-symfony-api"
Info from https://repo.packagist.org: #StandWithUkraine
Installing symfony/skeleton (v6.1.99)
  - Installing symfony/skeleton (v6.1.99): Extracting archive
Created project in /Users/till/Documents/workspaces/hostwithquantum/example-symfony-api
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking symfony/flex (v2.2.3)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing symfony/flex (v2.2.3): Extracting archive
Generating optimized autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!

Run composer recipes at any time to see the status of your Symfony recipes.
...

Initialize a git repository #

The next step is to initialize the project (with git) and delete .env:

$ cd example-symfony-api
$ rm .env
$ git init
$ git commit -a -m 'my symfony app'
[main (root-commit) 43ce7fe] Update: symfony api project
 16 files changed, 2649 insertions(+)
 create mode 100644 .gitignore
 create mode 100755 bin/console
 create mode 100644 composer.json
 create mode 100644 composer.lock
 create mode 100644 config/bundles.php
 create mode 100644 config/packages/cache.yaml
 create mode 100644 config/packages/framework.yaml
 create mode 100644 config/packages/routing.yaml
 create mode 100644 config/preload.php
 create mode 100644 config/routes.yaml
 create mode 100644 config/routes/framework.yaml
 create mode 100644 config/services.yaml
 create mode 100644 public/index.php
 create mode 100644 src/Controller/.gitignore
 create mode 100644 src/Kernel.php
 create mode 100644 symfony.lock

DotEnv #

The reason why we delete the .env file is because we don’t need them as these settings should be provided by the environment. The next step includes how to achieve this. Besides, runtime secrets should not be committed to version control.

To disable Symfony’s use of DotEnv completely, we add the following to the composer.json:

diff --git a/composer.json b/composer.json
index 9c6a831..740756f 100644
--- a/composer.json
+++ b/composer.json
@@ -63,6 +63,9 @@
         "symfony/symfony": "*"
     },
     "extra": {
+        "runtime": {
+            "disable_dotenv": true
+        },
         "symfony": {
             "allow-contrib": false,
             "require": "6.1.*"

… and update the composer.lock file afterwards:

$ composer update --lock
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Writing lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating optimized autoload files
26 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

Run composer recipes at any time to see the status of your Symfony recipes.

Executing script cache:clear [OK]
Executing script assets:install public [OK]

No security vulnerability advisories found

Deployment with runway #

Every time you make changes, the changes need to be checked into the repository. So don’t forget to git commit, before you deploy.

Create an application on runway:

$ runway app create
INFO    created app "fair-emission"                  
create app fair-emission: done
next steps:  
* commit your changes  
* runway app deploy  
* runway open

… and set the necessary environment variables:

$ runway app config set APP_ENV=production
INFO    configuring app "fair-emission"              
configure app fair-emission: done
Name    Value      
APP_ENV production 
$ runway app config set APP_SECRET=YOUR-SUPER-SECRET-HERE
INFO    configuring app "fair-emission"              
configure app fair-emission: done
Name       Value                            
APP_ENV    production                       
APP_SECRET YOUR-SUPER-SECRET-HERE

Finish with: runway app deploy.