LoginSignup
1
1

More than 3 years have passed since last update.

Herokuにデプロイする時に躓いたこと

Posted at

はじめに

ポートフォリオサイトを作るための最初の一歩として
試しに静的サイトをherokuにデプロイしてみたら少し躓いたので
備忘録として残そうと思いました。
誰かの参考になれば嬉しいです。


Herokuのアカウント作成とHerokuCLIのインストール

Herokuのアカウント作成は必要な情報を入力してメール認証するだけ。
https://www.heroku.com/

つぎにHerokuアプリをターミナルから直接簡単に作成、管理できるようにするため
HerokuCLIをインストール。
https://devcenter.heroku.com/articles/heroku-cli
ターミナルから以下を入力するだけでインストールできます。

% curl https://cli-assets.heroku.com/install.sh | sh

途中でパスワードを求められるけど、これは自分のPCにサインインするときのパスワード。

インストールできたら、以下のコマンドを入力して

% heroku --version

バージョン名が出てくれば成功。

以下のコマンドを入力すると

% heroku login

ブラウザが起動してログインする。

いざデプロイ

herokuにアプリケーションを作成するために

% heroku create

を実行。
するとherokuのダッシュボードに新しく追加されるはずです。

そして適当に

% mkdir sample

でディレクトリを作って


<?php echo "hoge"; ?>

をindex.phpという名前で保存。
そして以下のコマンドを実行。

% cd sample
% git init
% git add .
% git commit -m "first commit"
% git push heroku master
% heroku open

でブラウザが開き、画面に「hoge」が表示されれば完了です。

ここでエラー発生

git push heroku masterを実行すると

fatal: 'heroku' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

という文が表示されました。
どうやらgitがリモートのリポジトリを参照できていないようです。

% cat .git/config

を実行し、[remote "heroku"]が無ければ追加します。

% git remote add heroku https://git.heroku.com/アプリケーション名.git

これでリモートにherokuが追加されたはずです。
ちなみにアプリケーション名は、さっきharoku createを実行した直後の
Creating app... done, ⬢以降にあるのがアプリケーション名です。

私はこれに気づかず、アプリケーション名を適当に「sample」とかにしちゃってました。
このアプリケーション名と、ダッシュボードにあるアプリケーション名が不一致のまま
git push heroku masterを実行すると、今度は

remote: !   Your account [自分のメールアドレス] does not have access to sample.
fatal: unable to access 'https://git.heroku.com/sample.git/': The requested URL returned error: 403

という文がでます。あたりまえですよね。

対処法としては
vim .git/configを実行して、編集画面にします。
iを押すとインサートモードになるので、アプリケーション名のところまでカーソルを移動して編集します。
編集が終わったらescでインサートモードを終了し
:wqで保存して終了です。
念のためもう一度cat .git/configで変更できてるか確認してみましょう。

これでもう一度pushし、やっとデプロイすることができました。

1
1
2

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
1
1