Linuxで仮想ホストを設定する方法

Linux でバーチャルホストを設定するには、以下の手順に従います。

  1. 適切なWebサーバソフトウェア(例:ApacheやNginx)をインストール、設定してください。
  2. 仮想ホスト Web サイトのファイルを格納するための新しい Web サイトディレクトリをサーバー上に作成します。
  3. Webサーバのコンフィグレーションファイルを開き、仮想ホストの設定を見つけて編集します。

Apacheサーバの場合、/etc/httpd/conf/httpd.conf か /etc/httpd/conf.d/vhosts.conf を編集し、ファイルの末尾に以下を追加する。

<VirtualHost *:80>
ServerName yourdomain.com
DocumentRoot /path/to/your/website/directory
<Directory /path/to/your/website/directory>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

yourdomain.com を仮想ホストのドメイン名に、/path/to/your/website/directory を仮想ホストウェブサイトディレクトリのパスに置き換えてください。

Nginx サーバでは、/etc/nginx/nginx.conf ファイルまた /etc/nginx/conf.d/default.conf ファイルを編集「http」ブロック内に以下を追加します。

server {
listen       80;
server_name  yourdomain.com;
root   /path/to/your/website/directory;
location / {
index  index.html index.htm;
}
}

yourdomain.comを、レンタルサーバーのドメイン名に、/path/to/your/website/directory を、レンタルサーバーのサイトディレクトリのパスに書き換えてください。

  1. 構成ファイルを保存して閉じて、ウェブサーバを再起動します。

Apacheサーバーを再起動するには、次のコマンドを使用します。

sudo systemctl restart httpd

Nginx サーバーの場合、次のコマンドで再起動できます

sudo systemctl restart nginx
  1. 仮想ホストのドメインをDNSサーバー、またはローカルのhostsファイルに解析を追加し、サーバーのIPアドレスを指すようにします。

これでバーチャルホストが正常に設定されました。上記の手順を繰り返すことで、さらにバーチャルホストを追加することができます。

コメントを残す 0

Your email address will not be published. Required fields are marked *