bind9的初步使用(1)

本文首发于我的博客:bind9的初步使用(1)前言周五把自己的电脑重装了一下,还是使用的经典的windows+vmware+ubuntu的经典方式(对我来说)。但是我不想每次都修改host文件来实现我的域名访问,所以我在想有没有一个更好的方式,可以让我实现域名映射。这个时候我想到了自己架设一个dns服务器。说干就干,我就准备用dns的开源系统bind9来搞一番。环境介绍Ubuntu: 18.10 (ip: 192.168.1.230)bind9: 9.11.4Windows 10 (ip: 192.168.1.230)安装其实安装非常简单,一条命令就搞定了sudo apt install bind9管理命令启动:sudo systemctl start bind9停止:sudo systemctl stop bind9重启:sudo systemctl restart bind9状态:sudo systemctl status bind9配置域名举个例子,比如现在我们有个域名是:baoguoxiao.pro。现在我们要对这个域名进行虚拟映射。首先打开/etc/bind/named.conf.local,追加如下内容到文件尾部:zone “baoguoxiao.pro” { type master; file “/etc/bind/zones/baoguoxiao.pro.db”;};那么现在这个文件的内容完整如下://// Do any local configuration here//// Consider adding the 1918 zones here, if they are not used in your// organization//include “/etc/bind/zones.rfc1918”;zone “baoguoxiao.pro” { type master; file “/etc/bind/zones/baoguoxiao.pro.db”; // 这个文件定义了文件地址};我们定义的地址是/etc/bind/zones/baoguoxiao.pro.db。但是我们的/etc/bind/并没有该目录。所以需要如下命令进行创建:cd /etc/bind/sudo mkdir zones进入该目录:cd zones然后创建该文件baoguoxiao.pro.db,并追加如下命令:; BIND data file for baoguoxiao.pro;$TTL 14400@ IN SOA ns1.baoguoxiao.pro. host.baoguoxiao.pro. (201006601 ; Serial7200 ; Refresh120 ; Retry2419200 ; Expire604800) ; Default TTL;baoguoxiao.pro. IN NS ns1.baoguoxiao.pro. ;baoguoxiao.pro. IN A 192.168.1.231 ns1 IN A 192.168.1.231www IN A 192.168.1.231这样就设置完成了。然后我们将bind9进行重启。测试DNS效果$ dig @192.168.1.231 www.baoguoxiao.pro; <<>> DiG 9.11.4-3ubuntu5-Ubuntu <<>> @192.168.1.231 www.baoguoxiao.pro; (1 server found);; global options: +cmd;; Got answer:;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 35630;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2;; OPT PSEUDOSECTION:; EDNS: version: 0, flags:; udp: 4096; COOKIE: f077ba72f04b75a1ac9b27275c16148f4732abac11c21ce8 (good);; QUESTION SECTION:;www.baoguoxiao.pro. IN A;; ANSWER SECTION:www.baoguoxiao.pro. 14400 IN A 192.168.1.231;; AUTHORITY SECTION:baoguoxiao.pro. 14400 IN NS ns1.baoguoxiao.pro.;; ADDITIONAL SECTION:ns1.baoguoxiao.pro. 14400 IN A 192.168.1.231;; Query time: 0 msec;; SERVER: 192.168.1.231#53(192.168.1.231);; WHEN: Sun Dec 16 17:02:07 CST 2018;; MSG SIZE rcvd: 117好了,这样就表示已经配置成功了设置默认本机DNS可用之前的设置我们需要指定本机的DNS服务器才可以使用,如果我们不指定的话,那么查询该域名是没有效果的:$ dig baoguoxiao.pro; <<>> DiG 9.11.4-3ubuntu5-Ubuntu <<>> baoguoxiao.pro;; global options: +cmd;; Got answer:;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 52385;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1;; OPT PSEUDOSECTION:; EDNS: version: 0, flags:; udp: 65494;; QUESTION SECTION:;baoguoxiao.pro. IN A;; Query time: 274 msec;; SERVER: 127.0.0.53#53(127.0.0.53);; WHEN: Sun Dec 16 17:03:59 CST 2018;; MSG SIZE rcvd: 43在ubuntu17.10之后,网卡配置已经更新为netplan。该配置文件的目录是/etc/netplan/。不过里面的文件不一定是相同的名字。我的文件打开是这样的。$ cat /etc/netplan/50-cloud-init.yaml # This file is generated from information provided by# the datasource. Changes to it will not persist across an instance.# To disable cloud-init’s network configuration capabilities, write a file# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:# network: {config: disabled}network: ethernets: ens33: addresses: [192.168.1.231/24] dhcp4: false dhcp6: false gateway4: 192.168.1.1 nameservers: addresses: [192.168.1.231,114.114.114.114] version: 2注意,我在倒数第二行的数组里面添加本机的服务器192.168.1.231。关于该文件的配置,可以查看我的另外一篇文章:UBUNTU17.10设置固态IP。这样我们在本机就可以不指定dns服务器的基础上进行获取域名的ip了。$ dig www.bgx.me; <<>> DiG 9.11.4-3ubuntu5-Ubuntu <<>> www.baoguoxiao.pro;; global options: +cmd;; Got answer:;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 58219;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2;; OPT PSEUDOSECTION:; EDNS: version: 0, flags:; udp: 4096; COOKIE: 15d0881d8eed3292569558cd5c1623fa33a2d05212e7e662 (good);; QUESTION SECTION:;www.baoguoxiao.pro. IN A;; ANSWER SECTION:www.baoguoxiao.pro. 14400 IN A 192.168.1.231;; AUTHORITY SECTION:baoguoxiao.pro. 14400 IN NS ns1.baoguoxiao.pro.;; ADDITIONAL SECTION:ns1.baoguoxiao.pro. 14400 IN A 192.168.1.231;; Query time: 0 msec;; SERVER: 192.168.1.231#53(192.168.1.231);; WHEN: Sun Dec 16 18:07:54 CST 2018;; MSG SIZE rcvd: 117本篇文章就说到这里。下一篇讲如何配置可局域网访问。 ...

December 17, 2018 · 2 min · jiezi

Debian9(Stretch) 下编译安装LNMP环境

Debian9下源码安装LNMP一、前言之前,我的开发环境是Windows-10+PHP-7.1+Nginx-1.10+MariaDB-10.1。后面开发需要使用到memcached,redis等nosql比较多,而在Windows下定制难度,很多PHP拓展并没有.dll文件,且PHP拓展在Windows下compile难度还是比较大的。所以促使我转向Linux下开发。首先,我search了一下,主要是Red Hat 与Debian。基于Red Hat:商业版,Centos,Fedora基于Debian: Debian,Ubuntu我选择了Debian 9,PHP-7.2,MariaDB-10.2,Nginx-1.13二、Requirements一般安装顺序,mariadb > nginx > php,以下的涉及的软件,库名均是基于Debian(Ubuntu)。2.1 PHP的需要的额外库:## 源码需要的词法分析器apt install bison## 源码都是c程序,需要c编译器,注意编译器版本apt install gcc-6## C++编译器apt install g++## xml解析库apt install libxml2 libxml2-dev## make cmake m4 autoconfapt install make cmake m4 autoconf## webp 格式,能够带来更小体积的图片apt install libwebp6 libwebp-dev## jpeg格式支持apt install libjpeg-dev## png格式支持apt install libpng-dev## 免费开源字体引擎apt install libfreetype6 libfreetype6-dev## ssl加密库支持(源码安装openssl,可以选择使用Debian 包来安装openssl)apt install openssl## ssh2 库(源码安装)apt install libssh2-1-dev## mhash 库apt install libmhash2## zlib 压缩库(源码安装)apt install zlib1g zlib1g-dev## pcre 正则表达式库(源码安装)apt install libpcre3-dev libpcre3## gzipapt install gzip## bz2apt install libbz2-1.0 libbz2-dev## soduim php7.2新特性 现代加密标准apt install libsodium-dev## argon2 php7.2新特性 新的加密函数,由PHC(Password Hashing Competition)发布apt install argon2 libargon2-0 libargon2-0-dev2.2 Nginx 需要的额外库主要是三个,openssl,zlib,pcre,可以通过Debian本身的库安装,也可以选择源码安装。我选择后者,所以,并不会与上面的冲突,后面会涉及到原因。2.3 MariaDB 需要的额外库## bison词法分析器apt install bison## libncurses 一个可用于编写独立终端基于文本的的程序库apt install libncurses5 libncurses5-dev## libevent-dev 一个事件库apt install libevent-dev## openssl 一个加密库apt install openssl三、 安装过程按照MariaDB > Nginx > PHP的顺序安装,安装前请再次检查上述所需的额外库都已安装。3.1 对应的系统用户创建为什么要创建用户? 答:因为安装完成后,我们只需要这些程序只用于系统服务就好(daemon或者其他自己运行的进程),并不需要使用一个具体用户身份去操作他。即创建系统账户,以及系统用户组。groupadd -r mysqluseradd -r -g mysql -s /bin/false -M mysqlmkdir /usr/local/data/mysqlchown -R mysql:mysql /usr/local/data/mysql note 参数含义通过man groupadd 或者man useradd 可以调出具体的手册-r 创建系统用户或者用户组-g 指定用户所属用户组-s 指定用户登录shell名字,sh,bash,因为是系统用户,并不需要,设置 /bin/false或者/usr/sbin/nologin-M 不创建用户主目录同样,分别创建nginx,php-fpmgroupadd -r php-fpmuseradd -r -g php-fpm -s /bin/false -M php-fpmgroupadd -r nginxuseradd -r -g nginx -s /bin/false -M nginx 3.2 MariaDBMariaDB 安装可能略显麻烦,并不是常见的make方式,而是cmake方式。获取mariadb-10.2源码wget http://mirror.jaleco.com/mariadb//mariadb-10.2.12/source/mariadb-10.2.12.tar.gz tar -zxvf mariadb-10.2.12.tar.gzmkdir build-mariadbcd build-mariadbcmake ../ -DCMAKE_INSTALL_PREFIX=/opt/soft/mariadb-10.3.4 -DMYSQL_DATADIR=/var/data/mysql -DSYSCONFDIR=/etc -DWITHOUT_TOKUDB=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STPRAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWIYH_READLINE=1 -DWIYH_SSL=system -DVITH_ZLIB=system -DWITH_LOBWRAP=0 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DBUILD_LIBPROTOBUF_FROM_SOURCES=ONmake && make install 如果失败 使用 rm -rf CMakeCache.txt3.2.1 配置MariaDBvim /etc/profile.d/mariadb.shaddexport PATH=$PATH:/opt/soft/mariadb-10.2/binsource /etc/profile.d/mariadb.shcd /opt/soft/mariadb-10.2scripts/mysql_install_db –user=mysql –datadir=/usr/local/data/mysql成功输出信息:Installing MariaDB/MySQL system tables in ‘/data/mysql’ …OKTo start mysqld at boot time you have to copysupport-files/mysql.server to the right place for your systemPLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !To do so, start the server, then issue the following commands:’./bin/mysqladmin’ -u root password ’new-password’’./bin/mysqladmin’ -u root -h localhost.localdomain password ’new-password’Alternatively you can run:’./bin/mysql_secure_installation’which will also give you the option of removing the testdatabases and anonymous user created by default. This isstrongly recommended for production servers.See the MariaDB Knowledgebase at http://mariadb.com/kb or theMySQL manual for more instructions.You can start the MariaDB daemon with:cd ‘.’ ; ./bin/mysqld_safe –datadir=’/data/maria’You can test the MariaDB daemon with mysql-test-run.plcd ‘./mysql-test’ ; perl mysql-test-run.plPlease report any problems at http://mariadb.org/jiraThe latest information about MariaDB is available at http://mariadb.org/.You can find additional information about the MySQL part at:http://dev.mysql.comConsider joining MariaDB’s strong and vibrant community:https://mariadb.org/get-involved/复制cd /opt/soft/mariadb-10.2cp support-files/my-large.cnf /etc/my.cnf或者cp support-files/my-large.cnf /etc/mysql/my.cnf创建系统启动脚本(使用systemd)cd /etc/systemd/systemvim mysqld.service [Unit]Description=MariaDB ServerAfter=network.target[Service]ExecStart=/opt/soft/mariadb-10.2/bin/mysqld –defaults-file=/etc/mysql/my.cnf –datadir=/usr/local/data/mysql –socket=/tmp/mysql.sockUser=mysqlGroup=mysqlWorkingDirectory=/opt/soft/mariadb-10.2[Install]WantedBy=multi-user.targetsystemctl daemon-reloadsystemctl restart mysqld.servicesystemctl status mysqld.servie 如果没有启动,请使用journalctl -xn 或者 journalctl -xl来查看错误信息如果想开机启动,请使用systemctl enable mysqld.service安全设置$:mysql_secure_installation Enter current password for root (enter for none): 输入当前root密码(没有输入)Set root password? [Y/n] 设置root密码?(是/否)New password: 输入新root密码Re-enter new password: 确认输入root密码Password updated successfully! 密码更新成功By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created forthem. This is intended only for testing, and to make the installationgo a bit smoother. You should remove them before moving into aproduction environment.默认情况下,MariaDB安装有一个匿名用户,允许任何人登录MariaDB而他们无需创建用户帐户。这个目的是只用于测试,安装去更平缓一些。你应该进入前删除它们生产环境。Remove anonymous users? [Y/n] 删除匿名用户?(是/否)Normally, root should only be allowed to connect from ’localhost’. Thisensures that someone cannot guess at the root password from the network.通常情况下,root只应允许从localhost连接。这确保其他用户无法从网络猜测root密码。Disallow root login remotely? [Y/n] 不允许root登录远程?(是/否)By default, MariaDB comes with a database named ’test’ that anyone canaccess. This is also intended only for testing, and should be removedbefore moving into a production environment.默认情况下,MariaDB提供了一个名为“测试”的数据库,任何人都可以访问。这也只用于测试,在进入生产环境之前应该被删除。Reloading the privilege tables will ensure that all changes made so farwill take effect immediately.重新加载权限表将确保所有到目前为止所做的更改将立即生效。Reload privilege tables now? [Y/n] 现在重新加载权限表(是/否)All done! If you’ve completed all of the above steps, your MariaDBinstallation should now be secure.全部完成!如果你已经完成了以上步骤,MariaDB安装现在应该安全。Thanks for using MariaDB!至此,mariaddb已经安装完成,可以使用 ps -aux | grep mysql 查看服务现在测试一下,mysql -u root -p 或者 mysql -h localhost -P 5001 -u shanechiu -p 3.3 PHP 安装PHP 安装比较简单,主要是选择你要安装的拓展或者需要开启的功能可以使用./configure –help 来浏览源码安装提供的安装选项有些属于PHP内置的功能,你只需要 enable或者disable,比如php-fpm,是需要启用的。有些拓展是可以动态加载的,称之为shared extension,但是官方也说了,并不是所有的拓展都是能够shared.获取源码:wget http://am1.php.net/distributions/php-7.2.1.tar.bz2解压:tar -xvf php-7.2.1.tar.bz2cd php-7.2.1./configure –prefix=/opt/soft/php7.2 --with-config-file-path=/opt/soft/php7.2/etc --with-mysql-sock=/tmp/mysql.sock --with-openssl --with-mhash --with-mysqli=shared,mysqlnd --with-pdo-mysql=shared,mysqlnd --with-pdo-pgsql=/opt/soft/pgsql --with-gd --with-iconv --with-zlib --enable-exif --enable-intl --enable-calendar --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop --enable-mbregex --enable-mbstring --enable-ftp --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-sockets --enable-ipv6 --with-bz2 --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session --with-curl=/opt/soft/curl7.57–enable-debug --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-opcache --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-sodium --with-libxml-dir --with-password-argon2 --without-gdbm --with-pcre-regex --with-pcre-jit --enable-fast-install --enable-fileinfo配置进入源码文件,cp php.ini.development /opt/soft/php-7.2/php.ini修改以下部分extension_dir=/opt/soft/php-7.2/lib/php/extensions/no-debug-non-zts-20170718/extension=mysqlitime_zone=PRC同时要添加php-fpm配置文件,安装目录下 etc/ 下 cp php-fpm.conf.default php-fpm.conf 和 cp php.conf.d/www.conf.default php.conf.d/www.confPHP-FPM启动脚本(systemd)PHP 非常人性化,在源码目录下/sapi/fpm下可以找到php-fpm.service文件,复制到/etc/systemd/system/php-fpm.service中systemdctl start php-fpm.servicesystemdctl status php-fpm.service如果发生错误,使用journalctl -xn查看具体错误信息开机启动,sytemctl enable php-fpm.service3.4 Nginx 源码安装Nginx的编译安装难易程度应该是LNMP环境中最简单的首先需要三个源码包,一个zlib(压缩库),一个pcre(正则表达式库),一个openssl(加密库,如果要使用HTTPS,这个库是必须的),当然你如果是通过debian本身的包管理器安装的,这个可以省略,但是一定要安装两个,一个是软件本身,同时还要安装开发库,像这种,apt -y install openssl opensll-dev。命令:–configure –prefix=/opt/soft/nginx --user=nginx --group=nginx --with-http_ssl_module \ # 这个默认是不开启的,如需使用TLS,请带上这一项编译。–with-pcre=../pcre-8.41 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.1.0g 然后,make 和 make install注意,如果是使用二进制包安装了zlib,pcre,openssl,及相应的开发库,不需要指定路径。配置:编写nginx守护进程文件,还是利用systemd工具vim /etc/sytemd/system/nginx.service[Unit]Description=The NGINX HTTP and reverse proxy serverAfter=syslog.target network.target remote-fs.target nss-lookup.target[Service]Type=forkingPIDFile=/opt/soft/nginx/logs/nginx.pidExecStartPre=/opt/soft/nginx/sbin/nginx -tExecStart=/opt/soft/nginx/sbin/nginxExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s QUIT $MAINPIDPrivateTmp=true[Install]WantedBy=multi-user.target这个可以在nginx 官网找到,可以按照自己需求修改。注意路径修改成自己的安装路径。systemctl start nginx.service 启动Nginxsystemctl enable nginx.service 开机启动记得,如果中途修改了service文件,一定要先运行systemctl daemon-reload重新加载守护进程文件。然后运行 systemctl start nginx.service重启服务。四、APPEND后续会添加一键安装脚本。五、参考资料systemd 入门教程CentOS7.3编译安装MariaDB10.2.6CentOS7.3编译安装php7.1GNU bisonGD-support configure PHPArgon2The Sodium crypto library (libsodium)")get the mariadb code,buildit,test itGeneric Build InstructionsInstalling System Tables (mysql_install_db)")“Compiling MariaDB From Source"ncursesCMakephp-manulPHP7.2 NEW FEATUREBuilding nginx from Sources ...

November 9, 2018 · 4 min · jiezi

Ubuntu下配置NFS

什么是NFSNFS(Network FileSystem)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。NFS组成NFS有一个服务端和若干个客户端组成NFS作用可以将一些文件放到远程,节省本地存储空间在其他服务器上也能访问相同的文件,可解决负载均衡中,文件同步问题文件便于集中管理,备份服务端安装及配置// 安装服务端sudo apt install nfs-kernel-server// 修改配置sudo vi /etc/exports// 重启sudo service nfs-kernel-server restart配置说明// ip不限/var/data *(rw,sync,no_root_squash)//目录 ip(权限) ip(权限)/var/data 127.0.0(rw,sync,no_root_squash) 127.0.0(rw,sync,no_root_squash)rw 可读写的权限ro 只读的权限no_root_squash 登入NFS主机,使用该共享目录时相当于该目录的拥有者,如果是root的话,那么对于这个共享的目录来说,他就具有root的权限,这个参数『极不安全』,不建议使用all_squash 不论登入NFS的使用者身份为何,他的身份都会被压缩成为匿名使用者,通常也就是nobodyanonuid 可以自行设定这个UID的值,这个UID必需要存在于你的/etc/passwd当中anongid 同anonuid,但是变成groupID就是了sync 资料同步写入到内存与硬盘当中 async 资料会先暂存于内存当中,而非直接写入硬盘 insecure 允许从这台机器过来的非授权访问配置生效,无需重启sudo exportfs -rvexportfs命令常用选项-a 全部挂载或者全部卸载-r 重新挂载-u 卸载某一个目录-v 显示共享目录showmount// 显示NFS服务器上所有的共享目录showmount -e// 仅显示已被NFS客户端加载的目录showmount -a原文地址 https://www.unix.com/man-page…固定mountd端口系统 RPC服务在 nfs服务启动时默认会为 mountd动态选取一个随机端口(32768–65535)来进行通讯,我们可以通过编辑/etc/services 文件为 mountd指定一个固定端口# vi /etc/services在末尾添加 mountd 端口号/udpmountd 端口号/tcpNFS的守护进程rpc.nfsd:它是基本的NFS守护进程,主要功能是管理客户端是否能够登录服务器rpc.mountd:它是RPC安装守护进程,主要功能是管理NFS的文件系统。当客户端顺利通过rpc.nfsd登录NFS服务后,在使用NFS服务所提供的文凭前,还必须通过文件使用权限的验证。它会读取NFS的配置文件/etc/exports来对比客户端权限。portmap:portmap的主要功能是进行端口映射工作。当客户端尝试连接并使用RPC服务器提供的服务(如NFS服务)时,portmap会将所管理的与服务对应的端口提供给客户端,从而使客户可以通过该端口向服务器请求服务。查看 rpc进程rpcinfo -p 防火墙配置阿里云有虚拟防火墙,需要配置安全组,如图客户端配置安装apt-get install nfs-common挂载showmount -e 服务端ip// 查看服务端共享目录,若超时了,可能防火墙有问题,需要开放相应端口mount -t nfs 服务端ip:共享目录 挂载目录挂载命令详细说明查看所有挂载df -h

October 7, 2018 · 1 min · jiezi