LoginSignup
4
4

More than 5 years have passed since last update.

Nginxのconfファイルの管理ツールをつくりました

Last updated at Posted at 2017-05-05

1つのサーバに複数のサイトを入れている場合、/etc/nginx/sites-availableに、各サイト用の設定ファイルを個別につくって、/etc/nginx/sites-enabledに反映させたいファイルのリンクをはって、Nginxを再起動させるというのが一般的な管理方法だと思ってますが、sites-availableにファイルを作って、sites-enabledにリンクはって、設定間違ってたからまたavailableのファイルを開いて修正して、リンク張ろうと思ったらファイル名忘れて、availableのファイル一覧確認してとかやってるとつかれるので、ツールを作ってみました。

Githubのリポジトリ

ここです。

環境

  • zshを使っています。(bashでも多分動くと思います)
  • Ubuntu16.10で動作確認しました。

出来ること

  • sites-availableのファイル一覧をすぐに確認できる
  • sites-enabledのファイル一覧もすぐに確認できる
  • 設定ファイルをどこからでもvimで開ける
  • enabledへの反映もどこからでもリンクできる
  • enabledのリンク削除もすぐできる
  • availableへの追加も簡単にできる
  • nginxの設定ファイルチェックも短いコマンドでできる
  • nginxの再起動も短いコマンドでできる
  • 設定ファイルの削除(リンク・ファイル削除)もすぐできる
  • enabledのリンク全削除もすぐできる
  • certbotを使ったssl取得も簡単にできる(準備作業は必要)

インストール

gitから落として、PATH通ってるフォルダ(/usr/local/binを想定)に置きます。

$ git clone https://github.com/endoyuta/ngcon.git
$ sudo mv ngcon/ngcon /usr/local/bin
$ sudo rm -rf ngcon

使い方

基本的にroot権限が必要です。

簡単なヘルプ

$ ngcon
commands [lsa, lse, add, new, a, e, test, re, vim, rm, rmall, delete, get_ssl]

site-availableのファイル一覧を確認

$ ngcon lsa

site-enabledのファイル一覧を確認

$ ngcon lse

site-enabledにファイルを反映(availableのリンクをはる)

$ ngcon add [conf file name]

site-availableにファイルを追加

既存ファイル(base file)をコピーして新規ファイルを(new file)という名前で作成します。作成後、new fileをvimで開きます。

$ ngcon new [base file name] [new file name]

site-availableのパスを表示します

$ ngcon a

site-enabledのパスを表示します

$ ngcon e

nginxの設定ファイルチェックをします

$ ngcon test

nginxを再起動します

$ ngcon re

vimで設定ファイルを開きます

$ ngcon vim [conf file name]

site-enabledのリンクを削除します

$ ngcon rm [conf file name]

site-enabledのリンクを全部削除します

$ ngcon rmall

設定ファイルを削除します。

enabledからもavailableからも削除します。

$ ngcon delete [conf file name]

Let's Encryptの証明書を取得します。(おまけ)

certbotのインストール

certbotが必要ですので、このコマンドを使う場合、事前にインストールが必要です。

$ sudo apt-get install certbot

Nginx設定ファイルの作成

下記のような簡単な設定ファイルを、t_forcertという名前(ファイル名は変えられます)で、site-abailableに入れておきます。(server_nameは、SSL証明書を取得したいドメインにします)。また、/usr/share/nginx/html配下にindex.htmlが存在する必要があります。(Nginxのデフォルト状態ならあります。)

server {
  listen 80;
  server_name example.com;
  index index.html;
  root /usr/share/nginx/html;
}

コマンド実行

$ ngcon get_ssl example.com

これで取得完了できます。やっているのは、上記設定ファイルを反映してNginxを再起動させて、certbot certonlyコマンドを実行しています。証明書取得後に、上記設定ファイルを削除して、再度Nginxを再起動しています。

補足

--standaloneを使ってインストールできるなら、逆にめんどくさいだけだと思います。すでにサイトが動いていて、Nginxをなるべく止めたくないとかの場合に、ある程度有用かもしれません。

コード

#!/usr/bin/zsh

confpath="/etc/nginx/nginx.conf"
apath="/etc/nginx/sites-available"
epath="/etc/nginx/sites-enabled"
default_web_root="/usr/share/nginx/html"
conf_filename_for_cert="t_forcert"

chk_file () {
  if [ -e $1 ]; then
    return 1
  else
    echo $1" is not found."
    return 0
  fi
}

rmconf () {
  chk_file $epath/$1
  if [ "$?" = 1 ]; then
    rm $epath/$1
    return 1
  fi
  return 0
}

delete () {
  if [ -e $epath/$1 ]; then
    rm $epath/$1
  fi
  if [ -e $apath/$1 ]; then
    rm $apath/$1
  fi
}

add () {
  if [ "$1" = "" ]; then
    echo "please input filename."
    return 0
  else
    chk_file $apath/$1
    if [ "$?" = 1 ]; then
      ln -s $apath/$1 $epath
      return 1
    fi
    return 0
  fi
}

new () {
  if [ -e $apath/$1 -a "$2" != "" ]; then
    cp $apath/$1 $apath/$2
    vim $apath/$2
  else
    echo $1" is not found, or target file name is blank."
  fi
}

get_ssl () {
  if [ "$1" = "" ]; then
    echo "please input domain."
  else
    if [ -e $apath/$conf_filename_for_cert ]; then
      add $conf_filename_for_cert
      if [ $? = 1 ]; then
        nginx_restart
        certbot certonly --webroot -w $default_web_root -d $1
        rmconf $conf_filename_for_cert
      fi
    else
      echo "$apath/$conf_filename_for_cert is not found."
    fi
  fi
}

nginx_test () {
  nginx -t -c $confpath
}

nginx_restart () {
  systemctl restart nginx
}

if [ "$1" = "lsa" ]; then
  ls -al $apath

elif [ "$1" = "lse" ]; then
  ls -al $epath

elif [ "$1" = "add" ]; then
  add $2

elif [ "$1" = "new" ]; then
  new $2 $3

elif [ "$1" = "a" ]; then
  echo $apath

elif [ "$1" = "e" ]; then
  echo $epath

elif [ "$1" = "test" ]; then
  nginx_test

elif [ "$1" = "re" ]; then
  nginx_restart

elif [ "$1" = "vim" ]; then
  chk_file $apath/$2
  if [ "$?" = 1 ]; then
    vim $apath/$2
  fi

elif [ "$1" = "rm" ]; then
  rmconf $2

elif [ "$1" = "rmall" ]; then
  rm $epath/*

elif [ "$1" = "delete" ]; then
  delete $2

elif [ "$1" = "get_ssl" ]; then
  get_ssl $2

else
  echo "commands [lsa, lse, add, new, a, e, test, re, vim, rm, rmall, delete, get_ssl]"
fi
4
4
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
4
4