小型LAMP架构搭建
2019-05-17 00:25
1796 查看
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.配置主节点服务器
[root@master ~]# 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服务
[root@master ~]# systemctl start mariadb
4.创建一个用来主从复制的账号
[root@master ~]# mysql -e "GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.73.%' IDENTIFIED BY '111111';"
5.查看二进制日志,记录日志大小
[root@master ~]# mysql -e "SHOW MASTER LOGS;" +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 26753 | | mysql-bin.000002 | 921736 | | mysql-bin.000003 | 402 | +------------------+-----------+
Slave1节点操作
1.修改配置文件
[root@slave1 ~]# vim /etc/my.cnf [mysqld] server-id=2 read-only
2.启动服务
[root@slave1 ~]# systemctl start mariadb
3.CHANGE MASTER TO
[root@slave1 ~]# 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.测试
主节点导入测试表
[root@master ~]# mysql < hellodb_innodb.sql
8.slave1节点查看
[root@slave1 ~]# mysql -e "SHOW DATABASES;" +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | +--------------------+
Slave1节点搭建完毕
Slave2节点操作
1.修改配置文件
[root@slave2 ~]# vim /etc/my.cnf [mysqld] server-id=3 read-only
2.启动MySQL服务
[root@slave2 ~]# systemctl restart mariadb
3.CHANGE MASTER TO
[root@slave2 ~]# 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相关模块是否启用
[root@httpd ~]# 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配置文件
[root@httpd ~]# 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配置文件
[root@httpd ~]# 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中时区
[root@httpd ~]# vim /etc/php.ini date.timezone = Asia/Shanghai
5.启动httpd、php-fpm
[root@httpd ~]# systemctl start httpd php-fpm
6.创建测试页
[root@httpd ~]# mkdir /data/test [root@httpd ~]# 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
[root@httpd ~]# scp /etc/httpd/conf/httpd.conf 192.168.73.111:/etc/httpd/conf/httpd.conf [root@httpd ~]# scp /etc/php-fpm.d/www.conf 192.168.73.111:/etc/php-fpm.d/www.conf
2.启动服务
[root@httpd2 ~]# systemctl start httpd php-fpm
3.创建测试页
[root@httpd ~]# mkdir /data/test [root@httpd ~]# 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服务
[root@dns ~]# yum install bind -y
2.修改主配置文件
[root@dns ~]# vim /etc/named.conf // listen-on port 53 { 127.0.0.1; }; // listen-on-v6 port 53 { ::1; }; // allow-query { localhost; };
3.添加区域记录
[root@dns ~]# vim /etc/named.rfc1912.zones zone "mylinuxops.com" IN { type master; file "mylinuxops.com.zone"; };
4.创建区域数据库文件
[root@dns etc]# cp -p /var/named/named.localhost /var/named/mylinuxops.com.zone [root@dns 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.检查语法错误
[root@dns etc]# named-checkconf [root@dns etc]# named-checkzone mylinuxops.com /var/named/mylinuxops.com.zone zone mylinuxops.com/IN: loaded serial 0 OK
6.启动服务
[root@dns etc]# systemctl start named
7.测试
[root@dns 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.创建出需要共享的目录,修改权限
[root@localhost ~]# mkdir /data/test/ [root@localhost ~]# setfacl -R -m u:nfsnobody:rwx /data/test
2.修改配置文件
[root@localhost ~]# vim /etc/exports /data/test *(rw)
3.在httpd挂载nfs
[root@httpd ~]# showmount -e 192.168.73.120 Export list for 192.168.73.120: /data/test * [root@httpd ~]# mount 192.168.73.120:/data/test /data/test
4.在httpd2上挂载nfs
[root@httpd2 ~]# showmount -e 192.168.73.120 Export list for 192.168.73.120: /data/test * [root@httpd2 ~]# 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
[root@localhost ~]# 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 [root@localhost ~]# 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
[root@httpd ~]# tar wordpress-5.0.3-zh_CN.tar.gz -C /data/test/
2.创建wordpress所需数据库及账户
[root@master ~]# mysql -e "CREATE DATABASE wordpress" [root@master ~]# mysql -e "GRANT ALL ON wordpress.* TO 'wpuser'@'192.168.73.%' IDENTIFIED BY '111111';"
3.修改wordpress配置文件
[root@httpd ~]# cp /data/test/wordpress/wp-config-sample.php /data/test/wordpress/wp-config.php [root@httpd ~]# 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.在客户端测试访问
相关文章推荐
- UCloud上LAMP小型网站搭建与测试
- LAMP架构搭建与优化(3.3-3.5)
- 无人值守安装系统及lamp和storage架构的搭建【Linux运维之道之脚本案例】
- LAMP架构原理及搭建详解 推荐
- 全新64位CentOS6.X上LAMP架构搭建备忘-apache的安装问题解决
- LAMP网站架构搭建及应用案例-亨利工作室
- linux系统之基于lamp架构搭建一个简单论坛
- lamp架构的搭建(apache、mysql、php编译安装)
- LAMP架构搭建和disuz论坛搭建
- LAMP架构的搭建
- LAMP架构网站搭建
- 搭建LAMP架构— 3、手工编译安装PHP
- 用LAMP架构搭建自己的博客
- LAMP架构的论坛搭建
- Lamp架构搭建 --之论坛搭建和交叉存储
- LAMP架构搭建+Discuz论坛搭建【weber出品必属精品】
- LAMP架构的搭建(Linux+Nginx+MySQL+PHP源码安装 )+Discuz论坛的搭建
- 项目成长记(三) ―― 小型架构搭建
- LAMP网站架构的基础构思及搭建解析——CentOS7.0
- linux——lamp简介,架构搭建,Linux+Apache+Mysql/MariaDB+Php