PHPとMySQLを使用して作成したアプリをHerokuにデプロイする際、HerokuでMySQLを使えるかどうかで悩んだので備忘録として書きます。また、PHPからのDBへの接続にはPDOを使用しました。(個人ブログの昔の記事からの移行です)
※今回作成したアプリではcomposerのセットアップまで行っておりませんので、デプロイ時にWarningが出ます。デプロイ出来ればいいものとして、composerの設定に関してはまた必要に応じてまとめようと思います。
・composerの設定に関しては以下URL参照
https://devcenter.heroku.com/articles/php-support
Herokuで動作させたい環境
- PHP
- MySQL
設定手順
1.ローカルGitリポジトリの初期化
アプリのあるフォルダに移動後、git init
やgit add
、git commit
のコマンド実行。実行できたら、あとはDBのセットアップを行い、デプロイするだけです。
ryuki@ryuki MINGW64 ~/Desktop/test_app$ git init
Initialized empty Git repository in C:/Users/ryuki/Desktop/test_app/.git/
ryuki@ryuki MINGW64 ~/Desktop/test_app (master)$ git add --all .
ryuki@ryuki MINGW64 ~/Desktop/test_app (master)$ git commit -m "php mysql deploy test"
[master (root-commit) 042dcd0] php mysql deploy test
36 files changed, 8591 insertions(+)
create mode 100644 base.css
create mode 100644 core_system/book_search.php
create mode 100644 core_system/login.php
create mode 100644 core_system/my_book_detail.php
.......
2.herokuリモートリポジトリの作成
heroku createでHerokuリモートリポジトリを作成。このリポジトリの中にコードをデプロイすることでコードが動作するようになります。
ryuki@ryuki MINGW64 ~/Desktop/test_app(master)$ heroku create
Creating app… done, arcane-ravine-17252
https://arcane-ravine-17252.herokuapp.com/ | https://git.heroku.com/arcane-ravine-17252.git
このローカルリポジトリ内でアプリを作成したのが初めてであれば、作成したHerokuGitリポジトリが自動的にリモートリポジトリとして設定されています。git remote -vコマンドで確認することが出来ます。
ryuki@ryuki MINGW64 ~/Desktop/test_app (master)$ git remote -v
heroku https://git.heroku.com/arcane-ravine-17252.git (fetch)
heroku https://git.heroku.com/arcane-ravine-17252.git (push)</pre>
3.ClearDBでMySQLを使用する
まず、herokuのアカウント上でクレジットカード情報を登録しておきます。Freeプランであれば無料ですのでご安心を。
ClearDB アドオンは、アプリにインストールして実行できます。次のコマンドを実行して、ClearDB をアプリケーションに追加します。
ryuki@ryuki MINGW64 ~/Desktop/test_app (master)$ heroku addons:create cleardb:ignite
Creating cleardb:ignite on arcane-ravine-17252... free
Created cleardb-lively-54080 as CLEARDB_DATABASE_URL
Use heroku addons:docs cleardb to view documentation
次のコマンドを実行して、データベース URL を取得します。
$ heroku config
=== arcane-ravine-17252 Config Vars
CLEARDB_DATABASE_URL: mysql://<username>:<password>@<hostname>/<dbname>?reconnect=true</pre>
CLEARDB_DATABASE_URLの値をusername,password,hostname,dbnameに分け次のコマンドにコピーして実行します。
ryuki@ryuki MINGW64 ~/Desktop/test_app (master)$ heroku config:set DATABASE_URL="mysql://<username>:<password>@<hostname>/<dbname>?reconnect=true"
Setting DATABASE_URL and restarting arcane-ravine-17252... done, v5
DATABASE_URL: mysql://<username>:<password>@<hostname>/<dbname>?reconnect=true
以上で、DBの設定は終わりです。
4.PHPのプログラム
PHPのプログラム内でのDB環境の値の取得は以下のように行います。
$url = parse_url(getenv("CLEARDB_DATABASE_URL"));
$db_name = substr($url["path"], 1);
$db_host = $url["host"];
$user = $url["user"];
$password = $url["pass"];
$dsn = "mysql:dbname=".$db_name.";host=".$db_host;
$pdo=new PDO($dsn,$user,$password,array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
最後にデプロイして終了です。
ryuki@ryuki MINGW64 ~/Desktop/test_app (master)$ git push heroku master
Enumerating objects: 45, done.
Counting objects: 100% (45/45), done.
Delta compression using up to 4 threads
Compressing objects: 100% (43/43), done.
Writing objects: 100% (45/45), 112.66 KiB | 1.03 MiB/s, done.
Total 45 (delta 7), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> PHP app detected
remote:
remote: ! WARNING: No 'composer.json' found!
remote: !
remote: ! Your project only contains an 'index.php', no 'composer.json'.
remote: !
remote: ! Using 'index.php' to declare app type as PHP is deprecated and
remote: ! may lead to unexpected behavior.
remote: !
remote: ! Please consider updating your codebase to utilize Composer and
remote: ! modern dependency management in order to benefit from the latest
remote: ! PHP runtimes and improved application performance, as well as
remote: ! control over the PHP versions and extensions available.
remote: !
remote: ! For an introduction to dependency management with Composer and
remote: ! how to get the most out of PHP on Heroku, refer to the docs at
remote: ! https://getcomposer.org/doc/00-intro.md and
remote: ! https://devcenter.heroku.com/articles/getting-started-with-php
remote:
remote: -----> Bootstrapping...
remote: -----> Installing platform packages...
remote: NOTICE: No runtime required in composer.lock; using PHP ^7.0.0
remote: - php (7.3.11)
remote: - apache (2.4.41)
remote: - nginx (1.16.1)
remote: -----> Installing dependencies...
remote: Composer version 1.9.0 2019-08-02 20:55:32
remote: -----> Preparing runtime environment...
remote: NOTICE: No Procfile, using 'web: heroku-php-apache2'.
remote: -----> Checking for additional extensions to install...
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 15.8M
remote: -----> Launching...
remote: Released v6
remote: https://arcane-ravine-17252.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/arcane-ravine-17252.git
* [new branch] master -> master
composer.jsonとcomposer.lockがないというWarningが出ています。これへの対処は冒頭に示したURLを参照してください。
参考サイト
-
Deploying with Git
https://devcenter.heroku.com/articles/git -
ClearDB MySQL
https://devcenter.heroku.com/articles/cleardb