📜 ⬆️ ⬇️

Warm 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, go here .

Go


So, imagine that we have a ready php + mysql application. For a start, register here . On the mail will receive a letter confirming registration. Next, click on the link, enter the password and confirmation, click save. The first stage is passed, we go further.

After successful registration, the service offers us to download a client. Naturally, for each OS and 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 will not write about it, on the Internet is full of information about it.

After installation, you need to enter the application:

 $ heroku login 


Enter the 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 public ssh:



To view 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”. Go to the directory where our application is stored, and write in the console:

 $ heroku create 


As a result, you should see something like this:



Next, we push our application in the heroku master:

 $ git push heroku master 


Then we write to the console:

 $ heroku open 


Our site will open in a browser. If we didn’t use mysql, then this would be the end of it, but we have to sweat a little more. Most likely, errors appeared on the screen stating that it was impossible to connect to mysql. Also errors can be viewed by opening the logs:

 $ heroku logs 


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



If you do not do this, when installing ClearDB you will see an error:

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


Below is the command to install ClearDB:

 $ heroku addons:add cleardb:ignite 
at

ClearDB is installed, now let's look at the database access:

 $ heroku config 


We get the result in the form:

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

Using the obtained accesses through any convenient MySQL client, we upload 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 appropriate 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 file composer.json to the root:

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


If you already have one, you just need to add "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 would be glad if this "manual" will help someone.

Thank you all for attention.

Source: https://habr.com/ru/post/236211/


All Articles