|
前言: 用NFS、LAMP、BIND结合的方法,应对网络中访问量增大而导致服务器负载运行的情况。以实现Web服务器之间使用同一个MYSQL和相同的网页配置文件。
实验环境:
HostName | IP | 规划 | nfs.preferred.com | 192.168.1.6 | NFS | ns.preferred.com | 192.168.1.5 | DNS | mysql.preferred.com | 192.168.1.4 | MYSQL | www.preferred.com | 192.168.1.3 | WEB | www.preferred.com | 192.168.1.2 | WEB |
实验拓扑:
实验步骤: 一、搭建NFS 1
| 以下操作在nfs.preferred.com服务器上实现
|
1
2
3
| [iyunv@localhost ~]# yum install nfs-utils -y <---这里需要安装nfs这个包(基本的NFS命令与监控程序)
[iyunv@localhost ~]# rpm -qa | grep nfs-utils --color
nfs-utils-1.2.3-39.el6.x86_64
|
创建共享目录并配置nfs文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| [iyunv@localhost~]# mkdir /mydata
[iyunv@localhost~]# mkdir /webdata
[iyunv@localhost~]# cd /webdata/
[iyunv@localhost webdata]# unzip wordpress-3.2.1-zh_CN.zip &> /dev/null
[iyunv@localhost webdata]# chmod -R 777 wordpress
[iyunv@localhost webdata]# ls -l
drwxrwxrwx 5 root root 4096 Oct 11 2016 wordpress
-rw-r--r-- 1 root root 4372680 Oct 11 2016 wordpress-3.2.1-zh_CN.zip
[iyunv@localhost webdata]# vim /etc/exports
/mydata 192.168.1.4(rw,no_root_squash)
/webdata 192.168.1.2(rw,no_root_squash) 192.168.1.3(rw,no_root_squash)
[iyunv@localhost ~]# service rpcbind start
注:rpcbind是一个RPC服务,主要是在nfs共享的时候负责通知客户端、服务器nfs端口号。
[iyunv@localhost ~]# service nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
Starting RPC idmapd: [ OK ]
[iyunv@localhost ~]# showmount -e 192.168.1.6 <---查看当前共享的目录
Export list for 192.168.1.6:
/webdata 192.168.1.3,192.168.1.2
/mydata 192.168.1.4
|
二、搭建DNS
1
| 以下操作在ns.preferred.com服务器上实现
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
| [iyunv@localhost ~]# yum install bind -y | tail -n 5
Installed:
bind.x86_64 32:9.8.2-0.47.rc1.el6_8.1
Complete!
[iyunv@localhost ~]# vim /etc/named.rfc1912.zones <---添加zone(正向与反向的)
zone "preferred.com" IN {
type master;
file "preferred.com.zone";
};
zone "1.168.192.in-addr.arpa" IN {
type master;
file "192.168.1.5.zone";
};
[iyunv@localhost ~]# vim /var/named/preferred.com.zone <---配置DNS正向区域解析
$TTL 86400
$ORIGIN preferred.com.
@ IN SOA ns.preferred.com admin.preferred.com (
2016101001
1D
5M
7D
1D )
IN NS ns
ns IN A 192.168.1.5
www IN A 192.168.1.2
www IN A 192.168.1.3
nfs IN A 192.168.1.6
[iyunv@localhost ~]# vim /var/named/192.168.1.5.zone <---配置DNS反向区域解析
$TTL 86400
@ IN SOA ns.preferred.com. admin.preferred.com (
2016101001
1H
5M
7D
1D )
IN NS ns.preferred.com.
5 IN PTR ns.preferred.com.
2 IN PTR www.preferred.com.
3 IN PTR www.preferred.com.
6 IN PTR nfs.preferred.com.
[iyunv@localhost ~]# systemctl restart named
[iyunv@localhost ~]# dig -t A www.preferred.com @192.168.1.5 <---测试DNS能否正向解析
; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.4 <<>> -t A www.preferred.com @192.168.1.5
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23764
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.preferred.com. IN A
;; ANSWER SECTION:
www.preferred.com. 86400 IN A 192.168.1.2
www.preferred.com. 86400 IN A 192.168.1.3
;; AUTHORITY SECTION:
preferred.com. 86400 IN NS ns.preferred.com.
;; ADDITIONAL SECTION:
ns.preferred.com. 86400 IN A 192.168.1.5
;; Query time: 0 msec
;; SERVER: 192.168.1.5#53(192.168.1.5)
;; WHEN: Tue Oct 11 04:22:36 EDT 2016
;; MSG SIZE rcvd: 111
[iyunv@localhost ~]# dig -x 192.168.1.2 @192.168.1.5 <---测试DNS能否反向解析
; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.4 <<>> -x 192.168.1.2 @192.168.1.5
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21950
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;2.1.168.192.in-addr.arpa. IN PTR
;; ANSWER SECTION:
2.1.168.192.in-addr.arpa. 86400 IN PTR www.preferred.com.
;; AUTHORITY SECTION:
1.168.192.in-addr.arpa. 86400 IN NS ns.preferred.com.
;; ADDITIONAL SECTION:
ns.preferred.com. 86400 IN A 192.168.1.5
;; Query time: 0 msec
;; SERVER: 192.168.1.5#53(192.168.1.5)
;; WHEN: Tue Oct 11 04:23:57 EDT 2016
;; MSG SIZE rcvd: 117
|
三、搭建MYSQL
1
| 以下操作在mysql.preferred.com服务器上实现
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
| [iyunv@localhost ~]# mkdir /mydata/
[iyunv@localhost ~]# mount -t nfs 192.168.1.6:/mydata/ /mydata/ <---挂载共享目录
[iyunv@localhost ~]# groupadd -r mysql <---创建mysql组和用户
[iyunv@localhost ~]# useradd -r -g mysql -s /sbin/nologin mysql
[iyunv@localhost ~]# cd /usr/local/
[iyunv@localhost local]# ln -sv /root/mariadb-5.5.43-linux-x86_64 mysql
"mysql" -> "/root/mariadb-5.5.43-linux-x86_64"
[iyunv@localhost local]# cd mysql/
[iyunv@localhost mysql]# ls
bin COPYING.LESSER EXCEPTIONS-CLIENT INSTALL-BINARY man README share support-files
COPYING data include lib mysql-test scripts sql-bench
[iyunv@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/mydata/ <---初始化数据库
Installing MariaDB/MySQL system tables in '/mydata' ...
161010 13:33:59 [Note] ./bin/mysqld (mysqld 5.5.43-MariaDB) starting as process 2183 ...
OK
Filling help tables...
161010 13:33:59 [Note] ./bin/mysqld (mysqld 5.5.43-MariaDB) starting as process 2191 ...
OK
...
[iyunv@localhost mysql]# ls /mydata/ <--查看初始后生成的数据
aria_log.00000001 aria_log_control mysql performance_schema test
[iyunv@localhost mysql]# mkdir /etc/mysql/
[iyunv@localhost mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf
[iyunv@localhost mysql]# vim /etc/mysql/my.cnf <---修改配置文件,添加以下三行
datadir = /mydata
innodb_file_per_table = on
skip_name_resolve = on
[iyunv@localhost mysql]# service mysqld start
Starting MySQL... [确定]
[iyunv@localhost mysql]# ss -tnl | grep 3306 <---服务端口已经开启
LISTEN 0 50 *:3306 *:* *:*
[iyunv@localhost mysql]# mysql
-bash: mysql: command not found
注:尝试登陆MYSQL却报错,不要紧张。这是因为/usr/local/bin目录下确实mysql导致,只需要建立一个软连接即可解决。
[iyunv@localhost ~]# ln -sv /usr/local/mysql/bin/mysql /usr/bin/
"/usr/bin/mysql" -> "/usr/local/mysql/bin/mysql"
[iyunv@localhost ~]# mysql <---连接数据库创建用户密码与授权
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 115
Server version: 5.5.43-MariaDB-log MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE pre;
Query OK, 1 rows affected (0.00 sec)
MariaDB [(none)]> GRANT ALL ON pre.* TO 'jds'@'%' IDENTIFIED BY 'redhat'; <---创建用户并赋予权限
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
|
四、搭建WEB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| [iyunv@localhost ~]# yum install httpd php-mysql php -y | tail -n 10
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Importing GPG key 0xC105B9DE:
Userid: CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
Package: centos-release-6-5.el6.centos.11.1.x86_64 (@anaconda-CentOS-201311272149.x86_64/6.5)
From: /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
php-cli.x86_64 0:5.3.3-48.el6_8 php-common.x86_64 0:5.3.3-48.el6_8
php-pdo.x86_64 0:5.3.3-48.el6_8
Updated:
httpd.x86_64 0:2.2.15-54.el6.centos
Dependency Updated:
httpd-tools.x86_64 0:2.2.15-54.el6.centos
Complete!
[iyunv@localhost ~]# mkdir /webdata
[iyunv@localhost ~]# vim /etc/httpd/conf/httpd.conf <---添加以下几行
ServerName www.preferred.com:80
<VirtualHost 192.168.1.2:80>
ServerName www.preferred.com
DocumentRoot /webdata/wordpress
</VirtualHost>
[iyunv@localhost ~]# service rpcbind start
[iyunv@localhost ~]# mount -t nfs 192.168.1.6:/webdata /webdata
[iyunv@localhost ~]# ls /webdata/
wordpress wordpress-3.2.1-zh_CN.zip
[iyunv@localhost ~]# service httpd start
Starting httpd: [ OK ]
|
接下来安装wordpress,没有的话可以自己去下载一个https://wordpress.org/download/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| [iyunv@localhost ~]# cd /webdata/wordpress
[iyunv@localhost wordpress]# ls
index.php wp-app.php wp-config.php wp-includes wp-pass.php wp-settings.php
license.txt wp-atom.php wp-config-sample.php wp-links-opml.php wp-rdf.php wp-signup.php
readme.html wp-blog-header.php wp-content wp-load.php wp-register.php wp-trackback.php
wp-activate.php wp-comments-post.php wp-cron.php wp-login.php wp-rss2.php xmlrpc.php
wp-admin wp-commentsrss2.php wp-feed.php wp-mail.php wp-rss.php
[iyunv@localhost wordpress]# cp wp-config-sample.php wp-config.php <---这是一个示例配置文件,更名并配置
[iyunv@localhost wordpress]# vim wp-config.php
// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress 数据库的名称 */
define('DB_NAME', 'pre'); <---连接数据库所使用的,与刚刚在mysq中创建的一致,以下几个都是。
/** MySQL 数据库用户名 */
define('DB_USER', 'jds');
/** MySQL 数据库密码 */
define('DB_PASSWORD', 'redhat');
/** MySQL 主机 */
define('DB_HOST', '192.168.1.4');
/** 创建数据表时默认的文字编码 */
define('DB_CHARSET', 'utf8');
/** 数据库整理类型。如不确定请勿更改 */
define('DB_COLLATE', '');
[iyunv@localhost ~]# mysql -ujds -predhat -h 192.168.1.4 <---尝试登陆
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.4' (113)
注:遇到这个问题怎么个人都不好了,在MYSQL服务器上取消防火墙也不行。所以尝试在MYSQL服务器的防火墙上加上端口的允许。
[iyunv@localhost ~]# iptables -L -n | grep 3306
[iyunv@localhost ~]# iptables -I INPUT -s 0/0 -p tcp --dport 3306 -j ACCEPT
[iyunv@localhost mysql]# iptables -L -n | grep 3306
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306
[iyunv@localhost ~]# mysql -ujds -predhat -h 192.168.1.4 <---尝试再次登陆
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 116
Server version: 5.5.43-MariaDB-log MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| pre | <---之前创建的数据库可以访问
| test |
+--------------------+
5 rows in set (0.00 sec)
|
测试访问正常!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| [iyunv@localhost ~]# yum install httpd php-mysql php -y
[iyunv@localhost ~]# mkdir /webdata
[iyunv@localhost ~]# vim /etc/httpd/conf/httpd.conf <---添加以下几行
ServerName www.preferred.com:80
<VirtualHost 192.168.1.3:80>
ServerName www.preferred.com
DocumentRoot /webdata/wordpress
</VirtualHost>
[iyunv@localhost ~]# service rpcbind start
[iyunv@localhost ~]# mount -t nfs 192.168.1.6:/webdata /webdata
[iyunv@localhost ~]# ls /webdata/
wordpress wordpress-3.2.1-zh_CN.zip
[iyunv@localhost ~]# service httpd start
Starting httpd:
[ OK ]
|
1
| 先用WEB(192.168.1.3)进行测试。发布一条文章
|
1
| 用WEB(192.168.1.2)进行测试。现在访问看看
|
看到了在WEB(192.168.1.3)上发布的文章,我们在WEB(192.168.1.2)上也发布一篇
1
| 最后我们在WEB(192.168.1.3)上查看
|
至此,我们实验的目的已经达到。
最后:
这是本人结合学习所写出来的一篇博客。勉强供小白学习借鉴,不喜勿喷。如果大家有什么疑问可以给我留言,有大神发现哪有错误也欢迎指出!
|
|