实验环境:CentOS 6.5 64bit
Apache+PHP 一台服务器192.168.70.129
MySQL Master 一台服务器192.168.70.133
MySQL Slave 一台服务器192.168.70.134
实验目的:
1.源码安装LAMP
2.配置虚拟主机分别访问discuz和blog
3.配置主从同步备份服务器,master突然down机,手动配置slave接替Master工作
实验步骤:
注:需先关闭防火墙和SELinux/etc/init.d/iptables stop
chkconfig iptables off
setenforce 0 --用于临时关闭selinux
vi /etc/selinux/config
将SELINUX=enforcing
改成SELINUX=disabled
## (一)源码安装LAMP之Apache+PHP:
1)Apache源码安装:
yum install apr-devel arp-util-devel -y;
cd /usr/src
wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.2.29.tar.gz
wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.2.29.tar.gz
最新tar包
http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.12.tar.gz
tar zxf httpd-2.2.29.tar.gz
./configure --prefix=/usr/local/apache --enable-so --enable-rewrite
报错提示1: 执行./configure报错:
configure: error: Cannot use an external APR with the bundled APR-util
原因分析:
apache2.0.x与apache2.2.x在apr有很大区别,前者为依赖公用apr,后者依赖于自身的apr。一般前者也就是2.0.x的编译基本上没有apr方面的问题。如果是后者也出现关于apr的问题,则将安装前的apr去除后,在编译apache2.2.x自身的apr,在srclib目录中。
解决方法:
因此根据提示解决此问题:
1、安装APR:
--------------------------------------
cd httpd-2.2.29
cd srclib/apr
./configure --prefix=/usr/local/apr
make && make install
--------------------------------------
2、安装APR-util
--------------------------------------
cd ../apr-util/
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
make && make install
--------------------------------------
3、安装apache,加入参数(--with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util)
--------------------------------------
cd /srv/soft/httpd-2.2.29
./configure --prefix=/usr/local/apache --enable-so --enable-rewrite --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
make && make install
--------------------------------------
错误提示2:
安装最新httpd-2.4.12 编译问题:
configure: error: APR not found. Please read the documentation
方法:
安装apr,apr-util
http://apr.apache.org/download.cgi
--------------------------------------
错误提示3:
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
解决方法:
yum install pcre-devel
就是缺了开发包,后来查明白了
带有devel是开发包,需要编译环境才需要安装。而我查到的 .so 是动态库,所以我装apache的时候需要的是开发包,而不是已经装好的库。
#############################################################
2)PHP源码安装
1.先安装依赖包
yum install libxml2 libxml2-devel -y
## apache+php和mysql分开装论坛访问空白解决了吗?
前提php版本5.3及以上
php编译安装时用下面这个
tar zxf php-5.4.39.tar.gz
cd php-5.4.39
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd
如果在phpinfo输出的mysql项中发现client API Version : mysqlnd, 说明mysqlnd驱动安装成功.
百度搜解释:
http://zhangxugg-163-com.iteye.com/blog/1894990
(二)源码安装Apache+PHP整合
1.整合apache+php环境,修改httpd.conf配置文件,然后加入如下语句:
LoadModule php5_module modules/libphp5.so(默认已存在)
AddType application/x-httpd-php .php
DirectoryIndex index.php index.html (把index.php加index.html之前)
2.然后在/usr/local/apache/htdocs目录下创建index.php测试页面,执行如下命令:
[root@LiuHui conf]# cat >>/usr/local/apache/htdocs/index.php<<EOF
<?php
phpinfo();
?>
EOF
3.启动apache服务
/usr/local/apache/bin/apachectl start
检查服务是否启动成功:
netstat -ntl |grep 80
ps -ef |grep httpd
lsof -i:80
3.访问http://IP/测试是否整合成功
(三)MySQL主从同步配置
Master:192.168.70.133
Slave:192.168.70.134
1.安装mysql数据库
1)安装依赖包
yum install ncurses-devel -y
2)下载源码包编译安装
cd /src/soft;
wget http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.1/mysql-5.1.72.tar.gz
tar zxf mysql-5.1.71.tar.gz
cd mysql-5.1.71
./configure --prefix=/usr/local/mysql --enable-assembler
make && make install
============================================
3)配置mysql服务为系统服务:
cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
cp /usr/local/mysql/share/mysql/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 345 mysqld on
cd /usr/local/mysql/
useradd mysql
chown -R mysql.mysql /usr/local/mysql
/usr/local/mysql/bin/mysql_install_db --user=mysql --datadir=/usr/local/mysql/var
chown -R mysql var/
启动Mysql:
/etc/init.d/mysqld start
或
/usr/local/mysql/bin/mysqld_safe --user=mysql &
注:MySQL Slave服务器安装同Master。
------------------------------------------
mysql 主从同步原理:
1、在master主开启bin-log日志功能, 记录更新、插入、删除的语句。
2、必须开启三个线程,主上开启io线程,从上开启io线程和sql线程。
3、从上io线程去连接master,master通过io线程检查有slave过来的请求,请求日志、postsion的位置。
4、master将这些相应的日志返回给slave,slave自己去下载到本地的realy_log里面,写入一个master-info日志记录同步的点。
5、slave的sql线程检查到realy-log日志有更新,然后在本地去exec执行。
6、主从同步是属于异步方式。
将空格替换成空。
%s/^ //g
%s/ $//g
2.MySQL主从配置
1)在主上配置主要添加servier-id和log-bin:
[mysqld]
log-bin=mysql-bin
server-id = 1
auto_increment_offset=1
auto_increment_increment=2
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
replicate-do-db=discuz,wordpress
保存退出,重启mysql服务:
/etc/init.d/mysqld restart
登录主数据库授权slave同步的用户,查看同步点。
grant replication slave on *.* to 'tongbu'@'192.168.80.132' identified by '123456';
show master status;
mysql-bin.000001 348
-------------------------------------------
scp -r /etc/my.cnf [email protected]:/etc/my.cnf
2)从slave上主要配置server-id:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
server-id = 2
auto_increment_offset=2
auto_increment_increment=2
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
3)启动mysql
/etc/init.d/mysqld restart
--------------------------------------
4)从上登录数据库change master,并开启slave
change master to master_host='192.168.70.133',master_user='tongbu',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=348;
slave start;
查看是否同步:
show slave status\G;
如有以下两个YES,代表同步成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
(四)LAMP配置虚拟主机搭建discuz和wordpress
1)下载discuz和wordpress主程序
Discuz_X3.2_SC_UTF8.zip
wordpress-4.1-zh_CN.tar.gz
2)创建程序目录,并解压主程序
mkdir /data/{bbs,blog} -p
tar -zxf wordpress-4.1-zh_CN.tar.gz -C /data/blog/
unzip Discuz_X3.2_SC_UTF8.zip -d /data/bbs/
cd /data/bbs/
mv upload/* .
chmod o+w data/ config/ uc_client/ uc_server/ -R
cd ../blog/
mv wordpress/* .
3)创建存储blog和bbs数据的数据库并授权:
mysql> create database wordpress charset=utf8;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on discuz.* to discuz@'192.168.70.129' identified by '123456';
Query OK, 0 rows affected (0.06 sec)
mysql> grant all on wordpress.* to wordpress@'192.168.70.129' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
4)配置虚拟主机后,访问安装测试
1.配置虚拟主机
/usr/local/apache/conf/extra/httpd-vhosts.conf
[root@LiuHui extra]# cat httpd-vhosts.conf
NameVirtualHost *:80
#
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/data/bbs"
ServerAlias bbs.yhh88.com
<Directory "/data/bbs">
AllowOverride All
Options -Indexes FollowSymLinks
Order allow,deny
Allow from all
</Directory>
ErrorLog "logs/bbs.yhh88-error_log"
CustomLog "logs/bbs.yhh88-access_log" common
</VirtualHost>
#
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/data/blog"
ServerAlias blog.yhh88.com
<Directory "/data/blog">
AllowOverride All
Options -Indexes FollowSymLinks
Order allow,deny
Allow from all
</Directory>
ErrorLog "logs/blog.yhh88-error_log"
CustomLog "logs/blog.yhh88-access_log" common
</VirtualHost>
2.在apache主配置文件httpd.conf去掉此行注释#:
Include conf/extra/httpd-vhosts.conf
3.重启apache服务
/usr/local/apache/bin/apachectl restart
4.配置本机hosts文件,设置本地域名解析
C:\Windows\System32\drivers\etc\hosts
192.168.70.129 blog.yhh88.com
192.168.70.129 bbs.yhh88.com
访问http://bbs.yhh88.com进行安装论坛
访问http://blog.yhh88.com进行安装博客
(五)Mysql主从同步,master突然down机,如何恢复:
1、slave数据库必须启动,在slave上授权网站IP对数据库的访问权限。
2、修改网站服务器config目录下:
config_global.php
config_ucenter.php 把原先master ip改成slave ip地址。
3、重启httpd服务,切换成功。
具体手动更改操作步聚:
1.在slave上授权web服务器可以访问从服务器:
mysql> grant all on discuz.* to 'discuz'@'192.168.70.129' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on wordpress.* to 'wordpress'@'192.168.70.129' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
2.再在Apache+PHP上更改配置discuz和wordpress连接数据库IP改为slave的IP
[root@LiuHui blog]# grep 192.168.70.134 wp-config.php
define('DB_HOST', '192.168.70.134');
[root@LiuHui blog]# cd ../bbs/config/
[root@LiuHui config]# grep 192.168.70.134 config_ucenter.php
define('UC_DBHOST', '192.168.70.134');
[root@LiuHui config]# grep 192.168.70.134 config_global.php
$_config['db']['1']['dbhost'] = '192.168.70.134';
3.再测试访问正常