|
这几天看见个题目:有两台服务器,其中一台部署apache+php+nginx+discuz,另外一台单独跑mysql数据库,其中nginx监听80端口,负责跑静态网页,apache监听88端口,负责跑动态网页(php相关)并且由nginx代理。最后在A设备上安装一个mysql数据库与B设备上的数据库构成mysql主从架构。
实验环境:
1、VMware Workstation 10
2、真机IP:192.168.0.113
2、设备A:nginx+apache+php+discuz+mysql,IP地址:192.168.145.133,host:master1
3、设备B:
mysql,IP地址 192.168.145.134,host:master2
4、Linux发行版:Centos 6.5 x86_64;
5、nginx:nginx-1.8.0.tar.gz
6、apache:httpd-2.2.27.tar.bz2
7、php:php-5.6.12.tar.bz2
8、mysql:mysql-5.6.32-linux-glibc2.5-x86_64.tar.gz(B设备master)
mysql-5.1.73-linux-x86_64-glibc23.tar.gz(A设备slave)
9、discuz:Discuz_X3.2_SC_UTF8.zip
实验步骤:
1、在B设备上安装mysql数据库
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
| tar zxvf /usr/local/src/mysql-5.6.32-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.32-linux-glibc2.5-x86_64 /usr/local/mysql
useradd -s /sbin/nologin mysql
cd /usr/local/mysql
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql #数据库初始化
cp support-files/my-large.cnf /etc/my.cnf #拷贝数据库配置文件
cp support-files/mysql.server /etc/init.d/mysqld #拷贝数据库启动脚本
chmod 755 /etc/init.d/mysqld
vim /etc/init.d/mysqld #修改datadir=/usr/local/mysql
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
mysql -h127.0.0.1 -uroot #登陆数据库
>create database discuz; #创建discuz库
>grant all on discuz.* to 'xaioyuan'@'192.168.145.133' identified by '123456';
#这条语句是授权ip为192.168.145.133的xiaoyuan用户可以访问discuz库,访问密码为123456
>quit #退出数据库
|
2、在设备A上安装apache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| tar jxvf /usr/local/src/httpd-2.2.27.tar.bz2
cd /usr/loca/src/httpd-2.2.27
./configure \--prefix=/usr/local/apache2 \--with-included-apr \--enable-so \
--enable-deflate=shared \--enable-expires=shared \--enable-rewrite=shared
make & make install
cp /usr/local/apache2/bin/apachectl /etc/init.d/httpd #拷贝apache的启动脚本
vim /etc/init.d/httpd
在第一行#!/bin/sh下增加两行文字
# chkconfig: 35 70 30
# description: Apache
保存退出
chkconfig --level 35 httpd on
|
3、在设备A上安装php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| tar zxf /usr/local/src/php-5.6.12.tar.gz
cd php-5.6.12
./configure \--prefix=/usr/local/php \--with-apxs2=/usr/local/apache2/bin/apxs \
--with-config-file-path=/usr/local/php/etc \--with-mysql=mysqlnd \--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \--with-libxml-dir \--with-gd \--with-jpeg-dir \--with-png-dir \
--with-freetype-dir \--with-iconv-dir \--with-zlib-dir \--with-bz2 \--with-openssl \
--with-mcrypt \--enable-soap \--enable-gd-native-ttf \--enable-mbstring \--enable-sockets \
--enable-exif \--disable-ipv6
#在编译php的时候如果遇到反复报错,则应该观察编译选项是否在不该出现空格的时候出现了空格,如:
“\--with-mysql=mysqlnd \--with-mysqli=mysqlnd”写成了“\--with-mysql=mysqlnd \ --with
-mysqli=mysqlnd”。
make && make install
cp /usr/local/src/php-5.6.12/php.ini-production /usr/local/php/etc/php.ini
|
4、配置apache与php关联
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
| vim /usr/local/apache2/conf/httpd.conf
找到:AddType application/x-gzip .gz .tgz
在其下面添加:AddType application/x-httpd-php .php
找到:
<IfModule dir_module>
DirectoryIndex index.html
/IfModule>
将其改为:
<IfModule dir_module>
DirectoryIndex index.html index.htm index.php
</IfModule>
找到:#ServerName www.example.com:80 修改为:ServerName localhost:88
找到:listen:80 修改为:listen:88
找到:
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
改为:
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>
找到:#Include conf/extra/httpd-vhosts.conf 把最前面的#去掉
查看是否存在modules/libphp5.so
vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
#打开apache虚拟主机配置文件在最后添加:
<VirtualHost *:88>
DocumentRoot "/date/discuz/"
ServerName bbs.xiaoyua.com
ErrorLog "logs/bbs.xiaoyuan.com-error_log"
CustomLog "logs/bbs.xiaoyuan.com-access_log" common
</VirtualHost>
service httpd -t (检查错误)
service httpd graceful(加载配置)
查看httpd的运行情况
netstat -lnp | grep httpd
|
5、在设备A上安装nginx
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
| tar zxvf /usr/local/src/nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --prefix=/usr/local/nginx --with-pcre
make & make instal
vim /etc/init.d/nginx #编写启动脚本:
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart(){
stop
start
}
configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
将nginx服务启动:
chmod a+x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
/usr/local/nginx/conf/nginx.conf #编写nginx的配置文件:
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
'$host "$request_uri" $status'
'"$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm application/xml;
include /usr/local/nginx/conf/vhosts/*.conf;
mkdir -p /usr/local/nginx/conf/vhosts #创建nginx虚拟主机
vim /usr/local/nginx/conf/vhosts/bbs.conf ##配置nginx虚拟主机(discuz)
server
{
listen 80;
server_name bbs.chinaops.com;
index index.html index.htm index.php;
root /date/bbs;
#限制user_agent
if ($http_user_agent ~ 'bingbot/2.0|MJ12bot/v1.4.2|Spider/3.0|YoudaoBot|
Tomato|Gecko/20100315'){
return 403;
}
location ~ admin.php {
allow 192.168.0.113; ##只允许真机访问admin.php
deny all;
proxy_pass http://127.0.0.1:88;
proxy_set_header Host $host;
}
#代理apache
location ~ \.php$ {
proxy_pass http://127.0.0.1:88;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#设置静态缓存
location ~ .*\.(js|css)?$
{
expires 24h;
access_log off;
}
#设置防盗链
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ {
expires 7d;
valid_referers none blocked server_names *.baidu.com\
*.google.com *.google.cn *.soso.com ;
if ($invalid_referer) {
return 403;
#rewrite ^/ http://www.example.com/nophoto.gif;
}
access_log off;
}
rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
access_log /home/logs/discuz.log combined_realip;
}
|
5、在设备A上安装discuz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| mkdir /data/discuz
cd /data/discuz
wget
unzip Discuz_X3.2_SC_UTF8.zip
mv upload/* .
在浏览器中输入:bbs.xiaoyuan.com/install
cd /data/bbs
chown -R daemon.daemon config/ data/ uc_client/data/ uc_server/data/
按照提示的安装步骤安装即可。
|
6、搭建mysql主从
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
| # B机器的mysql为master,A机器上的mysql为slave
配置master:
vim /usr/local/mysql/my.cnf #修改或添加:
server-id=1
log-bin=mysql-bin
两个可选参数(2选1):
binlog-do-db=discuz #需要同步的库
binlog-ignore-db=db1,db2 #忽略不同步的库
修改配置文件后,重启mysql
mysql -h127.0.0.1 -uroot #登陆mysql数据库
>grant replication slave on *.* to 'repl'@'192.168.145.133' identified by '123123';
# 授权slave可以访问master
>flush tables with read lock;
>show master status; # 会用到前两列的内容
安装(和master步骤相同)和配置slave:
vim /etc/my.cnf #修改或增加
server-id = 2 #这个数值不能和主一样
两个可选参数(2选1):
replicate-do-db=discuz #需要同步的库
replicate-ignore-db=db1,db2 #忽略不同步的库
mysql -h127.0.0.1 -uroot #登陆mysql数据库
slave stop;
change master to master_host='192.168.145.134', master_port=3306, master_user='repl',
master_password='123123', master_log_file='mysql-bin.000006', master_log_pos=474952;
slave start;
主上: mysql -uroot -S /tmp/mysql2.sock -p123456 -e "unlock tables"
从上查看从的状态:show slave status\G
当看到 Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: discuz
则表示mysql主从搭建成功。更进一步的验证方法是在master的discuz库里创建一个表,然后去slave
查看此表是否出现在slave的discuz库。
|
总结:nginx处理静态文件能力很强,apache处理动态文件的能力不错而且很稳定,把二者综合起来效果会更好。
在刚拿到题目时我有一个困惑:我们平时搭建lnmp和lamp时编译php时都会直接指定mysql的路径,将php和mysql直接关联起来,而这次php和mysql不在同一台机器上,这该怎么办呢?
随后通过查资料打消了我的疑虑:原来从mysql5.3.0以后就有了mysqlnd驱动,编译php时直接加上mysqlnd就可以关联php和mysql了。
在搭建mysql主从时,由于master和slave采用的不同版本,导致在操作环境中一直报错:Slave can not handle replication events with the checksum that master is configured to log。这个错误一般出现在master5.6,slave在低版本的情况下。这是由于5.6使用了crc32做binlog的checksum。
解决方法:
1
2
3
4
5
| vim /usr/local/mysql/my.conf #打开master的配置文件
在最后一行添加:
binlog-checksum = none
|
|
|