The symfony Cookbook

ウェブサーバーの設定をする方法

Be trained by symfony experts
Dec 10: Paris (1.1 - Francais)
Dec 10: Atlanta (1.1 - English)
Dec 17: Montreal (1.1 - Francais)
Jan 21: Paris (1.1 - Francais)
Feb 18: Paris (1.1 - Francais)
and more...

About

You are currently reading "The symfony Cookbook" which is licensed under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License license.

Search


powered by google

Chapter Content

概要

はじめに

バーチャルホスト

URLの書き換え

エイリアス

1つのプロジェクト内で複数のアプリケーション

バーチャルホスト

エイリアス

IIS

You are currently browsing "The symfony Cookbook" in Japanese for the 1.2 version. Switch to another version: Switch to another language:
Creative Commons License This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License.
Translation of this work into another language is explicitly allowed.

概要

symfonyアプリケーションにアクセスできるようにするためにウェブサーバーを設定する方法は複数あります。この章では異なる設定方法とアクセスを最適化するためのこつを説明します。

はじめに

上で説明した例において、myprojectプロジェクトはmyappアプリケーションを含みます。このアプリケーションのフロントコントローラはindex.phpと呼ばれ/home/steve/myproject/web/ディレクトリ内に存在します。symfonyのデータディレクトリは$data_dirです。

バーチャルホスト

バーチャルホスティングのおかげでsymfonyアプリケーションがドメイン(もしくはサブドメイン)のrootに表示されるようにウェブサーバーをセットアップできます:

http://myapp.example.com/

Apacheサーバーを稼働させることを前提とします。myappアプリケーションのためにバーチャルホストをセットアップするために、次の行をhttpd.confファイルに追加します:

<Directory "/$data_dir/symfony/web/sf">
 AllowOverride All
 Allow from All
</Directory>
<VirtualHost *:80>
  ServerName myapp.example.com
  DocumentRoot "/home/steve/myproject/web"
  DirectoryIndex index.php
  Alias /sf /$data_dir/symfony/web/sf

  <Directory "/home/steve/myproject/web">
   AllowOverride All
   Allow from All
  </Directory>
</VirtualHost>

真ん中のAliasディレクティブはデバッグ用のサイドバーの画像を表示するために必要です。$data_dirプレースホルダはPEARのdataディレクトリに置き換えなければなりません。例えば、*nixに対しては、次の行を入力します:

Alias /sf /usr/local/lib/php/data/symfony/web/sf

PEARディレクトリについてもっと詳しい情報はインストールの章で見つかります

Apacheを再起動すれば作業は終了です: ウェブアプリケーションは呼び出し可能になり標準的なウェブブラウザを通して次のURLで閲覧できます:

http://myapp.example.com/

デバッグモードの場合次のURLです:

http://myapp.example.com/myapp_dev.php/

URLの書き換え

デフォルトでは、ウェブサーバーはURL内で本番環境のフロントコントローラ(index.php)を参照することを回避するように設定されます。このことは次のURLを表示する代わりに:

http://myapp.example.com/index.php/

サーバーは下記のURLを表示し認知することを意味します:

http://myapp.example.com/

これを実現するためにはApacheのmod_rewriteモジュールを利用して、myproject/web/.htaccess内に存在する次の行を必要とします(下記はデフォルトの場合):

# .somethingを持つすべてのファイルをスキップする
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]
# その他はフロントウェブコントローラにリダイレクトされる
RewriteRule ^(.*)$ index.php [QSA,L]

URLの最後に.htmlを追加したい場合、見た目の修正方法は複数存在します。通常、mainモジュールのindexアクションを呼び出すとき、デフォルトのルーティング設定によって、表示されるURLは次のようになります:

http://myapp.example.com/main/index

アプリケーションのデフォルトのsettings.ymlファイルはコメントになっているsuffixパラメータを含みます:

all:
#  .settings:
#    suffix:        .

mainモジュールのindexアクションを次のように表示するためには:

http://myapp.example.com/main/index.html

コメントを解除してsuffixパラメータの値を.htmlに設定できます:

all:
  .settings:
    suffix:        .html

エイリアス

既存のドメイン名の上にウェブサイトを持っており、symfonyのアプリケーションをそのドメイン内でアクセスできるようにしたい場合、バーチャルホストの解決方法ではうまくゆきません。例えば、symfonyのアプリケーションにアクセスしたい場合を考えましょう:

http://www.example.com/myapp/

これを実現するためには、httpd.confファイルを開き、次の行を追加します:

Alias /myapp/ /home/steve/myproject/web/
<Directory "/home/steve/myproject/web">
   AllowOverride All
   Allow from All
</Directory>

myproject/web/ディレクトリに設置されている.htaccessファイルも編集し、最後の書き換えルール

RewriteRule ^(.*)$ index.php [QSA,L]

を次のように変更します

RewriteRule ^(.*)$ /myapp/index.php [QSA,L]

Apacheを再起動し、ウェブアプリケーションにアクセスしてみます:

http://www.example.com/myapp/

1つのプロジェクト内で複数のアプリケーション

プロジェクトの設計過程の期間において、1つもしくは複数のアプリケーションの問題に遭遇します。例えば、myprojectプロジェクト内で、myappアプリケーションを公開してadminアプリケーションが内容を管理するとします(通常はバックオフィスと呼ばれます)。1つのプロジェクトの範囲内で複数のアプリケーションにアクセスすることを許可するためにはどうしたらよいでしょうか?

バーチャルホスト

新しいバーチャルホストをApacheのhttpd.confファイルに追加します。これはかなり簡単に理解できます:

<Directory "/$data_dir/symfony/web/sf">
 AllowOverride All
 Allow from All
</Directory>

<VirtualHost *:80>
  ServerName myapp.example.com
  DocumentRoot "/home/steve/myproject/web"
  DirectoryIndex index.php
  Alias /sf /$data_dir/symfony/web/sf

  <Directory "/home/steve/myproject/web">
   AllowOverride All
   Allow from All
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  ServerName admin.example.com
  DocumentRoot "/home/steve/myproject/web"
  DirectoryIndex admin.php
  Alias /sf /$data_dir/symfony/web/sf

  <Directory "/home/steve/myproject/web">
    AllowOverride All
    Allow from All
  </Directory>
</VirtualHost>

エイリアス

別の方法としては、新しいエイリアスを追加できます。この設定によってそれぞれのアプリケーションごとに切り離されたウェブの内容(.css、.js、images、など)を持つことができるようになります。これによってhttod.confファイルを直接編集しなくて済みます。

最初に、webディレクトリ内に新しいディレクトリを作ります:

$ mkdir /home/steve/myproject/web/admin

それから、adminアプリケーションのフロントコントローラをこの新しいディレクトリに移動させて、この新しいアプリーションのために.htaccessファイルをコピーします:

$ cd /home/steve/myproject/web
$ mv admin.php admin/index.php
$ mv admin_dev.php admin/
$ cp .htaccess admin/

それから、エイリアスをセットアップするために上で説明した2つのステップを実行します。myproject/web/admin/ディレクトリに設置された.htaccessファイルのコード:

RewriteRule ^(.*)$ index.php [QSA,L]

を下記のように変更します

RewriteRule ^(.*)$ /admin/index.php [QSA,L]

最終的に、両方のフロントコントローラ(myproject/web/admin/index.phpmyproject/web/admin/admin_dev.php)を編集し変更します:

define('SF_ROOT_DIR', realpath(dirname(__FILE__).'/..'));
 

変更後は次の通りです:

define('SF_ROOT_DIR', realpath(dirname(__FILE__).'/../..'));
 

adminアプリケーションにアクセスできるようにするためにはこれで十分です

http://whateveryourmainurlis/admin/

(cssjsimagesuploadsディレクトリを持つ)古典的なwebディレクトリのようにweb/adminディレクトリ内で同じファイル構造を再現する必要があります。現時点でのrootへのすべてのパスはこのadminディレクトリを指し示すからです。

IIS

symfonyはIISと互換性があります。IIS環境でsymfonyプロジェクトのインストールと設定方法についてすべてを学ぶためには、関連チュートリアルをご覧下さい。

Questions & Feedback

If you find a typo or an error, please register and open a ticket.

If you need support or have a technical question, please post to the user mailing-list or to the forum.