Apacheのインストールと初期設定

カテゴリ:Webサーバ

CentOS/Ubuntu 両対応

Apacheのインストール

WebサーバーのプログラムであるApache(httpd)をインストールします。

CentOS 7 の場合:

# yum -y install httpd

CentOS 8 以降の場合:

# dnf -y install httpd

Ubuntu の場合:

# apt -y install apache2

PHPを使用するためのDirectoryIndexの設定

Apacheの設定ファイルをviで開きます。

CentOS の場合:

# vi /etc/httpd/conf/httpd.conf

Ubuntu の場合:

# vi /etc/apache2/mods-available/dir.conf

PHPのindexファイルを認識できるように、DirectoryIndex に index.php を追加します。

DirectoryIndex index.html index.html.var index.php

この設定により、ファイル名が指定されていないURLリクエストが来た場合にindex.phpを探すようになります。

ウェルカムページ(Test Page)の無効化

welcome.conf で以下のようにコメントアウトします。
※マルチドメイン環境でIPアドレスで接続した際に Test Page を表示しないため

CentOS の場合:

# vi /etc/httpd/conf.d/welcome.conf

変更後:

#<LocationMatch "^/+$">
#    Options -Indexes
#    ErrorDocument 403 /error/noindex.html
#</LocationMatch>

Ubuntu の場合:

/var/www/html/index.html ファイルを削除します。

# rm /var/www/html/index.html

以下のコマンドでディレクトリ一覧の表示を無効にしておきます。

# a2dismod --force autoindex

Apacheの起動

以下のコマンドでApacheを起動します。

CentOS の場合:

# systemctl start httpd

Ubuntu の場合:

# systemctl start apache2

Apacheの自動起動を有効にする

以下のコマンドを実行し、Apacheを自動起動に設定します。

# systemctl enable httpd

Ubuntu の場合:

# systemctl enable apache2

SSLの設定

ApacheへのSSL証明書の設定を参照してください。

公開日時:2014年07月19日 21:08:11
最終更新日時:2022年03月10日 20:48:51

なお、VPS選びで迷ったらこちらの記事で主要VPSのメモリ容量ごとの月額、年額料金を比較していますので、是非参考にしてみてください。

Webサーバに戻る

このページのトップに戻る