Webサーバーの設定

はじめに

独自ドメインの購入やネームサーバーの設定を事前に行っていることが前提。
購入の方法ネームサーバーの設定は借りているサーバーのマニュアルを参考にする。

 

ディレクトリとファイルの作成

ディレクトリを作成する

$ mkdir -p /var/www/html/domain-name.com/public_html
public_html フォルダとは何ですか?

 

index.htmlファイルを作成する

public_htmlディレクトリにindex.htmlファイルを作成する。
domain-name.comにアクセスしたときに「ドメインの設定が完了!」と表示させる。

$ echo ‘ドメインの設定が完了!’ > /var/www/html/domain-name.com/public_html/index.html

 

名前ベースのバーチャルホストの設定

名前ベースのバーチャルホストでは1つのIPアドレスに複数のFQDN(完全装飾ドメイン名)、要は複数の独自ドメインを設定できる。

httpd.confに追加する

$ sudo vim /etc/httpd/conf/httpd.conf

 

「i」キー で-INSERT-モードになり編集する。
ファイルの最終行に追加する。

# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See for detailed information.
# In particular, see
#
# for a discussion of each configuration directive.
#
.
.
.
etc…
‐‐‐‐‐↓追加部分↓‐‐‐‐‐
NameVirtualHost xxx.xxx.xxx.xxx
<VirtualHost xxx.xxx.xxx.xxx:80>
ServerName domain-name.com
DocumentRoot “/var/www/domain-name.com/public_html”
DirectoryIndex index.html index.php
ErrorLog /var/log/httpd/domain-name.com_error_log
CustomLog /var/log/httpd/ddomain-name.com_access_log combined
AddDefaultCharset UTF-8
</VirtualHost>
‐‐‐‐‐↑追加部分↑‐‐‐‐‐

xxx.xxx.xxx.xxxはIPアドレス。
「Esc」キーで-INSERT-モードを終了し「:wq」で保存し終了する。

 

NameVirtualHostは必要ない?

ファイルを変更したのでファイルのテストをすると、エラーが返ってくる。

$ sudo service httpd configtest
AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/httpd/conf/httpd.conf:369
NameVirtualHost xxx.xxx.xxx.xxxは必要なくなった。

 

ファイルのテストと再起動

httpdファイルをテストする

$ sudo service httpd configtest
Syntax OK
もし、エラーになった場合、指示に従って直す。

 

httpdを再起動して設定を反映させる

$ sudo systemctl restart httpd.service

 

hostsの設定

SentOS7がバーチャルホストへアクセスをするときの、IPアドレスに対応する名前解決をする。

$ sudo vim /etc/hosts

 

「i」キー で-INSERT-モードになり編集する。
ファイルの最終行に追加する。

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
#::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

‐‐‐‐‐↓追加部分↓‐‐‐‐‐
xxx.xxx.xxx.xxx domainname.com domainname.biz domeinname.info
‐‐‐‐‐↑追加部分↑‐‐‐‐‐

xxx.xxx.xxx.xxxはIPアドレス。
「Esc」キーで-INSERT-モードを終了し「:wq」で保存し終了する。

 

ドメインにアクセスする

設定したドメインにアクセスして「index.htmlファイルを作成する」で作成した「ドメインの設定が完了!」が表示されたら独自ドメインの設定は完了。

 

参照元情報

CentOS 7 構築・運用・管理パーフェクトガイド
CentOS 7 構築・運用・管理
パーフェクトガイド