小型LAMP架构搭建

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小型LAMP架构搭建相关的知识,希望对你有一定的参考价值。

LAMP架构实现

技术图片
构建上图所示lamp架构

主机类型 主机名 系统 IP
Client Clinet Fedora30 192.168.73.153
DNS DNS CentOS7.6 192.168.73.101
HTTPD+php HTTPD CentOS7.6 192.168.73.110
HTTPD+PHP HTTPD2 CentOS7.6 192.168.73.111
NFS nfs CentOS7.6 192.168.73.120
mysql Master CentOS7.6 192.168.73.130
MySQL Slave1 CentOS7.6 192.168.73.131
MySQL Slave2 CentOS7.6 192.168.73.132

一、搭建MySQL主从

在所有MySQL服务器安装MySQL服务

yum install mariadb-server -y

Master节点操作

1.配置主节点服务器

[[email protected] ~]# vim /etc/my.cnf
[mysqld]
server-id=1
log-bin=/data/bin/mysql-bin
binlog-format=row

2.创建二进制日志目录

[mysqld]
server-id=1
log-bin=/data/bin/mysql-bin
binlog-format=row

3.启动MySQL服务

[[email protected] ~]# systemctl start mariadb

4.创建一个用来主从复制的账号

[[email protected] ~]# mysql -e "GRANT REPLICATION SLAVE ON *.* TO ‘repluser‘@‘192.168.73.%‘ IDENTIFIED BY ‘111111‘;"

5.查看二进制日志,记录日志大小

[[email protected] ~]# mysql -e "SHOW MASTER LOGS;"
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |     26753 |
| mysql-bin.000002 |    921736 |
| mysql-bin.000003 |       402 |
+------------------+-----------+

Slave1节点操作

1.修改配置文件

[[email protected] ~]# vim /etc/my.cnf
[mysqld]
server-id=2
read-only

2.启动服务

[[email protected] ~]# systemctl start mariadb

3.CHANGE MASTER TO

[[email protected] ~]# mysql
MariaDB [(none)]> CHANGE MASTER TO   MASTER_HOST=‘192.168.73.130‘,   MASTER_USER=‘repluser‘,   MASTER_PASSWORD=‘111111‘,   MASTER_PORT=3306,   MASTER_LOG_FILE=‘mysql-bin.000003‘,   MASTER_LOG_POS=402;
Query OK, 0 rows affected (0.01 sec)

4.查看slave状态

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.73.130
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 402
               Relay_Log_File: mariadb-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: No
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 402
              Relay_Log_Space: 245
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
1 row in set (0.00 sec)

ERROR: No query specified

5.启动复制线程

MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

6.查看slave状态

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.73.130
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 402
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 402
              Relay_Log_Space: 825
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)

ERROR: No query specified

7.测试
主节点导入测试表

[[email protected] ~]# mysql < hellodb_innodb.sql 

8.slave1节点查看

[[email protected] ~]# mysql -e "SHOW DATABASES;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+

Slave1节点搭建完毕

Slave2节点操作

1.修改配置文件

[[email protected] ~]# vim /etc/my.cnf
[mysqld]
server-id=3
read-only

2.启动MySQL服务

[[email protected] ~]# systemctl restart mariadb 

3.CHANGE MASTER TO

[[email protected] ~]# mysql
MariaDB [(none)]> CHANGE MASTER TO   MASTER_HOST=‘192.168.73.130‘,   MASTER_USER=‘repluser‘,   MASTER_PASSWORD=‘111111‘,   MASTER_PORT=3306,   MASTER_LOG_FILE=‘mysql-bin.000003‘,   MASTER_LOG_POS=402;
Query OK, 0 rows affected (0.01 sec)

4.查看slave状态

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.73.130
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 402
               Relay_Log_File: mariadb-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: No
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 402
              Relay_Log_Space: 245
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
1 row in set (0.00 sec)

ERROR: No query specified

5.启动复制线程

MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

6.查看数据库是否同步

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

数据库主从搭建完毕

二、搭建HTTPD+PHP

分别在两台httpd主机上安装httpd和php-fpm

yum install httpd php-fpm php-mysql -y

配置httpd服务器

1.查看fcgi相关模块是否启用

[[email protected] ~]# httpd -M | grep proxy
 proxy_module (shared)                      #已经启用
 proxy_ajp_module (shared)
 proxy_balancer_module (shared)
 proxy_connect_module (shared)
 proxy_express_module (shared)
 proxy_fcgi_module (shared)                 #已经启用
 proxy_fdpass_module (shared)
 proxy_ftp_module (shared)
 proxy_http_module (shared)
 proxy_scgi_module (shared)
 proxy_wstunnel_module (shared)

2.修改httpd配置文件

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf 
    DirectoryIndex index.php index.html

addtype application/x-httpd-php .php
addtype applictaion/x-httpd-php-source .phps
proxyrequests off
proxyPassMatch ^/(.*\.php)$ unix:/var/run/php.sock|fcgi://localhost/data/test/$1

<Virtualhost *:80>
servername blog.mylinuxops.com
Documentroot /data/test
<directory /data/test>
require all granted
</directory>
</Virtualhost>

3.修改php-fpm配置文件

[[email protected] ~]# vim /etc/php-fpm.d/www.conf 
;listen = 127.0.0.1:9000
listen = /var/run/php.sock
listen.mode = 0666
;listen.allowed_clients = 127.0.0.1

4.修改php.ini中时区

[[email protected] ~]# vim /etc/php.ini 
date.timezone = Asia/Shanghai

5.启动httpd、php-fpm

[[email protected] ~]# systemctl start httpd php-fpm

6.创建测试页

[[email protected] ~]# mkdir /data/test
[[email protected] ~]# vim /data/test/index.php
<?php
$dsn=‘mysql:host=192.168.73.130;dbname=test‘;
$username=‘repluser‘; $passwd=‘111111‘;
$dbh=new PDO($dsn,$username,$passwd);
var_dump($dbh);
phpinfo();
?>

7.测试
技术图片

配置httpd2服务器

1.从httpd服务器将httpd配置文件及php-fpm配置文件复制至httpd2

[[email protected] ~]# scp /etc/httpd/conf/httpd.conf  192.168.73.111:/etc/httpd/conf/httpd.conf 
[[email protected] ~]# scp /etc/php-fpm.d/www.conf 192.168.73.111:/etc/php-fpm.d/www.conf 

2.启动服务

[[email protected] ~]# systemctl start httpd php-fpm 

3.创建测试页

[[email protected] ~]# mkdir /data/test
[[email protected] ~]# vim /data/test/index.php
<?php
$dsn=‘mysql:host=192.168.73.130;dbname=test‘;
$username=‘repluser‘; $passwd=‘111111‘;
$dbh=new PDO($dsn,$username,$passwd);
var_dump($dbh);
phpinfo();
?>

4.测试
技术图片

配置dns服务器

1.安装bind服务

[[email protected] ~]# yum install bind -y

2.修改主配置文件

[[email protected] ~]# vim /etc/named.conf 
//      listen-on port 53 { 127.0.0.1; };
//      listen-on-v6 port 53 { ::1; };

//      allow-query     { localhost; };

3.添加区域记录

[[email protected] ~]# vim /etc/named.rfc1912.zones 
zone "mylinuxops.com" IN {
        type master;
        file "mylinuxops.com.zone";
};

4.创建区域数据库文件

[[email protected] etc]# cp -p /var/named/named.localhost /var/named/mylinuxops.com.zone
[[email protected] etc]# vim /var/named/mylinuxops.com.zone 
$TTL 1D
@       IN SOA  master admin.mylinuxops.com (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      master
master  A       192.168.73.101
www     A       192.168.73.110
www     A       192.168.73.111

5.检查语法错误

[[email protected] etc]# named-checkconf 
[[email protected] etc]# named-checkzone mylinuxops.com /var/named/mylinuxops.com.zone 
zone mylinuxops.com/IN: loaded serial 0
OK

6.启动服务

[[email protected] etc]# systemctl start named

7.测试

[[email protected] etc]# dig www.mylinuxops.com @192.168.73.101

; <<>> DiG 9.9.4-RedHat-9.9.4-72.el7 <<>> www.mylinuxops.com @192.168.73.101
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 10093
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 2

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.mylinuxops.com.        IN  A

;; ANSWER SECTION:
www.mylinuxops.com. 86400   IN  A   192.168.73.111
www.mylinuxops.com. 86400   IN  A   192.168.73.110

;; AUTHORITY SECTION:
mylinuxops.com.     86400   IN  NS  master.mylinuxops.com.

;; ADDITIONAL SECTION:
master.mylinuxops.com.  86400   IN  A   192.168.73.101

;; Query time: 0 msec
;; SERVER: 192.168.73.101#53(192.168.73.101)
;; WHEN: Fri May 17 06:00:35 CST 2019
;; MSG SIZE  rcvd: 116

配置NFS服务

1.创建出需要共享的目录,修改权限

[[email protected] ~]# mkdir /data/test/
[[email protected] ~]# setfacl -R -m u:nfsnobody:rwx /data/test

2.修改配置文件

[[email protected] ~]# vim /etc/exports
/data/test *(rw)

3.在httpd挂载nfs

[[email protected] ~]# showmount -e 192.168.73.120
Export list for 192.168.73.120:     
/data/test *         
[[email protected] ~]# mount 192.168.73.120:/data/test /data/test

4.在httpd2上挂载nfs

[[email protected] ~]# showmount -e 192.168.73.120
Export list for 192.168.73.120:
/data/test *
[[email protected] ~]# mount 192.168.73.120:/data/test /data/test

5.在/data/test中创建测试页

<?php
$dsn=‘mysql:host=192.168.73.130;dbname=test‘;
$username=‘repluser‘; $passwd=‘111111‘;
$dbh=new PDO($dsn,$username,$passwd);
var_dump($dbh);
phpinfo();
?>

客户端测试

1.测试DNS

[[email protected] ~]# dig www.mylinuxops.com

; <<>> DiG 9.11.5-P4-RedHat-9.11.5-13.P4.fc30 <<>> www.mylinuxops.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 37179
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 2

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.mylinuxops.com.        IN  A

;; ANSWER SECTION:
www.mylinuxops.com. 86400   IN  A   192.168.73.111
www.mylinuxops.com. 86400   IN  A   192.168.73.110

;; AUTHORITY SECTION:
mylinuxops.com.     86400   IN  NS  master.mylinuxops.com.

;; ADDITIONAL SECTION:
master.mylinuxops.com.  86400   IN  A   192.168.73.101

;; Query time: 0 msec
;; SERVER: 192.168.73.101#53(192.168.73.101)
;; WHEN: Thu May 16 22:54:46 CST 2019
;; MSG SIZE  rcvd: 116

[[email protected] ~]# dig www.mylinuxops.com

; <<>> DiG 9.11.5-P4-RedHat-9.11.5-13.P4.fc30 <<>> www.mylinuxops.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 63256
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 2

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.mylinuxops.com.        IN  A

;; ANSWER SECTION:
www.mylinuxops.com. 86400   IN  A   192.168.73.110
www.mylinuxops.com. 86400   IN  A   192.168.73.111

;; AUTHORITY SECTION:
mylinuxops.com.     86400   IN  NS  master.mylinuxops.com.

;; ADDITIONAL SECTION:
master.mylinuxops.com.  86400   IN  A   192.168.73.101

;; Query time: 0 msec
;; SERVER: 192.168.73.101#53(192.168.73.101)
;; WHEN: Thu May 16 22:54:47 CST 2019
;; MSG SIZE  rcvd: 116

2.测试站点访问
技术图片

测试安装wordpress

1.解压wordpress到/data/test

[[email protected] ~]# tar wordpress-5.0.3-zh_CN.tar.gz -C /data/test/

2.创建wordpress所需数据库及账户

[[email protected] ~]# mysql -e "CREATE DATABASE wordpress"
[[email protected] ~]# mysql -e "GRANT ALL ON wordpress.* TO ‘wpuser‘@‘192.168.73.%‘ IDENTIFIED BY ‘111111‘;"

3.修改wordpress配置文件

[[email protected] ~]# cp /data/test/wordpress/wp-config-sample.php /data/test/wordpress/wp-config.php
[[email protected] ~]# vim /data/test/wordpress/wp-config.php
// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define(‘DB_NAME‘, ‘wordpress‘);

/** MySQL数据库用户名 */
define(‘DB_USER‘, ‘wpuser‘);

/** MySQL数据库密码 */
define(‘DB_PASSWORD‘, ‘111111‘);

/** MySQL主机 */
define(‘DB_HOST‘, ‘192.168.73.130‘);

4.在客户端测试访问
技术图片
技术图片

以上是关于小型LAMP架构搭建的主要内容,如果未能解决你的问题,请参考以下文章

linux下Yum搭建lamp网站架构

LAMP架构搭建以及基于LAMP架构的主流论坛和博客搭建过程详解

ansible roles详解+搭建LAMP架构

linux架构学习第二十六天之LAMP架构原理及搭建详解

搭建LAMP架构

Web服务器群集之LAMP平台部署(LAMP架构搭建)