|
| 1 | +.. index:: |
| 2 | + single: Deployment; Deploying to Heroku Cloud |
| 3 | + |
| 4 | +Deploying to Heroku Cloud |
| 5 | +========================= |
| 6 | + |
| 7 | +This step by step cookbook describes how to deploy a Symfony2 web application to |
| 8 | +the Heroku cloud platform. Its contents are based on `the original article`_ |
| 9 | +published by Heroku. |
| 10 | + |
| 11 | +Setting up |
| 12 | +---------- |
| 13 | + |
| 14 | +To setup a new Heroku website, first `signup with Heroku`_ or sign in |
| 15 | +with your credentials. Then download and install the `Heroku Toolbet`_ on your |
| 16 | +local computer. |
| 17 | + |
| 18 | +You can also check out the `getting Started with PHP on Heroku`_ guide to gain |
| 19 | +more familiarity with the specifics of working with PHP applications on Heroku. |
| 20 | + |
| 21 | +Preparing your Application |
| 22 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 23 | + |
| 24 | +Deploying a Symfony2 application to Heroku doesn't require any change in its |
| 25 | +code, but it requires some minor tweaks to its configuration. |
| 26 | + |
| 27 | +By default, the Symfony2 app will log into your application's ``app/log/`` |
| 28 | +directory. This is not ideal as Heroku uses an `ephemeral file system`_. On |
| 29 | +Heroku, the best way to handle logging is using `Logplex`_. And the best way to |
| 30 | +send log data to Logplex is by writing to ``STDERR`` or ``STDOUT``. Luckily, |
| 31 | +Symfony2 uses the excellent Monolog library for logging. So, a new log |
| 32 | +destination is just a change to a config file away. |
| 33 | + |
| 34 | +Open the ``app/config/config_prod.yml`` file, locate the |
| 35 | +``monolog/handlers/nested`` section (or create it if it doesn't exist yet) and |
| 36 | +change the value of ``path`` from |
| 37 | +``"%kernel.logs_dir%/%kernel.environment%.log"`` to ``"php://stderr"``: |
| 38 | + |
| 39 | +.. code-block:: yaml |
| 40 | +
|
| 41 | + # app/config/config_prod.yml |
| 42 | + monolog: |
| 43 | + # ... |
| 44 | + handlers: |
| 45 | + # ... |
| 46 | + nested: |
| 47 | + # ... |
| 48 | + path: "php://stderr" |
| 49 | +
|
| 50 | +Once the application is deployed, run ``heroku logs --tail`` to keep the |
| 51 | +stream of logs from Heroku open in your terminal. |
| 52 | + |
| 53 | +Creating a new Application on Heroku |
| 54 | +------------------------------------ |
| 55 | + |
| 56 | +To create a new Heroku application that you can push to, use the CLI ``create`` |
| 57 | +command: |
| 58 | + |
| 59 | +.. code-block:: bash |
| 60 | +
|
| 61 | + $ heroku create |
| 62 | +
|
| 63 | + Creating mighty-hamlet-1981 in organization heroku... done, stack is cedar |
| 64 | + http://mighty-hamlet-1981.herokuapp.com/ | [email protected]:mighty-hamlet-1981.git |
| 65 | + Git remote heroku added |
| 66 | +
|
| 67 | +You are now ready to deploy the application as explained in the next section. |
| 68 | + |
| 69 | +Deploying your Application on Heroku |
| 70 | +------------------------------------ |
| 71 | + |
| 72 | +To deploy your application to Heroku, you must first create a ``Procfile``, |
| 73 | +which tells Heroku what command to use to launch the web server with the |
| 74 | +correct settings. After you've done that, you can simply ``git push`` and |
| 75 | +you're done! |
| 76 | + |
| 77 | +Creating a Procfile |
| 78 | +~~~~~~~~~~~~~~~~~~~ |
| 79 | + |
| 80 | +By default, Heroku will launch an Apache web server together with PHP to serve |
| 81 | +applications. However, two special circumstances apply to Symfony applications: |
| 82 | + |
| 83 | +1. The document root is in the ``web/`` directory and not in the root directory |
| 84 | + of the application; |
| 85 | +2. The Composer ``bin-dir``, where vendor binaries (and thus Heroku's own boot |
| 86 | + scripts) are placed, is ``bin/`` , and not the default ``vendor/bin``. |
| 87 | + |
| 88 | +.. note:: |
| 89 | + |
| 90 | + Vendor binaries are usually installed to ``vendor/bin`` by Composer, but |
| 91 | + sometimes (e.g. when running a Symfony Standard Edition project!), the |
| 92 | + location will be different. If in doubt, you can always run |
| 93 | + ``composer config bin-dir`` to figure out the right location. |
| 94 | + |
| 95 | +Create a new file called ``Procfile`` (without any extension) at the root |
| 96 | +directory of the application and add just the following content: |
| 97 | + |
| 98 | +.. code-block:: text |
| 99 | +
|
| 100 | + web: bin/heroku-php-apache2 web/ |
| 101 | +
|
| 102 | +If you prefer working on the command console, execute the following commands to |
| 103 | +create the ``Procfile`` file and to add it to the repository: |
| 104 | + |
| 105 | +.. code-block:: bash |
| 106 | +
|
| 107 | + $ echo "web: bin/heroku-php-apache2 web/" > Procfile |
| 108 | + $ git add . |
| 109 | + $ git commit -m "Procfile for Apache and PHP" |
| 110 | + [master 35075db] Procfile for Apache and PHP |
| 111 | + 1 file changed, 1 insertion(+) |
| 112 | +
|
| 113 | +Pushing to Heroku |
| 114 | +~~~~~~~~~~~~~~~~~ |
| 115 | + |
| 116 | +Next up, it's finally time to deploy your application to Heroku. If you are |
| 117 | +doing this for the very first time, you may see a message such as the following: |
| 118 | + |
| 119 | +.. code-block:: bash |
| 120 | +
|
| 121 | + The authenticity of host 'heroku.com (50.19.85.132)' can't be established. |
| 122 | + RSA key fingerprint is 8b:48:5e:67:0e:c9:16:47:32:f2:87:0c:1f:c8:60:ad. |
| 123 | + Are you sure you want to continue connecting (yes/no)? |
| 124 | +
|
| 125 | +In this case, you need to confirm by typing ``yes`` and hitting ``<Enter>`` key |
| 126 | +- ideally after you've `verified that the RSA key fingerprint is correct`_. |
| 127 | +
|
| 128 | +Then, deploy your application executing this command: |
| 129 | + |
| 130 | +.. code-block:: bash |
| 131 | +
|
| 132 | + $ git push heroku master |
| 133 | +
|
| 134 | + Initializing repository, done. |
| 135 | + Counting objects: 130, done. |
| 136 | + Delta compression using up to 4 threads. |
| 137 | + Compressing objects: 100% (107/107), done. |
| 138 | + Writing objects: 100% (130/130), 70.88 KiB | 0 bytes/s, done. |
| 139 | + Total 130 (delta 17), reused 0 (delta 0) |
| 140 | +
|
| 141 | + -----> PHP app detected |
| 142 | +
|
| 143 | + -----> Setting up runtime environment... |
| 144 | + - PHP 5.5.12 |
| 145 | + - Apache 2.4.9 |
| 146 | + - Nginx 1.4.6 |
| 147 | +
|
| 148 | + -----> Installing PHP extensions: |
| 149 | + - opcache (automatic; bundled, using 'ext-opcache.ini') |
| 150 | +
|
| 151 | + -----> Installing dependencies... |
| 152 | + Composer version 64ac32fca9e64eb38e50abfadc6eb6f2d0470039 2014-05-24 20:57:50 |
| 153 | + Loading composer repositories with package information |
| 154 | + Installing dependencies from lock file |
| 155 | + - ... |
| 156 | +
|
| 157 | + Generating optimized autoload files |
| 158 | + Creating the "app/config/parameters.yml" file |
| 159 | + Clearing the cache for the dev environment with debug true |
| 160 | + Installing assets using the hard copy option |
| 161 | + Installing assets for Symfony\Bundle\FrameworkBundle into web/bundles/framework |
| 162 | + Installing assets for Acme\DemoBundle into web/bundles/acmedemo |
| 163 | + Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution |
| 164 | +
|
| 165 | + -----> Building runtime environment... |
| 166 | +
|
| 167 | + -----> Discovering process types |
| 168 | + Procfile declares types -> web |
| 169 | +
|
| 170 | + -----> Compressing... done, 61.5MB |
| 171 | +
|
| 172 | + -----> Launching... done, v3 |
| 173 | + http://mighty-hamlet-1981.herokuapp.com/ deployed to Heroku |
| 174 | +
|
| 175 | + To [email protected]:mighty-hamlet-1981.git |
| 176 | + * [new branch] master -> master |
| 177 | +
|
| 178 | +And that's it! If you now open your browser, either by manually pointing |
| 179 | +it to the URL ``heroku create`` gave you, or by using the Heroku Toolbelt, the |
| 180 | +application will respond: |
| 181 | + |
| 182 | +.. code-block:: bash |
| 183 | +
|
| 184 | + $ heroku open |
| 185 | + Opening mighty-hamlet-1981... done |
| 186 | +
|
| 187 | +You should be seeing your Symfony2 application in your browser. |
| 188 | + |
| 189 | +.. _`the original article`: https://devcenter.heroku.com/articles/getting-started-with-symfony2 |
| 190 | +.. _`signup with Heroku`: https://signup.heroku.com/signup/dc |
| 191 | +.. _`Heroku Toolbet`: https://devcenter.heroku.com/articles/getting-started-with-php#local-workstation-setup |
| 192 | +.. _`getting Started with PHP on Heroku`: .. _`Heroku Toolbet`: https://devcenter.heroku.com/articles/getting-started-with-php |
| 193 | +.. _`ephemeral file system`: https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem |
| 194 | +.. _`Logplex`: https://devcenter.heroku.com/articles/logplex |
| 195 | +.. _`verified that the RSA key fingerprint is correct`: https://devcenter.heroku.com/articles/git-repository-ssh-fingerprints |
0 commit comments