1. 程式人生 > >apache + php-fpm + mariadb 分離部署

apache + php-fpm + mariadb 分離部署

lamp分離部署

前面我已經寫過lamp的本機部署,但是現在生活中幾個服務是不可能部署在一臺伺服器上的,所以,我接下來介紹lamp 的分離部署,下面的計劃圖
apache + php-fpm + mariadb 分離部署
如上圖,準備了三臺伺服器(藍色字樣),分別執行mariadb,php-fpm,httpd,每臺機子處在不同的網段,他們有不同的網路橋(紅色字樣)分開,這樣保證他們是互不相通的,每個網橋之間通過堡壘機相連,實現不同的的網段的通訊,br0網橋可以上網,最後由PC機訪問httpd伺服器的80埠,展現在php-fpm裡的php程式,php程式讀取mariadb伺服器的資料.


實驗準備

如上圖,準備三臺虛擬機器,在VMware裡執行的主機做堡壘機,在這個堡壘機裡使用kvm虛擬出三個虛擬主機,首先做出一個模板機,然後複製出三臺虛擬主機來,

  1. 安裝所需包
    [[email protected] ~]# yum -y install libvirt-daemon-kvm qemu-kvm virt-manager libvirt
    [[email protected] ~]# systemctl restart libvirtd
  2. 製作虛擬磁碟檔案,可以通過基礎映象模板快速拷貝安裝
    [[email protected] ~]# mkdir -pv /virtualmachines/centos7_base
    #建立qcow2映象檔案,大小為80G,預先配置為元資料(metadata),相容1.1,指定映象檔案位置
    [
    [email protected]
    ~]# qemu-img create -f qcow2 -o size=80G,preallocation=metadata,compat=1.1 /virtualmachines/centos7_base/centos7.img
  3. 準備映象,做模板機
    #提前準備好centos7 映象檔案,這一步去VMware上去做,要求堡壘機安裝圖形化介面
    [[email protected] qemu]# virt-manager 
    #正常安裝centos7系統,把系統安裝在 /virtualmachines/centos7_base/centos7.img中即可
    #做完以後在virt-manger中,點選執行centos7模板機,進去安裝一些必要軟體包,如nmcli,vim.base等
  4. 根據模板機做出三個虛擬主機
    [[email protected] ~]# mkdir -pv /virtualmachines/{httpd,php,mysql}
    [[email protected] ~]# cp /virtualmachines/centos7_base/centos7.img /virtualmachines/httpd/httpd.img
    [[email protected] ~]# cp /virtualmachines/centos7_base/centos7.img /virtualmachines/php/php.img
    [[email protected] ~]# cp /virtualmachines/centos7_base/centos7.img /virtualmachines/mysql/mysql.img
    [[email protected] ~]# cd /etc/libvirt/qemu/
    #開始做虛擬主機,修改centos7.img映象檔案即可
    [[email protected] qemu]# cp centos7.5.xml httpd.xml
    [[email protected] qemu]# vim httpd.xml 
    #修改如下四列選項
    <name>httpd</name>#虛擬主機名稱
    <uuid>a8dbf8ae-d691-4f00-835b-3c29ce8972a0</uuid>  #虛擬主機uuid,可以用uuidgen生成
    <source file='/virtualmachines/httpd/httpd.img'/> #映象檔案位置
    <mac address='52:54:00:cc:f6:d1'/>   #mac地址
    [[email protected](nanyibo) qemu]# virsh define httpd.xml   #定義httpd.img映象配置檔案
    [[email protected](nanyibo) qemu]# virsh autostart httpd  #使其開機自動開啟
    [[email protected](nanyibo) qemu]# virsh start httpd  #執行映象
    #剩下的兩個虛擬主機如法炮製
    [[email protected] networks]# virsh list  #檢視虛擬主機的狀況
    Id    Name                           State
    ----------------------------------------------------
    12    mysql                          running
    14    php                            running
    16    httpd                          running
  5. 建立http-fpm網橋
    [[email protected] ~]# cd /etc/libvirt/qemu/networks/
    [[email protected] networks]# cp default.xml httpfpm.xml
    [[email protected] networks]# vim httpfpm.xml
    <network>
          <name>httpfpm</name> #修改網橋名
          <uuid>06a6d52e-6c03-4b29-bdf7-2cfe6d5e937a</uuid>  #修改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'/>  #分配的IP地址範圍
            </dhcp>
          </ip>
        </network>
    [[email protected] ~]# virsh net-define /etc/libvirt/qemu/networks/httpfpm.xml 
    [[email protected] ~]# virsh net-start httpfpm #開啟網橋
    [[email protected] ~]# virsh net-autostart httpfpm  #自啟動

    mysql -php網橋如法配置

  6. 建立物理網橋,把物理網絡卡ens33和虛擬橋br0聯絡一起,讓br0橋擁有ens33的IP,達成可以和我的物理機相通的目的
    [[email protected] ~]# nmcli connection add type bridge con-name br0 ifname br0  #新增網橋方案br0
    [[email protected] ~]# nmcli connection modify br0 ipv4.method manual ipv4.addresses 172.18.254.39/16 ipv4.gateway 172.18.0.1 ipv4.dns 172.18.0.1#給橋設定IP(這個IP是堡壘機的ens33網絡卡的IP),閘道器,dns
    [[email protected] ~]# nmcli connection up br0 #方案生效
    [[email protected] ~]# nmcli connection add type bridge-slave ifname ens33 master br0  #設定主從方案
    [[email protected] ~]# nmcli connection up bridge-slave-ens33 
    [[email protected] networks]# nmcli connection show    #檢視網橋是否生效
    NAME                UUID                                  TYPE      DEVICE   
    br0                 4f24bc16-320a-4fa9-9ead-fa6ff089ff2e  bridge    br0      
    bridge-slave-ens33  7a91f27a-f961-4ba4-a598-a87cc0063e6d  ethernet  ens33    
    ens38               be9e2b6b-674b-771d-7251-f3b49b3d23e0  ethernet  ens38    
    httpdfpm            bd0ca03f-071c-4422-8f1f-2231156defd0  bridge    httpdfpm 
    phpmysql            7666e396-9fc3-4a7a-8713-9566ac74147b  bridge    phpmysql 
    virbr0              7fd973b4-bf9a-453a-ad52-1e3477ff8b5b  bridge    virbr0   
    vnet0               5f89eb2d-6029-4d35-945d-4ac47b4be79c  tun       vnet0    
    [[email protected] networks]# ifconfig 
    br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.252.106  netmask 255.255.0.0  broadcast 172.18.255.255
        ...
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:9f:42:af  txqueuelen 1000  (Ethernet)
        ...
  7. 在httpd伺服器配置
    [[email protected] ~]# yum -y install httpd
    [[email protected] conf.d]# vim /etc/httpd/conf/httpd.conf  #修改優先順序
    <IfModule dir_module>
            DirectoryIndex index.php index.html
        </IfModule>
    [[email protected] conf.d]#    vim pma.conf #建立一個虛擬主機
    <VirtualHost *:80>
    DocumentRoot /vhosts/pam/htdocs
    #ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$  fcgi://10.10.0.11:9000/vhosts/pam/htdocs/$1
    <Directory /vhosts/pam/htdocs>
    Options None
    AllowOverride None
    Require all granted
    </Directory>
    </VirtualHost>
    [[email protected] conf.d]# httpd -t
    [[email protected] conf.d]#    mkdir -pv /vhosts/pam/htdocs
    [[email protected] conf.d]#    systemctl restart httpd
  8. 配置 php伺服器
    [[email protected] ~]# yum -y install php-fpm php-mysql php-mbstring
    [[email protected] ~]# vim /etc/php-fpm.d/www.conf  #修改兩處即可
    listen = 0.0.0.0:9000  #是所有網絡卡監聽9000埠
    ;listen.allowed_clients = 127.0.0.1   #預設所以可以連線
    [[email protected] ~]# ss -tnl |grep 9000.
    LISTEN     0      128          *:9000                     *:* 
    [[email protected] ~]# systemctl restart php-fpm
    [[email protected] ~]# mkdir -pv /vhosts/pam
    [[email protected] ~]# cd  /vhosts/pam
    [[email protected] pam]# tar xvf wordpress-4.9.4-zh_CN.tar.gz 
    [[email protected] pam]# ln -sv wordpress htdocs
  9. 配置mariadb伺服器
    [[email protected] ~]# yum -y install mariadb-server
    [[email protected] ~]# vim /etc/my.cnf
    skip_name_resolve  #增加一條 ,關閉域名解析
    [[email protected] ~]# mysql
    MariaDB [(none)]> create database wpdb;
    MariaDB [(none)]> grant all on wpdb.* to 'wpuser'@'%' identified by 'wppass'  with grant option;#建立連線使用者
  10. 現在可以訪問htppd伺服器了
    在物理機瀏覽器輸入172.18.252.10就可以訪問你的wordpress了,接著填入你的資訊,完成安裝即可

至此,lamp分離部署實現.