Ubuntu 18.04 - LXD 3.0でRESTful APIを有効化する

参考サイト


作業環境


詳細

SHELL> hostnamectl status
   Static hostname: lxd.ie-server.info
         Icon name: computer-vm
           Chassis: vm
~
    Virtualization: kvm
  Operating System: Ubuntu 18.04.1 LTS
            Kernel: Linux 4.15.0-32-generic
      Architecture: x86-64

LXD/LXCのバージョンを確認

lxd --version/lxc --versionコマンドを実行

SHELL> lxd --version
3.0.1
SHELL> lxc --version
3.0.1

RESTful APIを有効化


パスワードを設定

「******」は、適時パスワードに変更すること

SHELL> sudo lxc config set core.trust_password ******

接続制限を設定

以下は、全てのアクセスを許可する 危険

SHELL> sudo lxc config set core.https_address [::]

接続情報を保存

lxc remoteコマンドでサーバーに接続する
lxcコマンドがインストールされている環境からなら、どこからでも以下コマンドを実行できるはず

SHELL> sudo lxc remote add api 127.0.0.1:8443
Generating a client certificate. This may take a minute...
Certificate fingerprint: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ok (y/n)? y
Admin password for api:
Client certificate stored at server:  api

接続に成功するとホームディレクトリに鍵が生成される

SHELL> ls ~/.config/lxc/
client.crt  client.key  config.yml  cookies  servercerts

Curlで RESTful APIを利用する

SHELL> sudo curl -k -v --cert ~/.config/lxc/client.crt --key ~/.config/lxc/client.key https://127.0.0.1:8443/

JSONが表示されれば、きっと完了

{
  "type": "sync",
  "status": "Success",
  "status_code": 200,
  "operation": "",
  "error_code": 0,
  "error": "",
  "metadata": [
    "/1.0"
  ]
}

Ubuntu 18.04 - LXD 3.0をインストールする

参考サイト


作業環境


詳細

SHELL> hostnamectl status
   Static hostname: lxd.ie-server.info
         Icon name: computer-vm
           Chassis: vm
~
    Virtualization: kvm
  Operating System: Ubuntu 18.04.1 LTS
            Kernel: Linux 4.15.0-32-generic
      Architecture: x86-64

事前準備


パッケージリストとパッケージを更新

SHELL> sudo apt update && sudo apt list --upgradable && sudo apt upgrade -y

再起動

SHELL> sudo reboot

LXDインストール


aptを使い、LXD構築に必要なパッケージをインストール

SHELL> sudo apt install -y lxd lxd-client zfsutils-linux bridge-utils criu

フューチャーブランチを利用する場合は、下記コマンドでインストール

SHELL> sudo apt install -t xenial-backports lxd lxd-client zfsutils-linux bridge-utils criu

LXD/LXCのバージョンを確認

lxd --version/lxc --versionコマンドを実行

SHELL> lxd --version
3.0.1
SHELL> lxc --version
3.0.1

ブリッジインターフェースを作成


「/etc/network/interfaces」にブリッジインターフェースの以下設定を追加

SHELL> sudo vim /etc/network/interfaces

追記する内容

source /etc/network/interfaces.d/*
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
  address 0.0.0.0

auto br0
iface br0 inet static
  address 172.16.2.100
  netmask 255.240.0.0
  network 172.16.0.0
  broadcast 172.31.255.255
  gateway 172.16.0.1
  dns-nameservers 8.8.8.8 8.8.4.4
  bridge_ports eth0
  bridge_stp off
  bridge_fd 0
  bridge_maxwait 0

ホスト機のリソース制限編集

以下設定を「limits.conf」の末尾に追記

SHELL> sudo vim /etc/security/limits.conf

追記する内容

*               soft    nofile          1048576
*               hard    nofile          1048576
root            soft    nofile          1048576
root            hard    nofile          1048576
*               soft    memlock         unlimited
*               hard    memlock         unlimited

以下設定を「sysctl.conf」の末尾に追記

SHELL> sudo vim /etc/sysctl.conf

追記する内容

fs.inotify.max_queued_events = 1048576
fs.inotify.max_user_instances = 1048576
fs.inotify.max_user_watches = 1048576
vm.max_map_count = 262144

再起動

SHELL> sudo reboot

LXDセットアップ


セットアップ

lxd initコマンドを実行

SHELL> sudo lxd init
Would you like to use LXD clustering? (yes/no) [default=no]: no
Do you want to configure a new storage pool? (yes/no) [default=yes]: yes
Name of the new storage pool [default=default]: pool-01
Name of the storage backend to use (dir, zfs) [default=zfs]: zfs
Create a new ZFS pool? (yes/no) [default=yes]: yes
Would you like to use an existing block device? (yes/no) [default=no]: no
Size in GB of the new loop device (1GB minimum) [default=18GB]: 18
Would you like to connect to a MAAS server? (yes/no) [default=no]: no
Would you like to create a new local network bridge? (yes/no) [default=yes]: yes
What should the new bridge be called? [default=lxdbr0]: lxdbr0
What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: auto
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: none
Would you like LXD to be available over the network? (yes/no) [default=no]: yes
Address to bind LXD to (not including port) [default=all]: all
Port to bind LXD to [default=8443]: 8443
Trust password for new clients:
Again:
Would you like stale cached images to be updated automatically? (yes/no) [default=yes] yes
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]: no

上記のような入力が面倒な場合は、下記コマンドでもセットアップ可能

※「************」の箇所は、適時パスワードに変更する

SHELL>  cat <<EOF | sudo lxd init
no
yes
lxd-storage-pool01
zfs
yes
no
18
no
yes
lxdbr0
auto
none
yes
all
8443
************
************
yes
no
EOF

コンテナイメージ取得


ローカルのコンテナイメージ一覧を取得

lxc image listコマンドを実行 はじめは、コンテナイメージは表示されないはず

SHELL> sudo lxc image list
+-------+-------------+--------+-------------+------+------+-------------+
| ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE |
+-------+-------------+--------+-------------+------+------+-------------+

リモートのコンテナイメージを検索

lxc image list images: ディストリビューション名コマンドを実行 (下記では、リモートのCentOSを検索した)

SHELL> sudo lxc image list images: centos
+------------------------+--------------+--------+---------------------------------+--------+---------+-------------------------------+
|         ALIAS          | FINGERPRINT  | PUBLIC |           DESCRIPTION           |  ARCH  |  SIZE   |          UPLOAD DATE          |
+------------------------+--------------+--------+---------------------------------+--------+---------+-------------------------------+
| centos/6 (3 more)      | d183363eeba1 | yes    | Centos 6 amd64 (20180820_02:16) | x86_64 | 75.55MB | Aug 20, 2018 at 12:00am (UTC) |
+------------------------+--------------+--------+---------------------------------+--------+---------+-------------------------------+
| centos/6/i386 (1 more) | a089525a7a67 | yes    | Centos 6 i386 (20180820_02:16)  | i686   | 75.79MB | Aug 20, 2018 at 12:00am (UTC) |
+------------------------+--------------+--------+---------------------------------+--------+---------+-------------------------------+
| centos/7 (3 more)      | 2718b6765dda | yes    | Centos 7 amd64 (20180820_02:16) | x86_64 | 83.43MB | Aug 20, 2018 at 12:00am (UTC) |
+------------------------+--------------+--------+---------------------------------+--------+---------+-------------------------------+

コンテナ作成・一覧取得


コンテナ作成

lxc launch images:イメージ名 コンテナ名を実行 以下では、CentOS7 64bitのイメージを選択してvm01を作成する場合 ※初回はリモートからコンテナイメージを取得するのに時間がかかる

SHELL> sudo lxc launch images:centos/7/amd64 vm01
Creating vm01
Starting vm01

コンテナ一覧を取得

lxc listを実行

SHELL> sudo lxc list
+------+---------+-----------------------+------+------------+-----------+
| NAME |  STATE  |         IPV4          | IPV6 |    TYPE    | SNAPSHOTS |
+------+---------+-----------------------+------+------------+-----------+
| vm01 | RUNNING | 10.162.182.150 (eth0) |      | PERSISTENT | 0         |
+------+---------+-----------------------+------+------------+-----------+

コンテナ操作


コンテナ停止

lxc stop コンテナ名で該当コンテナを停止

SHELL> sudo lxc stop vm01

lxc listコマンドで、コンテナ停止を確認

SHELL> sudo lxc list
+------+---------+------+------+------------+-----------+
| NAME |  STATE  | IPV4 | IPV6 |    TYPE    | SNAPSHOTS |
+------+---------+------+------+------------+-----------+
| vm01 | STOPPED |      |      | PERSISTENT | 0         |
+------+---------+------+------+------------+-----------+

コンテナ起動

lxc start コンテナ名で該当コンテナを起動

SHELL> sudo lxc start vm01

コンテナにログイン

lxc exec コンテナ名 bashコマンドを実行

SHELL> sudo lxc exec vm01 bash
[root@vm01 ~]#

コンテナ削除

lxc delete コンテナ名コマンドを実行(コンテナが停止している必要がある)

SHELL> sudo lxc delete vm01

コンテナイメージの作成

SHELL> lxc publish vm01 --alias CentOS7
コンテナは以下のフィンガープリントで publish されます: 7b25b19d8b75fff2dea225bcd6d9580579b0e033f889147405afaf2f810c5d5b

作成したコンテナイメージを確認

SHELL> sudo lxc image list
+-------+--------------+--------+---------------------------------+--------+---------+------------------------------+
| ALIAS | FINGERPRINT  | PUBLIC |           DESCRIPTION           |  ARCH  |  SIZE   |         UPLOAD DATE          |
+-------+--------------+--------+---------------------------------+--------+---------+------------------------------+
|       | 2718b6765dda | no     | Centos 7 amd64 (20180820_02:16) | x86_64 | 83.43MB | Aug 20, 2018 at 6:25am (UTC) |
+-------+--------------+--------+---------------------------------+--------+---------+------------------------------+

CentOS - gitコマンドの入力補完(git-completion)、ローカルブランチの表示(git-prompt)

作業環境

SHELL> hostnamectl status
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
-
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-693.21.1.el7.x86_64
      Architecture: x86-64
SHELL> echo $SHELL
/bin/bash
SHELL> git --version
git version 1.8.3.1

git-completionの導入

gitコマンドの入力補完を導入するためにgit-completionを有効にします。 GitをYumでインストールした場合、以下ディレクトリにgit-completionはあるはず。

/usr/share/doc/git-1.8.3.1/contrib/completion/git-completion.bash

「~/.bash_profile」に以下、1行の設定を追記する

. /usr/share/doc/git-1.8.3.1/contrib/completion/git-completion.bash

設定の読み込み

SHELL> source ~/.bash_profile

ローカルブランチの表示

gitコマンドの入力補完を導入するためにgit-promptを有効にします。 GitをYumでインストールした場合、以下ディレクトリにgit-promptはあるはず。

/usr/share/doc/git-1.8.3.1/contrib/completion/git-prompt.sh

「~/.bash_profile」に以下、3行の設定を追記する

. /usr/share/doc/git-1.8.3.1/contrib/completion/git-prompt.sh
GIT_PS1_SHOWDIRTYSTATE=true
export PS1='[\[\033[1;32m\]\u\[\033[00m\]@\h \[\033[1;34m\]\w\[\033[1;31m\]$(__git_ps1)\[\033[00m\]]\$ '

設定の読み込み

SHELL> source ~/.bash_profile

CentOS7 - ipset + firewalld or iptables 国別アクセス制御

作業環境

hostnamectl status
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
~
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-693.2.2.el7.x86_64
      Architecture: x86-64

国別IPアドレスリストに取得

IPアドレスリストをダウンロード

shell> curl -O 'http://nami.jp/ipv4bycc/cidr.txt.gz'

ファイルを解凍

shell> gunzip cidr.txt.gz

ブラックリストの作成

ルールを作成

shell> ipset create -exist BLACKLIST hash:net

ブラックリストを初期化

shell> ipset flush BLACKLIST

US(アメリカ)をブラックリストに登録

shell> awk '$1 ~ /^US/ {print $2}' cidr.txt | xargs -i ipset -q add BLACKLIST {}

ブラックリストに登録済みのIPアドレス一覧を表示

shell> ipset list BLACKLIST

firewalld or iptablesブラックリストのルールを追加

firewalld

shell> firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 1 -m set --match-set BLACKLIST src -j REJECT
shell> firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -m set --match-set BLACKLIST src -j REJECT

iptables

shell> iptables -I INPUT -m state --state NEW -p tcp --dport 22 -m set --match-set BLACKLIST src -j REJECT

ホワイトリストを作成

ルールを作成

shell> ipset create -exist WHITELIST hash:net

ホワイトリストを初期化

shell> ipset flush WHITELIST

JP(日本)をホワイトリストに登録

shell> awk '$1 ~ /^JP/ {print $2}' cidr.txt | xargs -i ipset -q add WHITELIST {}

firewalld or iptablesホワイトリストのルールを追加

firewalld

shell> firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -m set --match-set WHITELIST src -j ACCEPT
shell> firewall-cmd --direct --add-rule ipv4 filter INPUT 0 -m set --match-set WHITELIST src -j ACCEPT

iptables

shell> iptables -I INPUT -m state --state NEW -p tcp --dport 22 -m set --match-set WHITELIST src -j ACCEPT

ホワイトリストIPアドレスを追加/削除

ブラックリストに変更を加える場合は、「WHITELIST」を「BLACKLIST」置換するだけ

追加

shell> ipset -q add WHITELIST 1.1.1.1/32

削除

shell> ipset -q del WHITELIST 1.1.1.1/32

NGXIN + ngx_mrubyを用いたSSL動的読み込みの環境構築

構築環境


shell> hostnamectl status
   Static hostname: www.example.com
         Icon name: computer-vm
           Chassis: vm
〜
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-693.17.1.el7.x86_64
      Architecture: x86-64

※ 全て管理者権限(root)のあるユーザで実行している

NIGNX + ngx-mrubyの環境構築


各種ソースコードをダウンロード

NGINXのソースコード

最新版(Ver 1.14.0)をダウンロード(2018/05/04現在)

shell> cd /usr/local/src/ && \
curl -O http://nginx.org/download/nginx-1.14.0.tar.gz && \
tar zxf /usr/local/src/nginx-1.14.0.tar.gz

ngx_mrubyのソースコード

最新版(Ver 1.20.2)をダウンロード(2018/05/04現在)

shell> cd /usr/local/src/ && \
curl -LO https://github.com/matsumotory/ngx_mruby/archive/v1.20.2.tar.gz && \
tar zxf /usr/local/src/v1.20.2.tar.gz

Rubyをインストール

ngx_mrubyのビルドには、RubyのRakeライブラリが必要なためインストール

shell> yum install -y ruby ruby-devel openssl openssl-devel
shell> gem install rake

ngx_mrubyをビルド

shell> cd /usr/local/src/ngx_mruby-1.20.2/
shell> ./configure --with-ngx-src-root=/usr/local/src/nginx-1.14.0
shell> make build_mruby -j 2
shell> make generate_gems_config -j 2

NGINXをビルド

shell> cd /usr/local/src/nginx-1.14.0
shell> ./configure --with-compat \
--add-module=/usr/local/src/ngx_mruby-1.20.2/dependence/ngx_devel_kit \
--add-module=/usr/local/src/ngx_mruby-1.20.2 \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_gunzip_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-http_slice_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--without-stream_access_module \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'
shell> make install -j 2

NGINXに設定を投入

NGINXのコンフィグをバックアップ

既存のnginx.confをリネーム

shell> mv -v /usr/local/nginx/conf/nginx.conf{,.backup}

nginx.confを新規作成

shell> mkdir /usr/local/nginx/conf/conf.d
shell> cat /usr/local/nginx/conf/nginx.conf
user  nginx;
worker_processes  auto;
worker_rlimit_nofile 100000;
pid /usr/local/nginx/logs/nginx.pid;

events {
    worker_connections  2048;
}

http {
    log_format ltsv 'time:$time_iso8601\t'
                    'remote_addr:$remote_addr\t'
                    'request_method:$request_method\t'
                    'request_length:$request_length\t'
                    'request_uri:$request_uri\t'
                    'https:$https\t'
                    'uri:$uri\t'
                    'query_string:$query_string\t'
                    'status:$status\t'
                    'bytes_sent:$bytes_sent\t'
                    'body_bytes_sent:$body_bytes_sent\t'
                    'referer:$http_referer\t'
                    'useragent:$http_user_agent\t'
                    'forwardedfor:$http_x_forwarded_for\t'
                    'request_time:$request_time\t'
                    'upstream_response_time:$upstream_response_time';

    access_log /usr/local/nginx/logs/access.log ltsv;
    error_log /usr/local/nginx/logs/error.log info;

    proxy_set_header    Host                $host;
    proxy_set_header    X-Real-IP           $remote_addr;
    proxy_set_header    X-Forwarded-Host    $host;
    proxy_set_header    X-Forwarded-Server  $host;
    proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
    proxy_set_header    X-Forwarded-Proto   https;

    client_max_body_size 2G;

    gzip on;
    gzip_types text/css application/javascript application/json application/font-woff application/font-tff image/gif image/png image/jpeg application/octet-stream;

    server_tokens off;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    include       mime.types;
    default_type  application/octet-stream;
    keepalive_timeout  10;
    server_names_hash_bucket_size 20000;
    include /usr/local/nginx/conf/conf.d/*.conf;
}

NGINXユーザを作成

shell> useradd -M --shell /sbin/nologin nginx

NGINXのシンボリックリンクを作成

shell> ln -s /usr/local/src/nginx-1.14.0/objs/nginx /usr/local/bin/nginx

シンタックスチェック

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

NGINXをSystemdに登録

sehll> cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/bin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

NGINXを起動

shell> systemctl enable nginx && systemctl start nginx

動作確認用

テストページ用のコンフィグを作成

http://www.example.com/helloにアクセスして「hello ngx_mruby world」が表示される設定を追加

shell> /usr/local/nginx/conf/conf.d/www.example.com.conf
server{
    listen 80;
    server_name  www.example.com;

    location /hello {
      mruby_content_handler_code 'Nginx.echo "hello ngx_mruby world"';
    }

    location ^~ /.well-known/acme-challenge/ {
        root /usr/local/nginx/html;
    }
}

追加したコンフィグを反映

shell> nginx -s reload

テストページにアクセス

shell> curl -s http://www.example.com/hello
hello ngx_mruby world

Let’s Encryptの導入


SSL証明書を入手するために無料で期間の短い証明書を無料で発行してくれるサービスを利用する

ソースコードをダウンロード

shell> cd /opt && git clone https://github.com/letsencrypt/letsencrypt

Let’s Encryptの環境構築

shell> cd letsencrypt && ./letsencrypt-auto --help all

証明書を発行

コマンドオプションには、ドキュメントルート(-w)・コモンネーム(-d)・メールアドレス(-m)を指定

shell> ./letsencrypt-auto certonly \
--rsa-key-size 4096 \
--webroot -w /usr/local/nginx/html \
-d www.example.com \
-m root@www.example.com

SSL証明書を確認

shell> tree /etc/letsencrypt/live/
/etc/letsencrypt/live/
`-- www.example.com
    |-- README
    |-- cert.pem -> ../../archive/www.example.com/cert1.pem
    |-- chain.pem -> ../../archive/www.example.com/chain1.pem
    |-- fullchain.pem -> ../../archive/www.example.com/fullchain1.pem
    `-- privkey.pem -> ../../archive/www.example.com/privkey1.pem

SSL証明書パーミッションを変更

NGINXユーザからSSL証明書にアクセスする場合、「live」「archive」のパーミッションを700 -> 755に変更必要がある

shell> chmod 755 /etc/letsencrypt/{live,archive}

テストページ用のコンフィグにSSLの設定を追加

shell> /usr/local/nginx/conf/conf.d/www.example.com.conf
server{
    listen 80;
    listen 443 ssl http2;
    server_name www.example.com;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_session_timeout 1h;
    ssl_session_cache shared:SSL:10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    ssl_certificate "/etc/letsencrypt/live/www.example.com/fullchain.pem";
    ssl_certificate_key "/etc/letsencrypt/live/www.example.com/privkey.pem";
    ssl_dhparam "/usr/local/nginx/conf/dhparam.pem";

    location /hello {
      mruby_content_handler_code 'Nginx.echo "hello ngx_mruby world"';
    }

    location ^~ /.well-known/acme-challenge/ {
        root /usr/local/nginx/html;
    }
} 

dhparamを生成

shell> openssl dhparam 4096 -out /usr/local/nginx/conf/dhparam.pem

追加したコンフィグを反映

shell> nginx -s reload

テストページにアクセス

shell> curl -s https://www.example.com/hello
hello ngx_mruby world

SSL証明書の動的読み込み


ドキュメントルートは、「/usr/local/nginx/html/< ドメイン名 >」でドメイン毎に作成
今回の場合は、「/usr/local/nginx/html/www.example.com

shell> cat /usr/local/nginx/conf/conf.d/mruby.conf
server {
      listen 80;
      server_name _;

      location ^~ /.well-known/acme-challenge/ {
        root /usr/local/nginx/html;
    }
    location / {
        mruby_set_code $root '
          r = Nginx::Request.new
          "/usr/local/nginx/html/#{r.hostname}"
        ';

        root $root;
    }
}

server {
    listen 443 ssl http2;
    server_name  _;

    # SSL
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_session_timeout 1h;
    ssl_session_cache shared:SSL:10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;
    add_header Strict-Transport-Security max-age=31536000;

    ssl_certificate "/etc/letsencrypt/live/www.example.com/fullchain.pem";
    ssl_certificate_key "/etc/letsencrypt/live/www.example.com/privkey.pem";
    ssl_dhparam "/usr/local/nginx/conf/dhparam.pem";

    mruby_ssl_handshake_handler_code '
      ssl = Nginx::SSL.new
      ssl.certificate     = "/etc/letsencrypt/live/#{ssl.servername}/fullchain.pem"
      ssl.certificate_key = "/etc/letsencrypt/live/#{ssl.servername}/privkey.pem"
    ';

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    location / {
        mruby_set_code $root '
          r = Nginx::Request.new
          "/usr/local/nginx/html/#{r.hostname}"
        ';

        root $root;
    }

    ## リバースプロキシの設定例
    #location / {
    #   resolver 8.8.8.8 1.1.1.1 8.8.4.4 valid=15m;
    #   resolver_timeout 10s;
    #   add_header Strict-Transport-Security max-age=31536000;
    #   mruby_set_code $backend 'Nginx::Request.new.hostname';
    #   proxy_pass http://$backend;
    #}
}

シンタックスチェック

murbyの記述箇所については、下記コマンドではエラーを確認できないので「/usr/local/nginx/logs/error.log」を確認すること

shell> nginx -t

設定の反映

shell> nginx -s reload

正しく設定が反映されない場合は、再起動を実施

shell> systemctl restart nginx

CoreOS - CoreOSにdocker-composeをインストールする

How to install Docker-comporse on CoreOS

環境

shell> hostnamectl status
   Static hostname: localhost
         Icon name: computer-vm
           Chassis: vm
---
    Virtualization: kvm
  Operating System: Container Linux by CoreOS 1688.5.3 (Rhyolite)
            Kernel: Linux 4.14.32-coreos
      Architecture: x86-64

参考サイト

CoreOSにdocker-composeをインストール

手順

  1. コマンド設置用ディレクトリを作成
  2. 最新のdocker-composeをダウンロード(2018/04/25時点)
  3. リネームして設置
  4. 実行権限を付与
  5. PATHを指定
  6. 動作確認

1.コマンド設置用ディレクトリを作成

shell> sudo mkdir -p /opt/bin && sudo chmod -R 755 /opt

2.最新のdocker-composeをダウンロード(2018/04/25時点)

shell> sudo wget -P /opt/ https://github.com/docker/compose/releases/download/1.21.0/docker-compose-Linux-x86_64

3.リネームして設置

shell> sudo mv /opt/docker-compose-Linux-x86_64 /opt/bin/docker-compose

4.実行権限を付与

shell> sudo chmod 755 /opt/bin/docker-compose

5.PATHを指定

shell> PATH=$PATH:/opt/bin && export PATH

6.動作確認

shell> docker-compose --version
docker-compose version 1.21.0, build 5920eb0

CentOS7 - MariaDB 10.2をインストール

目次

1.Yumリポジトリを作成
2.MariaDB 10.1をインストール
3.MariaDB 10.2の起動と自動起動設定

作業環境

shell> hostnamectl status
   Static hostname: localhost
         Icon name: computer-vm
           Chassis: vm
〜
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-693.2.2.el7.x86_64
      Architecture: x86-64

1.Yumリポジトリを作成

Nginx用のリポジトリを作成する

shell> cat << EOS > /etc/yum.repos.d/mariadb.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOS

2.MariaDB 10.2をインストール

YumコマンドでMariaDB 10.2をインストール

shell> yum install -y MariaDB-server MariaDB-client MariaDB-devel

バージョンを確認

shell> mysql -V
mysql  Ver 15.1 Distrib 10.2.13-MariaDB, for Linux (x86_64) using readline 5.1

3.MariaDB 10.2の起動と自動起動設定

systemctlコマンドでMariaDB 10.2の起動と自動起動の設定

shell> systemctl enable mariadb
shell> systemctl start mariadb