【PHP】Heroku 环境配置与 PHP 和 MySQL

將一個使用PHP和MySQL創建的應用部署到Heroku時,我在煩惱是否可以在Heroku上使用MySQL,所以我寫下了這篇筆記。同時,我使用PDO來連接PHP與數據庫。(這是我從個人博客的舊文章轉過來的)

在这个应用程序中,我们没有进行composer的设置,所以在部署时会出现警告。虽然只要能够成功部署就可以,但关于composer的设置,我们会根据需要再进行总结。
・有关composer的设置,请参考以下网址:
https://devcenter.heroku.com/articles/php-support

我希望在Heroku上运行的环境。

    • PHP

 

    MySQL

设定步骤

1. 初始化本地Git存储库

在应用程序文件夹中移动后,执行git init、git add和git commit命令。一旦执行成功,就只需进行数据库设置并进行部署。

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>

使用ClearDB来操作MySQL。

首先,在Heroku账户上注册并添加信用卡信息。如果选择的是免费计划,那么就不需要付费,可以放心使用。

您可以将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://&lt;username>:&lt;password>@&lt;hostname>/&lt;dbname>?reconnect=true</pre>

将CLEARDB_DATABASE_URL的值分割为username、password、hostname和dbname,并将其复制到以下命令并执行。

ryuki@ryuki MINGW64 ~/Desktop/test_app (master)$ heroku config:set DATABASE_URL="mysql://&lt;username>:&lt;password>@&lt;hostname>/&lt;dbname>?reconnect=true"
Setting DATABASE_URL and restarting arcane-ravine-17252... done, v5
DATABASE_URL: mysql://&lt;username>:&lt;password>@&lt;hostname>/&lt;dbname>?reconnect=true

以上是关于数据库设置的所有内容了。

4. PHP程序

在PHP程序中获取数据库环境的值可以按以下方式进行操作。

$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文件。请参考开头提供的URL进行处理。

请参考网站

    • Deploying with Git

 

    • https://devcenter.heroku.com/articles/git

 

    • ClearDB MySQL

 

    https://devcenter.heroku.com/articles/cleardb
广告
将在 10 秒后关闭
bannerAds