How to Set Up a PHP and MySQL App on Heroku

Learn how to set up a PHP and MySQL app on Heroku with this step-by-step guide. Follow these instructions to get your app up and running on one of the cloud platform

Here is a step-by-step guide on how to set up a PHP and MySQL app on Heroku:

  1. First, you'll need to create a Heroku account if you haven't already. You can do this by going to heroku.com and clicking the "Sign up" button.

  2. Once you've created an account, log in to the Heroku Dashboard.

  3. Click the "New" button in the top right corner of the dashboard and select "Create new app".

  4. Give your app a name and select the region you want it to be hosted in.

  5. Click the "Create app" button.

  6. Next, you'll need to add a new add-on to your app. Search for the "ClearDB MySQL" add-on and select it.

  7. Choose the pricing plan that best suits your needs and click "Provision".

  8. After the add-on is provisioned, go to the "Settings" tab of your app and click the "Reveal Config Vars" button.

  9. You'll need to add a few environment variables to connect to your MySQL database. The exact variable names may vary depending on your add-on, but they should look something like this:

    • CLEARDB_DATABASE_URL: This variable will contain the URL of your MySQL database. You'll need to use this URL to connect to your database in your PHP code.

    • CLEARDB_DATABASE_NAME: This variable will contain the name of your MySQL database.

    • CLEARDB_DATABASE_USERNAME: This variable will contain the username you'll use to connect to your MySQL database.

    • CLEARDB_DATABASE_PASSWORD: This variable will contain the password you'll use to connect to your MySQL database.

  10. With your MySQL database set up, you can now create a PHP app on Heroku. You'll need to create a composer.json file in the root directory of your app that specifies your PHP dependencies. Here's an example
     {
        "require": {
            "php": ">=7.0.0"
        }
    }
  11. Next, create a Procfile in the root directory of your app that tells Heroku how to run your app. Here's an example:
     web: vendor/bin/heroku-php-apache2 public/

    This Procfile tells Heroku to use the heroku-php-apache2 buildpack to run your app, and to serve the files in the public directory.

  12. Now you can push your app to Heroku by running the following commands in your terminal:
     git init
    git add .
    git commit -m "Initial commit"
    heroku git:remote -a <your-app-name>
    git push heroku master
    
  13. Once your app is deployed, you can access it by going to https://<your-app-name>.herokuapp.com.

That's it! You now have a PHP and MySQL app running on Heroku.