Search code examples
phpherokucommand-linecommand-line-interfaceheroku-toolbelt

How to run a Command Line PHP Script on Heroku App


I have PHP apps deployed on Heroku. How can I run a Command Line PHP Script on an app even though I don't have command line access or terminal access to the server?


Solution

  • ( this assumes that you already have an existing PHP app deployed to Heroku )

    1) Install Heroku's Command Line Tool "Heroku Toolbelt" in your local dev environment.

    2) Then run this in your local terminal window:

    PHP Internal Commands:

    heroku run php --version
    

    Your PHP Scripts:

    sudo heroku run php path/to/file.php
    

    ( may not need to use sudo - taken from an example - I don't use sudo )

    How to run the command on a specific App. I have several apps running on Heroku and I usually add -a :

    heroku run php path/to/file.php -a <app name>
    

    A Default App can be set to avoid having to specify the app every time:

    heroku git:remote -a <app name>