|
1 |
| -Deployment |
2 |
| -========== |
| 1 | +.. index:: |
| 2 | + single: Deployment; Deployment tools |
| 3 | + |
| 4 | +.. _how-to-deploy-a-symfony2-application: |
| 5 | + |
| 6 | +How to Deploy a Symfony Application |
| 7 | +=================================== |
| 8 | + |
| 9 | +Deploying a Symfony application can be a complex and varied task depending on |
| 10 | +the setup and the requirements of your application. This article is not a step- |
| 11 | +by-step guide, but is a general list of the most common requirements and ideas |
| 12 | +for deployment. |
| 13 | + |
| 14 | +.. _symfony2-deployment-basics: |
| 15 | + |
| 16 | +Symfony Deployment Basics |
| 17 | +------------------------- |
| 18 | + |
| 19 | +The typical steps taken while deploying a Symfony application include: |
| 20 | + |
| 21 | +#. Upload your code to the production server; |
| 22 | +#. Install your vendor dependencies (typically done via Composer and may be done |
| 23 | + before uploading); |
| 24 | +#. Running database migrations or similar tasks to update any changed data structures; |
| 25 | +#. Clearing (and optionally, warming up) your cache. |
| 26 | + |
| 27 | +A deployment may also include other tasks, such as: |
| 28 | + |
| 29 | +* Tagging a particular version of your code as a release in your source control |
| 30 | + repository; |
| 31 | +* Creating a temporary staging area to build your updated setup "offline"; |
| 32 | +* Running any tests available to ensure code and/or server stability; |
| 33 | +* Removal of any unnecessary files from the ``web/`` directory to keep your |
| 34 | + production environment clean; |
| 35 | +* Clearing of external cache systems (like `Memcached`_ or `Redis`_). |
| 36 | + |
| 37 | +How to Deploy a Symfony Application |
| 38 | +----------------------------------- |
| 39 | + |
| 40 | +There are several ways you can deploy a Symfony application. Start with a few |
| 41 | +basic deployment strategies and build up from there. |
| 42 | + |
| 43 | +Basic File Transfer |
| 44 | +~~~~~~~~~~~~~~~~~~~ |
| 45 | + |
| 46 | +The most basic way of deploying an application is copying the files manually |
| 47 | +via FTP/SCP (or similar method). This has its disadvantages as you lack control |
| 48 | +over the system as the upgrade progresses. This method also requires you |
| 49 | +to take some manual steps after transferring the files (see `Common Post-Deployment Tasks`_) |
| 50 | + |
| 51 | +Using Source Control |
| 52 | +~~~~~~~~~~~~~~~~~~~~ |
| 53 | + |
| 54 | +If you're using source control (e.g. Git or SVN), you can simplify by having |
| 55 | +your live installation also be a copy of your repository. When you're ready |
| 56 | +to upgrade it is as simple as fetching the latest updates from your source |
| 57 | +control system. |
| 58 | + |
| 59 | +This makes updating your files *easier*, but you still need to worry about |
| 60 | +manually taking other steps (see `Common Post-Deployment Tasks`_). |
| 61 | + |
| 62 | +Using Platforms as a Service |
| 63 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 64 | + |
| 65 | +The specific deployment steps vary greatly from one service provider to another, |
| 66 | +so check out the dedicated article for the service of your choose: |
3 | 67 |
|
4 | 68 | .. toctree::
|
5 | 69 | :maxdepth: 1
|
6 | 70 | :glob:
|
7 | 71 |
|
8 | 72 | deployment/*
|
| 73 | + |
| 74 | +Using Build Scripts and other Tools |
| 75 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 76 | + |
| 77 | +There are also tools to help ease the pain of deployment. Some of them have been |
| 78 | +specifically tailored to the requirements of Symfony. |
| 79 | + |
| 80 | +`Capistrano`_ with `Symfony plugin`_ |
| 81 | + `Capistrano`_ is a remote server automation and deployment tool written in Ruby. |
| 82 | + `Symfony plugin`_ is a plugin to ease Symfony related tasks, inspired by `Capifony`_ |
| 83 | + (which works only with Capistrano 2 ) |
| 84 | + |
| 85 | +`sf2debpkg`_ |
| 86 | + Helps you build a native Debian package for your Symfony project. |
| 87 | + |
| 88 | +`Magallanes`_ |
| 89 | + This Capistrano-like deployment tool is built in PHP, and may be easier |
| 90 | + for PHP developers to extend for their needs. |
| 91 | + |
| 92 | +`Fabric`_ |
| 93 | + This Python-based library provides a basic suite of operations for executing |
| 94 | + local or remote shell commands and uploading/downloading files. |
| 95 | + |
| 96 | +`Deployer`_ |
| 97 | + This is another native PHP rewrite of Capistrano, with some ready recipes for |
| 98 | + Symfony. |
| 99 | + |
| 100 | +Bundles |
| 101 | + There are some `bundles that add deployment features`_ directly into your |
| 102 | + Symfony console. |
| 103 | + |
| 104 | +Basic scripting |
| 105 | + You can of course use shell, `Ant`_ or any other build tool to script |
| 106 | + the deploying of your project. |
| 107 | + |
| 108 | +Common Post-Deployment Tasks |
| 109 | +---------------------------- |
| 110 | + |
| 111 | +After deploying your actual source code, there are a number of common things |
| 112 | +you'll need to do: |
| 113 | + |
| 114 | +A) Check Requirements |
| 115 | +~~~~~~~~~~~~~~~~~~~~~ |
| 116 | + |
| 117 | +Check if your server meets the requirements by running: |
| 118 | + |
| 119 | +.. code-block:: bash |
| 120 | +
|
| 121 | + $ php app/check.php |
| 122 | +
|
| 123 | +B) Configure your ``app/config/parameters.yml`` File |
| 124 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 125 | + |
| 126 | +This file should *not* be deployed, but managed through the automatic utilities |
| 127 | +provided by Symfony. |
| 128 | + |
| 129 | +C) Install/Update your Vendors |
| 130 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 131 | + |
| 132 | +Your vendors can be updated before transferring your source code (i.e. |
| 133 | +update the ``vendor/`` directory, then transfer that with your source |
| 134 | +code) or afterwards on the server. Either way, just update your vendors |
| 135 | +as you normally do: |
| 136 | + |
| 137 | +.. code-block:: bash |
| 138 | +
|
| 139 | + $ composer install --no-dev --optimize-autoloader |
| 140 | +
|
| 141 | +.. tip:: |
| 142 | + |
| 143 | + The ``--optimize-autoloader`` flag improves Composer's autoloader performance |
| 144 | + significantly by building a "class map". The ``--no-dev`` flag ensures that |
| 145 | + development packages are not installed in the production environment. |
| 146 | + |
| 147 | +.. caution:: |
| 148 | + |
| 149 | + If you get a "class not found" error during this step, you may need to |
| 150 | + run ``export SYMFONY_ENV=prod`` before running this command so that |
| 151 | + the ``post-install-cmd`` scripts run in the ``prod`` environment. |
| 152 | + |
| 153 | +D) Clear your Symfony Cache |
| 154 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 155 | + |
| 156 | +Make sure you clear (and warm-up) your Symfony cache: |
| 157 | + |
| 158 | +.. code-block:: bash |
| 159 | +
|
| 160 | + $ php app/console cache:clear --env=prod --no-debug |
| 161 | +
|
| 162 | +E) Dump your Assetic Assets |
| 163 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 164 | + |
| 165 | +If you're using Assetic, you'll also want to dump your assets: |
| 166 | + |
| 167 | +.. code-block:: bash |
| 168 | +
|
| 169 | + $ php app/console assetic:dump --env=prod --no-debug |
| 170 | +
|
| 171 | +F) Other Things! |
| 172 | +~~~~~~~~~~~~~~~~ |
| 173 | + |
| 174 | +There may be lots of other things that you need to do, depending on your |
| 175 | +setup: |
| 176 | + |
| 177 | +* Running any database migrations |
| 178 | +* Clearing your APC cache |
| 179 | +* Running ``assets:install`` (already taken care of in ``composer install``) |
| 180 | +* Add/edit CRON jobs |
| 181 | +* Pushing assets to a CDN |
| 182 | +* ... |
| 183 | + |
| 184 | +Application Lifecycle: Continuous Integration, QA, etc |
| 185 | +------------------------------------------------------ |
| 186 | + |
| 187 | +While this entry covers the technical details of deploying, the full lifecycle |
| 188 | +of taking code from development up to production may have a lot more steps |
| 189 | +(think deploying to staging, QA (Quality Assurance), running tests, etc). |
| 190 | + |
| 191 | +The use of staging, testing, QA, continuous integration, database migrations |
| 192 | +and the capability to roll back in case of failure are all strongly advised. There |
| 193 | +are simple and more complex tools and one can make the deployment as easy |
| 194 | +(or sophisticated) as your environment requires. |
| 195 | + |
| 196 | +Don't forget that deploying your application also involves updating any dependency |
| 197 | +(typically via Composer), migrating your database, clearing your cache and |
| 198 | +other potential things like pushing assets to a CDN (see `Common Post-Deployment Tasks`_). |
| 199 | + |
| 200 | +.. _`Capifony`: http://capifony.org/ |
| 201 | +.. _`Capistrano`: http://capistranorb.com/ |
| 202 | +.. _`sf2debpkg`: https://github.com/liip/sf2debpkg |
| 203 | +.. _`Fabric`: http://www.fabfile.org/ |
| 204 | +.. _`Magallanes`: https://github.com/andres-montanez/Magallanes |
| 205 | +.. _`Ant`: http://blog.sznapka.pl/deploying-symfony2-applications-with-ant |
| 206 | +.. _`bundles that add deployment features`: http://knpbundles.com/search?q=deploy |
| 207 | +.. _`Memcached`: http://memcached.org/ |
| 208 | +.. _`Redis`: http://redis.io/ |
| 209 | +.. _`Symfony plugin`: https://github.com/capistrano/symfony/ |
| 210 | +.. _`Deployer`: http://deployer.org/ |
0 commit comments