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

2015年3月23日(月)
大月 宇美(おおつき たかみ)できるシリーズ編集部

Apacheの設定と起動

ここでは、Webサーバーとして最もインストール数が多いといわれている、Apache HTTP Server(以下、Apache)の設定を行います。Apacheの役割は、Linux上の、指定したディレクトリをクライアントに公開し、ディレクトリに置かれた情報を、クライアントから参照できるようにすることです。Apacheで公開されるディレクトリは、インターネットからのアクセスにも対応するため、セキュリティ対応も同時に行う必要があります。セキュリティ対策はパスワード認証やIPアドレスによるアクセス制限などがありますが、ここでは、iptableによるファイアウォールの設定を行います。

Apacheのインストールを確認する

yumコマンドでパッケージのインストール状況を確認する

1[root@www ~]# yum list installed | grep httpd⏎
2httpd.x86_64           2.2.15-39.el6.centos
3httpd-manual.noarch    2.2.15-39.el6.centos
4httpd-tools.x86_64     2.2.15-39.el6.centos
5 
6[root@www ~]#
  1. 「yum」コマンドを入力
  2. 「list installed」でインストールしたパッケージを確認する
  3. インストールしたパッケージの中から、「httpd」という文字をgrepコマンドで検索する

Apache HTTP Server

WebブラウザでURLを指定すると、そのURLに対応したWebサーバーにアクセスし、WebブラウザにWebページを転送します。このWebサーバーとして、世界で最も使われているのが、Apache HTTP Server、通称Apacheです。Apacheは、Linuxだけでなく、MacOS、Windowsにも対応しています。

Apacheのバージョンを確認するには

コマンドラインで、「httpd -v」と入力します。バージョンに限らず、Apacheのコマンドラインオプションを確認するには、「httpd -h」と入力します。manコマンドを利用すると、より詳細な説明が表示されます。

httpdの設定を書き換える

Apacheの設定ファイルを開く

httpd.confは大きな設定ファイルなので、vi起動後に、「/」で修正個所を探します。続けて同じ文字を検索する場合は、「n」を入力します。デフォルトの設定は、行頭に#を付けて無効にします。

viでhttpd.confを開く

1# vi /etc/httpd/conf/httpd.conf⏎

viが起動しました。

1# ServerAdmin: Your address, where problems with the server should be
2# e-mailed.  This address appears on some server-generated pages, such
3# as error documents.  e.g. admin@your-domain.com
4#
5#ServerAdmin root@localhost
6ServerAdmin dpro@dekiru.gr.jp (※)
7#

※ServerAdminを書き換える。

1# If your host doesn't have a registered DNS name, enter its IP address here.
2# You will have to access it by its address anyway, and this will make
3# redirections work in a sensible way.
4#
5#ServerName www.example.com:80
6ServerName www.dekiru.gr.jp:80 (※)

※ServerNameを書き換える。

1#UseCanonicalName Off
2UseCanonicalName On (※)

※UseCanonicalNameの設定をOnにする。

01# The index.html.var file (a type-map) is used to deliver content-
02# negotiated documents.  The MultiViews Option can be used for the
03# same purpose, but it is much slower.
04#
05DirectoryIndex index.html index.html.var index.htm index.php (1)
06 
07#
08# Specify a default charset for all content served; this enables
09# interpretation of all content as UTF-8 by default.  To use the
10# default browser choice (ISO-8859-1), or to allow the META tags
11# in HTML content to override this choice, comment out this
12# directive:
13#
14AddDefaultCharset UTF-8 (2)
  1. index.htm index.phpを追加
  2. AddDefaultCharsetがUTF-8になっていることを確認する

保存して終了します。ServerNameには、第8回目〜10回目で設定したホスト名を指定してください。

Apacheの状態を確認する

Apacheの自動起動の設定を確認する

1[root@www ~]# chkconfig --list httpd⏎
2httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off
3[root@www ~]#

1.「chkconfig」コマンドを入力2.「list」状態を表示3.「httpdz」Apacheのサービス名

Apacheがまだ起動されていないことを確認しました。

Apacheの起動と自動起動を設定する

Apacheを起動する

「service」コマンドを入力

1[root@www ~]# service httpd start⏎
2httpd を起動中:                                            [  OK  ]

Apacheを自動起動に設定する

「chkconfig」コマンドを入力

1[root@www ~]# chkconfig httpd on⏎

Apacheの自動起動の設定変更を確認する

「chkconfig」コマンドを入力

1[root@www ~]# chkconfig --list httpd⏎
2httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
3[root@www ~]#

GUIでのサービスの確認と設定

[システム]メニュー[管理]−[サービス]から、GUIのサービス管理ツールが起動できます。サービスの開始・停止・カスタマイズ(ランレベルの有効・無効)が行えます。

著者
大月 宇美(おおつき たかみ)

1964 年、茨城県生まれ。コンピュータ商社、出版社勤務を経て1999 年に独立。コンピュータ書籍を中心に、執筆・編集・校正などを幅広く手がける。著書に『ひと目でわかるOutlook2013』(日経BP 社)、『新標準HTML & CSS3 辞典』(インプレスジャパン)ほか。

著者
できるシリーズ編集部

連載バックナンバー

Think ITメルマガ会員登録受付中

Think ITでは、技術情報が詰まったメールマガジン「Think IT Weekly」の配信サービスを提供しています。メルマガ会員登録を済ませれば、メルマガだけでなく、さまざまな限定特典を入手できるようになります。

Think ITメルマガ会員のサービス内容を見る

他にもこの記事が読まれています