Apache

2013/6/4 Apache 2.2系 の説明。


  • インストール
[root@server ~]# yum -y install httpd ← httpdインストール
[root@server ~]# yum -y install php php-mbstring ← php、php-mbstringインストール

  • 設定 Apacheの設定ファイルは/etc/httpd/conf/httpd.confだ。これを編集して、CGIの許可、.htaccessの許可、ドキュメントルートの指定などを設定できる。
[root@server ~]# vi /etc/httpd/conf/httpd.conf ← httpd設定ファイル編集
ServerTokens OS 
↓
ServerTokens Prod ← エラーページ等でOS名を表示しないようにする

#ServerName www.example.com:80
↓
ServerName server.local:80 ← サーバー名を指定(/etc/hostsファイルに記述している任意の名前)

<Directory "/var/www/html">

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important.  Please see
# http://httpd.apache.org/docs-2.0/mod/core.html#options
# for more information.
#
    Options Indexes FollowSymLinks 
  ↓
    Options Includes ExecCGI FollowSymLinks ← CGI,SSIの許可

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
    AllowOverride None
  ↓
    AllowOverride All ← .htaccessの許可

# 
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined 
↓
LogFormat "%h %l %u %t \"%!414r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined ← 長すぎるURI(414エラー)はログに記録しない 

#
# For a single logfile with access, agent, and referer information
# (Combined Logfile Format), use the following directive:
#
SetEnvIf Request_URI "default\.ida" no_log ← 追加(wormからのアクセスをログに記録しない)
SetEnvIf Request_URI "cmd\.exe" no_log ← 〃
SetEnvIf Request_URI "root\.exe" no_log ← 〃
SetEnvIf Request_URI "Admin\.dll" no_log ← 〃
SetEnvIf Request_URI "NULL\.IDA" no_log ← 〃
SetEnvIf Remote_Addr 192.168.1 no_log ← 追加(内部からのアクセスをログに記録しない)
SetEnvIf Remote_Addr 127.0.0.1 no_log ← 追加(自ホストからのアクセスをログに記録しない)
CustomLog logs/access_log combined env=!no_log ← 上記以外のアクセスをログに記録する 

ServerSignature On 
↓
ServerSignature Off ← エラーページでサーバー情報を表示しないようにする

AddDefaultCharset UTF-8
↓
#AddDefaultCharset UTF-8 ← コメントアウト(文字化け対応)

#AddHandler cgi-script .cgi
↓
AddHandler cgi-script .cgi .pl ← CGIスクリプトに.plを追加

<Directory "/var/www/icons">
    Options Indexes MultiViews
    ↓
    Options MultiViews ← iconsディレクトリのファイル一覧を表示しないようにする
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

  • 起動

[root@server ~]# /etc/rc.d/init.d/httpd start ← httpd起動 httpd を起動中: [ OK ]

[root@server ~]# chkconfig httpd on ← httpd自動起動設定


  • ポート開放
開放ポート:80
インターネット側IP:グローバルIP(自動)
LAN側IP:192.168.11.10(このマシンのIP)

  • テスト
  • htmlのテスト
[root@server ~]# vi /var/www/html/index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>テスト</title>
</head>
<body> 
テスト
</body>
</html>

http://サーバー名(例:hogehoge.com)/ でアクセス

  • cgiのテスト
[root@centos ~]# vi /var/www/html/test.cgi
#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n";
print "<title>テスト</title>\n";
print "</head>\n";
print "<body>\n";
print "CGIのテスト\n";
print "</body>\n";
print "</html>\n";
[root@server ~]# chmod 755 /var/www/html/test.cgi ← パーミッション変更

http://サーバー名(例:hogehoge.com)/test.cgi でアクセス

  • phpのテスト
[root@server ~]# vi /var/www/html/test.php
<?php
  phpinfo();
?>

http://サーバー名(例:hogehoge.com)/test.php でアクセス

  • phpのshort_open_tagの有効化 phpプログラム内で、phpタグの短縮型 (<? ?>) を使用可能にするかどうかを設定する。php5.4.0より前では、これを有効にしなければ短縮形は使えない。
[root@server ~]# vi /etc/php.ini
; This directive determines whether or not PHP will recognize code between
; <? and ?> tags as PHP source which should be processed as such. It's been
; recommended for several years that you not use the short tag "short cut" and
; instead to use the full <?php and ?> tag combination. With the wide spread use
; of XML and use of these tags by other languages, the server can become easily
; confused and end up parsing the wrong code in the wrong context. But because
; this short cut has been a feature for such a long time, it's currently still
; supported for backwards compatibility, but we recommend you don't use them.
; Default Value: On
; Development Value: Off
; Production Value: Off
; http://www.php.net/manual/en/ini.core.php#ini.short-open-tag
short_open_tag = Off
↓
short_open_tag = On

http://jp2.php.net/manual/ja/ini.core.php#ini.short-open-tag

http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q12108361842


  • バーチャルホスト化

一つの IP アドレスでいくつかの名前ベースのウェブサイトを実行する。ここでは、例として以下のような構成とする。

1つ目のサイト: /var/www/html/virtual_hoge1/ サーバー名:hogehoge.com
2つ目のサイト: /var/www/html/virtual_hoge2/ サーバー名:piyopiyo.com
[root@server ~]# vi /etc/httpd/conf/httpd.conf ← Apache設定ファイル編集
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If this is not set to valid DNS name for your host, server-generated
# redirections will not work.  See also the UseCanonicalName directive.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
# You will have to access it by its address anyway, and this will make
# redirections work in a sensible way.
#
#ServerName centossrv.com:80 ← 行頭に#を追加してコメントアウト

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80 ← コメント解除
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#

# ~~~~~~~~ 中略 ~~~~~~~~~~~
# ファイル最後尾に以下を追記
<VirtualHost *:80>
   ServerName any
   <Location />
       Order deny,allow
       Deny from all
   </Location>
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /var/www/html/virtual_hoge1
  ServerName hogehoge.com
  ErrorLog logs/hoge1-error_log
  CustomLog logs/hoge1-access_log combined env=!no_log
  # Other directives here
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /var/www/html/virtual_hoge2
  ServerName piyopiyo.com
  ErrorLog logs/hoge2-error_log
  CustomLog logs/hoge2-access_log combined env=!no_log
  # Other directives here
</VirtualHost>
  • /var/www/html/内の .htaccessを削除する。
    [root@server ~]rm -f .htaccess
    
  • /var/www/html/virtual_hoge1 と /var/www/html/virtual_hoge2を作成する
    [root@server ~]mkdir /var/www/html/virtual_hoge1
    [root@server ~]mkdir /var/www/html/virtual_hoge2
    

あとはこれらのディレクトリの中にhtmlとかを入れればおk パーミッション755を忘れずに。

最終更新:2013年06月06日 01:23