Deploy php + MySQL on heroku

Good day to all. I want to share with you my experience in deploying php + mysql applications on the heroku service . If this is your first time hearing about this, you are here .

Go


So, let's imagine that we have a ready-made php + mysql application. To get started, register here . A confirmation email will be sent to the post office. Next, follow the link, enter the password and confirmation, click save. The first stage is passed, go further.

After successful registration, the service offers us to download the client. Naturally, each OS has its own installation option. Further we will consider an example for UNIX.

We write in the console:

wget -qO- https://toolbelt.heroku.com/install.sh | sh


There should be no problems with the installation. We also need installed and configured git, I won’t write about it, on the Internet there is a lot of information about it.

After installation, you need to enter the application:


$ heroku login


Enter your email and password. The application should authorize you and automatically download your public ssh key. If this does not happen, go to your account (https://dashboard.heroku.com/account) and add the public ssh:



To view the public ssh, we write in the console:


$ cat ~/.ssh/id_rsa.pub


Keys can also be added using the command:


$ heroku keys:add


So, everything is ready to create your first “heroku application”. We go to the directory where our application is stored, and write in the console:


$  heroku create


As a result, you should see something similar to this:



Next, push our application into heroku master:


$ git push heroku master


Then we write in the console:


$ heroku open


Our site will open in a browser. If we had not used mysql, then this would have ended, but we will have to sweat a little more. Most likely, errors appeared on the screen stating that it was impossible to connect to mysql. You can also see errors by opening the logs:


$ heroku logs


For work with mysql we will use the ClearDB addon . To install it, first you need to fill in your credit card information on the dashboard.heroku.com/account page :



If you do not do this, you will see an error when installing ClearDB: Below is the command to install ClearDB:

Adding cleardb:ignite on dry-taiga-2649... failed
! Please verify your account to install this add-on
! For more information, see devcenter.heroku.com/categories/billing
! Verify now at heroku.com/verify





$ heroku addons:add cleardb:ignite
in

ClearDB installed, now let's see access to the database:


$ heroku config


We will get the result in the form:

CLEARDB_DATABASE_URL:mysql://USER:PASSWORD@HOSTNAME/DBASENAME?reconnect=true

Using the obtained access through any convenient MySQL client, fill in the database dump to the server.

Access to the database in php can be obtained as follows:

    $url=parse_url(getenv("CLEARDB_DATABASE_URL"));
    $server = $url["host"];
    $username = $url["user"];
    $password = $url["pass"];
    $db = substr($url["path"],1);
    mysqli_connect($server, $username, $password);
    mysqli_select_db($db);


In order not to change the config for the local site and heroku each time, you can add a check:

if ($_SERVER['SERVER_NAME'] == "thawing-island-242342379.herokuapp.com") {
	$url = parse_url(getenv("CLEARDB_DATABASE_URL"));
	$host = $url["host"];
	$username = $url["user"];
	$password = $url["pass"];
	$dbname = substr($url["path"], 1);
} else {
	$host = 'localhost';
	$dbname = 'db';
	$username = 'user';
	$password = '123';
}


It would be more correct to do this through APPLICATION_ENV, but I did not find information on how to do this. If someone in the know - write.

Almost everything is ready. It remains to add the composer.json file to the root:

{
    "require": {
        "ext-mysql": "*"
    }
}


If you already have one, you just need to add it. "ext-mysql": "*"

We write in the console:


$ git add .
$ git commit -am "added db credentials"
$ git push heroku master
$ heroku open


The browser opens, we see the working site.

I will be glad if this "manual" helps someone.

Thank you all for your attention.

Also popular now: