NFS+Rsync+Inotify+Nginx+php

本文档详细介绍了如何在Linux环境中搭建LNMP架构,包括安装nginx、PHP和MariaDB,配置WordPress和知乎站点,以及通过NFS实现两台web服务器的文件同步。同时,设置了NFS服务器上的目录进行rsync实时备份,并在前端部署了负载均衡。整个过程覆盖了服务器配置、数据库管理、文件同步和高可用性设置等多个方面。
摘要由CSDN通过智能技术生成

在这里插入图片描述

题目:

1.搭建lnmp架构
2.搭建博客与知乎
3.两台web服务器实现文件同步
4.nfs文件实时备份到backup

解:

一、web01与web02客户端操作

1.安装nginx和PHP服务

(1)配置官方源

[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo 
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

(2)yum安装nginx

[root@web01 ~]# yum install -y nginx

(3)配置nginx

[root@web01 ~]# vim /etc/nginx/nginx.conf 
user  www;
...
http {
    client_max_body_size 200m;
}

(4)创建用户

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

(5)启动

[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx   #开机自启动

(6)上传php包

[root@web01 ~]# rz
[root@web01 ~]# ll
-rw-r--r--  1 root root 19889622 Nov 22 15:52 php.tar.gz

(7)安装

[root@web01 ~]# tar xf php.tar.gz 
[root@web01 ~]# yum localinstall -y *.rpm

8)配置PHP

[root@web01 ~]# vim /etc/php-fpm.d/www.conf 
user = www
group = www

[root@web01 ~]# vim /etc/php.ini
upload_max_filesize = 200M
post_max_size = 200M

(9)启动

[root@web01 ~]# systemctl start php-fpm
[root@web01 ~]# systemctl enable php-fpm   #开机自启动
2.搭建wordpress、知乎

(1)上传代码包

[root@web01 ~]# mkdir /code
[root@web01 ~]# cd /code/
[root@web01 code]# rz
[root@web01 code]# ll
total 86372
-rw-r--r-- 1 root root  8451194 Dec  1 09:07 WeCenter_3-2-1.zip
-rw-r--r-- 1 root root 11098483 Sep 12 17:52 wordpress-5.0.3-zh_CN.tar.gz

(2)解压代码包

[root@web01 code]# tar xf wordpress-5.0.3-zh_CN.tar.gz
[root@web01 code]# unzip WeCenter_3-2-1.zip

[root@web01 code]# mv WeCenter_3-2-1 zhihu

[root@web01 code]# ll
drwxr-xr-x  5 1006  1006     4096 Jan 11  2019 wordpress
drwx------ 14 root root       296 Jun  4  2018 zhihu

(3)授权代码

[root@web01 code]# chown -R www.www /code/

(4)配置wordpress的nginx

[root@web01 ~]# vim /etc/nginx/conf.d/linux.wp.com.conf
server {
    listen 80;
    server_name linux.wp.com;

    location / {
        root /code/wordpress;
        index index.php;
    }

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME /code/wordpress/$fastcgi_script_name;
        include fastcgi_params;
    }
}

(5)配置知乎的nginx配置

[root@web03 ~]# vim /etc/nginx/conf.d/linux.zh.com.conf
server {
    listen 80;
    server_name linux.zh.com;
    root /code/zhihu;

    location / {
        index index.php;
    }

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

(6)重启nginx

[root@web03 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web03 ~]# systemctl restart nginx

(7)配置本地hosts访问测试

10.0.0.7 linux.zh.com   linux.wp.com   
10.0.0.8 linux.zh.com   linux.wp.com   

二、db01 mariadb数据库服务端操作

1.安装 mariadb
[root@db01 ~]# yum install -y mariadb-server
2.启动
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# systemctl enable mariadb
3.设置数据库密码
[root@db01 ~]# mysqladmin -uroot password
New password: 123
Confirm new password: 123
4.使用密码连接数据库测试
[root@db01 ~]# mysql -uroot -p
Enter password: 123
5.查看数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6.数据库建库
MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)
​
MariaDB [(none)]> create database zhihu;
Query OK, 1 row affected (0.00 sec)
​
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
| zhihu              |
+--------------------+
6 rows in set (0.00 sec)
7.创建数据库用户并授权
MariaDB [(none)]> grant all on wordpress.* to wp@'172.16.1.%' identified by '123';
Query OK, 0 rows affected (0.07 sec)

MariaDB [(none)]> grant all on zhihu.* to zh@'172.16.1.%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

#查看用户
MariaDB [(none)]> select user,host from mysql.user;
+------+------------+
| user | host       |
+------+------------+
| root | 127.0.0.1  |
| wp   | 172.16.1.% |
| zh   | 172.16.1.% |
| root | ::1        |
|      | db01       |
| root | db01       |
|      | localhost  |
| root | localhost  |
| wp   | localhost  |
| zh   | localhost  |
+------+------------+
10 rows in set (0.00 sec)
8.浏览器搜索:linux.zh.com.install 或 linux.wp.com ===》根据页面提示操作

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

三、Nfs服务端共享操作

(1)关闭防火墙和selinux
(2)安装NFS

[root@nfs ~]# yum install -y nfs-utils

(3)创建挂载目录

[root@nfs ~]# mkdir /data/wp -p

(4)配置NFS

[root@nfs ~]# vim /etc/exports
/data/wp 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/zh 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

(5)创建用户

[root@nfs ~]# groupadd www -g 666
[root@nfs ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

(6)授权与启动nfs

[root@nfs ~]# chown -R www.www /data
[root@nfs ~]# systemctl start nfs

(7)检查配置

[root@nfs ~]# cat /var/lib/nfs/etab 
/data/zh	172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=666,anongid=666,sec=sys,rw,secure,root_squash,all_squash)
/data/wp	172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=666,anongid=666,sec=sys,rw,secure,root_squash,all_squash)

四、web01与web02客户端挂载到NFS

(1)安装nfs和rpcbind

[root@web01 ~]# yum install -y nfs-utils rpcbind

(2)启动nfs、rpcbind

[root@web01 ~]# systemctl start nfs rpcbind

(3)查看挂载点

[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/wp 172.16.1.0/24

(4)确定挂载目录

[root@web01 ~]# ll /code/wordpress/wp-content/uploads/

(5)先推送挂载目录下的文件

[root@web01 ~]# rsync -avz /code/wordpress/wp-content/uploads/ 172.16.1.31:/data/wp/
[root@web02 ~]# rsync -avz /code/wordpress/wp-content/uploads/ 172.16.1.31:/data/wp/

#验证文件
[root@nfs ~]# ll /data/wp/2020/12/

(6)挂载

[root@web01 ~]# mount -t nfs 172.16.1.31:/data/wp /code/wordpress/wp-content/uploads
[root@web02 ~]# mount -t nfs 172.16.1.31:/data/wp /code/wordpress/wp-content/uploads

(7) 检查挂载

[root@web01 ~]# df -h
文件系统              容量  已用  可用 已用% 挂载点
/dev/sda3              18G  1.7G   17G   10% /
devtmpfs              479M     0  479M    0% /dev
tmpfs                 489M     0  489M    0% /dev/shm
tmpfs                 489M  6.7M  482M    2% /run
tmpfs                 489M     0  489M    0% /sys/fs/cgroup
/dev/sda1            1014M  119M  896M   12% /boot
tmpfs                  98M     0   98M    0% /run/user/0
172.16.1.31:/data/wp   18G  1.3G   17G    8% /code/wordpress/wp-content/uploads
172.16.1.31:/data/zh   18G  1.3G   17G    8% /code/zhihu/uploads

五、backup服务端配置rsync实时备份

(1)安装inotify-tools、rsync

[root@backup ~]# yum install -y rsync

(2)修改rsync配置文件

[root@backup ~]# vim /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = true
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[web_data]
comment = "该备份文件是web端挂载到nfs服务器的文件"
path = /data

(3)创建用户、修改权限

[root@backup ~]# groupadd www -g 666
[root@backup ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
#配置密码文件
[root@backup ~] echo "rsync_backup:123456" > /etc/rsyncd.passwd
[root@backup ~] chmod 600 /etc/rsyncd.passwd 

(4)创建共享目录、授权

[root@backup ~]# mkdir /data
[root@backup ~]# chown -R www.www /data

(5)启动rsync服务

[root@backup ~]# systemctl start rsyncd

六、NFS配置rsync实时备份

(1)下载inotify-tools

[root@nfs ~]# yum install -y inotify-tools

(2)编写实时备份脚本

[root@nfs ~]# vim rsyn-inotify.sh
#!/bin/bash
export RSYNC_PASSWORD=123456
dir=/data
    /usr/bin/inotifywait -mrq --format '%w %f' -e create,delete,attrib,close_write $dir | while read line;do
    cd $dir && rsync -az -R --delete . rsync_backup@172.16.1.41::web_data
>/dev/null 2>&1
done &

(3)运行实时备份脚本

[root@nfs ~]# sh rsyn-inotify.sh 

七、在web前搭建七层负载均衡(lb01)

1.配置wordpress负载均衡
[root@lb01 ~]# vim /etc/nginx/conf.d/linux.wp.com.conf
upstream blog {
    server 172.16.1.7;
    server 172.16.1.8;
}

server {
    listen 80;
    server_name linux.wp.com;

    location / {
        proxy_pass http://blog;
        include proxy_params;
    }   
}

#重启
[root@lb01 ~]# systemctl restart nginx
2.配置hosts查看网站
10.0.0.4 linux.wp.com
3.配置zh的负载均衡
[root@lb01 ~]# vim /etc/nginx/conf.d/linux.zh.com.conf
upstream zh {
    server 172.16.1.7;
    server 172.16.1.8;
}

server {
    listen 80;
    server_name linux.zh.com;

    location / {
        proxy_pass http://zh;
        include proxy_params;
    }
}
4.配置hosts查看网站
10.0.0.4 linux.zh.com
5.重启访问
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值