経理からエンジニア転向した人のメモ

元経理マンがエンジニアに転向して現在

vagrant上のDebianでVirtualHostの構築をしてみる。

環境

Vagrant 2.1.2

VirtualBox 5.2.16

MacOS High Sierra 10.13.6

Vagrant Box : Debian/jessie64

Debian version # cat /etc/debian_version 8.11

nginx install

# vi /etc/apt/sources.list

// 末尾に追加する。
deb http://nginx.org/packages/debian/ jessie nginx
deb-src http://nginx.org/packages/debian/ jessie nginx

アップデートしてからインストールする。

# apt update
# apt install nginx

installできた。

nginx -v
nginx version: nginx/1.14.0

ブラウザに「192.168.33.12(vagrantfileで設定)」を打ち込むとWelcomeが出てくればOK!

nginx の設定

これを参照にやってみる。 nginxでvirtualhostを設定する

設定ファイルにVirtualHostの設定

# vi /etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  hoge1.com;

    access_log  /var/log/nginx/log/hoge1.access.log  main;

    location / {
        root   /usr/share/nginx/html/hoge1;
        index  index.html index.htm;
    }
}

server {
    listen       80;
    server_name  hoge2.com;

    access_log  /var/log/nginx/log/hoge2.access.log  main;

    location / {
        root   /usr/share/nginx/html/hoge2;
        index  index.html index.htm;
    } 
}

server {
    listen       80;
    server_name  hoge3.com;

    access_log  /var/log/nginx/log/hoge3.access.log  main;

    location / {
        root   /usr/share/nginx/html/hoge3;
        index  index.html index.htm;
    }
}

ファイルの設置

/usr/share/nginx/html/直下にhoge1,hoge2,hoge3とフォルダを作成してそれぞれindex.htmlを配置。

# vi /usr/share/nginx/html/hoge1/index.html

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <title>hoge</title>
</head>
<body>
  <h1>hoge1</h1>
</body>
<html>

これをhoge2、hoge3に配置。

hostsファイルの書き換え(Mac側)

# vi /private/etc/hosts

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
127.0.0.1       hoge1.com   // 追加
127.0.0.1       hoge2.com   // 追加
127.0.0.1       hoge3.com   // 追加
255.255.255.255 broadcasthost
::1             localhost

nginxの再起動

# nginx -s reload

ブラウザにアクセス

ブラウザに「hoge1.com:8080」と入力するとh1タグでhoge1と出力。 f:id:ryomoyr:20180823183003p:plain

もちろんhoge2,3も f:id:ryomoyr:20180823183034p:plain

f:id:ryomoyr:20180823182934p:plain

access_logの確認

# mkdir /var/log/nginx/log

作成する。

# touch  /var/log/nginx/log/hoge1.access.log
# touch  /var/log/nginx/log/hoge2.access.log
# touch  /var/log/nginx/log/hoge3.access.log

監視する 1行目 : リロード 2/3行目 : スーパーリロード

# tail -f hoge1.access.log 

10.0.2.2 - - [23/Aug/2018:09:58:56 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"

10.0.2.2 - - [23/Aug/2018:09:59:03 +0000] "GET / HTTP/1.1" 200 117 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"

10.0.2.2 - - [23/Aug/2018:09:59:03 +0000] "GET /favicon.ico HTTP/1.1" 404 571 "http://hoge1.com:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"

200のレスポンスでなくて304なのはキャッシュの関係らしい。 多分はじめて訪問した際は200と404が出ているはず。

次起動するときは

# service nginx start

起動確認

# systemctl status nginx

● nginx.service - nginx - high performance web server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
   Active: active (running) since Fri 2018-08-24 06:40:31 GMT; 1s ago
     Docs: http://nginx.org/en/docs/
  Process: 820 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 840 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 841 (nginx)
   CGroup: /system.slice/nginx.service
           ├─841 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─842 nginx: worker process

起動に自動起動させておくには

# systemctl enable nginx

備考

特に設定していないのにポートが8080で動くのはvagrantの初期設定上。ホスト側の8080番ポートでゲストの80番の代わりをしている的な。 vagrant起動時に default: 80 (guest) => 8080 (host) (adapter 1)となっている。