How to Deploy a Symfony Application ¶
Deploying a Symfony application can be a complex and varied task depending on the setup and the requirements of your application. This article is not a step-by-step guide, but is a general list of the most common requirements and ideas for deployment.
Symfony Deployment Basics ¶
The typical steps taken while deploying a Symfony application include:
- Upload your code to the production server;コードを本番サーバーにアップロードします。
- Install your vendor dependencies (typically done via Composer and may be done
before uploading);ベンダーの依存関係をインストールします (通常は Composer を介して実行され、アップロード前に実行される場合もあります)。
- Running database migrations or similar tasks to update any changed data structures;データベースの移行または同様のタスクを実行して、変更されたデータ構造を更新します。
- Clearing (and optionally, warming up) your cache.キャッシュをクリア (およびオプションでウォームアップ) します。
A deployment may also include other tasks, such as:
- Tagging a particular version of your code as a release in your source control
repository;ソース管理リポジトリでコードの特定のバージョンをリリースとしてタグ付けする。
- Creating a temporary staging area to build your updated setup "offline";更新されたセットアップを「オフライン」で構築するための一時的なステージング領域を作成します。
- Running any tests available to ensure code and/or server stability;コードおよび/またはサーバーの安定性を確保するために利用可能なテストを実行します。
- Removal of any unnecessary files from the
public/
directory to keep your production environment clean;public/ ディレクトリから不要なファイルを削除して、本番環境をクリーンに保ちます。 - Clearing of external cache systems (like Memcached or Redis).外部キャッシュ システムのクリア (Memcached や Redis など)。
How to Deploy a Symfony Application ¶
There are several ways you can deploy a Symfony application. Start with a few basic deployment strategies and build up from there.
Basic File Transfer ¶
The most basic way of deploying an application is copying the files manually via FTP/SCP (or similar method). This has its disadvantages as you lack control over the system as the upgrade progresses. This method also requires you to take some manual steps after transferring the files (see Common Deployment Tasks).
Using Source Control ¶
If you're using source control (e.g. Git or SVN), you can simplify by having your live installation also be a copy of your repository. When you're ready to upgrade, fetch the latest updates from your source control system. When using Git, a common approach is to create a tag for each release and check out the appropriate tag on deployment (see Git Tagging).
This makes updating your files easier, but you still need to worry about manually taking other steps (see Common Deployment Tasks).
Using Platforms as a Service ¶
Using a Platform as a Service (PaaS) can be a great way to deploy your Symfony app quickly. There are many PaaS, but we recommend Platform.sh as it provides a dedicated Symfony integration and helps fund the Symfony development.
Using Build Scripts and other Tools ¶
There are also tools to help ease the pain of deployment. Some of them have been specifically tailored to the requirements of Symfony.
- Deployer
-
This is another native PHP rewrite of Capistrano, with some ready recipes for
Symfony.
これは Capistrano の別のネイティブ PHP リライトであり、Symfony の準備が整ったレシピがいくつかあります。
- Ansistrano
-
An Ansible role that allows you to configure a powerful deploy via YAML files.
YAML ファイルを介して強力なデプロイを構成できる Ansible ロール。
- Magallanes
-
This Capistrano-like deployment tool is built in PHP, and may be easier
for PHP developers to extend for their needs.
この Capistrano のような展開ツールは PHP で構築されており、PHP 開発者がニーズに合わせて拡張するのがより簡単になる場合があります。
- Fabric
-
This Python-based library provides a basic suite of operations for executing
local or remote shell commands and uploading/downloading files.
この Python ベースのライブラリは、ローカルまたはリモートのシェル コマンドを実行し、ファイルをアップロード/ダウンロードするための一連の基本的な操作を提供します。
- Capistrano with Symfony plugin
-
Capistrano is a remote server automation and deployment tool written in Ruby.
Symfony plugin is a plugin to ease Symfony related tasks, inspired by Capifony
(which works only with Capistrano 2).
Capistrano は、Ruby で記述されたリモート サーバー自動化およびデプロイ ツールです。Symfony プラグインは、Capifony (Capistrano 2 でのみ動作) に触発された、Symfony 関連のタスクを容易にするプラグインです。
Common Deployment Tasks ¶
Before and after deploying your actual source code, there are a number of common things you'll need to do:
A) Check Requirements ¶
There are some technical requirements for running Symfony applications. In your development machine, the recommended way to check these requirements is to use Symfony CLI. However, in your production server you might prefer to not install the Symfony CLI tool. In those cases, install this other package in your application:
1 |
$ composer require symfony/requirements-checker
|
Then, make sure that the checker is included in your Composer scripts:
1 2 3 4 5 6 7 8 9 10 11 12 |
{
"...": "...",
"scripts": {
"auto-scripts": {
"vendor/bin/requirements-checker": "php-script",
"...": "..."
},
"...": "..."
}
}
|
B) Configure your Environment Variables ¶
Most Symfony applications read their configuration from environment variables.
While developing locally, you'll usually store these in .env
and .env.local
(for local overrides). On production, you have two options:
- Create "real" environment variables. How you set environment variables, depends
on your setup: they can be set at the command line, in your Nginx configuration,
or via other methods provided by your hosting service;「実際の」環境変数を作成します。環境変数の設定方法は、セットアップによって異なります。コマンド ライン、Nginx 構成、またはホスティング サービスが提供する他の方法で設定できます。
- Or, create a
.env.local
file like your local development.または、ローカル開発のような .env.local ファイルを作成します。
There is no significant advantage to either of the two options: use whatever is most natural in your hosting environment.
Tip
You might not want your application to process the .env.*
files on
every request. You can generate an optimized .env.local.php
which
overrides all other configuration files:
1 |
$ composer dump-env prod
|
The generated file will contain all the configuration stored in .env
. If you
want to rely only on environment variables, generate one without any values using:
1 |
$ composer dump-env prod --empty
|
If composer
is not installed on your server, you can generate this optimized
file with a command provided by Symfony itself, which you must register in
your application before using it:
1 2 3 |
# config/services.yaml
services:
Symfony\Component\Dotenv\Command\DotenvDumpCommand: ~
|
1 |
$ APP_ENV=prod APP_DEBUG=0 php bin/console dotenv:dump
|
C) Install/Update your Vendors ¶
Your vendors can be updated before transferring your source code (i.e.
update the vendor/
directory, then transfer that with your source
code) or afterwards on the server. Either way, update your vendors
as you normally do:
1 |
$ composer install --no-dev --optimize-autoloader
|
Tip
The --optimize-autoloader
flag improves Composer's autoloader performance
significantly by building a "class map". The --no-dev
flag ensures that
development packages are not installed in the production environment.
Caution
If you get a "class not found" error during this step, you may need to
run export APP_ENV=prod
(or export SYMFONY_ENV=prod
if you're not
using Symfony Flex) before running this command so
that the post-install-cmd
scripts run in the prod
environment.
D) Clear your Symfony Cache ¶
Make sure you clear and warm-up your Symfony cache:
1 |
$ APP_ENV=prod APP_DEBUG=0 php bin/console cache:clear
|
E) Other Things! ¶
There may be lots of other things that you need to do, depending on your setup:
- Running any database migrationsデータベース移行の実行
- Clearing your APCu cacheAPCu キャッシュをクリアする
- Add/edit CRON jobsCRON ジョブの追加/編集
- Building and minifying your assets with Webpack EncoreWebpack Encore を使用してアセットを構築および縮小する
- Pushing assets to a CDNアセットを CDN にプッシュする
- On a shared hosting platform using the Apache web server, you may need to
install the symfony/apache-pack packageApache Web サーバーを使用する共有ホスティング プラットフォームでは、symfony/apache-pack パッケージをインストールする必要がある場合があります
- etc.等
Application Lifecycle: Continuous Integration, QA, etc. ¶
While this article covers the technical details of deploying, the full lifecycle of taking code from development up to production may have more steps: deploying to staging, QA (Quality Assurance), running tests, etc.
The use of staging, testing, QA, continuous integration, database migrations and the capability to roll back in case of failure are all strongly advised. There are simple and more complex tools and one can make the deployment as easy (or sophisticated) as your environment requires.
Don't forget that deploying your application also involves updating any dependency (typically via Composer), migrating your database, clearing your cache and other potential things like pushing assets to a CDN (see Common Deployment Tasks).
Troubleshooting ¶
Deployments not Using the composer.json
File
¶
The project root directory
(whose value is used via the kernel.project_dir
parameter and the
getProjectDir() method) is
calculated automatically by Symfony as the directory where the main
composer.json
file is stored.
In deployments not using the composer.json
file, you'll need to override the
getProjectDir() method
as explained in this section.
Learn More ¶
- How to Configure Symfony to Work behind a Load Balancer or a Reverse Proxyロードバランサーまたはリバースプロキシの背後で動作するように Symfony を構成する方法