一、数据库迁移

1.常见原因

1.数据库要做升级
2.数据库服务器到期需要迁移

2.新服务器搭建数据库

[root@db02 ~]# yum install -y mariadb-server

3.启动

[root@db02 ~]# systemctl start mariadb
[root@db02 ~]# systemctl enable mariadb

4.配置数据库密码

[root@db02 ~]# mysqladmin -uroot password 'Linhd@123'

5.密码连接数据库

[root@db02 ~]# mysql -uroot -p
Enter password:

6.旧数据导出数据库

[root@db01 ~]# mysqldump -uroot -p -B wordpress zh edusoho > /tmp/full.sql 
Enter password:
[root@db01 ~]# ll /tmp/full.sql
-rw-r--r-- 1 root root 1244924 Aug 28 16:46 /tmp/full.sql

7.新数据库授权

MariaDB [(none)]> grant all on *.* to root@'172.16.1.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

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

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

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

8.旧数据导入新数据库

[root@db01 ~]# mysql -uroot -p -h172.16.1.52 < /tmp/full.sql 
Enter password: 123456

9.修改项目配置

1.wordpress代码
[root@web01 ~]# vim /code/wordpress/wp-config.php
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');

/** MySQL数据库用户名 */
define('DB_USER', 'wp');

/** MySQL数据库密码 */
define('DB_PASSWORD', '123');

/** MySQL主机 */
define('DB_HOST', '172.16.1.52');
2.知乎代码
[root@web01 ~]# vim /code/zh/system/config/database.php 
<?php

$config['charset'] = 'utf8';^M
$config['prefix'] = 'aws_';^M
$config['driver'] = 'MySQLi';^M
$config['master'] = array (
 'charset' => 'utf8',
 'host' => '172.16.1.52',
 'username' => 'zh',
 'password' => '234',
 'dbname' => 'zh',
);^M
$config['slave'] = false;^M
3.edosoho代码
[root@web01 ~]# vim /code/edusoho/app/config/parameters.yml 
parameters:
  database_driver: pdo_mysql
  database_host: 172.16.1.52
  database_port: 3306
  database_name: edusoho
  database_user: edu
  database_password: '345'

10.排错

1.查看浏览器检查,找到出问题的接口或者文件页面
2.查看日志
[root@web01 ~]# ll /var/log/nginx/access.log
[root@web01 ~]# ll /var/log/nginx/error.log
业务进程日志
3.把日志给开发
4.4xx自己解决
5.5xx给开发

二、拆分PHP

1.修改nginx配置

[root@web01 ~]# vim /etc/nginx/conf.d/linux.blog.com.conf 
server {
  listen 80;
  server_name linux.blog.com;
  root /code/wordpress;
  location / {
      index index.php;
  }
  location ~* \.php$ {
      fastcgi_pass 172.16.1.8:9000;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
  }
}

2.重启访问

[root@web01 ~]# systemctl restart nginx

#访问页面http://linux.blog.com/,报错502

3.修改PHP监听端口

[root@web02 ~]# vim /etc/php-fpm.d/www.conf
#修改监听地址
listen = 172.16.1.8:9000
#修改允许连接的ip地址
listen.allowed_clients = 172.16.1.7

#重启服务
[root@web02 /code]# systemctl restart php-fpm

4.再次访问页面

#报错页面找不到或者文件找不到

5.同步nginx站点目录

[root@web01 /code]# scp -r wordpress 172.16.1.8:/code/

6.再次访问测试

#图片丢失

7.php服务器挂载

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

8.再次访问测试没有问题

三、数据文件实时备份

1.服务端(backup)

1.安装
[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 = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[data]
comment = welcome to oldboyedu backup!
path = /data

3.创建目录
[root@backup ~]# groupadd www -g 666
[root@backup ~]# useradd www -u 666 -g 666

4.创建密码文件
[root@backup ~]# echo "rsync_backup:123456" > /etc/rsync.passwd
[root@backup ~]# chmod 600 /etc/rsync.passwd

5.创建目录
[root@backup ~]# mkdir /data
[root@backup ~]# chown -R www.www /data/

6.启动
[root@backup ~]# systemctl start rsyncd
[root@backup ~]# ps -ef | grep rsync

2.客户端(nfs)

1.安装
[root@nfs ~]# yum install -y rsync inotify-tools

#安装sersync
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/sersync

2.配置sersync
[root@nfs ~]# vim /usr/local/sersync/confxml.xml

3.创建密码文件
[root@nfs ~]# echo 123456 > /etc/rsync.password
[root@nfs ~]# chmod 600 /etc/rsync.password

4.启动
[root@nfs /data]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml

四、代理

1.什么是代理?

代理一词往往并不陌生, 该服务我们常常用到如(代理理财、代理租房、代理收货等等),如下图所示

2.没有代理

在没有代理模式的情况下,客户端和Nginx服务端,都是客户端直接请求服务端,服务端直接响应客户端。

3.有代理

那么在互联网请求里面,客户端往往无法直接向服务端发起请求,那么就需要用到代理服务,来实现客户端和服务通信,如下图所示

4.代理常见模式

1.常用模式
Nginx作为代理服务,按照应用场景模式进行总结,代理分为
1.正向代理
2.反向代理
2.正向代理
正向代理,(内部上网)客户端<—>代理->服务端

3.反向代理
反向代理,用于公司集群架构中,客户端->代理<—>服务端

4.正向代理和反向代理的区别
1.区别在于形式上服务的"对象"不一样
2.正向代理代理的对象是客户端,为客户端服务
3.反向代理代理的对象是服务端,为服务端服务

五、nginx代理服务支持协议

1.支持的协议

2.代理的模式

3.代理的模块

反向代理模式与Nginx代理模块总结如表格所示
反向代理模式 Nginx配置模块
http、websocket、https、tomcat(java程序) ngx_http_proxy_module
fastcgi(PHP程序) ngx_http_fastcgi_module
uwsgi(python程序) ngx_http_uwsgi_module
grpc(go程序)(golang) ngx_http_v2_module

六、nginx代理配置

1.代理语法

Syntax:    proxy_pass URL;
Default:   —
Context:   location, if in location, limit_except

2.环境准备

主机 IP 身份
lb01 10.0.0.4,172.16.1.4 代理
web01 172.16.1.7 服务端

3.配置web01界面

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

  location / {
      root /code/proxy;
      index index.html;
  }
}

[root@web01 ~]# systemctl restart nginx

4.编写网站

[root@web01 ~]# mkdir /code/proxy
[root@web01 ~]# vim /code/proxy/index.html
web01 .............

5.访问测试

#配置本地hosts
网页访问linux.proxy.com

6.配置代理

#安装nginx
#配置nginx
#创建用户
#配置站点配置文件
[root@lb01 ~]# vim /etc/nginx/conf.d/daili.conf
server {
  listen 80;
  server_name linux.proxy.com;

  location / {
      proxy_pass http://172.16.1.7:80;
       #proxy_pass http://10.0.0.7:80; #正常情况我们使用内网IP,抓包测试使用外网
  }
}
[root@lb01 ~]# systemctl start nginx

7.访问页面测试

#访问http://linux.proxy.com/返回结果不是我们要的内容
原因:
代理请求后端时,没有使用域名,是使用IP访问的,匹配的配置文件是nginx中第一个配置文件

8.配置携带域名去管理

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

  location / {
      proxy_pass http://10.0.0.7:80;
      proxy_set_header Host $http_host;
  }
}

七、实战演练

1.需求

1.恢复快照
2.搭建两台LNMP
3.数据库独立
4.文件共享
5.文件实时备份
6.代理一台机器

2.环境准备

主机 角色 IP
webo1 ngixn服务器 10.0.0.7
web02 nginx服务器 10.0.0.8
web03 代理服务器 10.0.0.9
db01 数据库服务器 10.0.0.51
nfs01 文件共享服务器 10.0.0.61
backup 备份服务器 10.0.0.41

3.web01服务配置

1.搭建nginx服务
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.安装依赖
[root@web01 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

3.安装nginx
[root@web01 ~]# yum -y install nginx

4.配置nginx
[root@web01 ~]# vim /etc/nginx/nginx.conf
user www;
client_max_body_size 200m;

5.创建统一用户
[root@web01 ~]# groupadd -g 666 www
[root@web01 ~]# useradd www -u 666 -g 666

6.检查配置
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

7.启动服务并设置开机自启
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
2.安装PHP
1.创建目录
[root@web01 /code]# mkdir /package

2.上传源码包并解压到指定文件夹
[root@web01 ~]# rz                                                                         [root@web01 ~]# ll  
-rw-r--r--  1 root root 19889622 2020-08-30 14:01 php.tar.gz
[root@web01 ~]# tar xf php.tar.gz -C /package/

3.安装PHP
[root@web01 /package]# cd /package/
[root@web01 /package]# yum -y localinstall *.rpm

4.配置PHP
[root@web01 /package]# vim /etc/php-fpm.d/www.conf
user = www
group = www

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

4.启动PHP并设置开机自启
[root@web01 /package]# systemctl start php-fpm.service
[root@web01 /package]# systemctl enable php-fpm.service
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
3.配置本地数据库
1.安装数据库
[root@web01 /package]# yum -y install mariadb-server

2.启动服务并设置开机自启
[root@web01 /package]# systemctl start mariadb.service
[root@web01 /package]# systemctl enable mariadb.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

3.设置数据库密码
[root@web01 /package]# mysqladmin -u root password
New password:
Confirm new password:

4.验证密码
[root@web01 /package]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
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)]>

5.创建数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql             |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> create database zh;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)
4.安装知乎、wordpress
1.配置知乎站点目录
[root@web01 ~]# vim /etc/nginx/conf.d/linux.zh.com.conf
server {
  listen 80;
  server_name linux.zh.com;
  root /code/zh;

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;
}
}

2.配置wordpress站点目录
[root@web01 ~]# cp /etc/nginx/conf.d/linux.zh.com.conf /etc/nginx/conf.d/linux.wordpress.com.conf
[root@web01 ~]# vim /etc/nginx/conf.d/linux.wordpress.com.conf
server {
  listen 80;
  server_name linux.wordpress.com;
  root /code/wordpress;

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;
}
}

3.创建目录
[root@web01 ~]# mkdir /code

4.上传源码包并解压
[root@m01 ~]# scp ./* root@172.16.1.7:/root
[root@web01 ~]# tar xf wordpress-5.0.3-zh_CN.tar.gz -C /code/
[root@web01 ~]# unzip WeCenter_3-2-1.zip -d /code/
[root@web01 /code]# cd /code
[root@web01 /code]# ll
total 4
drwxr-xr-x  5 1006 1006 4096 2019-01-11 18:00 wordpress
drwx------ 14 root root  296 2018-06-04 14:12 zh

5.授权目录
[root@web01 /code]# chown -R www:www   /code/

6.检查配置并重启配置
[root@web01 /package]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /package]# systemctl restart nginx

7.配置本地hosts文件
C:\Windows\System32\drivers\etc
10.0.0.7 linux.wordpress.com
10.0.0.7 linux.zh.com

4.web02服务配置

1.搭建nginx服务
1.配置官方源
[root@web02 ~]# 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.安装依赖
[root@web02 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

3.安装nginx
[root@web02 ~]# yum -y install nginx

4.配置nginx
[root@web02 ~]# vim /etc/nginx/nginx.conf
user www;
client_max_body_size 200m;

4.创建统一用户
[root@web02 ~]# groupadd -g 666 www
[root@web02 ~]# useradd www -u 666 -g 666

5.检查配置
[root@web02 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

6.启动服务并设置开机自启
[root@web02 ~]# systemctl start nginx
[root@web02 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

7.配置nginx站点目录
[root@web02 /package]# scp root@172.16.1.7:/etc/nginx/conf.d/* /etc/nginx/conf.d/
The authenticity of host '172.16.1.7 (172.16.1.7)' can't be established.
ECDSA key fingerprint is SHA256:g6buQ4QMSFl+5MMAh8dTCmLtkIfdT8sgRFYc6uCzV3c.
ECDSA key fingerprint is MD5:5f:d7:ad:07:e8:fe:d2:49:ec:79:2f:d4:91:59:c5:03.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.7' (ECDSA) to the list of known hosts
root@172.16.1.7's password:  
linux.wordpress.com.conf                                                               100%  282   185.9KB/s   00:00    
linux.zh.com.conf                                                                      100%  268    78.3KB/s   00:00    

8.检查服务并重启服务
[root@web02 /package]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 /package]# systemctl restart nginx

[root@web02 /package]#
[root@web02 /package]# systemctl restart nginx

9.创建目录
[root@web02 /package]# mkdir /code

10.授权目录
[root@web02 /package]# chown -R www:www /code/

11.推送web01站点文件
[root@web02 /package]# scp -r root@172.16.1.7:/code/* /code/

12.配置本地hosts
C:\Windows\System32\drivers\etc
10.0.0.8 linux.wordpress.com
10.0.0.8     linux.zh.com
2.安装PHP
1.创建目录
[root@web02 ~]# mkdir /package

2.上传源码包并解压到指定目录
[root@web02 ~]# ll
-rw-r--r--  1 root root 19889622 2020-08-30 15:33 php.tar.gz
[root@web02 ~]# tar xf php.tar.gz -C /package/

3.安装PHP
[root@web02 ~]# cd /package/
[root@web02 /package]# yum -y localinstall *.rpm

4.配置php
[root@web02 /package]# vim /etc/php-fpm.d/www.conf
user = www
group = www

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

5.启动PHP并设置开机自启
[root@web02 /package]# systemctl start php-fpm.service
[root@web02 /package]# systemctl enable php-fpm.service
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

5.分离数据库

1.db01安装数据库
1.安装数据库
[root@db01 ~]# yum -y install mariadb-server.x86_64

2.启动数据库并设置开机自启
[root@db01 ~]# systemctl start mariadb.service
[root@db01 systemctl enable mariadb.servicevice
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

3.设置数据密码
[root@db01 ~]# mysqladmin -uroot password
New password:
Confirm new password:

4.验证密码
[root@db01 ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
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)]>
2.web01导出旧数据
1.导出web01数据
[root@web01 /code]# mysqldump -uroot -proot -B zh >/tmp/zh.sql
You have new mail in /var/spool/mail/root
[root@web01 /code]# mysqldump -uroot -proot -B wordpress >/tmp/wordpress.sql

2.推送web01数据
[root@web01 /code]# scp /tmp/zh.sql root@172.16.1.51:/tmp
The authenticity of host '172.16.1.51 (172.16.1.51)' can't be established.
ECDSA key fingerprint is SHA256:g6buQ4QMSFl+5MMAh8dTCmLtkIfdT8sgRFYc6uCzV3c.
ECDSA key fingerprint is MD5:5f:d7:ad:07:e8:fe:d2:49:ec:79:2f:d4:91:59:c5:03.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.51' (ECDSA) to the list of known hosts.
root@172.16.1.51's password:
zh.sql                                                                                 100% 121KB   3.7MB/s   00:00    
[root@web01 /code]# scp /tmp/wordpress.sql   root@172.16.1.51:/tmp
root@172.16.1.51's password:
wordpress.sql                                                                          100% 868KB   7.9MB/s   00:00    
[root@web01 /code]#

3.配置wordpresss连接数据库文件
[root@web01 /code]# vim /code/wordpress/wp-config.php
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');

/** MySQL数据库用户名 */
define('DB_USER', 'wp');

/** MySQL数据库密码 */
define('DB_PASSWORD', 'wp123');

/** MySQL主机 */
define('DB_HOST', '172.16.1.51');

/** 创建数据表时默认的文字编码 */
define('DB_CHARSET', 'utf8mb4');

/** 数据库整理类型。如不确定请勿更改 */
define('DB_COLLATE', '');

4.配置知乎连接数据库文件
[root@web01 /code]# vim /code/zh/system/config/database.php
<?php
$config['charset'] = 'utf8';^M
$config['prefix'] = 'aws_';^M
$config['driver'] = 'MySQLi';^M
$config['master'] = array (
 'charset' => 'utf8',
 'host' => '172.16.1.51',
 'username' => 'zh',
 'password' => 'zh123',
 'dbname' => 'zh',
);^M
$config['slave'] = false;^M
3.web02配置连接远程数据库
1.配置wordpresss连接数据库文件
[root@web01 /code]# vim /code/wordpress/wp-config.php
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');

/** MySQL数据库用户名 */
define('DB_USER', 'wp');

/** MySQL数据库密码 */
define('DB_PASSWORD', 'wp123');

/** MySQL主机 */
define('DB_HOST', '172.16.1.51');

/** 创建数据表时默认的文字编码 */
define('DB_CHARSET', 'utf8mb4');

/** 数据库整理类型。如不确定请勿更改 */
define('DB_COLLATE', '');

2.配置知乎连接数据库文件
[root@web01 /code]# vim /code/zh/system/config/database.php
<?php
$config['charset'] = 'utf8';^M
$config['prefix'] = 'aws_';^M
$config['driver'] = 'MySQLi';^M
$config['master'] = array (
 'charset' => 'utf8',
 'host' => '172.16.1.51',
 'username' => 'zh',
 'password' => 'zh123',
 'dbname' => 'zh',
);^M
$config['slave'] = false;^M
4.db01导入web01鸠数据
1.导入旧数据
[root@db01 ~]# mysql -uroot -p </tmp/zh.sql
Enter password:
[root@db01 ~]# mysql -uroot -p </tmp/wordpress.sql
Enter password:

2.检验数据库
[root@db01 ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
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)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql             |
| performance_schema |
| test               |
| wordpress         |
| zh                 |
+--------------------+
6 rows in set (0.00 sec)

3.数据库授权
MariaDB [(none)]> grant all on zh.* to zh@'172.16.1.%' identified by 'zh123';
Query OK, 0 rows affected (0.00 sec)

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

4.刷新数据库
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

5.页面刷新正常登陆

6.配置nfs文件共享

1.nfs服务器安装nfs
1.安装nfs
[root@nfs ~]# yum -y install rpcbind nfs-utils

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

3.创建统一用户
[root@nfs ~]# groupadd www -g 666
[root@nfs ~]# useradd www -u 666 -g 666

4.创建目录
[root@nfs ~]# mkdir -p /data/zh
[root@nfs ~]# mkdir -p /data/wp

5.授权目录
[root@nfs ~]# chown -R www:www /data/

6.启动服务并设置开机重启
[root@nfs ~]# systemctl start nfs
[root@nfs ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

7.检查配置
[root@nfs ~]# cat /var/lib/nfs/etab
/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)
/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)
2.web01配置nfs
1.安装nfs
[root@web01 ~]# yum -y install rpcbind nfs-utils

2.启动服务并设置开机自启
[root@web01 ~]# systemctl start nfs
[root@web01 ~]# systemctl start rpcbind
[root@web01 ~]# systemctl enable nfs rpcbind
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

3.查看挂载点
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/wp 172.16.1.0/24
/data/zh 172.16.1.0/24

4.推送源数据到挂载目录
[root@web01 ~]# scp -r /code/zh/uploads/* root@172.16.1.31:/data/zh
The authenticity of host '172.16.1.31 (172.16.1.31)' can't be established.
ECDSA key fingerprint is SHA256:g6buQ4QMSFl+5MMAh8dTCmLtkIfdT8sgRFYc6uCzV3c.
ECDSA key fingerprint is MD5:5f:d7:ad:07:e8:fe:d2:49:ec:79:2f:d4:91:59:c5:03.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.31' (ECDSA) to the list of known hosts.
root@172.16.1.31's password:
ffa5e93370534a62abe7746195595d5a.jpg                                                   100%   45KB   6.0MB/s   00:00    
20017b35a334e6966cd8fa21524be378.jpg                                                   100% 443KB   7.2MB/s   00:00    
170x110_20017b35a334e6966cd8fa21524be378.jpg                                           100% 4044     1.6MB/s   00:00    
90x90_20017b35a334e6966cd8fa21524be378.jpg                        

[root@web01 ~]# scp -r /code/wordpress/wp-content/uploads/* root@172.16.1.31:/data/wp
root@172.16.1.31's password:
231626-156769658639a9.jpg                                                              100%   75KB 323.6KB/s   00:00    
231626-156769658639a9-150x150.jpg                                                      100% 3516   138.1KB/s   00:00    
231626-156769658639a9-300x191.jpg                                                      100% 7046     2.6MB/s   00:00    

5.挂载目录
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/zh /code/zh/uploads/
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/wp /code/wordpress/wp-content/uploads/

6.查看是否挂载
[root@web01 ~]# df -h
Filesystem           Size Used Avail Use% Mounted on
/dev/sda3             98G  2.1G   96G   3% /
devtmpfs             980M     0 980M   0% /dev
tmpfs                 991M     0 991M   0% /dev/shm
tmpfs                 991M  9.6M 981M   1% /run
tmpfs                 991M     0 991M   0% /sys/fs/cgroup
/dev/sda1             497M 120M 378M  25% /boot
tmpfs                 199M     0 199M   0% /run/user/0
172.16.1.31:/data/zh   98G  1.7G   96G   2% /code/zh/uploads
172.16.1.31:/data/wp   98G  1.7G   96G   2% /code/wordpress/wp-content/uploads
2.web02配置nfs
1.安装nfs
[root@web02 ~]# yum -y install rpcbind nfs-utils

2.启动服务并设置开机自启
[root@web02 ~]# systemctl start rpcbind nfs-utils
[root@web02 ~]# systemctl enable rpcbind nfs-utils

3.查看挂载点
[root@web02 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/wp 172.16.1.0/24
/data/zh 172.16.1.0/24

4.推送源数据到挂载目录
[root@web02 ~]# scp -r /code/zh/uploads/* root@172.16.1.31:/data/zh
The authenticity of host '172.16.1.31 (172.16.1.31)' can't be established.
ECDSA key fingerprint is SHA256:g6buQ4QMSFl+5MMAh8dTCmLtkIfdT8sgRFYc6uCzV3c.
ECDSA key fingerprint is MD5:5f:d7:ad:07:e8:fe:d2:49:ec:79:2f:d4:91:59:c5:03.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.31' (ECDSA) to the list of known hosts.
root@172.16.1.31's password:
ffa5e93370534a62abe7746195595d5a.jpg                                                   100%   45KB   6.1MB/s   00:00    
20017b35a334e6966cd8fa21524be378.jpg                                                   100% 443KB  19.8MB/s   00:00    
170x110_20017b35a334e6966cd8fa21524be378.jpg                                           100% 4044     2.9MB/s   00:00    
90x90_20017b35a334e6966cd8fa21524be378.jpg                                             100% 2554     1.7MB/s   00:00    
[root@web02 ~]#

[root@web02 ~]# scp -r /code/wordpress/wp-content/uploads/* root@172.16.1.31:/data/wp
root@172.16.1.31's password:
231626-156769658639a9.jpg                                                              100%   75KB   9.5MB/s   00:00    
231626-156769658639a9-150x150.jpg                                                      100% 3516     1.8MB/s   00:00    
231626-156769658639a9-300x191.jpg                                                      100% 7046   993.6KB/s   00:00    
174855-1497088135bff2.jpg                                                              100% 443KB  20.9MB/s   00:00    
174855-1497088135bff2-150x150.jpg                                                      100% 3511     1.0MB/s   00:00    
174855-1497088135bff2-300x169.jpg                                                      100% 5825     1.5MB/s   00:00    

5.挂载目录
[root@web02 ~]# mount -t nfs 172.16.1.31:/data/zh /code/zh/uploads/
[root@web02 ~]# mount -t nfs 172.16.1.31:/data/wp /code/wordpress/wp-content/uploads/

6.查看是否挂载
[root@web02 ~]# df -h
Filesystem           Size Used Avail Use% Mounted on
/dev/sda3             98G  1.9G   96G   2% /
devtmpfs             980M     0 980M   0% /dev
tmpfs                 991M     0 991M   0% /dev/shm
tmpfs                 991M  9.6M 981M   1% /run
tmpfs                 991M     0 991M   0% /sys/fs/cgroup
/dev/sda1             497M 120M 378M  25% /boot
tmpfs                 199M     0 199M   0% /run/user/0
172.16.1.31:/data/zh   98G  1.7G   96G   2% /code/zh/uploads
172.16.1.31:/data/wp   98G  1.7G   96G   2% /code/wordpress/wp-content/uploads

7.实现文件实时备份

1.backup服务端配置
1.安装rsync
[root@backup ~]# yum -y install 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
#####################################
[data]
comment = "数据备份目录"
path = /data

3.创建统一用户
[root@backup ~]# groupadd www -g 666
[root@backup ~]# useradd www -u 666 -g 666

4.创建目录并授权
[root@backup ~]# mkdir /data
[root@backup ~]# chown -R www:www /data/

5.创建密码文件并修改权限
[root@backup ~]# echo "rsync_backup:123456" >/etc/rsync.passwd
[root@backup ~]# chmod 600 /etc/rsync.passwd

6.启动服务并设置开机自启
[root@backup ~]# systemctl start rsyncd
[root@backup ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.

7.检查服务
[root@backup ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      29190/rsync        
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      6135/rpcbind        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7151/sshd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7291/master        
tcp6       0      0 :::873                 :::*                   LISTEN      29190/rsync        
tcp6       0      0 :::111                 :::*                   LISTEN      6135/rpcbind        
tcp6       0      0 :::22                   :::*                   LISTEN      7151/sshd          
tcp6       0      0 ::1:25                 :::*                   LISTEN      7291/master    
2.nfs客户端配置
1.安装rsync和inotify
[root@nfs ~]# yum install rsync inotify-tools -y

2.上传源码包
[root@m01 ~]# scp sersync2.5.4_64bit_binary_stable_final.tar.gz root@172.16.1.31:/root
sersync2.5.4_64bit_binary_stable_final.tar.gz                                          100% 710KB  18.9MB/s   00:00    

3.解压安装包
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz

4.移动并重名
[root@nfs ~]# mv GNU-Linux-x86/ /usr/local/sersync

5.修改配置文件
[root@nfs ~]# vim /usr/local/sersync/confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
  <host hostip="localhost" port="8008"></host>
  <debug start="false"/>
  <fileSystem xfs="false"/>
  <filter start="false">
      <exclude expression="(.*)\.svn"></exclude>
      <exclude expression="(.*)\.gz"></exclude>
      <exclude expression="^info/*"></exclude>
      <exclude expression="^static/*"></exclude>
  </filter>
  <inotify>
      <delete start="true"/>
      <createFolder start="true"/>
      <createFile start="true"/>
      <closeWrite start="true"/>
      <moveFrom start="true"/>
      <moveTo start="true"/>
      <attrib start="true"/>
      <modify start="true"/>
  </inotify>

  <sersync>
      <localpath watch="/data">
          <remote ip="172.16.1.41" name="data"/>
      </localpath>
      <rsync>
          <commonParams params="-artuz"/>
          <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
          <userDefinedPort start="false" port="874"/><!-- port=874 -->
          <timeout start="false" time="100"/><!-- timeout=100 -->
          <ssh start="false"/>

6.创建密码文件并修改权限
[root@nfs ~]# echo "123456" >/etc/rsync.password
[root@nfs ~]# chmod 600 /etc/rsync.password

7.启动服务实现数据实时备份
[root@nfs ~]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d run as a daemon
option: -r rsync all the local files to the remote servers before the sersync work
option: -o config xml name: /usr/local/sersync/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
will ignore the inotify createFile event
daemon start,sersync run behind the console
use rsync password-file :
user is rsync_backup
passwordfile is /etc/rsync.password
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data && rsync -artuz -R --delete ./ rsync_backup@172.16.1.41::data --password-file=/etc/rsync.password >/dev/null 2>&1
run the sersync:
watch path is: /data

8.实现web01代理

1.web01相关配置
1.配置nginx站点目录
[root@web01 ~]# vim /etc/nginx/conf.d/linux.proxy.com.conf
server {
  listen 80;
  server_name linux.proxy.com;

  location / {
      root /code/proxy;
      index index.html;
  }
}

2.检查配置并重启服务
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl restart nginx

3.创建index文件
[root@web01 ~]# cd /code/
[root@web01 /code]#
[root@web01 /code]#
[root@web01 /code]# mkdir /code/proxy
[root@web01 /code]# vim /code/proxy/index.html
检查配置并重启服务
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
ystemctl  restart nginx

4.配置本地hosts
C:\Windows\System32\drivers\etc
10.0.0.7 linux.proxy.com
2.web03配置代理
1.配置官方源
[root@web03 ~]# 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.安装依赖
[root@web03 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

3.安装nginx
[root@web03 ~]# yum -y install nginx

4.配置nginx
[root@web03 ~]# vim /etc/nginx/nginx.conf
user www;

5.创建统一用户
[root@web03 ~]# groupadd -g 666 www
[root@web03 ~]# useradd www -u 666 -g 666

6.配置nginx代理站点文件
[root@web03 ~]# vim /etc/nginx/conf.d/daili.conf
server {
  listen 80;
  server_name linux.proxy.com;

location / {
   #proxy_pass http://172.16.1.7:80;
  proxy_pass http://10.0.0.7:80;
  proxy_set_header Host $http_host;
}
}
                 
7.检查配置
[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

8.启动服务并设置开机自启
[root@web03 ~]# systemctl start nginx
[root@web03 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

第十三章 nginx代理服务的更多相关文章

  1. 第二十三章 多项目集中权限管理及分布式会话——《跟我学Shiro》

    二十三章 多项目集中权限管理及分布式会话——<跟我学Shiro> 博客分类: 跟我学Shiro 跟我学Shiro  目录贴:跟我学Shiro目录贴 在做一些企业内部项目时或一些互联网后台时 ...

  2. PRML读书会第十三章 Sequential Data(Hidden Markov Models,HMM)

    主讲人 张巍 (新浪微博: @张巍_ISCAS) 软件所-张巍<zh3f@qq.com> 19:01:27 我们开始吧,十三章是关于序列数据,现实中很多数据是有前后关系的,例如语音或者DN ...

  3. <构建之法>第十三章到十七章有感以及这个项目读后感

    <构建之法>第十三章到十七章有感 第13章:软件测试方法有哪些? 主要讲了软件测试方法:要说有什么问题就是哪种效率最高? 第14章:质量保障 软件的质量指标是什么?怎么样能够提升软件的质量 ...

  4. 《Linux命令行与shell脚本编程大全》 第二十三章 学习笔记

    第二十三章:使用数据库 MySQL数据库 MySQL客户端界面 mysql命令行参数 参数 描述 -A 禁用自动重新生成哈希表 -b 禁用 出错后的beep声 -B 不使用历史文件 -C 压缩客户端和 ...

  5. 《Android群英传》读书笔记 (5) 第十一章 搭建云端服务器 + 第十二章 Android 5.X新特性详解 + 第十三章 Android实例提高

    第十一章 搭建云端服务器 该章主要介绍了移动后端服务的概念以及Bmob的使用,比较简单,所以略过不总结. 第十三章 Android实例提高 该章主要介绍了拼图游戏和2048的小项目实例,主要是代码,所 ...

  6. Gradle 1.12 翻译——第十三章 编写构建脚本

    有关其它已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或訪问:http://gradledoc.qiniudn.com ...

  7. [汇编学习笔记][第十三章int指令]

    第十三章int指令 13.1 int指令 格式: int n, n 为中断类型码 可以用int指令调用任何一个中断的中断处理程序(简称中断例程). 13.4 BIOS和DOS 所提供的中断例程 BIO ...

  8. perl5 第十三章 Perl的面向对象编程

    第十三章 Perl的面向对象编程 by flamephoenix 一.模块简介二.Perl中的类三.创建类四.构造函数 实例变量 五.方法六.方法的输出七.方法的调用八.重载九.析构函数十.继承十一. ...

  9. 第十三章——表和索引分区(1)——使用Range Left进行表分区

    原文:第十三章--表和索引分区(1)--使用Range Left进行表分区 前言: 如果数据表的数据持续增长,并且表中的数据量已经达到数十亿甚至更多,数据的查询和操作将非常困难,面对非常庞大的表,几时 ...

随机推荐

  1. ajax之---上传文件

    “伪”ajax向后台提交文件        <iframe style="display: none" id="iframe1" name="i ...

  2. Fiddler的安装和APP抓包

    前言 1.Fiddler安装包 2.安卓手机 3.iOS手机 1.下载fiddler软件:可以去官网下载https://www.telerik.com/fiddler,可以下载最新版 2.百度云盘(非 ...

  3. js监听事件的绑定与移除

    监听事件的绑定与移除主要是addEventListener和removeEventListener的运用. addEventListener语法 element.addEventListener(ty ...

  4. HTTP 【值得你看个究竟】

    我是一名程序员,我的主要编程语言是 Python,我更是一名 Web 开发人员,所以我必须要了解 HTTP,所以本篇文章就来带你从 HTTP 入门到进阶,看完让你有一种恍然大悟.醍醐灌顶的感觉. 认识 ...

  5. Vue等待父组件异步请求回数据'后'传值子组件

    问题: 让子组件在父组件有哪个数据的时候再渲染, 解决方案: 可以在父组件上加一个判断条件, 举例说明: <a-component :opt="opt" v-if=" ...

  6. Spring学习(一)初识Spring

    什么是Spring 定义:Spring 是一个轻量级的 DI / IoC 和 AOP 容器的开源框架,目的为了简化java开发. DI:注入 IOC:控制反转 AOP:面向切面编程 原理:利用了jav ...

  7. SQL Server 2008 R2执行存储过程sp_MailItemResultSets引起大量PREEMPTIVE_OS_WAITFORSINGLEOBJEC等待

      从监控工具DPA中发现一个数据库(SQL Server 2008 R2)的等待事件突然彪增,下钻分析发现数据库执行存储过程sp_MailItemResultSets时,引起了非常严重的等待(Hig ...

  8. 听我的,看完这30道MySQL基础题再去面试

    可以微信搜索公众号「 后端技术学堂 」回复「1024」获取50本计算机电子书,回复「进群」拉你进读者技术交流群,文章每周持续更新,我们下期见! 一个典型的互联网产品架构包含接入层.逻辑处理层以及存储层 ...

  9. 记一次数据库主从导致严重的bug解决过程

    1.事情起始: 我们每个月要给商家进行出账,所以有定时任务去跑商家的订单和售后进行出账,这个功能已经上线很久了,代码执行多次都没问题,突然有一天,产品找我说出现bug了: 这时,去生产库查询重复的订单 ...

  10. Hadoop框架:HDFS读写机制与API详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.读写机制 1.数据写入 客户端访问NameNode请求上传文件: NameNode检查目标文件和目录是否已经存在: NameNode响应客 ...