美文网首页
搭建WEB管理环境(NamedManager)下的DNS服务器

搭建WEB管理环境(NamedManager)下的DNS服务器

作者: osi | 来源:发表于2017-12-08 20:47 被阅读0次

    基础组件

    Bind
    NamedManager

    简易安装

    全程Yum安装
    这个没什么可说的,需要注意的是

    /etc/namedmanager/config-bind.php 
    /etc/namedmanager/config.php
    

    这两个文件的配置信息。
    最开始应该直接用IP的方式访问,搞清楚namedmanager的机制之后再切换域名来弄。
    若是测试域名记得修改

    /etc/hosts
    

    编译安装

    这个就有点麻烦了,麻烦的是很多服务/配置都得自己写一遍,但是为了用最新版本还是采用这个跑了起来。

    准备

    编译安装BIND
    此教程看到安装完成即可。(如果安装失败,那我也无能为力)
    大约需要20分钟。
    安装LNMP
    可以自己yum安装也可以用现成的工具,本人比较懒直接用工具装的
    CENTOS6.9 + PHP 5.6.32(php7也没问题) + Mysql5.6.38 (5.7没测过) + Tengine
    大约需要30分钟。
    下载NamedManager
    以上默认完成了bind的编译,LNMP环境的搭建,namedmanager的下载(zip解压到/data/wwwroot/www)

    配置namedmanager

    前提说明

    #配置文件信息
    1.named 编译目录 
    /usr/local/bind #所以 有些服务的位置就在 /usr/local/bind/sbin 后面会用
    2.named 配置文件目录 / 额外参数目录 
    /etc/named
    /var/named
    3.namedmanager 目录 
    /data/wwwroot/www
    4.namedmanager 网站修改后会同步到数据库及 /etc/named.namedmanager.conf (自动修改 此处仅说明)
    5.同步机制 每分钟自动运行
    /etc/cron.d/namedmanager-bind
    6.namedmanager 数据库连接及配置信息 /etc/namedmanager/config.php
    /etc/namedmanager/config-bind.php
    

    直接贴配置

    #named.conf
    key "rndc-key" {
            algorithm hmac-md5;
            secret "BC3r7nPgyUwL8o47sjrlbC=a";
    };
    
    controls {
            inet 127.0.0.1 port 953
                    allow { 127.0.0.1; } keys { "rndc-key"; };
    };
    options {
            listen-on port 53 { any; };
            listen-on-v6 port 53 { ::1; };
            pid-file "/var/named/named.pid";
            directory       "/var/named"; #创建目录
            dump-file       "/var/named/data/cache_dump.db"; #touch 文件
            statistics-file "/var/named/data/named_stats.txt"; #touch 文件
            memstatistics-file "/var/named/data/named_mem_stats.txt"; #touch 文件
            allow-query     { any; };
            allow-query-cache     { any; };
            recursion yes;
    
            dnssec-enable yes;
            dnssec-validation yes;
    
            /* Path to ISC DLV key */
            bindkeys-file "/var/named/named.iscdlv.key"; 
    
            managed-keys-directory "/var/named/dynamic"; #创建目录 并且给777
    };
    
    logging {
            channel default_debug {
                    file "/var/named/data/named.run"; #创建/var/named/data 目录 并且给777
                    severity dynamic;
            };
    };
    zone "." IN {
            type hint;
            file "named.ca";
    };
    
    include "/etc/named.namedmanager.conf"; #这个是关键
    
    #/etc/namedmanager/config.php
    
    $config["db_host"] = "localhost";                       // hostname of the MySQL server
    $config["db_name"] = "namedmanager";                            // database name
    $config["db_user"] = "namedmanager";                            // MySQL user
    $config["db_pass"] = "passwd";                                // MySQL password (if any)
    
    $_SERVER['HTTPS'] = "TRUE"; #这个加的话 就不强制HTTPS
    
    #/etc/namedmanager/config-bind.php
    $config["api_url"]              = "http://dns.abc.cn"; //如果是IP记得在网站后台也输入IP
    $config["api_server_name"]      = "dns.abc.cn";//和上方的一样
    $config["api_auth_key"]         = "anyset";
    
    $config["bind"]["config"]       = "/etc/named.namedmanager.conf";
    $config["bind"]["zonefiledir"]  = "/var/named/";
    $config["log_file"]             = "/var/log/messages";
    $config["bind"]["reload"] = "/usr/local/bind/sbin/rndc reload"; //关键
    $config["bind"]["verify_zone"]          = "/usr/local/bind/sbin/named-checkzone";               // Used to verify each generated zonefile as OK
    $config["bind"]["verify_config"]        = "/usr/local/bind/sbin/named-checkconf";               // Used to verify generated NamedManager configuration
    

    注意1 service start

    编译安装的没有/etc/init.d/named服务 主要修改 /usr/local/bind/sbin 相关的位置 若编译位置在 /usr/local/bind 的直接可用

    #!/bin/bash
    #
    # named           This shell script takes care of starting and stopping
    #                 named (BIND DNS server).
    #
    # chkconfig: - 13 87
    # description: named (BIND) is a Domain Name Server (DNS) \
    # that is used to resolve host names to IP addresses.
    # probe: true
    
    ### BEGIN INIT INFO
    # Provides: $named
    # Required-Start: $local_fs $network $syslog
    # Required-Stop: $local_fs $network $syslog
    # Default-Start:
    # Default-Stop: 0 1 2 3 4 5 6
    # Short-Description: start|stop|status|restart|try-restart|reload|force-reload DNS server
    # Description: control ISC BIND implementation of DNS server
    ### END INIT INFO
    
    # Source function library.
    . /etc/rc.d/init.d/functions
    
    #[ -r /etc/named ] && . /etc/named
    
    RETVAL=0
    export KRB5_KTNAME=${KEYTAB_FILE:-/etc/named.keytab}
    
    named='named'
    if [ -x /usr/local/bind/sbin/named-sdb ]; then
        named='named-sdb'
    fi
    
    # Don't kill named during clean-up
    NAMED_SHUTDOWN_TIMEOUT=${NAMED_SHUTDOWN_TIMEOUT:-25}
    
    if [ -n "$ROOTDIR" ]; then
       ROOTDIR=`echo $ROOTDIR | sed 's#//*#/#g;s#/$##'`;
       rdl=`/usr/bin/readlink $ROOTDIR`;
       if [ -n "$rdl" ]; then
          ROOTDIR="$rdl";
       fi;
    fi
    
    PIDFILE="/var/named/named.pid"
    
    ROOTDIR_MOUNT='/etc/pki/dnssec-keys /var/named /etc/named/named.conf
    /etc/named.dnssec.keys /etc/named/named.rfc1912.zones /etc/named/rndc.conf /etc/named/rndc.key
    /usr/lib64/bind /usr/lib/bind /usr/local/bind /etc/named/named.iscdlv.key /etc/named/named.root.key
    /etc/services /etc/protocols'
    
    for LIB in /lib /lib64
    do
      if [ -d "$ROOTDIR$LIB" ]; then
        for NSSLIB in "$LIB"/libnss_files.so.[1-9]
        do
          ROOTDIR_MOUNT+=" $NSSLIB"
        done
      fi
    done
    
    mount_chroot_conf()
    {
      if [ -n "$ROOTDIR" ]; then
        for all in $ROOTDIR_MOUNT; do
          # Skip nonexistant files
          [ -e "$all" ] || continue
    
          # If mount source is a file
          if ! [ -d "$all" ]; then
            # mount it only if it is not present in chroot or it is empty
            if ! [ -e "$ROOTDIR$all" ] || [ "`stat -c'%s' "$ROOTDIR$all"`" -eq 0 ]; then
              touch "$ROOTDIR$all"
              mount --bind "$all" "$ROOTDIR$all"
            fi
          else
            # Mount source is a directory. Mount it only if directory in chroot is
            # empty.
        if [ -e "$all" ] && [ "`ls -1A "$ROOTDIR$all" | wc -l`" -eq 0 ]; then
              mount --bind "$all" "$ROOTDIR$all"
        fi
          fi
        done
      fi
    }
    
    umount_chroot_conf()
    {
      if [ -n "$ROOTDIR" ]; then
        for all in $ROOTDIR_MOUNT; do
          # Check if file is mount target. Do not use /proc/mounts because detecting
          # of modified mounted files can fail.
          if mount | grep -q '.* on '"$ROOTDIR$all"' .*'; then
            umount "$ROOTDIR$all"
            # Remove temporary created files
            [ -f "$all" ] && rm -f "$ROOTDIR$all"
          fi
        done
      fi
    }
    
    check_pidfile() {
      PID="`pidofproc -p "$ROOTDIR$PIDFILE" "$named"`"
      if [ -n "$PID" ] && [ "`ps -p "$PID" --no-headers -o comm`" != "$named" ]; then
        rm -f $ROOTDIR$PIDFILE &> /dev/null
      fi
    }
    
    pidofnamed() {
        pidofproc -p "$ROOTDIR$PIDFILE" "$named";
    }
    
    # Check if all what named needs running
    start()
    {
      [ "$EUID" != "0" ] && exit 4
    
      # Source networking configuration.
      [ -r /etc/sysconfig/network ] && . /etc/sysconfig/network
    
      # Check that networking is up
      [ "${NETWORKING}" = "no" ] && exit 1
    
    
      [ -x /usr/local/bind/sbin/"$named" ] || exit 5
    
      if [ ! -s /etc/named/rndc.key -a ! -s /etc/named/rndc.conf ]; then
        # Generate rndc.key if doesn't exist AND there is no rndc.conf
        echo -n $"Generating /etc/named/rndc.key:"
        if /usr/local/bind/sbin/rndc-confgen -a -r /dev/urandom > /dev/null 2>&1; then
          chmod 640 /etc/named/rndc.key
          chown root.named /etc/named/rndc.key
          [ -x /sbin/restorecon ] && /sbin/restorecon /etc/named/rndc.key
          success $"/etc/named/rndc.key generation"
          echo
        else
          failure $"/etc/named/rndc.key generation"
          echo
        fi
      fi
    
      # Handle -c option
      previous_option='unspecified';
      for a in $OPTIONS; do
        if [ $previous_option = '-c' ]; then
          named_conf=$a;
        fi;
        previous_option=$a;
      done;
    
      named_conf=${named_conf:-/etc/named/named.conf};
    
      # check if named is running before mounting files/dirs
      echo -n $"Starting named: "
      check_pidfile
      if [ -n "`pidofnamed`" ]; then
        echo -n $"named: already running"
        success
        echo
        exit 0;
      fi;
    
      mount_chroot_conf
    
      if [ ! -r $ROOTDIR$named_conf ]; then
        echo 'Cannot find configuration file. You could create it by system-config-bind'
        exit 6;
      fi;
    
      [ -x /sbin/portrelease ] && /sbin/portrelease named &>/dev/null || :
    
      if ! [ "$DISABLE_ZONE_CHECKING" = yes ]; then
        ckcf_options='-z'; # enable named-checkzone for each zone (9.3.1+) !
      fi;
    
      if [ -n "${ROOTDIR}" -a "x${ROOTDIR}" != "x/" ]; then
        OPTIONS="${OPTIONS} -t ${ROOTDIR}"
        ckcf_options="$ckcf_options -t ${ROOTDIR}";
        [ -s /etc/localtime ] && cp -fp /etc/localtime ${ROOTDIR}/etc/localtime;
      fi
    
      RETVAL=0
      # check if configuration is correct
      if [ -x /usr/local/bind/sbin/named-checkconf ] && [ -x /usr/local/bind/sbin/named-checkzone ] && /usr/local/bind/sbin/named-checkconf $ckcf_options ${named_conf} >/dev/null 2>&1; then
    
        daemon --pidfile "$ROOTDIR$PIDFILE" /usr/local/bind/sbin/"$named" -u named ${OPTIONS};
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
          rm -f /var/named/{named,named-sdb}.pid;
          ln -s "$ROOTDIR$PIDFILE" /var/named/"$named".pid;
        fi;
    
      else
        named_err="`/usr/local/bind/sbin/named-checkconf $ckcf_options $named_conf 2>&1`";     
        echo
        echo "Error in named configuration:";
        echo "$named_err";
        failure
        echo
        [ -x /usr/bin/logger ] && echo "$named_err" | /usr/bin/logger -pdaemon.error -tnamed;
        umount_chroot_conf
        exit 2;
      fi;
      echo
      if [ $RETVAL -eq 0 ]; then
        touch /var/lock/subsys/named;
      else
        umount_chroot_conf
        exit 7;
      fi
      return 0;
    }
    
    stop() {
      [ "$EUID" != "0" ] && exit 4
    
      # Stop daemons.
      echo -n $"Stopping named: "
      check_pidfile
      [ -x /usr/local/bind/sbin/rndc ] && /usr/local/bind/sbin/rndc stop >/dev/null 2>&1;
      RETVAL=$?
      # was rndc successful?
      [ "$RETVAL" -eq 0 ] || \
        killproc -p "$ROOTDIR$PIDFILE" "$named" -TERM >/dev/null 2>&1
    
      timeout=0
      RETVAL=0
      while pidofnamed &>/dev/null; do
        if [ $timeout -ge $NAMED_SHUTDOWN_TIMEOUT ]; then
          RETVAL=1
          break
        else
          sleep 2 && echo -n "."
          timeout=$((timeout+2))
        fi;
      done
    
      umount_chroot_conf
    
      # remove pid files
      if [ $RETVAL -eq 0 ]; then
        rm -f /var/lock/subsys/named
        rm -f /var/named/{named,named-sdb}.pid
      fi;
    
      if [ $RETVAL -eq 0 ]; then
        success
      else
        failure
        RETVAL=1
      fi;
      echo
      return $RETVAL
    }
    
    
    rhstatus() {
      [ -x /usr/local/bind/sbin/rndc ] && /usr/local/bind/sbin/rndc status;
      check_pidfile
      status -p "$ROOTDIR$PIDFILE" -l named /usr/sbin/"$named";
      return $?
    }   
    
    restart() {
        stop
        start
    }
    
    reload() {
            [ "$EUID" != "0" ] && exit 
    
            echo -n $"Reloading "$named": "
        check_pidfile
        p=`pidofnamed`
        RETVAL=$?
        if [ "$RETVAL" -eq 0 ]; then 
            /usr/local/bind/sbin/rndc reload >/dev/null 2>&1 || /bin/kill -HUP $p;
            RETVAL=$?
            fi
        [ "$RETVAL" -eq 0 ] && success $"$named reload" || failure $"$named reload"
            echo
        return $RETVAL
    }
    
    checkconfig() {
        ckcf_options='-z';
        if [ -n "${ROOTDIR}" -a "x${ROOTDIR}" != "x/" ]; then
            ckcf_options="$ckcf_options -t ${ROOTDIR}";
            if ! [ -e "$ROOTDIR/$PIDFILE" ]; then
                mount_chroot_conf
            fi
        fi;
        if [ -x /usr/local/bind/sbin/named-checkconf ] && [ -x /usr/local/bind/sbin/named-checkzone ] && /usr/local/bind/sbin/named-checkconf $ckcf_options ${named_conf} ; then
            RETVAL=0
        else
            RETVAL=1
        fi
        if ! [ -e "$ROOTDIR/$PIDFILE" ]; then
            umount_chroot_conf
        fi
        return $RETVAL
    }
    
    # See how we were called.
    case "$1" in
        start)
            start
            ;;
        stop)
            stop
            ;;
        status)
            rhstatus;
            RETVAL=$?
            ;;
        restart)
            restart
            ;;
        condrestart|try-restart)
            if [ -e /var/lock/subsys/named ]; then restart; fi
                    ;;
        reload)
            reload
            ;;
        force-reload)
            if ! reload; then restart; fi
            ;;
        checkconfig|configtest|check|test)
            checkconfig
            RETVAL=$?
            ;;
        *)
                echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
            [ "x$1" = "x" ] && exit 0
            exit 2
    esac
    
    exit $RETVAL
    

    注意2 安装namedmanager相关

    https://github.com/jethrocarr/namedmanager/wiki/Installation-Git
    因为我们使用的是 /data/wwwroot/www 存放这个网页服务

    #导入数据库
    cd /data/wwwroot/www
    mysql ­-u root ­-p < sql/version_LATESTDATE_install.sql
    #添加数据库用户及密码(这个就不贴了 只要新增的用户能够管理上一步操作的数据库即可)
    
    #设置配置文件
    mkdir /etc/namedmanager/
    #依然在 /data/wwwroot/www 目录
    cp htdocs/include/sample-config.php /etc/namedmanager/config.php
    #这一步是为了方便管理 只需要修改/etc/namedmanager/config.php 内容即可保证web服务运行,具体内容上面有贴入
    ln -s /etc/namedmanager/config.php /data/wwwroot/www/htdocs/include/config-settings.php
    #以下是创建config-bind.php 和 上面的config.php 不是一回事 切记 切记!!!
    cp bind/include/sample-config.php /etc/namedmanager/config-bind.php
    ln -s /etc/namedmanager/config-bind.php /data/wwwroot/www/bind/include/config-settings.php
    

    注意3 定时脚本及任务

    在原来的官方文档中定时脚本是和[注意2]一起出现的,经过本人的安装经历,决定单独放出来

    #依然在 /data/wwwroot/www
    cp resources/namedmanager_logpush.rcsysinit /etc/init.d/namedmanager_logpush
    cp resources/namedmanager-www.cron /etc/cron.d/namedmanager-www
    cp resources/namedmanager-bind.cron /etc/cron.d/namedmanager-bind
    #以上需要修改对应的具体内容,主要是改路径
    mkdir /var/log/namedmanager
    chown www:www /var/log/namedmanager
    #创建日志路径及权限
    

    https://github.com/jethrocarr/namedmanager/wiki/Installation-Integration-Bind
    官方文档提过

    php -q /usr/share/namedmanager/bind/namedmanager_bind_configwriter.php
    

    改为

    php -q /data/wwwroot/www/bind/namedmanager_bind_configwriter.php
    #这也就是定时任务中会用到的语句,记得修改定时任务中的内容
    

    总结

    BIND安装容易,但是NamedManager去管理不容易。
    过程中出现了几点麻烦单独再提出以下:
    1.WEB程序的的脚本路径问题;
    2.PHP的函数权限问题,需要打开exec,fopen等;
    3.PHP函数的max_vars的问题 因为子域名很多默认是1000,超过1000就要去修改(搜索ini的max关键字);
    4.修改后自动同步并重启服务的问题,主要观察php日志或/var/log/message的相关日志(本人出现这个问题是因为config-bind.php没有指定rndc的路径即$config["bind"]["reload"]);
    5.发现无法同步是通过查看定时任务日志找到的原因,日志在 /var/log/cron。

    当前还有镜像 有需要可以留言联系。

    相关文章

      网友评论

          本文标题:搭建WEB管理环境(NamedManager)下的DNS服务器

          本文链接:https://www.haomeiwen.com/subject/xywcixtx.html