7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPアプリをHerokuにデプロイする【Windows】

Posted at

以下のような方で、主にPHPを使用している人向けに書きました

  • 無料でWebサーバーを利用したい方
  • 自作のWebアプリやサイトを公開したい方

インストールなど

Git,Composerのインストール、Herokuのアカウント作成については終わらせているものとします

Gitの設定

パス

コマンドプロンプトを開き、cdコマンドでGitがインストールされているフォルダに移動
C:\ProgramData\Gitまで移動する

設定変数の設定

まずはこの二つを実行する

git config --global user.name "任意の名前" 
git config --global user.email "任意のemailアドレス"

コミットして変更内容を記述する時などに使うエディタを指定する

git config --global core.editor notepad

heroku上にアプリを作成する

以下のコマンドを任意のディレクトリで実行する

 heroku login
 heroku create (アプリ名※省略可)

https://(アプリ名).herokuapp.com/ | https://git.heroku.com/(アプリ名).git
のようにURLが取得できる。
createコマンドでアプリ名を指定した場合には、(アプリ名)の箇所に指定したものが入る。
指定しなかった場合は自動で割り当てられたものが入る。

次にアプリのルートディレクトリにcdコマンドで移動し、以下のコマンドを実行する
(以下、アプリのルートディレクトリを**$**とする)

$ git init
$ git add .
$ git commit -m "first commit"
$ git remote add heroku https://git.heroku.com/(アプリ名).git

作成されたことを確認する

$ git remote
heroku

デプロイを行う

以下のコマンドでデプロイを行う

$ git push heroku master

エラーの対処

※pushに失敗した場合のメッセージ

:
remote: Verifying deploy...
remote:
remote: !       Push rejected to (アプリ名).

エラーの内容を確認してみる

:
remote:  !     No default language could be detected for this app.
remote:                         HINT: This occurs when Heroku cannot detect the buildpack to use for this application a
:

このように言語を特定できないというエラーがでた場合、以下のコマンドを実行する

$ composer init

途中選択する箇所がでてくるが、とりあえずEnter押下で初期値を選択していく

アプリのルートディレクトリにcomposer.jsonファイルが作成されていることを確認する

ファイル作成などをGitで管理する

ファイルを追加、変更した際は、そのファイルが置かれているフォルダのパスで以下を実行する

(フォルダまでのパス) git add (追加、変更したファイル名)
$ git commit 

ファイルを削除した場合は、ルートディレクトリでgit commitするだけでいい
そのほか、commit可能な状態(staged)かどうか確認するコマンドとして$ git status, $ git diff
などが役に立つ

再度、git pushを実行する

$ git push heroku master
remote: Verifying deploy... done.
To https://git.heroku.com/(アプリ名).git
   190a832..eb17420  master -> master

これでアプリがデプロイされた

アプリをWeb上でアクセス可能にする

コマンドプロンプトでheroku openを実行するか、URLhttp://(アプリ名)/herokuapp.comを打ち込むとアプリにアクセスできる。

アプリのページが表示されない場合、ファイルパスの指定を行う必要がある。

Procfile作成の手順

アプリのルートディレクトリにProcfile(拡張子なし)のファイルを作成する
中身に以下のコードを記述する
web: vendor/bin/heroku-php-apache2 web/
最後のweb/の部分がルートディレクトリから.jsや.cssなどのファイルがある場所へのパスとなるように記述する

以下のコマンドを実行する

$ git add Procfile
$ git commit
$ git push heroku master

これでアプリへのアクセスができたらおわりです。

参考にした記事

GitもHerokuも初心者なので、たいへん参考になりました。
誤っている箇所の指摘などありましたら、コメントいただけたら嬉しいです。

7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?