Skip to content

Instantly share code, notes, and snippets.

@sunato-a
Created September 24, 2012 09:05
Show Gist options
  • Save sunato-a/3775046 to your computer and use it in GitHub Desktop.
Save sunato-a/3775046 to your computer and use it in GitHub Desktop.
120630_hackathon#1(Public)

0. 環境


  • さくらVPS 512(Debian)
  • カーネル 2.6.32

1. Gitのセットアップ

手始めに、gitをいれます。

~$ sudo apt-get install git
~$ git config --global user.email "[email protected]"
~$ git config --global user.name "asunato"

2. DietCakeのHello world サンプルを取得


githubにあるサンプルを持ってきて使います。
まずは/home/asunato/www を作成して、このディレクトリに移動。

~$ mkdir www/
~$ cd www/

続いて、git clone で個人用のローカルリポジトリを作成。中央リポジトリ(リモート)からコピーしてきます。

~/www$ git clone git://github.com/dietcake/dietcake-hello.git

取得したサンプルでは、DietCake自体がサブモジュールとして組み込まれていたのでこれに倣ってサブモジュールとして扱うことにしました。

~/www/dietcake-hello/$ 
~/www/dietcake-hello/$ git submodule init
~/www/dietcake-hello/$ git submodule update

3. GitHubのセットアップ


GitHubを使うために以下のことを行います。

  • アカウントを作成する
  • 公開鍵の登録を行う(画面右上の設定から) サーバの鍵を登録してもよかったのですが、今回はローカルPCの公開鍵を登録しました。
    エージェントフォワーディングでGitHubにアクセスできるからです。

最後に、コンソールからgithubにアクセスできるか確認します。

~/www$ ssh -T [email protected]
Hi nazerin! You've successfully authenticated, but GitHub does not provide shell access.

って言われたらおkです。

続いて、サブモジュールのアップデートをします。 サブモジュールは別のリポジトリで管理されているので、更新されている可能性もあります。
一番新しいバージョンを取得するために、下記の操作を行います。

~/www/dietcake-hello$ git submodule update

4. バーチャルホストの設定とDietCakeでのHello World体験

  • Debianのapacheの場合、バーチャルホストごとに設定ファイルをつくる。ここではdefaultをコピーして使う
~$ cd /etc/apache2/sites-available/
~/etc/apache2/sites-available$ sudo cp default asunato
~/etc/apache2/sites-available$ sudo vi asunato

設定はこんな感じ。(元ファイルにあった使わなそうなディレクティブは消した)

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        # ここはwebrootディレクトリのパスを書く
        DocumentRoot /home/asunato/www/dietcake-hello/app/webroot
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        # ここはerror.logのパスを書く
        ErrorLog /home/asunato/www/dietcake-hello/app/tmp/logs/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
        # ここはaccess.logのパスを書く。"combined"を忘れずに!
        CustomLog /home/asunato/www/dietcake-hello/app/tmp/logs/access.log combined
</VirtualHost>

debian apacheでは、a2ensiteコマンドを使用して、sites-enabled/にsites-available/にある設定へのsymlinkが張れます。
ので、そのようにしてsites-enabled/へのsymlinkをつくります。

~/etc/apache2/sites-available$ sudo a2ensite asunato

もともとあったdefaultは使わないので、無効にしておきます(下記コマンドでsymlinkを消す)

~/etc/apache2/sites-available$ sudo a2dissite default

また、DietCakeではmod_rewriteを使用するのでモジュールを有効にしておきます

~/etc/apache2/sites-available$ sudo a2enmod rewrite

最後にapacheを再起動したらサーバの準備は完了です。

~/etc/apache2/sites-available$ sudo /etc/init.d/apache2 restart

というわけで、この辺りで一度
http://www44039u.sakura.ne.jp/
にアクセスしてみました(◎-◎;)ドキドキ
( ^ω^)・・・

\(^o^)/

どうやら何もでていません。。
何が起きているのか確認するにはログ!というわけで、ログを見ようとしました。

…が、、
/www/dietcake-hello/app/tmp/logs 下の access.log と error.log がからっぽ/(^o^)\
これはなぜかというと、パーミッション的に書き込みができない状態になっていたようです。(確か755になっていた)
というわけで、
/www/dietcake-hello/app/tmp/以下のパーミッションを777にしました。

~/www/dietcake-hello/app$ chmod 777 tmp/
~/www/dietcake-hello/app$ cd tmp/
~/www/dietcake-hello/app/tmp$ chmod 777 logs/

そこで、もう一度ブラウザからアクセスした後、logs/下のログファイルを見てみました。

~/www/dietcake-hello/app/tmp$ cd logs/
~/www/dietcake-hello/app/tmp/logs$ less access.log
211.5.115.9 - - [30/Jun/2012:15:33:14 +0900] "GET / HTTP/1.1" 500 274 "-" "Mozilla/5.0 (Windows NT 6.0; rv:12.0) Gecko/20100101 Firefox/12.0"

( ゚д゚)ポカーン ステータス500を返してました。
サーバ側に何か問題が…?

エラーログも見てみます。

~/www/dietcake-hello/app/tmp/logs$ less error.log
PHP Fatal error:  require_once(): Failed opening required '/home/asunato/www/dietcake-hello/app/config/core.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/asunato/www/dietcake-hello/app/webroot/index.php on line 6

…というエラーが。
core.phpのオープンに失敗したよーって言ってます。
言われるがまま、~/www/dietcake-hello/app/config/core.php があるかどうか確認してみます。

~/www/dietcake-hello/app/config$ ls
bootstrap.php  core_production.php

core.phpがなかった\(^o^)/ 一連の面倒を見てくださったh13i32maruさん曰く、
core.phpはcore_production.phpをコピーして、適宜書き換えて使うのだとか。
というわけで、

~/www/dietcake-hello/app/config$ cp core_production.php core.php
~/www/dietcake-hello/app/config$ vim core.php

として、

define('APP_HOST', 'www44039u.sakura.ne.jp');
define('APP_URL', 'http://www44039u.sakura.ne.jp/');

と環境に合わせて書き換えてみました。

気を取り直してもう一度。

~/www/dietcake-hello/app/tmp/logs$ tail -f access.log error.log php.log

でログ監視をしながら再度アクセス…やはりエラー\(^o^)/ ログはこんな感じ。

[30/Jun/2012:15:34:49 +0900] "GET /index.php?dc_action=index/top HTTP/1.1" 500 274 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0.1"

そういえば… DietCakeでは、~/www/dietcake-hello/app/webroot 下にある .htaccess により、
アクセスしてきたURLの解釈等を行なっています。
例えば、URLが index.php/ で終わってたら、hello/indexを呼ぶ場合はこんな感じです。

RewriteRule ^$ index.php?dc_action=hello/index [QSA,L]

ここでは、「index.php」へのアクセスが来たら、「hello/index」を返すように設定されています。
また、「hello/indexを呼ぶ」というのは、「HelloControllerクラスのindexメソッドを呼んでいる」ことです。

上記のRewriteRuleがあるのに、なぜかindex/topを返そうとしている… もしかして、RewriteRuleが効いてない?

再度Apacheの設定を確認。

~$ cd /etc/apache2/sites-enabled/
~/etc/apache2/sites-enabled$ less asunato
        DocumentRoot /home/asunato/www/dietcake-hello/app/webroot
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>

AllowOverride Noneになっていた… .htaccess受け付けなくなってた。。  AllowOverride None -> All に。

622 sudo /etc/init.d/apache2 restart   →でけた!

5. gitやgithubへ

~$ cd www/dietcake-hello/
~/www/dietcake-hello$ ls -a

.git/が表示されます。 この隠しディレクトリ「.git/」によってリポジトリの管理がされています。

今までの.git/はいらないのでリネームしてしまいます。

~/www/dietcake-hello$ mv .git .git-backup
~/www/dietcake-hello$ git status

とリポジトリの状態を確認すると、リポジトリはない!って言われます。 リポジトリ管理をしている.git/を消したからです。

新規にリポジトリの作成をしてみます。

~/www/dietcake-hello$ git init
~/www/dietcake-hello$ git status
~/www/dietcake-hello$ vi .gitignore

.gitignore では管理対象外にしたいファイルを指定できます。 ここでは.git-backup/を管理対象外にしたいので、.gitignoreに追加しました。

次に管理対象にしたいファイルを追加します。ここではカレントディレクトリ以下を指定しました。

~/www/dietcake-hello$ git status
~/www/dietcake-hello$ git add ./

ここまでの設定等?をローカルリポジトリへコミットします。

~/www/dietcake-hello$ git status
~/www/dietcake-hello$ git commit

dietcake-helloのコードを修正してみます。

~/www/dietcake-hello$ cd app/controllers/
~/www/dietcake-hello/app/controllers$ vi hello_controller.php

なんか編集した(忘れた。orz) …とりあえず、中身はこんな感じ。

<?php
class HelloController extends AppController
{
    public function index()
    {
        // hello world
        $message = Hello::getMessage();
        $this->set(get_defined_vars());
    }
}

コミットします。

~/www/dietcake-hello/app/controllers$ git status
~/www/dietcake-hello/app/controllers$ git commit -a

githubにoriginを追加します

~/www/dietcake-hello/app/controllers$ git remote add origin [email protected]:nazerin/dietcake_test.git

originをmasterブランチに追加します

~/www/dietcake-hello/app/controllers$ git push origin master

6. ブランチ分けてみる


まずは、カレントブランチの確認。

~/www/dietcake-hello/app/controllers$ git branch

ここでは試しに"goodbye"というブランチをつくってみました。

~/www/dietcake-hello/app/controllers$ git branch goodbye

goodbyeブランチへの移動は次のようにやります。

~/www/dietcake-hello/app/controllers$ git checkout goodbye

goodbyeブランチ上で新しいファイルを作成します(コピっちゃいます。)。

~/www/dietcake-hello/app/controllers$ cp hello_controller.php goodbye_controller.php

作成したコントローラを編集します。

~/www/dietcake-hello/app/controllers$ vi goodbye_controller.php

中身はこんな感じ。

<?php
class GoodbyeController extends AppController
{
    public function index()
    {
        echo "goodbye,world";
    }
    
    public function hogehoge()
    {
        echo "hogehoge";
    }

}

gitのステータスを確認します。

~/www/dietcake-hello/app/controllers$ git status

goodbye_controller.phpが未コミットであることを確認したところで、 インデックスに追加します。

~/www/dietcake-hello/app/controllers$ git add goodbye_controller.php

そして、コミット。

~/www/dietcake-hello/app/controllers$ git commit goodbye_controller.php

一応、

~/www/dietcake-hello/app/controllers$ git status

して、未コミットのものがないか確認しました。

originにgoodbyeブランチをpushします。

~/www/dietcake-hello/app/controllers$ git push origin goodbye

masterブランチに移動します。

~/www/dietcake-hello/app/controllers$ git branch
~/www/dietcake-hello/app/controllers$ git checkout master

ここで、

~/www/dietcake-hello/app/controllers$ less ~/www/dietcake-hello/.git/config

としてみると、"origin"の情報が書き込まれているのがわかります!

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = [email protected]:nazerin/dietcake_test.git
        fetch = +refs/heads/*:refs/remotes/origin/*

リモートリポジトリ(github)のoriginをmasterブランチに反映します。

~/www/dietcake-hello/app/controllers$ git pull origin master

7. その他

pull requestって? →自分の変更差分をマージして(pull)って言うお願いのことです。

@siroken3
Copy link

すげー

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment