How to create an automated deploy pipeline with Symfony 5 and DigitalOcean App Platform

mattia toselli
4 min readNov 27, 2021

In this article we are going to develop a simple app with Symfony 5 on our local machine, then we will deploy this app using a service of DigitalOcean called App Platform.

Why Symfony?

Frameworks are designed to support and facilitate the programmer in the creation of projects, promoting the reuse of the code and thus facilitating the process. Building a website or a service from scratch means running into problems that often and willingly, to be solved, require an indefinite amount of time, time that often a programmer does not have, given the amount of work.
Among the frameworks of some relevance, we can certainly mention Symfony, a framework that adapts to any type of project and any type of need that a programmer has to manage every day.
Symfony boasts, in fact, an excellent reputation, as it is a software with excellent stability and therefore internationally recognized and adopted by many professionals active in this sector.
Consequently, Symfony has a large number of programmers, who have created an active community that actively participates in the continuous enrichment and updating of the framework.

Why DigitalOcean App Platform?

As said in the first page of the service documentation:

App Platform is a Platform-as-a-Service (PaaS) offering that allows developers to publish code directly to DigitalOcean servers without worrying about the underlying infrastructure.

App Platform can either automatically analyze and build code from your GitHub, GitLab or public Git repositories and publish your application to the cloud, or publish a container image you have already uploaded to DigitalOcean Container Registry or Docker Hub. It also has lifecycle management features, vertical and horizontal scaling, push-to-deploy support, introspection and monitoring features, built-in database management and integration, and everything a developer needs to get code live in production.

Requirements

Go to this link to see what you will need to install Symfony 5 on your PC.
You will also need a text editor, and possibly a database (I won’t use it in this fast tutorial, but most of the web applications need it, so you’ll probably want it). My setup was:

  • Personal Computer with Linux Ubuntu installed;
  • PHP 8.0;
  • Visual Studio Code (choose your favourite extensions).

Creating a new Symfony 5 Project

I suggest you to go here: https://symfony.com/download to take the correct commands for your OS.

The following commands will create our app. I created an App for my Business called “hct-calendar”.

symfony new hct-calendar && cd hct-calendar

Spin up the development server:

symfony serve -d

This will launch a development server on http://localhost:8000. You can stop it by using the command:

symfony server:stop

let’s install some modules, in particular we will need Twig (a PHP template engine), a assets serving module and a debugger. Actually we’ll need to install another module along the way, but i prefer to point that later.

composer require template
composer require profiler — dev
composer require symfony/asset

Create a test Application

In order to test the application, we’ll need to create two diffent methods on our a controller. First, comment all lines in the routes file:

config/routes.yml--------------------------------------------------------#index:#    path: /#    controller: App\Controller\DefaultController::index

then, create two different methods, one will serve a simple html page, the other will respond with a JSON.

scr/controller/DefaultController.php-----------------------------------------------------------<?phpnamespace App\Controller;
use \Symfony\Component\HttpFoundation\Response;
use \Symfony\Component\Routing\Annotation\Route;
use \Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class DefaultController extends AbstractController
{
/**
* @Route("/", name="app_index")
*/
public function index()
{
return $this->render('base.html.twig', []);
}
/**
* @Route("/login", name="app_login")
*/
public function show()
{
return new JsonResponse([
"status" => "ok",
"slug" => "login",
]);

}
}

then, change the auto-generated twig file:

templates/base.html.twig------------------------------------------------------<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Welcome!{% endblock %}</title>
{# Run `composer require symfony/webpack-encore-bundle`
and uncomment the following Encore helpers to start using Symfony UX #}
{% block stylesheets %}
{#{{ encore_entry_link_tags('app') }}#}
{% endblock %}
{% block javascripts %}
{#{{ encore_entry_script_tags('app') }}#}
{% endblock %}
</head>
<body>
{% block body %}
<a href="{{ path('app_login') }}">this link for login</a>
{% endblock %}
</body>
</html>

If you try to visit the home again, and click on the link, you will see the different responses.

When you are ready, create a new repository on GitHub and deploy your new code, do not edit the .gitignore file, you can deploy everything, we’ll take care of everything on the App Platform service.

Deploying on DigitalOcean App Platform

Create an Account on DigitalOcean, or log into it.
In the sidenav in the left, click on app and create e new app, then choose GitHub and click on your project’s repo, choose what branch will be the one to pull on the server, and decide if you need to redeploy the app manually, or everytime someone pushes to the chosen main branch.

Next, configure your PHP app. Remember to leave everything as it is, the only changes you’ll need to do are to set some environment variables and the public directory.

  • set a APP_ENV = prod environment variable;
  • set an APP_KEY environment variable
  • set public/ as public directory.
  • change the run command using this one: heroku-php-apache2 public/

after the deploy, you’ll see your app online, but there is a last problem to solve!

Activate Apache Rewrite Engine on App Platform

If you browse to your project’s root, you will be able to see the page, but when you click on the link, you will receive a 404 error.

Thing is that your app is not using the rewrite engine of Apache Server to redirect every route to index.php file (if you write on your URL bar domain/index.php/login it will be available).

In order to do it, be sure to read all the Heroku article about Symfony https://devcenter.heroku.com/articles/deploying-symfony4.

TL;DR:

you’ll need to install a symfony package, push the new code to the repo, and redeploy the app.

composer require symfony/apache-pack

As simple as that, your app is now online and working, Congratulations!

--

--

mattia toselli

Software developer at Fluentify LTD. AWS, Docker and programming enthusiast.