2012年12月1日土曜日

ローカル環境でWebの開発環境を構築したときのメモ

Webアプリケーションの開発といえば、

  • ローカルでPHP、Pythonなどのスクリプトをシコシコ書く
  • FTPや何かでモリモリファイルをアップロード
  • ブラウザで動作確認
あまり効率がよくないので「こんな環境どうだ」っていうのを考えてみた。

開発環境って

まず開発環境は、ほぼ皆さんWindowsなのでWindows主体の構成
  • Windows上でコーディング、開発
  • 動作環境はVMware Playerで対象サーバーを作成して実施
という感じで環境を整える、具体的には
  • VMware Playerで作成したLinux環境の特定のディレクトリをsambaを利用してWindowsへエクスポート
  • Windowsで仮想環境のLinuxからエクスポートされたディレクトリをマウント
  • Windowsへマウントしたフォルダへスクリプト展開
  • Eclipseなどのエディタでコーディング、デバック
  • Windows内のブラウザで仮想環境のWebサーバーへアクセスして動作検証

という感じ、で環境を構築してみる


環境構築

こんな感じの構成で作成



VMWare Player上のLinuxにWebサーバーを作成

仮想環境のOSはGentoo Linuxを利用しました、その他のOSの場合はそれなりに設定すればいくと思います
VMware Playerのインストールと設定は、はしょります。

Webサーバーインストール

supernova ~ # emerge www-servers/apache

PHPのインストール

supernova ~ # emerge dev-lang/php

ZendFrameworkのインストール

supernova ~ # emerge dev-php/ZendFramework

xdebugのインストール

supernova ~ # emerge dev-php/xdebug-client
supernova ~ # emerge dev-php/xdebug

ネットワークの設定

vhostでサーバーを設定するためIPアドレスをエイリアスで追加しておく
supernova ~ # cat /etc/conf.d/net
config_eth0=(
    "192.168.89.10/24"
    "192.168.89.11/24"
)
routes_eth0=("default via 192.168.89.1")
supernova ~ # /etc/init.d/net.eth0 restart
supernova ~ # ifconfig -a
eth0      Link encap:Ethernet  HWaddr xx:xx:xx:xx:xx:xx  
          inet addr:192.168.89.10  Bcast:192.168.89.255  Mask:255.255.255.0
...
eth0:1    Link encap:Ethernet  HWaddr xx:xx:xx:xx:xx:xx  
          inet addr:192.168.89.11  Bcast:192.168.89.255

apacheの設定

vhostを設定する、00_default_vhost.confは修正、website_vhost.confは新規に追加しサーバーを起動する。
サーバーを起動したら、Windows上のブラウザからhttp://192.168.89.10/へアクセスしサーバーが動いていることを確認する。
さらに、website_vhost.confのディレクトリを作成しておく。
supernova vhosts.d # cat /etc/apache2/vhosts.d/00_default_vhost.conf
<IfDefine DEFAULT_VHOST>
    <VirtualHost 192.168.89.10:80>
    ServerName localhost
    Include /etc/apache2/vhosts.d/default_vhost.include

    <IfModule mpm_peruser_module>
        ServerEnvironment apache apache
    </IfModule>
    </VirtualHost>

</IfDefine>

新規に追加

supernova vhosts.d # cat /etc/apache2/vhosts.d/website_vhost.conf   
<IfDefine DEFAULT_VHOST>
<VirtualHost 192.168.89.11:80>
    ServerName website
    ServerAdmin webmaster@gmail.com
    
    ErrorLog /var/log/apache2/website_error_log
    CustomLog /var/log/apache2/website_access_log common

    DocumentRoot "/home/apache/website"
    <Directory "/home/apache/website">
           Options All
           AllowOverride All
           Order allow,deny
           Allow from all
    </Directory>

    <IfModule mpm_peruser_module>
        ServerEnvironment apache apache
    </IfModule>
</VirtualHost>
</IfDefine>

supernova home # mkdir -p /home/apache/website
supernova home # chown -R apache:apache /home/apache/website
supernova home # ls -l
drwxr-xr-x  3 apache apache 4096 May  2 04:43 apache
supernova vhosts.d # /etc/init.d/apache2 start

sambaのインストール、設定

Windowsとディレクトリを共有するためにsambaをインストールし設定する。
とりあえずWindowsと共有できればいいので、セキュリティーとか無視(ローカル環境だし...)
supernova home # emerge net-fs/samba

sambaサーバーの設定

/etc/samba.smb.conを修正
 workgroup = [Windowsへ設定したワークグループ]

コメントをはずす
 passdb backend = tdbsam

エクスポートするディレクトリを追加する
  [apache]
     comment = Web Directories
     browseable = yes
     writable = yes
     path = /home/apache

<span class="deco" style="font-weight:bold;">サーバーの起動</span>
supernova conf.d # /etc/init.d/samba start

samba用ユーザーとパスワードの設定

ユーザーの設定、共有フォルダにアクセスするときに必要なパスワードを設定しておく。
パスワードなしでアクセスできるような設定がわからないので....
supernova conf.d # smbpasswd -U apache
New SMB password:
Retype new SMB password:

Windows上へネットワークドライブを作成

sambaでエクスポートしたディレクトリにアクセスできるようにする、ネットワークからフォルダにアクセスするとユーザーとパスワードを入力しろといわれるので、とりあえずしておく。

sambaのユーザー設定で作成したユーザーとパスワードで入力


アクセスできたらネットワークドライブを作成する


「コンピュータ」-> 「ネットワークドライブの割り当て」で新規にドライブを作成


Z:ドライブとしてアクセスできるようになりましたのでここへWebスクリプトを展開しますよぉ。

スクリプトの展開

適当にZendFrameworkをセットアップしWebからアクセスできるか確認します。
作成したZ:ドライブにかんな感じでファイルをマップします
-- Z:\website
    |-- application
    |   |-- controllers
    |   |   |-- ErrorController.php
    |   |   |-- IndexController.php
    |   |   `-- SampleController.php
    |   `-- views
    |       |-- sample
    |       |   `-- sample.phtml
    |       `-- index
    |           `-- index.phtml
    `-- www
        |-- .htaccess
        `-- index.php

.htaccess

RewriteEngine on
RewriteBase /
RewriteRule !\.(js|ico|gif|jpg|png|css|html|xml)$ index.php

index.php

<?php
require_once('Zend/Controller/Front.php');

$front = $front = Zend_Controller_Front::getInstance();
$front->setControllerDirectory('../application/controllers');
$front->setParam('noViewRenderer', true);
$front->dispatch();

IndexController.php

<?php
require_once('Zend/Controller/Action.php');
require_once('Zend/View.php');

class IndexController extends Zend_Controller_Action {
    public $view;
    public $res;

    public function init(){
        $this->view = new Zend_View();
        $this->view->setScriptPath('../application/views');
        
        $this->res = $this->getResponse();
    }

    public function indexAction() {
        $this->res->insert('default', $this->view->render('index/index.phtml'));
    }
}

SampleController.php

<?php
require_once('Zend/Controller/Action.php');
require_once('Zend/View.php');

class SampleController extends Zend_Controller_Action {
    public $view;
    public $res;

    public function init(){
        $this->view = new Zend_View();
        $this->view->setScriptPath('../application/views');
        
        $this->res = $this->getResponse();
    }
    
    public function sampleAction() {
        $this->view->boo = 'よくわからない';
        $this->res->insert('default', $this->view->render('sample/sample.phtml'));
    }
    
    public function infoAction() {
        echo phpinfo();
    }
    
    public function indexAction() {
        echo "Sample index!!!";
    }
}

ErrorController.php

<?php
require_once('Zend/Controller/Action.php');
require_once('Zend/View.php');

class ErrorController extends Zend_Controller_Action {
    public function errorAction() {
        $errors = $this->_getParam('error_handler');
        echo "Error!!";
    }
}

index.phtml

<html>
<head>
<title>サンプルページ</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>

<body bgcolor="#FFFFFF">
<p>OK index page!</p>
</body>
</html>

sample.phtml

<html>
<head>
<title>Hogeページ</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>

<body bgcolor="#FFFFFF">
<p><?php print($this->boo) ?></p>
</body>
</html>
で、http://192.168.89.11/sample/info/ にアクセスするとphpinfoが見れるはずです。

コーディングは、Windowsから好みのエディタで行えますし、変更したファイルをFTPしなくてもそのままローカルで実行することが可能です。

仮想環境のVMを持ち歩けるってとこが良さげなのですが、デメリットとといえば、VMware Playerで仮想環境を動かせるぐらいのスペックがPCに必要ぐらいですか?

VMware Playerの共有フォルダの機能を利用しても同様のことができるのですが、今回はsambaを利用してみました。
Web開発の先生方から、「こんな環境があるぜっ!」っていうのがあれば教えてください。

「何のためにxdebug入れたんだ」って思ってる人がいると思いますが、ちょっとまってて...つかれた

0 件のコメント:

コメントを投稿