0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ubuntu 20.04のNginxでバーチャルホスト設定

Last updated at Posted at 2020-05-14

Nginxの一台で複数のドメインのバーチャルホストを設定する方法を説明します。
今回は例として設定対象ドメインはfx.askbox.netstocks.askbox.netとします。

Ubuntu 20.04にnginxをインストール

$ sudo apt update
$ sudo apt upgrade
$ sudo apt install nginx

nginxのバージョンの確認

$ nginx -v
nginx version: nginx/1.17.10 (Ubuntu)

NginxでBasic認証

$ sudo printf "user_name:$(openssl passwd -crypt password)\n" >> /etc/nginx/.htpasswd

fx.askbox.netドメイン設定ファイルの確認

$ sudo cat /etc/nginx/sites-available/fx.askbox.net
server {
        listen 80;
        listen [::]:80;

        server_name fx.askbox.net;

        access_log  /var/log/nginx/fx.askbox.net-access.log;
        error_log  /var/log/nginx/fx.askbox.net-error.log;

        root /var/www/fx.askbox.net;
        index index.html;


        # 認証領域の説明(名前)
        auth_basic "Only Member";
        # Basic認証のユーザー・パスワードのファイル
        auth_basic_user_file /etc/nginx/.htpasswd;

        location / {
                try_files $uri $uri/ =404;
        }
}

stocks.askbox.netドメイン設定ファイルの確認

$ sudo cat /etc/nginx/sites-available/stocks.askbox.net
server {
        listen 80;
        listen [::]:80;

        server_name stocks.askbox.net;

        access_log  /var/log/nginx/stocks.askbox.net-access.log;
        error_log  /var/log/nginx/stocks.askbox.net-error.log;

        root /var/www/stocks.askbox.net;
        index index.html;

        location / {
                try_files $uri $uri/ =404;
        }
}

バーチャルホスト設定の有効化

作成した2ドメインの設定ファイルのシンボリックリンクをsites-enabledディレクトリに張ります。

$ sudo ln -s /etc/nginx/sites-available/fx.askbox.net /etc/nginx/sites-enabled/fx.askbox.net
$ sudo ln -s /etc/nginx/sites-available/stocks.askbox.net /etc/nginx/sites-enabled/stocks.askbox.net

設定ファイルのシンタックスチェック

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

サービスのリロード

$ sudo systemctl reload nginx

ログファイルの確認

$ sudo tree /var/log/nginx/
/var/log/nginx/
├── access.log
├── fx.askbox.net-access.log
├── fx.askbox.net-error.log
├── error.log
├── stocks.askbox.net-access.log
└── stocks.askbox.net-error.log

0 directories, 6 files

Ubuntuのバージョンの確認


$ uname -a
Linux ip-172-31-8-191 5.4.0-1009-aws #9-Ubuntu SMP Sun Apr 12 19:46:01 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04 LTS"

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04 LTS
Release:        20.04
Codename:       focal
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?