Deploy Sylius to Heroku
Tags: php, commerce, symfony, sylius
Published: 2021-11-19 12:38:46

In this blog we are going to deploy sylius store to heroku, First you need to know that sylius will need php, node, mysql environments to load and build assets for our store so long story short we will install new app and add two buildpacks `heroku/php` and `heroku/nodejs` and will add add-on [JawsDb](https://elements.heroku.com/addons/jawsdb) for database storage and make some `nginx` and edit `composer.json` to make the project runnable by heroku.
### You should know
* heroku will look for the required php version from the `composer.json` and will look for `compile script` to run the project.
* sylius database is too big for the free version on `Jawsdb` so feel free to choose your package the `10$` done the job for me.

> get A fresh copy of sylius [here](https://docs.sylius.com/en/latest/book/installation/installation.html)

## Setup
1. navigate to your sylius project dir and init new heroku app using the cli

        $ heroku create APP_NAME

2. we need to add `heroku/php` and `heroku/node` 

        $ heroku buildpacks:set heroku/php --app APP_NAME
        $ heroku buildpacks:set heroku/nodejs --app APP_NAME
       `OR` you can navigate to settings and under buildpack add `php` and `nodeJs` 

3. add the Jawsweb database then set `APP_END=prod`

        $ heroku addons:create jawsdb --app APP_NAME # This command will also add a new env JAWSDB_URL
        $ heroku config:set APP_END=prod --app APP_NAME


4. create `Procfile` in your root directory and add this content 

        web: heroku-php-nginx -C nginx_app.conf public/


5. create nginx config file `nginx_app.conf` in the root directory

        location / {
            # try to serve file directly, fallback to rewrite
            try_files $uri @rewriteapp;
        }

        location @rewriteapp {
            # rewrite all to index.php
            rewrite ^(.*)$ /index.php/$1 last;
        }

        location ~ ^/index\.php(/|$) {
            try_files @heroku-fcgi @heroku-fcgi;
            # ensure that /index.php isn't accessible directly, but only through a rewrite 
        internal;
        }


6. we need to make some changes to our `composer.json` so heroku can run sylius install and build scripts

            "scripts": {
                "compile": [
                    "@php bin/console sylius:install --no-interaction --fixture-suite=default"
                ],
                "post-create-project-cmd": [
                    "@php bin/console sylius:inform-about-gus --ansi",
                    "@php bin/console sylius:show-available-plugins --ansi"
                ],
                "auto-scripts": {
                    "cache:clear": "symfony-cmd",
                    "assets:install %PUBLIC_DIR%": "symfony-cmd"
                }

       [Snippet Here](https://github.com/aa-ahmed-aa/SyliusToHeroku/blob/master/composer.json#L88-L99)

       and under `require` section add to enable this php extension on the heroku app
        
        "ext-intl": "*"
        

       then run

        
        $ composer update 
        

       do not forget to remove `composer.lock` from `.gitignore`

7. to the `package.json` file add this block to specify node and yarn versions
 
        "engines": {
            "node": "14.x",
            "yarn": "1.x"
        },
8. to make the server prod logs appear in the instance logs edit `/conig/packages/prod/monolog.yaml` with content

        monolog:
            handlers:
                main:
                    type: fingers_crossed
                    action_level: error
                    handler: nested
                    excluded_http_codes: [404, 405]
                nested:
                    type: stream
                    path: "php://stderr"
                    level: debug

9. remove the `.env` file from `.gitignore` and make sure it contain these two envs reading from global env
        
        APP_ENV=${APP_END}
        DATABASE_URL=${JAWSDB_URL}

10. we will create a repo on github and make heroku deploy from github so 

        $ git init
        $ git remote add origin <github_remote_url>
        $ git add .
        $ git commit -am "init commit"
        $ git push origin master


11. then go to the app heroku app to the `Deploy` tab then select `Github` from `Deployment method` then choose your repository and the branch to deploy then click on the `Deploy Branch`

11. Click on the `open app` and you just deployed your first sylius store to heroku.

### Referenceies
1. [Jawsdb mysql](https://devcenter.heroku.com/articles/jawsdb#provisioning-the-add-on)
2. [PHP buildpack](https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-php)
3. [NodeJs Apps on heroku](https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-nodejs)
4. [Customize nginx and php](https://devcenter.heroku.com/articles/custom-php-settings)
5. [Install sylius](https://docs.sylius.com/en/1.9/getting-started-with-sylius/installation.html)
6. [Symfony logs to the instance logs](https://devcenter.heroku.com/articles/deploying-symfony4#logging)