サーバー完成図書を自動で作りたいんだ 第3話 Apacheの基本設定まわり

高瀬 裕介 74views 更新:2015年4月7日
#!/bin/sh
IFS=$'\n'
SCRIPT_PATH=`echo $(cd $(dirname $0);pwd)`
DOCUMENT_ROOTS=""
ACCESS_LOGS=""
ERROR_LOGS=""

function search_conf() {
  _SERVER_ROOT=$1
  _CONF_FILE=$2

  if [[ ! "${_CONF_FILE}"  =~ ^/ ]]; then
    _CONF_FILE="${_SERVER_ROOT}/${_CONF_FILE}"
  fi

  echo "[INFO] Apacheの設定ファイル ${_CONF_FILE} をチェックします" >&2
  if [ `egrep -r "^[ \t]*DocumentRoot " ${CONF_FILE} | wc -l` -gt 0 ]; then
    for LINE in `egrep -r "^[ \t]*DocumentRoot " ${CONF_FILE} | sed -e "s/^[ \t]*DocumentRoot[ \t]*//g" | sed -e "s/[\'\"]//g" | sed -e "s/#.*//g"`
    do
      if [ `echo ${DOCUMENT_ROOTS} | grep "${LINE}," | wc -l` -eq 0 ]; then
        DOCUMENT_ROOTS="${DOCUMENT_ROOTS} ${LINE},"
      fi
    done
  fi

  if [ `egrep -r "^[ \t]*Log " ${CONF_FILE} | wc -l` -gt 0 ]; then
    for LINE in `egrep -r "^[ \t]*Log " ${CONF_FILE} | sed -e "s/^[ \t]*Log[ \t]*//g" | sed -e "s/[\'\"]//g" | sed -e "s/[# ].*$//g"`
    do
      if [[ ! "${LINE}"  =~ ^/ ]]; then
        LINE="${_SERVER_ROOT}/${LINE}"
      fi
      if [ `echo ${ACCESS_LOGS} | grep "${LINE}," | wc -l` -eq 0 ]; then
        ACCESS_LOGS="${ACCESS_LOGS} ${LINE},"
      fi
    done
  fi

  if [ `egrep -r "^[ \t]*CustomLog " ${CONF_FILE} | wc -l` -gt 0 ]; then
    for LINE in `egrep -r "^[ \t]*CustomLog " ${CONF_FILE} | sed -e "s/^[ \t]*CustomLog[ \t]*//g" | sed -e "s/[\'\"]//g" | sed -e "s/[# ].*$//g"`
    do
      if [[ ! "${LINE}"  =~ ^/ ]]; then
        LINE="${_SERVER_ROOT}/${LINE}"
      fi
      if [ `echo ${ACCESS_LOGS} | grep "${LINE}," | wc -l` -eq 0 ]; then
        ACCESS_LOGS="${ACCESS_LOGS} ${LINE},"
      fi
    done
  fi

  if [ `egrep -r "^[ \t]*ErrorLog " ${CONF_FILE} | wc -l` -gt 0 ]; then
    for LINE in `egrep -r "^[ \t]*ErrorLog " ${CONF_FILE} | sed -e "s/^[ \t]*ErrorLog[ \t]*//g" | sed -e "s/[\'\"]//g" | sed -e "s/[# ].*$//g"`
    do
      if [[ ! "${LINE}"  =~ ^/ ]]; then
        LINE="${_SERVER_ROOT}/${LINE}"
      fi
      if [ `echo ${ACCESS_LOGS} | grep "${LINE}," | wc -l` -eq 0 ]; then
        ERROR_LOGS="${ACCESS_LOGS} ${LINE},"
      fi
    done
  fi

  if [ `egrep -r "^[ \t]*Include " ${_CONF_FILE} | wc -l` -gt 0 ]; then
    for INCLUDE in `egrep -r "^[ \t]*Include " ${_CONF_FILE} | sed -e "s/^[ \t]*Include[ \t]*//g" | sed -e "s/[\'\"]//g" | sed -e "s/#.*//g"`
    do
      if [[ ! "${INCLUDE}"  =~ ^/ ]]; then
        INCLUDE="${_SERVER_ROOT}/${INCLUDE}"
      fi
      if [ `ls -1 ${INCLUDE} | wc -l` -gt 0 ]; then
        for CHILE_CONF in `ls -1 ${INCLUDE}`
        do
          search_conf "${_SERVER_ROOT}" "${CHILE_CONF}"
        done
      fi
    done
  fi
}

if [ -f "/etc/init.d/httpd" ]; then
  CONF_FILE=`cat /etc/init.d/httpd | grep config: | grep -v chkconfig | grep -v sysconfig | cut -f2 -d: | sed -e "s/[ \t]\+//g"`
  if [ -f "${CONF_FILE}" ]; then
    SERVER_ROOT=`egrep -r "^[ \t]*ServerRoot " ${CONF_FILE} | sed -e "s/^[ \t]*ServerRoot[ \t]*//g" | sed -e "s/[\'\"]//g" | sed -e "s/#.*//g"`
    if [ -d "${SERVER_ROOT}" ]; then
      echo "|_. 項目|_. 値|"
      search_conf "${SERVER_ROOT}" "${CONF_FILE}"

      echo "|設定ファイル|${CONF_FILE}|"
      DOCUMENT_ROOTS=`echo ${DOCUMENT_ROOTS} | sed -e "s/,$//"`
      echo "|ドキュメントルート|${DOCUMENT_ROOTS}|"

      ACCESS_LOGS=`echo ${ACCESS_LOGS} | sed -e "s/,$//"`
      echo "|アクセスログ|${ACCESS_LOGS}|"

      ERROR_LOGS=`echo ${ERROR_LOGS} | sed -e "s/,$//"`
      echo "|エラーログ|${ERROR_LOGS}|"

      if [ -f "/etc/logrotate.d/httpd" ]; then
        sh ${SCRIPT_PATH}/textile_logrotate.sh /etc/logrotate.d/httpd
      fi
    fi
  fi
fi

ログイン / 新規登録してコメントする

このソースコードをストックして後で利用したり、作業に利用したソースコードをまとめることができます。

こちらもお役に立つかもしれません