部署社交平台
实验目的
- 学会搭建SVN服务器
- 学会部署社交网站
- 对关键服务器进行双机热备
- 学会对服务器调优
案例分析
公司的社交网站采用PHP语言开发,为了管理PHP程序员开发的代码,上级领导要求搭建SVN服务器进行版本控制。社交网站的第个版木部署在LNMP平台之上,前端为Nginx服务器,通过fastcgi 协议访问后端的PHP服务器。为了保证数据安全,要求搭建MySQL数据库主从集群。
社交网站项目包含用户的相册功能,允许用户上传照片,上传照片需要使用共享存储来存放。针对共享存储可用的开源方案有很多,如MFS、FastDFS等。公司快定使用MFS分布式文件系统来实现,并将MFS挂载在PHP服务器的相关目录下。本案例拓扑图如图5.1所示。
案例实施
根据公司的需求,实施过程大致分为以下步骤
- 部署SVN服务器,为PHP程序员创建repo目录的访问账户,通知程序员石以导入代码。
- 部署MySQL主从服务器,根据PHP程序员的要求创建数据库与表。
- 部署Nginx服务器。
- 部署PHP服务器。
-部署MFS,将MFS文件系统挂载在前端PHP服务器的相关目录下。
-通知上线部署人员可以发布上线。
-保证数据库服务、PHP服务、Nginx服务依次启动,并通知测试人员开始测网站维护人员检查Nginx、PHP与数据库服务器是否正常工作。
部署SVN服务器
(1)安装SVN
[[email protected] ~]# yum install -y subversion
[[email protected] ~]# svnserve --version
svnserve, version 1.7.14 (r1542130)
compiled Nov 20 2015, 19:25:09 #//查看版本
(2)为PHP程序员创建仓库目录repo
[[email protected] ~]# mkdir -p /opt/svn/repo #//创建目录
[[email protected] ~]# svnadmin create /opt/svn/repo/ #//创建仓库
[[email protected] ~]# ls !$ #//目录创建完成后会生成所需要的配置文件
ls /opt/svn/repo/
conf db format hooks locks README.txt
(3)调整SVN参数
[[email protected] ~]# vim /opt/svn/repo/conf/svnserve.conf
anon-access = read #//把read改为none匿名用户
auth-access = write #//认证用户具有写入权限
password-db = passwd #//把passwd改为 /opt/svn/repo/conf/passwd 用户密码密码的存放路径
authz-db = authz #//把authz改为 /opt/svn/repo/conf/authz 用户信息存放路径
(4)创建用户验证密码文件和用户信息文件并启动服务
[[email protected] ~]# cd /opt/svn/repo/conf/
[[email protected] conf]# ls #//对passwd文件和authz文件进行修改
authz passwd svnserve.conf
[[email protected] conf]# vim passwd
[users]
admin = abc123 #//admin是PHP程序员创建的账户密码为abc123
[[email protected] conf]# vim authz #//在文件最后一行创建
[/] #//对repo根目录的操作权限
admin = r #//admin账户具有读取权限
[/webphp] #//对根目录下webphp目录的权限
admin = rw #//admin账户具有读取写入权限
[[email protected] repo]# mkdir webphp #//在repo根目录下创建需要操作的子目录
[[email protected] repo]# svn import webphp file:///opt/svn/repo/webphp/ -m "初始化目录" #//初始化导入webphp目录
Committed revision 1.
[[email protected] repo]# svnserve -d -r /opt/svn/repo/ #//启动svn服务
[[email protected] repo]# netstat -lnupt | grep svnserve #//查看端口
tcp 0 0 0.0.0.0:3690 0.0.0.0:* LISTEN 1918/svnserve
部署Nginxf服务器对Nginx重要服务器做keepalived双机热备
(1)安装keepalived
[[email protected] ~]# yum install keepalived -y
[[email protected] ~]# vim /etc/keepalived/keepalived.conf #//修改配置文件
global_defs {
notification_email {
[email protected] #//邮箱地址可以改为自己的邮箱地址在一台服务区down掉后会通过邮件发送给你
}
router_id LVS_DEVEL #//可改可不改,从服务器上要改因为不能和主服务器重叠router是唯一标识这边我该为了LVS_A从服务器要改为LVS_B
#//删除router_id下列四行
vrrp_instance VI_1 {
state MASTER #//主服务气味MASTER从服务器为BASKUP
interface eth0 #//网卡改为本服务器网卡centos7的网卡是ens33
virtual_router_id 51 #//keepalived组好主从要相同
priority 100 #//优先级从服务器小于100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.32.188 #//默认有三个去掉两个或者改为自己设定的VIP主从要一样
}
#//把虚拟ip下面的全部删除
(2)启动并查看VIP有没有开启
[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# setenforce 0 #//关闭防火墙和setenforce
[[email protected] ~]# systemctl start keepalived.service
[[email protected] ~]# ps -ef | grep keepalived
root 11384 1 0 13:19 ? 00:00:00 /usr/sbin/keepalived -D
root 11385 11384 0 13:19 ? 00:00:00 /usr/sbin/keepalived -D
root 11386 11384 0 13:19 ? 00:00:00 /usr/sbin/keepalived -D
root 11405 10998 0 13:20 pts/2 00:00:00 grep --color=auto keepalived
[[email protected] ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:3f:7f:08 brd ff:ff:ff:ff:ff:ff
inet 192.168.32.209/24 brd 192.168.32.255 scope global dynamic ens33
valid_lft 1184sec preferred_lft 1184sec
inet 192.168.32.188/32 scope global ens33 #//VIP
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe3f:7f08/64 scope link
valid_lft forever preferred_lft forever
#尝试down主服务器的keepalived看一下188
#的虚拟IP会不会绑定到从服务器上验证一下
#主服务器从新启动keepalived,188
#端口又会回到主服务器上
(3)安装Nginx服务器
[[email protected] ~]# yum install pcre-devel zlib-devel gcc gcc-c++ make -y #//安装语言开发包和工具
[[email protected] ~]# tar zxvf /abc/nginx-1.12.0.tar.gz -C /opt/ #//解压源代码包到/opt目录下
[[email protected] ~]# useradd -M -s /sbin/nologin nginx #//创建管理用户不允许本地登录
[[email protected] ~]# cd /opt/nginx-1.12.0/ #//进入nginx解压的文件夹进行编译
[[email protected] nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --user=nginx --group=nginx #//指定安装路径用户和组
[[email protected] nginx-1.12.0]# make && make install #//用make 和 make install 转换为系统可识别的文件
(4)修改Nginx配置文件,需要配置PHP程序的fastcgi访问接口
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html; #//把html改为html/wedphp
index index.html index.htm;
}
#//在地下加入下面这段话
location ~ \.php$ {
root /var/www/html/webphp; #//页面存放位置
fastcgi_pass 192.168.32.209:9000; #//PHP服务器地址
fastcgi_index index.php;
include fastcgi.conf;
[[email protected] ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #//建立软连接便于启动
[[email protected] ~]# nginx
nginx: [emerg] unexpected end of file, expecting "}" in /usr/local/nginx/conf/nginx.conf:117
#//如果在启动nginx中遇到这种报错说明要在117行加一个这样符号}
[[email protected] ~]# nginx #//启动
[[email protected] ~]# netstat -ntap | grep 80 #//查看端口号
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14066/nginx: master
部署PHP服务器
(1)安装PHP
[[email protected] ~]# tar zxvf /abc/php-5.5.38.tar.gz -C /opt/ #//解压源代码包
[[email protected] ~]# yum -y install > gd > libxml2-devel > libjpeg-devel > libpng-devel > zlib-devel > fontconfig-devel > openssl-devel > bzip2-devel #//首先安装GD库及GD库关联程序
[[email protected] ~]# useradd -M -s /sbin/nologin php #//创建用户不允许本地登录
[[email protected] php-5.5.38]# cp /usr/lib64/mysql/libmysqlclient.so.18 /usr/lib/libmysqlclient.so #//PHP默认去/usr/lib搜索libmysqlclient.so.18
[[email protected] php-5.5.38]# ./configure --prefix=/usr/local/php5 --with-gd --with-zlib --with-mysql=/usr/local/mysql \
--with-config-file-path=/usr/local/php5 --enable-mbstring --with-jpeg-dir --with-openssl --disable-ipv6 --enable-fpm
------如果在执行操作中出现错误请按照以下操作如果没有请跳过-------------------------------
#注:在执行以上操作是出现一下错误
Note that the MySQL client library is not bundled anymore!
#请查找到mysql.h文件,我们更改参数为--with-mysql=/usr,如果没有的话,请安装mysql-devel包,
[[email protected] php-5.5.6]# find / -name mysql.h
/usr/include/mysql/mysql.h
[[email protected] php-5.5.6]# yum install mysql-devel -y
---------------------------------------------
[[email protected] php-5.5.38]# make && make install #//转换文件类型
[[email protected] php-5.5.38]# cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf
[[email protected] php-5.5.38]# vim /usr/local/php5/etc/php-fpm.conf
;pid = run/php-fpm.pid #//去掉恰免得冒号
user = nobody #//nobody改为php
group = nobody #//nobody改为php
listen = 127.0.0.1:9000 #//监听地址改为0.0.0.0:9000
pm.max_children = 5 #//改为50
pm.start_servers = 2 #//改为20
pm.min_spare_servers = 1 #//改为5
pm.max_spare_servers = 3 #//改为35
[[email protected] php-5.5.38]# /usr/local/php5/sbin/php-fpm #//启动服务
[[email protected] php-5.5.38]# netstat -ntap | grep 9000
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 26085/php-fpm: mast
[[email protected] php-5.5.38]# mkdir -p /var/www/html/webphp/ #//创建首页目录
[[email protected] php-5.5.38]# vim /var/www/html/webphp/index.php #//创建首页
<?
phpinfo();
?>
PHP做完了通过主服务器ip、从服务器ip、虚拟ip:192.168.32.188分别访问,看到以下界面如果无法访问请关闭防火墙和setenforce 0
测试:
主,192.168.32.209
![image]
从:192.168.32.220
![image]
VIP:192.168.32.188
![image]
部署主从数据库(因为MySQL安装时间较长改为用yum安装使用mairadb)
(1)安装mariadb并修改配置文件
#//yum安装
[[email protected] webphp]# yum -y install mariadb-server mariadb
#//修改配置文件
[[email protected] webphp]# vim /etc/my.cnf
--------------------------------------------------------------------
#//删除以下9行
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
-----------------------------------------------------------------
#//重新指定配置路径
[mysqld]
log_error=/var/lib/mysql/mysql.err
log=/var/lib/mysql/mysql_log.log
log_slow_queries=/var/lib/mysql_slow_queris.log
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=1 #//从服务器要修改server_id
log_slave_updates=true
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
(2)启动数据库并登录
[[email protected] webphp]# systemctl start mariadb.service
#//用mysql命令直接登陆
[[email protected] webphp]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MariaDB [(none)]>
(3)构建主从
#//主服务器授权给从服务器
MariaDB [(none)]> grant replication slave on *.* to ‘replication‘@‘192.168.32.%‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.00 sec)
#//查看是否授权成功
MariaDB [(none)]> show grants for "replication"@"192.168.32.%";
+-----------------------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected]% |
+-----------------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO ‘replication‘@‘192.168.32.%‘ IDENTIFIED BY PASSWORD ‘*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9‘ |
+-----------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
#//主服务器查看二进制日志和偏移量
MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 | 410 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
#//从服务器用授权用户密码登录master,通过二进制日志和偏移量开始备份主服务器
MariaDB [(none)]> change master to master_host=‘192.168.32.209‘,master_user=‘replication‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000003‘,master_log_pos=410;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)
#//查看IO,SQL线程有没有连接成功,显示为YES表示为成功
MariaDB [(none)]> show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
MFS部署并挂载
搭建 Master Server
1、安装MFS
yum install -y zlib-devel gcc gcc-c++ //安装依赖包环境包
useradd mfs -s /sbin/nologin -M //创建管理用户
tar xzvf mfs-1.6.27-5.tar.gz -C /opt/
cd /opt/mfs-1.6.27/
./configure --prefix=/usr/local/mfs --with-default-user=mfs --with-default-group=mfs --disable-mfschunkserver --disable-mfsmount
make && make install
2、复制文件
cd /usr/local/mfs/etc/mfs/
cp mfsmaster.cfg.dist mfsmaster.cfg # 主配置文件
cp mfsexports.cfg.dist mfsexports.cfg # 挂载权限
cp mfstopology.cfg.dist mfstopology.cfg # top架构感知
cd /usr/local/mfs/var/mfs/
cp metadata.mfs.empty metadata.mfs #防断裂
3、启动 Master Server
/usr/local/mfs/sbin/mfsmaster start //开启
/usr/local/mfs/sbin/mfsmaster -s //停止
netstat -antp | grep mfs
systemctl stop firewalld.service
setenforce 0 //关闭防火墙和安全功能
搭建 Metalogger Server
1、安装MFS
yum install -y zlib-devel gcc gcc-c++ //安装依赖包环境包
useradd mfs -s /sbin/nologin -M //创建管理用户
tar xzvf mfs-1.6.27-5.tar.gz -C /opt/
cd /opt/mfs-1.6.27/
./configure --prefix=/usr/local/mfs --with-default-user=mfs --with-default-group=mfs --disable-mfschunkserver --disable-mfsmount
make && make install
2、复制文件
cd /usr/local/mfs/etc/mfs/
cp mfsmaster.cfg.dist mfsmaster.cfg
cp mfsexports.cfg.dist mfsexports.cfg
cp mfsmetalogger.cfg.dist mfsmetalogger.cfg
vim mfsmetalogger.cfg
......
MASTER_HOST = 192.168.43.118 //IP指向master
3、启动 Metalogger Server
/usr/local/mfs/sbin/mfsmetalogger start //开启
/usr/local/mfs/sbin/mfsmetalogger -s //停止
netstat -antp | grep mfs
systemctl stop firewalld.service
setenforce 0 //关闭防火墙和安全功能
搭建 Chunk Server
1、安装MFS
yum install -y zlib-devel gcc gcc-c++ //安装依赖包环境包
useradd mfs -s /sbin/nologin -M //创建管理用户
tar xzvf mfs-1.6.27-5.tar.gz -C /opt/
cd /opt/mfs-1.6.27/
./configure --prefix=/usr/local/mfs --with-default-user=mfs --with-default-group=mfs --disable-mfsmaster --disable-mfsmount
make && make install
2、复制文件
cd /usr/local/mfs/etc/mfs/
cp mfschunkserver.cfg.dist mfschunkserver.cfg
cp mfshdd.cfg.dist mfshdd.cfg
vim mfschunkserver.cfg
......
MASTER_HOST = 192.168.43.118 //IP指向master
vim mfshdd.cfg
......
/data //添加一行/data,在这里/data是一个给MFS的分区,生产环境最好使用独立的分区或磁盘挂载到此目录
mkdir /data //创建文件夹
chown -R mfs:mfs /data //属主属组都改为mfs
3、启动Chunk Server
/usr/local/mfs/sbin/mfschunkserver start //开启
/usr/local/mfs/sbin/mfschunkserver start -s //停止
netstat -antp | grep mfs
systemctl stop firewalld.service
setenforce 0 //关闭防火墙和安全功能
客户端配置
此处PHP服务器是客户端角色,在PHP服务器上搭建
1、安装fuse
yum install -y zlib-devel gcc gcc-c++ //安装依赖包环境包
tar xzvf fuse-2.9.2.tar.gz -C /opt //mfs客户端依赖于fuse
cd /opt/fuse-2.9.2
./configure
make && make install
2、设置环境变量
vim /etc/profile
#在末尾处插入
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
source /etc/profile //加载立即生效
3、安装MFS客户端
useradd mfs -s /sbin/nologin -M //创建管理用户
tar xzvf mfs-1.6.27-5.tar.gz -C /opt/
cd /opt/mfs-1.6.27/
./configure --prefix=/usr/local/mfs --with-default-user=mfs --with-default-group=mfs --disable-mfschunkserver --enable-mfsmount //注意此处开启客户端
make && make install
4、挂载MFS文件系统
将MFS文件系统挂载在前端PHP服务器的/var/www/html/webphp/uploads/photos目录
mkdir -p /var/www/html/webphp/uploads/photos //创建挂载点
modprobe fuse //加载fuse模块到内核
/usr/local/mfs/bin/mfsmount /var/www/html/webphp/uploads/photos -H 192.168.32.211 //挂载MFS
最后部署网站
---------建库,授权----------
mysql> CREATE DATABASE bbs; //创建一个数据库//
MariaDB [(none)]> GRANT all ON bbs.* TO ‘bbsuser‘@‘%‘ IDENTIFIED BY ‘admin123‘;
//把bbs数据库里面所有表的权限授予给bbsuser,并设置密码//
MariaDB [(none)]> flush privileges;
//刷新数据库//
---------测试数据库--------------
vi /var/www/html/webphp/index.php
<?php
$link=mysql_connect(‘192.168.32.209‘,‘bbsuser‘,‘admin123‘);
if($link) echo "Success!!";
else echo "Fail!!";
mysql_close();
?>
测试出来的页面
![image]!
安装论坛
[[email protected] ~]# unzip /abc/Discuz_X2.5_SC_UTF8.zip -d /opt/dis
[[email protected] ~]# cp -r /opt/dis/upload/ /var/www/html/webphp
[[email protected]localhost ~]# cd /var/www/html/webphp
[[email protected] bbs]# chown -R php ./config
[[email protected] bbs]# chown -R php ./data
[[email protected] bbs]# chown -R php ./uc_client
[[email protected] bbs]# chown -R php ./uc_server/data
用http://192.168.32.188/bbs/install/index.php 进入安装页面:
![image]
![image]
安装完成
![image]
用http://192.168.32.188/bbs/index.php访问:
![image]
原文地址:http://blog.51cto.com/13645280/2153507
时间: 2024-10-20 02:19:25