参考 「ネコでもわかる!さくらのVPS講座~第三回」

1.HTTPサーバーApacheのインストール

# yum install httpd

httpdサービスの自動起動登録

# systemctl enable httpd

httpdサービスの起動

# systemctl start httpd

2.ファイアウォールの設定

これは既に行っているので、今回は不要だが、「ファイヤーウォールの設定」を参照のこと、念のため記載すると。

# firewall-cmd --add-service=http --zone=public --permanent
# firewall-cmd --add-service=https --zone=public --permanent
# # firewall-cmd --reload

ここまでできたら、試しにブラウザで
http://xxx.xxx.xxx.xxx (サーバのドメインアドレスか又はIPアドレス)を見ると
次の画面がでるはず。

3.ドキュメントルートの書き込み権限

httpdサービスの設定ファイルは、/etc/httpd/config/httpd.configファイル、その115行あたりに

# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

つまりこのフォルダにindex.htmlファイル等ををおく必要があるのだが、httpdをインストールした段階では、このフォルダはroot:rootの所有になっており、root以外は書き込みできない。
従って、このフォルダを、httpdインストール時に自動的に作成されるユーザーapache:apacheや、適当に自分で作成したユーザー:グループの所有にしたり、このフォルダへの書き込み権限を変更する必要がある。

# cd /var/www
# chown apache:apache html
# chmod 775 html

4.バーチャルホスト(Virtual Host)の設定

設定ファイル、/etc/httpd/configの最後行には次の記載があり、/etc/httpd/conf/conf.dフォルダにある「*.conf」ファイルをIncludeしてくれるので、個別のモジュールなどの設定はこのファイルに書けばよいようだ。

IncludeOptional conf.d/*.conf

今回、1台のサーバーで複数のサイトを運用するので、この仮想ホスト機能を利用することに。
設定は簡単で/etc/httpd/conf/conf.dにvhost.confのような拡張子が.confのファイルを作成し、次の記載をすれば良いだけ。

<VirtualHost *:80>
ServerName www.tako.example.jp
DocumentRoot /var/www/html/tako
CustomLog /var/log/httpd/tako.example.jp-access.log common
ErrorLog  /var/log/httpd/ tako.example.jp-error.log
<Directory "/var/www/html/tako">
    AllowOverride all
</Directory>
</VirtualHost>

<VirtualHost *:80>
ServerName www.ika.example.jp
DocumentRoot /var/www/html/ika
CustomLog /var/log/httpd/ika.example.jp-access.log common
ErrorLog  /var/log/httpd/ika.example.jp-error.log
<Directory "/var/www/html/ika">
    AllowOverride all
</Directory>
</VirtualHost>

これで、/var/www/html/tako、/var/www/html/ikaのフォルダを作成し、そこにそれぞれ適当なindex.htmlを置けば

http://www.tako.example.jp
http://www.ika.example.jp

の各サイトを開くことができる。
忘れていけないのは、再起動

# systemctl restart httpd

もっと大事なことを忘れていた。 例としてexample.jpのドメインを使用しているが、ドメインの登録が必要だった。

「次はドメイン名の登録」