KVM实现分布式部署lamp并安装WordPress

实验思维导图:

分析:

本实验要求使用KVM安装三台虚拟机,实现mysql,php,httpd,分布式部署,并完成lamp环境搭建WordPress

搭建整体环境,克隆一台新的虚拟机,在启动之前选择如下,从而使得本机支持虚拟化实现

首先安装kvm环境

[root@qijunchao ~]# yum -y install libvirt-daemon-kvm qemu-kvm virt-manager libvirt
[root@qijunchao ~]# systemctl restart libvirt

创建http-fpm网桥

[root@qijunchao ~]# cd /etc/libvirt/qemu/networks/
[root@qijunchao networks]# cp default.xml httpfpm.xml
[root@qijunchao networks]# vim httpfpm.xml 
#修改指定的mac,uuid,
<network>
  <name>httpfpm</name>
  <uuid>06a6d52e-6c03-4b29-bdf7-2cfe6d5e937a</uuid>
  <bridge name='httpfpm' stp='on' delay='0'/>
  <mac address='52:54:00:c6:21:33'/>
  <ip address='10.0.0.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='10.0.0.2' end='10.0.0.254'/>
    </dhcp>
  </ip>
</network>

定义并启动httpfpm网桥

[root@qijunchao ~]# virsh net-define /etc/libvirt/qemu/networks/httpfpm.xml 
[root@qijunchao ~]# virsh net-start httpfpm
[root@qijunchao ~]# virsh net-autostart httpfpm

创建php-mysql网桥

要求同上,具体如下

[root@qijunchao ~]# cd /etc/libvirt/qemu/networks/
[root@qijunchao networks]# cp httpfpm.xml phpmysql.xml 
<network>
  <name>phpmysql</name>
  <uuid>06a6d52e-6c03-4b29-bdf7-2cfe6d5e937b</uuid>
  <bridge name='phpmysql' stp='on' delay='0'/>
  <mac address='52:54:00:c6:21:34'/>
  <ip address='20.0.0.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='20.0.0.2' end='20.0.0.254'/>
    </dhcp>
  </ip>
</network>
[root@qijunchao ~]# virsh net-define /etc/libvirt/qemu/networks/phpmysql.xml 
Network phpmysql defined from /etc/libvirt/qemu/networks/phpmysql.xml

[root@qijunchao ~]# virsh net-start phpmysql
Network phpmysql started

[root@qijunchao ~]# virsh net-autostart phpmysql
Network phpmysql marked as autostarted

查看创建的网桥状态

[root@qijunchao ~]# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes
 httpfpm              active     yes           yes
 phpmysql             active     yes           yes

全部运行

创建物理桥 

[root@qijunchao ~]# nmcli connection add type bridge con-name br0 ifname br0
[root@qijunchao ~]# nmcli connection modify br0 ipv4.method manual ipv4.addresses 172.18.253.64/16 ipv4.gateway 172.18.0.1 ipv4.dns 172.18.0.1
[root@qijunchao ~]# nmcli connection up br0
[root@qijunchao ~]# nmcli connection add type bridge-slave ifname ens33 master br0
[root@qijunchao ~]# nmcli connection up bridge-slave-ens33

查看物理网桥,以及虚拟网桥,和自己指定网络端一样

[root@localhost ~]# ifconfig 
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.253.64  netmask 255.255.0.0  broadcast 172.18.255.255
        inet6 fe80::fa1a:9524:4b58:9ad3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c7:a9:bb  txqueuelen 1000  (Ethernet)
        RX packets 142114  bytes 2595479371 (2.4 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 99313  bytes 14884523 (14.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
httpfpm: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 10.0.0.1  netmask 255.255.255.0  broadcast 10.0.0.255
        ether 52:53:00:c6:21:33  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
phpmysql: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 20.0.0.1  netmask 255.255.255.0  broadcast 20.0.0.255
        ether 52:53:00:c6:21:34  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

创建虚拟磁盘文件,因为本机的根环境容量较小,就选择了挂载服务器的指定系统镜像目录,自己在做实验时候,可以下载下来要虚拟机centos镜像文件

[root@qijunchao ~]# mkdir -pv /virtualmachines/centos7_base
[root@qijunchao ~]# qemu-img create -f qcow2 -o size=80G,preallocation=metadata,compat=1.1 /virtualmachines/centos7_base/centos7.img

[root@qijunchao ~]# mount 172.18.0.1:/var/ftp/pub /mnt/
[root@qijunchao ~]# virt-manager 

启动虚拟化安装centos7,我们先安装一个模板机,然后直接复制模板机为我们想要的机器

选择网卡为br0这样方便待会我们方便下载仓库

过我已经安装过了,选择指定的centos7镜像,然后,安装到本地的指定目录下/virtualmachines/centos7_base/centos7.img

为了方便,选择了最小化安装,经过几分钟的centos7安装,和平常安装centos7一样

root登陆,在这个模板机器里面我们可以先下载好一些常用命令工具包,net-tools,以及服务,httpd,php-fpm,php-mysql,mariadb-server,php,,这样一会就可以不用再手动配置仓库.

安装完成之后,就可以进行虚拟机的复制了,我们先准备指定目录以及文件

[root@qijunchao ~]# mkdir -pv /virtualmachines/{httpd,php,mysql}
mkdir: created directory ‘/virtualmachines/httpd’
mkdir: created directory ‘/virtualmachines/php’
mkdir: created directory ‘/virtualmachines/mysql’
[root@qijunchao ~]# cp /virtualmachines/centos7_base/centos7.img /virtualmachines/httpd/httpd.img
[root@qijunchao ~]# cp /virtualmachines/centos7_base/centos7.img /virtualmachines/php/php.img
[root@qijunchao ~]# cp /virtualmachines/centos7_base/centos7.img /virtualmachines/mysql/mysql.img

通过模板机,配置实际需要三台机器的mac,uuid,以及安装源文件,以httpd为例

[root@qijunchao ~]# cd /etc/libvirt/qemu/
[root@qijunchao qemu]# cp centos7.5.xml httpd.xml
[root@qijunchao qemu]# vim httpd.xml 

 <name>httpd</name>
  <uuid>a8dbf8ae-d691-4f00-835b-3c29ce8972a0</uuid>
 <source file='/virtualmachines/httpd/httpd.img'/>
      <mac address='52:54:00:cc:f6:d1'/>

设置并启动httpd虚拟机,其他两台一样操作

[root@qijunchao qemu]# virsh define httpd.xml 
Domain httpd defined from httpd.xml

[root@qijunchao qemu]# virsh autostart httpd
Domain httpd marked as autostarted

[root@qijunchao qemu]# virsh start httpd
Domain httpd started

完成之后,再次

[root@qijunchao ~]# virt-manager 

发现已经变成了四台虚拟机了,这时候开始配置网卡

httpd需要两个网卡,一个是172.%  一个为10.%  如图所示分别选择br0和httpfpm网卡最后点右下角apply,再次进入系统后重启网络服务。需要的指定ip段就出来了
 

php需要20.%和10.%两个网卡,同样设置,在此略

mysql只需20.%网卡就行

至此,三台机器环境搭建完毕。

在httpd机器上设置虚拟网卡

[root@httpd ~]# mkdir -pv /vhosts/pam/htdocs
[root@httpd ~]# cd /etc/httpd/conf.d/
​[root@httpd ~]# vim pma.conf
    DirectoryIndex index.php                
	<VirtualHost *:80>                      
	ServerName bbs.magedu.com
	DocumentRoot /vhosts/pam/htdocs
	<Directory "/vhosts/pam/htdocs">                                     
	Require all granted                                     
	</Directory>  
    ProxyRequests Off                               
	ProxyPassMatch ^/(.*\.php)$  fcgi://10.0.0.231:9000/vhosts/pam/htdocs/$1                                  
	</VirtualHost>       

修改httpd主配置文件。

<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

修改/etc/hosts/配置

[root@httpd ~]# cd /etc/hosts
172.18.253.74  bbs.magedu.com

之后重启httpd服务

在php-fpm机器上修改主配置文件/etc/php-fpm.d/www.conf

修改以下配置

listen =0.0.0.o:9000
;listen.allowed_clients = 127.0.0.1

在mysql服务器上,启动mysql,创建数据库,db1, 用户root,密码user1

MariaDB [(none)]> create database db1;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all on db1.* to 'root'@'20.0.0.60' identified by 'user1';
Query OK, 0 rows affected (0.02 sec)

在php-fpm上安装wordpress

[root@php ~]# mkdir -pv /vhosts/pam
[root@php ~]# cd  /vhosts/pam
[root@php pam]# tar xvf wordpress-4.9.4-zh_CN.tar.gz 
[root@php pam]# ln -sv wordpress htdocs

现在可以访问htppd服务器了
在物理机浏览器输入172.18.253.74就可以访问你的wordpress了,接着填入你的信息,完成安装即可

猜你喜欢

转载自blog.csdn.net/qq_27281257/article/details/83660842