■概要

自宅サーバCenOS6 WebサーバApacheの構築手順です。

文字色の説明  ■説明文 ■コマンド(Input) ■注意事項

こちらでは、Web開発言語であるPHPの導入とCGI、SSI、.htaccessを使用できるような設定を同時に行います。

スポンサードリンク

※サーバ名は各自の環境に合わせて設定くださいね。

WebサーバApacheとPHPインストール

[root@hostname ~]# yum -y install httpd ⇐ httpdをインストール
[root@hostname ~]# yum -y install php php-mbstring ⇐ httpdをインストール

WebサーバApache設定

[root@hostname ~]# vi /etc/httpd/conf/httpd.conf ⇐ httpd設定ファイル編集
#変更が必要な場所のみ記載します。
#ServerTokens OS
ServerTokens Prod ⇐ エラーページ等でOS名を表示させない
#ServerName www.example.com:80
ServerName hostname.com:80 ⇐ サーバー名を指定
<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のすべのディレクトリに対して許可
#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>

Perコマンドへのアクセス設定

[root@hostname ~]# ln -s /usr/bin/perl /usr/local/bin/perl ⇐ /usr/bin/perlへリンク設定
[root@hostname ~]# whereis perl ⇐ リンクの設定確認perl: /usr/bin/perl /usr/local/bin/perl /usr/share/man/man1/perl.1.gz ⇐ perlのパスに/usr/bin/perlが表示されればOK

WebサーバApache起動と自動起動設定

[root@hostname ~]# /etc/rc.d/init.d/httpd start ⇐ httpdを起動
[root@hostname ~]# chkconfig httpd on ⇐ httpd自動起動設定

ルーターの80番ポートをOPEN

(1) ルーターIPアドレスを確認

WindowsPCからコマンドプロンプトを起動し、ipconfigコマンドを入力し、デフォルトゲートウェイのIPアドレスを確認します。

(2)ルーターへアクセス

chromeやIEを使ってIPアドレスを入力し、ルーターへアクセスします。

root01

※IDとパスワードはルーターの取り扱い説明書を参考ください。

(3)ポート開放設定

ルーターの型番によりますが、基本「詳細設定」⇒「ポートマッピング設定」にポート開放設定が入ってます。
※ルーターの管理画面には型番が表示されいますので、その型番で80番ポート開放設定で調べる方法もあります。

root02プレビュー

ポートマッピング設定より、LAN側ホストには 「サーバのIPアドレス」 プロトコルには 「TCP」 ポート番号には「80」~「80」を入力し、ルーターを再起動します。

Webサーバ表示確認

(1)テストページ作成

[root@hostname ~]# vi /var/www/html/test.html ⇐ テストページ作成
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>テスト</title>
</head>
<body>
テスト
</body>
</html>

(2)表示確認

chromeやIEから、サーバアドレスを入力し、表示確認を行います。
root03

不要テストファイルの削除

[root@hostname ~]# rm -f /var/www/html/test.html ⇐ テストファイルの削除
[root@hostname ~]# rm -f /etc/httpd/conf.d/welcome.conf ⇐ 削除
[root@hostname ~]# rm -f /var/www/error/noindex.html ⇐ 削除