SSI(Server Side Include)
Apacheサーバー構築法 - 第10回 動的コンテンツを作る「SSI」:ITproを見ながら遊んでみる。
前処理(mod_includeモジュールの導入)
mod_includeモジュールが無効であることを確認
# /usr/sbin/apache2ctl -M Loaded Modules: core_module (static) log_config_module (static) logio_module (static) mpm_prefork_module (static) http_module (static) so_module (static) alias_module (shared) auth_basic_module (shared) auth_digest_module (shared) authn_file_module (shared) authz_default_module (shared) authz_groupfile_module (shared) authz_host_module (shared) authz_user_module (shared) autoindex_module (shared) cgi_module (shared) dir_module (shared) env_module (shared) mime_module (shared) negotiation_module (shared) php5_module (shared) setenvif_module (shared) status_module (shared) Syntax OK
mod_includeモジュールを有効化
# a2enmod Which module would you like to enable? Your choices are: actions alias asis auth_basic auth_digest authn_alias authn_anon authn_dbd authn_dbm authn_default authn_file authnz_ldap authz_dbm authz_default authz_groupfile authz_host authz_owner authz_user autoindex cache cern_meta cgi cgid charset_lite dav dav_fs dav_lock dbd deflate dir disk_cache dump_io env expires ext_filter file_cache filter headers ident imagemap include info ldap log_forensic mem_cache mime mime_magic negotiation php5 proxy proxy_ajp proxy_balancer proxy_connect proxy_ftp proxy_http rewrite setenvif speling ssl status substitute suexec unique_id userdir usertrack version vhost_alias Module name? include Module include installed; run /etc/init.d/apache2 force-reload to enable.
apacheを再起動
# /etc/init.d/apache2 restart * Restarting web server apache2 [ OK ]
mod_includeモジュールが有効であることを確認
# /usr/sbin/apache2ctl -M Loaded Modules: (snip) include_module (shared) (snip) Syntax OK
SSIを実行するディレクトリの設定
いじったら、apacheの再起動を忘れないこと
- 11行目 OptionsでSSIを許可
- 15行目 AddOutputFilterで拡張子(.shtml)とSSI処理対象を関連付け
- 16行目 AddTypeで拡張子(.shtml)とMIME(text/html)を関連付け
# vi /etc/apache2/sites-available/default (snip) 10 <Directory /var/www/> 11 Options Indexes FollowSymLinks MultiViews Includes 12 AllowOverride None 13 Order allow,deny 14 allow from all 15 AddOutputFilter INCLUDES .shtml 16 AddType text/html .shtml 17 </Directory> (snip)
SSIを利用する
とほほのSSI入門を見ながら、あれこれ試してみる。やっぱり「とほほ」は頼りになる。(適当なところもあります。)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>SSIのテスト</title> <link rel="shortcut icon" href="favicon.ico"> </head> <body> <h1>SSIのテスト</h1> <h2># ファイル読みこみ(#include)</h2> <p>index.htmlの内容<br><!--#include file="index.html" --></p> <h2># ファイルサイズ(#fsize)</h2> <p>favicon.icoのファイルサイズ=<!--#fsize file="favicon.ico" --></p> <h2># ファイルサイズ表示形式(#config sizefmt)</h2> <p>abbrev指定した場合、<!--#config sizefmt="abbrev" --><!--#fsize file="favicon.ico" --></p> <p>bytes指定した場合、<!--#config sizefmt="bytes" --><!--#fsize file="favicon.ico" --></p> <h2># 最終更新時刻(#flastmod)</h2> <p>favicon.icoの最終更新日時=<!--#flastmod file="favicon.ico" --></p> <h2># 時刻表示形式(#config timefmt)</h2> <p>favicon.icoの最終更新日=<!--#config timefmt="%x" --><!--#flastmod file="favicon.ico" --></p> <h2># 環境変数表示(#echo)</h2> <p>HTTP_HOST=<!--#echo var="HTTP_HOST" --></p> <h2># 環境変数表示(#printenv)</h2> <pre>環境変数一覧<br><!--#printenv --></pre> <h2># 変数設定(#set)</h2> <p>変数"msg"に代入した値=<!--#set var="msg" value="It works!" --><!--#echo var="msg" --></p> <h2># エラーメッセージ(#config errmsg)</h2> <p>保留</p> <h2># コマンド実行(#exec)</h2> <pre>ディレクトリの内容を一覧を表示<br><!--#exec cmd="/bin/ls -l" --></pre> <h2># 制御構文(#if, #elif, #else, #endif)</h2> <p> <!--#if expr="$HTTP_USER_AGENT = /MSIE/" -->Internet Explorer をお使いですね。 <!--#elif expr="$HTTP_USER_AGENT = /Firefox/" -->Firefox をお使いですね。 <!--#elif expr="$HTTP_USER_AGENT = /Safari/" -->Safari をお使いですね。 <!--#endif --> </p> </body> </html>