一、 環境
二、流程(思路)
1、先安裝LNMP(192.168.1.22+192.168.1.24+122.51.223.193),安裝wordpress
2、配置nginx負載均衡
3、添加php均衡
4、配置mariadb主從
三、安裝lnmp及wordpress
1、安裝nginx(192.168.1.22)
1)、Nginx安裝
Nginx這裏使用阿里的epel源直接yum安裝
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# yum clean all
[root@localhost ~]# yum makecache
[root@localhost ~]# yum install –y nginx-1.16.1 安裝nginx-1.16.1版本
下載wordpress並放至項目目錄
[root@localhost src]# wget -c https://wordpress.org/latest.tar.gz -O /usr/share/nginx/html/wordpress.tar.gz
[root@localhost src]# cd /usr/share/nginx/html/
[root@localhost html]# tar xf wordpress.tar.gz
[root@localhost html]# chown -R nginx:nginx wordpress
2)、配置
vim /etc/nginx/nginx.conf
[root@localhost html]# vim /etc/nginx/nginx.conf
location / {
root /usr/share/nginx/html/wordpress;
index index.php;
}
location ~ \.php$ {
root /data/nginx/html/wordpress/; PHP的目錄
fastcgi_pass 192.168.1.24:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
2、安裝PHP(192.168.1.24)
1)、更換清華源,安裝php5.6,默認是5.4
[root@localhost ~]# yum install https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
[root@localhost yum.repos.d]# vim remi.repo
30 enabled=1
安裝php
[root@localhost ~]# yum install php php-fpm php-mysql php-devel php-xml –y
配置php-fpm用戶組
[root@localhost ~]# vim /etc/php-fpm.d/www.conf
user = nginx
group = nginx
listen = 192.168.1.24:9000
;listen.allowed_clients = 這裏指的是nginx服務的IP,這裏將他註釋
[root@localhost ~]# chown -R nginx:nginx /var/lib/php/session/
[root@localhost ~]# mkdir -p /data/nginx/html PHP的目錄
[root@localhost ~]# chown -R nginx:nginx /data/nginx/html/ PHP的目錄
項目同步,將nginx的項目目錄同步到PHP的目錄中,包含權限
[root@localhost ~]# rsync -av [email protected]:/usr/share/nginx/html/wordpress /data/nginx/html/
關閉selinux,開啓9000端口後訪問瀏覽器
3、安裝mariadb(122.51.223.193)
安裝
[root@VM_0_14_centos ~]# yum install mariadb mariadb-devel mariadb-server mariadb-libs –y
[root@VM_0_14_centos ~]# systemctl start mariadb
對PHP授權
[root@VM_0_14_centos ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database wordpress charset utf8;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on wordpress.* to "wordpress"@"%" identified by "123456";
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)
注:這裏是公網對內網授權,所以寫的是"%"
4、瀏覽器安裝wrodpress
注:如果有頁面排版亂碼的現象,需要反同步一下nginx的項目目錄
Php端
[root@localhost html]# rsync -av /data/nginx/html/wordpress/ [email protected]:/usr/share/nginx/html/wordpress/
四、安裝配置nginx集羣
1、配置
Nginx1:192.168.1.22服務器
[root@localhost ~]# vim /etc/nginx/nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
#server_name _;
root /usr/share/nginx/html/wordpress;
index index.php;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root /usr/share/nginx/html/wordpress;
index index.php;
}
location ~ \.php$ {
root /data/nginx/html/wordpress/;
fastcgi_pass 192.168.1.24:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Nginx1:192.168.1.23服務器 同上
Nginx1:192.168.1.21均衡服務器
[root@localhost ~]# vim /etc/nginx/nginx.conf
[root@localhost ~]# vim /etc/nginx/nginx.conf 注:這裏upstream塊與server平級,都在http塊中
upstream web {
ip_hash;
server 192.168.1.22 weight=1;
server 192.168.1.23 weight=1;
}
server {
listen 80;
server_name blog.yjy.com;
location /{
proxy_pass http://web;
proxy_next_upstream error http_404 http_502;
}
}
瀏覽器訪問
此時,停止任意一臺nginx服務器,都不影響客戶使用
五、配置php-fpm均衡
Php-fpm2 192.168.1.25配置
[root@localhost ~]# vim /etc/php-fpm.d/www.conf
user = nginx
group = nginx
listen = 192.168.1.24:9000
#listen.allowed_clients = 這裏指的是nginx服務的IP,這裏將他註釋
[root@localhost ~]# chown -R nginx:nginx /var/lib/php/session/
[root@localhost ~]# mkdir -p /data/nginx/html PHP的目錄
[root@localhost ~]# chown -R nginx:nginx /data/nginx/html/ PHP的目錄
其他安裝步驟一樣,直接同步動態目錄
[root@localhost ~]# rsync -av [email protected]:/data/nginx/html/ /data/nginx/html/
nginx1(192.168.1.22)及nginx2(192.168.1.23)上面配置php-fpm均衡
[root@localhost ~]# vim /etc/nginx/nginx.conf
upstream cgi_wordpress{
server 192.168.1.24:9000;
server 192.168.1.25:9000;
}
location ~ \.php$ {
root /data/nginx/html/wordpress/;
fastcgi_pass cgi_wordpress;
fastcgi_index index.php;
fastcgi_next_upstream error; 增加此行
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
測試php均衡,停止192.168.1.24上面的PHP-fpm,看是否可以繼續訪問網頁
六、配置主從
1、配置my.cnf
Mariadb master(122.51.223.193)配置
[root@VM_0_14_centos ~]# vim /etc/my.cnf
[mysqld]
log-bin=mariadb-bin
server-id=1
mariadb slave(60.165.98.10)配置
[root@instance-plpaum9p ~]# vim /etc/my.cnf
[mysqld]
server-id=2
2、啓動mariadb
[root@VM_0_14_centos ~]# systemctl restart mariadb master
[root@instance-plpaum9p ~]# systemctl restart mariadb slave
3、主庫wordpress數據導出且記錄bin文件與pos點
[root@VM_0_14_centos ~]# mysqldump -B --master-data wordpress >wordpress.sql
[root@VM_0_14_centos ~]# less wordpress.sql
4、主庫授權
MariaDB [(none)]> grant replication slave on *.* to "tongbu"@"182.61.46.116" identified by "123456";
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
5、從庫
新建wordpress數據庫
MariaDB [(none)]> create database wordpress charset utf8;
數據導入
[root@instance-plpaum9p ~]# mysql <wordpress.sql
連接主庫
MariaDB [(none)]> change master to master_host="122.51.223.193",
-> master_user="tongbu",
-> master_password="123456",
-> master_log_file="mariadb-bin.000001",
-> master_log_pos=245;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 122.51.223.193
Master_User: tongbu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 476
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 762
Relay_Master_Log_File: mariadb-bin.000001
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: 476
Relay_Log_Space: 1058
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)
6、主從測試
在wordpress中新建用戶,在從庫中查
測試OK
注:主從同步時,如果只同步一個數據庫,
1、在主的my.cnf中加上一句binlog-do-db=wrodpress,見下
[mysqld]
log-bin=mariadb-bin
server-id=1
binlog-do-db=wrodpress
2、主庫授權時,不用另說明授權庫
3、從庫change master時,不用添加同步數據庫的說明
------------end