|
| 1 | +.. index:: |
| 2 | + single: Deployment; Deploying to fortrabbit.com |
| 3 | + |
| 4 | +Deploying to fortrabbit |
| 5 | +======================= |
| 6 | + |
| 7 | +This step-by-step cookbook describes how to deploy a Symfony web application to |
| 8 | + `fortrabbit`_. You can read more about using Symfony with fortrabbit on the |
| 9 | + official fortrabbit `Symfony install guide`_. |
| 10 | + |
| 11 | +Setting up fortrabbit |
| 12 | +--------------------- |
| 13 | + |
| 14 | +Before getting started, you should have done a few things on the fortrabbit side: |
| 15 | + |
| 16 | +* `Sign up`_. |
| 17 | +* Add an SSH key to your Account (to deploy via Git) |
| 18 | +* Create an App |
| 19 | + |
| 20 | +Preparing your Application |
| 21 | +-------------------------- |
| 22 | + |
| 23 | +You don't need to change any code to deploy a Symfony application to fortrabbit. |
| 24 | +But it requires some minor tweaks to its configuration. |
| 25 | + |
| 26 | +Configure Logging |
| 27 | +~~~~~~~~~~~~~~~~~ |
| 28 | + |
| 29 | +Per default Symfony logs to a file. Modify the ``app/config/config_prod.yml`` file |
| 30 | +to redirect it to :phpfunction:`error_log`: |
| 31 | + |
| 32 | +.. configuration-block:: |
| 33 | + |
| 34 | + .. code-block:: yaml |
| 35 | +
|
| 36 | + # app/config/config_prod.yml |
| 37 | + monolog: |
| 38 | + # ... |
| 39 | + handlers: |
| 40 | + nested: |
| 41 | + type: error_log |
| 42 | +
|
| 43 | + .. code-block:: xml |
| 44 | +
|
| 45 | + <!-- app/config/config_prod.xml --> |
| 46 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 47 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 48 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 49 | + xmlns:monolog="http://symfony.com/schema/dic/monolog" |
| 50 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 51 | + http://symfony.com/schema/dic/services/services-1.0.xsd |
| 52 | + http://symfony.com/schema/dic/monolog |
| 53 | + http://symfony.com/schema/dic/monolog/monolog-1.0.xsd"> |
| 54 | +
|
| 55 | + <monolog:config> |
| 56 | + <!-- ... --> |
| 57 | + <monolog:handler |
| 58 | + name="nested" |
| 59 | + type="error_log" |
| 60 | + /> |
| 61 | + </monolog:config> |
| 62 | + </container> |
| 63 | +
|
| 64 | + .. code-block:: php |
| 65 | +
|
| 66 | + // app/config/config_prod.php |
| 67 | + $container->loadFromExtension('monolog', array( |
| 68 | + // ... |
| 69 | + 'handlers' => array( |
| 70 | + 'nested' => array( |
| 71 | + 'type' => 'error_log', |
| 72 | + ), |
| 73 | + ), |
| 74 | + )); |
| 75 | +
|
| 76 | +Configuring Database Access & Session Handler |
| 77 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 78 | + |
| 79 | +You can use the fortrabbit App Secrets to attain your database credentials. |
| 80 | +Create the file ``app/config_prod_secrets.php`` with the following contents:: |
| 81 | + |
| 82 | + <?php |
| 83 | + // Get the path to the secrects.json file |
| 84 | + if (!$secrets = getenv("APP_SECRETS")) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + // Read the file and decode json to an array |
| 89 | + $secrets = json_decode(file_get_contents($secrets), true); |
| 90 | + |
| 91 | + // Set database parameters to the container |
| 92 | + if (isset($secrets['MYSQL'])) { |
| 93 | + |
| 94 | + $container->setParameter('database_driver', 'pdo_mysql'); |
| 95 | + $container->setParameter('database_host', $secrets['MYSQL']['HOST']); |
| 96 | + $container->setParameter('database_name', $secrets['MYSQL']['DATABASE']); |
| 97 | + $container->setParameter('database_user', $secrets['MYSQL']['USER']); |
| 98 | + $container->setParameter('database_password', $secrets['MYSQL']['PASSWORD']); |
| 99 | + } |
| 100 | + |
| 101 | + // Check if the Memcache component is present |
| 102 | + if (isset($secrets['MEMCACHE'])) { |
| 103 | + |
| 104 | + $memcache = $secrets['MEMCACHE']; |
| 105 | + $handlers = []; |
| 106 | + |
| 107 | + foreach (range(1, $memcache['COUNT']) as $num) { |
| 108 | + $handlers [] = $memcache['HOST' . $num] . ':' . $memcache['PORT' . $num]; |
| 109 | + } |
| 110 | + |
| 111 | + // Apply ini settings |
| 112 | + ini_set('session.save_handler', 'memcached'); |
| 113 | + ini_set('session.save_path', implode(',', $handlers)); |
| 114 | + |
| 115 | + if ("2" === $memcache['COUNT']) { |
| 116 | + ini_set('memcached.sess_number_of_replicas', 1); |
| 117 | + ini_set('memcached.sess_consistent_hash', 1); |
| 118 | + ini_set('memcached.sess_binary', 1); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | +Make sure this file is listed in your *imports*: |
| 123 | + |
| 124 | +.. configuration-block:: |
| 125 | + |
| 126 | + .. code-block:: yaml |
| 127 | +
|
| 128 | + # app/config/config_prod.yml |
| 129 | + imports: |
| 130 | + - { resource: config.yml } |
| 131 | + - { resource: config_prod_secrets.php } |
| 132 | +
|
| 133 | + # .. |
| 134 | + framework: |
| 135 | + session: |
| 136 | + # set handler_id to null to use default session handler from php.ini (memcached) |
| 137 | + handler_id: ~ |
| 138 | + # .. |
| 139 | +
|
| 140 | + .. code-block:: xml |
| 141 | +
|
| 142 | + <!-- app/config/config_prod.xml --> |
| 143 | + <?xml version="1.0" encoding="UTF-8"?> |
| 144 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 145 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 146 | + xmlns:framework="http://symfony.com/schema/dic/symfony" |
| 147 | + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd |
| 148 | + http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> |
| 149 | +
|
| 150 | + <imports> |
| 151 | + <import resource="config.xml" /> |
| 152 | + <import resource="config_prod_secrets.php" /> |
| 153 | + </imports> |
| 154 | +
|
| 155 | + <!-- .. --> |
| 156 | + <framework:config> |
| 157 | + <!-- .. --> |
| 158 | + <framework:session save_path="null" /> |
| 159 | + </framework:config> |
| 160 | + </container> |
| 161 | +
|
| 162 | + .. code-block:: php |
| 163 | +
|
| 164 | + // app/config/config_prod.php |
| 165 | + $loader->import('config/config.php'); |
| 166 | + $loader->import('config_prod_secrets.php'); |
| 167 | +
|
| 168 | + $container->loadFromExtension('framework', array( |
| 169 | + 'session' => array( |
| 170 | + 'handler_id' => null, |
| 171 | + ), |
| 172 | + )); |
| 173 | +
|
| 174 | + // ... |
| 175 | +
|
| 176 | +Configuring the Environment in the Dashboard |
| 177 | +-------------------------------------------- |
| 178 | + |
| 179 | +PHP Settings |
| 180 | +~~~~~~~~~~~~ |
| 181 | + |
| 182 | +The PHP version and enabled extensions are configuable under the PHP settings |
| 183 | +of your App within the fortrabbit Dashboard. |
| 184 | + |
| 185 | +Environment Variables |
| 186 | +~~~~~~~~~~~~~~~~~~~~~ |
| 187 | + |
| 188 | +Set the ``SYMFONY_ENV`` environment variable to ``prod`` to make sure the right |
| 189 | +config files get loaded. ENV vars are configuable in fortrabbit Dashboard as well. |
| 190 | + |
| 191 | +Document Root |
| 192 | +~~~~~~~~~~~~~ |
| 193 | + |
| 194 | +The document root is configuable for every custom domain you setup for your App. |
| 195 | +The default is ``/htdocs``, but for Symfony you probably want to change it to |
| 196 | +``/htdocs/web``. You also do so in the fortrabbit Dashboard under ``Domain`` settings. |
| 197 | + |
| 198 | +Deploying to fortrabbit |
| 199 | +----------------------- |
| 200 | + |
| 201 | +It is assumed that your codebase is under version-control with Git and dependencies |
| 202 | +are managed with Composer (locally). |
| 203 | + |
| 204 | +Every time you push to fortrabbit composer install runs before your code gets |
| 205 | +deployed. To finetune the deployment behavior put a `fortrabbit.yml`_. deployment |
| 206 | +file (optional) in the project root. |
| 207 | + |
| 208 | +Add fortrabbit as a (additional) Git remote and add your configuration changes. |
| 209 | + |
| 210 | +.. code-block:: bash |
| 211 | +
|
| 212 | + $ git remote add fortrabbit git@deploy.eu2.frbit.com:your-app.git |
| 213 | + $ git add composer.json composer.lock |
| 214 | + $ git add app/config/config_prod_secrets.php |
| 215 | +
|
| 216 | +Commit and push |
| 217 | + |
| 218 | +.. code-block:: bash |
| 219 | +
|
| 220 | + $ git commit -m 'fortrabbit config' |
| 221 | + $ git push fortrabbit master -u |
| 222 | +
|
| 223 | +.. note:: |
| 224 | + |
| 225 | + Replace your-app with the name of your fortrabbit App. |
| 226 | + |
| 227 | +.. code-block:: bash |
| 228 | + |
| 229 | + Commit received, starting build of branch master |
| 230 | +
|
| 231 | + ––––––––––––––––––––––– ∙ƒ ––––––––––––––––––––––– |
| 232 | + |
| 233 | + B U I L D |
| 234 | +
|
| 235 | + Checksum: |
| 236 | + def1bb29911a62de26b1ddac6ef97fc76a5c647b |
| 237 | +
|
| 238 | + Deployment file: |
| 239 | + fortrabbit.yml |
| 240 | +
|
| 241 | + Pre-script: |
| 242 | + not found |
| 243 | + 0ms |
| 244 | +
|
| 245 | + Composer: |
| 246 | + - - - |
| 247 | + Loading composer repositories with package information |
| 248 | + Installing dependencies (including require-dev) from lock file |
| 249 | + Nothing to install or update |
| 250 | + Generating autoload files |
| 251 | + |
| 252 | + - - - |
| 253 | + 172ms |
| 254 | +
|
| 255 | + Post-script: |
| 256 | + not found |
| 257 | + 0ms |
| 258 | +
|
| 259 | + R E L E A S E |
| 260 | +
|
| 261 | + Packaging: |
| 262 | + 930ms |
| 263 | +
|
| 264 | + Revision: |
| 265 | + 1455788127289043421.def1bb29911a62de26b1ddac6ef97fc76a5c647b |
| 266 | +
|
| 267 | + Size: |
| 268 | + 9.7MB |
| 269 | +
|
| 270 | + Uploading: |
| 271 | + 500ms |
| 272 | +
|
| 273 | + Build & release done in 1625ms, now queued for final distribution. |
| 274 | +
|
| 275 | +
|
| 276 | +.. note:: |
| 277 | + |
| 278 | + The first ``git push`` takes much longer as all composer dependencies get |
| 279 | + downloaded. All subsequent deploys are done within seconds. |
| 280 | + |
| 281 | +That's it! Your application is being deployed on fortrabbit. More information |
| 282 | +about `database migrations and tunneling`_ can be found in the fortrabbit |
| 283 | +documentation. |
| 284 | + |
| 285 | +.. _`fortrabbit`: https://www.fortrabbit.com |
| 286 | +.. _`Symfony install guide`: https://help.fortrabbit.com/install-symfony |
| 287 | +.. _`fortrabbit.yml`: https://help.fortrabbit.com/deployment-file-v2 |
| 288 | +.. _`database migrations and tunneling`: https://help.fortrabbit.com/install-symfony-2#toc-migrate-amp-other-database-commands |
| 289 | +.. _`Sign up`: https://dashboard.fortrabbit.com |
0 commit comments