提供:すだちWiki
移動先: 案内検索

ヘルプ:Webサーバー構築(Apache)(CentOS7)

メインページ > ヘルプ:目次 > ヘルプ:自宅サーバー構築(CentOS7)  > Webサーバー構築(Apache)(CentOS7)

概要

httpd をインストールしてWebサーバーを構築します。Webサーバー(Apache)はInternet Explorer等のブラウザからWebページをみれるようにするためのサーバーです。なお、HTTP は 80/TCP を使用します。

ここでは、ホームページスペース提供サービスを行っている一般的なWebサーバーと同様に以下のことができるようにする。

  • CGIは任意のディレクトリで実行できるようにする。
  • SSIは拡張子がshtmlのもののみ実行できるようにする。
  • .htaccessを使用できるようにする。
  • PHPを使用できるようにする。
  • Rubyを使用できるようにする。

Webサーバーインストール

[root@host3 ~]# yum -y install httpd ← httpdインストール

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

[root@host3 ~]# yum -y install ruby ← Rubyインストール

[root@host3 ~]# yum -y install php-xml ← Fatal error: Class ‘DOMDocument’ not found 抑止のため。
php-xmlにphp-domも組み込まれています。

[root@host3 ~]# yum -y install php-soap ← SOAPモジュールのインストール

Webサーバー設定

Webサーバー設定

[root@host3 ~]# vi /etc/httpd/conf/httpd.conf ← httpd設定ファイル編集
#ServerName www.example.com:80
↓
ServerName sudachi.jp: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の許可

#
# 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エラー)はログに記録しない

#
# If you prefer a logfile with access, agent, and referer information
# (Combined Logfile Format) you can 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 ← 上記以外のアクセスをログに記録する

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

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


[root@centos ~]# vi /etc/httpd/conf.d/autoindex.conf ← autoindex設定ファイル編集
<Directory "/usr/share/httpd/icons">
    Options MultiViews ← iconsディレクトリのファイル一覧を表示しないようにする
    AllowOverride None
    Require all granted
</Directory>

[root@host3 ~]# rm -f /etc/httpd/conf.d/welcome.conf ← テストページ削除

[root@host3 ~]# rm -f /var/www/error/noindex.html ← テストページ削除

Perlコマンドへ/usr/local/bin/perlでもアクセスできるようにする。

[root@host3 ~]# ln -s /usr/bin/perl /usr/local/bin/perl ← /usr/local/bin/perlから/usr/bin/perlへリンクをはる

[root@host3 ~]# whereis perl ← Perlのパスを確認
perl: /usr/bin/perl /usr/local/bin/perl /usr/share/man/man1/perl.1.gz ← Perlのパスに/usr/local/bin/perlが表示されることを確認

ドキュメントルート所有者変更

[root@host3 ~]# chown sysop. /var/www/html/ ← ドキュメントルート所有者を変更

[root@host3 ~]# ll /var/www/ ← ドキュメントルート所有者変更確認
合計 0
drwxr-xr-x 2 root  root  6 11月 20 06:43 cgi-bin
drwxr-xr-x 2 sysop sysop 6  6月 10  2014 html

「httpd.conf」の文法チェック

[root@host3 ~]# apachectl configtest
Syntax OK

Webサーバー起動

[root@host3 ~]# systemctl start httpd ← httpd起動

[root@host3 ~]# systemctl enable httpd ← httpd自動起動設定

操作確認

[root@host3 ~]# systemctl status httpd
 httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2016-01-10 09:40:27 JST; 1min 5s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 12038 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─12038 /usr/sbin/httpd -DFOREGROUND
           ├─12039 /usr/sbin/httpd -DFOREGROUND
           ├─12040 /usr/sbin/httpd -DFOREGROUND
           ├─12041 /usr/sbin/httpd -DFOREGROUND
           ├─12042 /usr/sbin/httpd -DFOREGROUND
           └─12043 /usr/sbin/httpd -DFOREGROUND

 1月 10 09:40:26 host4.sudachi.jp systemd[1]: Starting The Apache HTTP Server...
 1月 10 09:40:27 host4.sudachi.jp systemd[1]: Started The Apache HTTP Server.