25.LNMP/负载均衡/高可用搭建

LNMP分别代表的是什么

image.png

LNMP 安装部署的流程

nginx:
nginx

mysql:
①数据库的软件安装
yum install mariadb-server mariadb -y

yum安装不需要初始化,如果是编译安装,需要初始化,指定参数
image.png

②启动数据库服务
systemctl start mariadb.service
systemctl enable mariadb.service

③给mysql数据库的服务设置密码
mysqladmin -u root password 'oldboy123'

php
①更新yum源/卸载系统自带的php软件

yum remove php-mysql php php-fpm php-common

#rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 安装失败

rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

若更新失败,参考
Peer reports incompatible or unsupported protocol version

②安装php
yum install -y php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb

③编写配置文件
需保证ngingx 进程的管理用户和php服务的管理用户一致
ps -ef |grep nginx
ps -ef |grep php

vim /etc/php-fpm.d/www.conf
#cat /etc/php-fpm.d/www.conf
user = www 
group = www
image.png

④启动php
systemctl start php-fpm
systemctl enable php-fpm

lnmp架构原理

image.png
image.png

LNMP直接建立联系

1.实现nginx+php
①编写nginx配置文件

cd /etc/nginx/conf.d
vim /etc/nginx/conf.d/blog.conf

server {
  listen 80;
  server_name blog.oldboy.com;
  location / {
    root /html/blog;
    index index.html;
  }

 location ~ \.php$ {
        root           /html/blog;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

配置的参数来自参数文件fastcgi_params

cat /etc/nginx/fastcgi_params

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;         --->
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;        --->
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;


检查nginx -t
重启服务systemctl restart nginx

文件的作用:
处理.php末尾的文件(动态的文件)若客户从web访问的是php的信息,就加载此location


image.png

相关的服务netstat -lntup|grep 9000


image.png

②编写php动态资源文件

vim /html/blog/test_php.php

③访问测试
blog.oldboy.com/test_php.php

image.png

2.实现php+mysql
cd /html/blog/
vim /html/blog/test_mysql.php

$server_name = "localhost";
$username = "root";
$password = "oldboy123";
$conn = mysqli_connect($server_name, $username, $password);
if ($conn) {
 echo "successful ! \n";
}else{
 die("connection failed:" . mysqli_connect_error());

}

?>

访问测试:

blog.oldboy.com/test_mysql.php
成功的回显:


image.png

失败的报错:(关闭db进行测试)
connection failed:No such file or directory


image.png

image.png

部署搭建网站页面(代码上线)

开源的代码网站:


image.png

采用wordpress测试
WordPress.org China 简体中文

下载,解压,将wordpress目录下的所有子文件放置到站点目录:
/html/blog
修改文件夹的属组属主
chown -R www.www blog
初始化网站页面,浏览器访问进行设置,
http://blog.oldboy.com/index.php

image.png

①按照界面信息提示准备好数据库的信息,选择现在就开始
image.png

②设置数据库的连接信息
image.png

需提前对数据库服务进行配置
mysql -u root -poldboy123
create database wordpress; -->注意sql以分号结尾
检查
show databases;


image.png

image.png

创建数据库的管理用户
grant all on wordpress.* to 'wordpress'@'localhost' identified by 'oldboy123';
检查
select user.host from mysql.user;


image.png


开始安装


image.png

并填写基本信息


image.png

成功
image.png

④以自动创建相关的wordpress的表
image.png

⑤利用网站发布博文

若是登录报错403 Forbidden
cat /etc/nginx/conf.d/blog.conf

请检查配置文件是否有配置index.php
image.png

登录成功


image.png

如何让LNMP和存储服务器建立关系

image.png

find /html/blog -type f -mmin -5

在07上检查存储服务是否正常

showmount -e 
[root@nfs01 ~]$ showmount -e 
Export list for nfs01:
/data 10.0.0.0/24,172.16.1.0/24

[root@web01 /html/blog]$ cd /mnt
[root@web01 /mnt]$ touch oldboy123.txt

检查31是否文件创建成功

cd /data
ls 

31修改配置文件

vim /etc/exports
/data/blog 172.16.1.0/24(rw,sync)
/data/www 172.16.1.0/24(rw,sync)
/data/bbs 172.16.1.0/24(rw,sync)

检查

[root@nfs01 /data]$ systemctl reload nfs
[root@nfs01 /data]$ showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/bbs  172.16.1.0/24
/data/www  172.16.1.0/24
/data/blog 172.16.1.0/24

挂载点准备

07机器
[root@web01 /html/blog/wp-content/uploads]$ ls
2020
[root@web01 /html/blog/wp-content/uploads]$ mv 2020/ /tmp/           ---->因为此目录要用于挂载,需要准备空的挂载点,稍后文件会mv回来

31机器
mkdir /data/{bbs,blog,www}

07上挂载
mount -t nfs 172.16.1.31:/data/blog /html/blog/wp-content/uploads

生产环境需实现开机自动挂载

把之前的数据移回来
mv /tmp/2020 /html/blog/wp-content/uploads

若是迁移回来的时候报权限错误:
无法建立目录wp-content/uploads/2020/03。有没有上级目录的写权限?

image.png

如何让LNMP和数据库服务器建立关系(为了实现数据的统一存储)

第一个历程:将web服务器本地数据库进行备份
mysqldump -uroot -poldboy123 --all-database >/tmp/web_back.sql

第二个历程:
将备份数据进行迁移
scp -rp /tmp/web_back.sql 172.16.1.51:/tmp

第三个历程:
恢复数据
mysql -uroot -poldboy123 

实现反向代理,负载均衡配置

环境准备
lb01   负载均衡服务器    10.0.0.5
web01  web集群服务器     10.0.0.7
web02  web集群服务器     10.0.0.8 
web03  web集群服务器     10.0.0.9

第一个历程: web服务器进行环境配置
配置文件内容: www.conf    
server {
    listen       80;
    server_name  www.oldboy.com;
    location / {
       root /html/www/;
       index index.html;
    }
}
#web01 
mkdir /html/www/ -p
echo "web01 www.oldboy.com" >/html/www/oldboy.html
#web02 
mkdir /html/www/ -p
echo "web02 www.oldboy.com" >/html/www/oldboy.html  
#web03
mkdir /html/www/ -p
echo "web02 www.oldboy.com" >/html/www/oldboy.html      

第二个历程: 在负载均衡服务器上进行测试
[root@lb01 ~]# curl -H host:www.oldboy.com  10.0.0.7/oldboy.html
web01 www.oldboy.com
[root@lb01 ~]# curl -H host:www.oldboy.com  10.0.0.8/oldboy.html
web02 www.oldboy.com
[root@lb01 ~]# curl -H host:www.oldboy.com  10.0.0.9/oldboy.html
web03 www.oldboy.com

第三个历程: 负载均衡配置部署
upstream     --- 负载均衡模块
proxy_pass   --- 反向代理模块

[root@lb01 nginx]# cat nginx.conf
user  www;
worker_processes  2;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    keepalive_timeout  0;
    upstream oldboy {              --- 定义可以进行负载的web节点信息           ,集群名oldboy 
        server  10.0.0.7:80;            
        server  10.0.0.8:80;            
        server  10.0.0.9:80;            
    } 
    server {
       listen         80;
       server_name    localhost;
       location / {
        proxy_pass http://oldboy;  --- 反向代理将请求发送给指定集群
       }
    }
}

第四个历程: 进行访问测试
DNS解析
10.0.0.5  www.oldboy.com

upstream 负载均衡模块说明

实现轮询分配请求
实现权重分配


Syntax:     upstream name { ... }
Default:    —
Context:    http

upstream backend {
   server backend1.example.com weight=5;
   server 127.0.0.1:8080       max_fails=3 fail_timeout=30s;
   server unix:/tmp/backend3;
   
   server backup1.example.com  backup;
}

#upstream模块功能参数:
1) weight:   权重参数
   可以实现权重轮询分配资源, 默认是轮询分配资源
   upstream oldboy {
    server  10.0.0.7:80 weight=3;
    server  10.0.0.8:80 weight=2;
    server  10.0.0.9:80 weight=1;
   }
   
2) least_conn: 按照节点连接数分配资源
   upstream oldboy {
    least_conn;
    server  10.0.0.7:80;
    server  10.0.0.8:80;
    server  10.0.0.9:80;
   }

3) ip_hash: 确保一个用户多次访问, 负载均衡都会分配给相同web节点 了解
  
   upstream oldboy {
    ip_hash;
    server  10.0.0.7:80;
    server  10.0.0.8:80;
    server  10.0.0.9:80;
   }

将用户的源地址转换成hash值


4) 负载均衡节点健康检查相关
   max_fails=3        --- 最大的失败次数  ,
   fail_timeout=30s   --- 失败后的重发间隔时间
   
   A请求发送到   --->  web01  1次失败  2次失败  3次失败   每次间隔 30s

    upstream oldboy {
       server  10.0.0.7:80 max_fails=3 fail_timeout=30s;
       server  10.0.0.8:80;
       server  10.0.0.9:80;
    }
5) 节点备选功能
正常访问会访问到08和07,当08和07宕机了,会访问到备选节点09
    upstream oldboy {
      server  10.0.0.7:80;
      server  10.0.0.8:80;
      server  10.0.0.9:80 backup;
    } 

proxy 反向代理模块

proxy_pass:       反向代理指令
proxy_set_header: 设置请求头信息  
01. 访问不同的url地址,显示不同的网站页面. 将客户请求的host信息传递给后端
若是不设置,每次访问都会到同一个页面
比如客户请求的host是www.oldboy.com ,但是根据配置文件会传递oldboy 给后端,但是没有叫oldboy 的服务器,所以会从配置文件选第一台服务器
proxy_set_header Host $host;

02. 使网站节点日志可以记录真实IP地址
对于后端服务器而言,访问后端服务器的是负载均衡
添加如下配置,传递客户真实IP
#严格区分大小写
proxy_set_header X-Forwarded-For $remote_addr;

03. 检查网站页面是否正确
#当出现异常访问时候,比如404, 跳过这个节点,访问别的节点
proxy_next_upstream error timeout http_404;
location / {
    proxy_pass http://oldboy;
    proxy_set_header Host $host;
#严格区分大小写
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_next_upstream error timeout http_404;
}


proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off
error --- 异常错误
timeout --- 超时信息
invalid_header --- 无效信息
http_xxx --- 异常状态码



企业案例配置:

image.png

负载均衡+集群+动静分离

1) 实现网站服务动静分离
不同的url访问不同的集群
环境规划:
上传集群   upload   10.0.0.7 web01  uri==upload  index.html  upload page 
静态集群   static   10.0.0.8 web02  uri==static  index.html  static page 
默认集群   default  10.0.0.9 web03  uri          index.html  default page 

第一个历程: 创建数据目录及文件
#web01:
mkdir /html/www/upload/
echo "upload page" >/html/www/upload/index.html
#web02
mkdir /html/www/static/
echo "static page" >/html/www/static/index.html
#web03
echo "default page" >/html/www/index.html

第二个历程: 编写负载均衡配置
[root@lb01 nginx]# cat nginx.conf
user  www;
worker_processes  2;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    keepalive_timeout  0;
    upstream upload {
        server  10.0.0.7:80;
    }  
    upstream static {
        server  10.0.0.8:80;
    }  
    upstream default {
        server  10.0.0.9:80;
    }  
    server {
       listen         80;
       server_name    localhost;
       location / {
          proxy_pass http://default;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_next_upstream error timeout http_404;
       }
       location /upload/ {
          proxy_pass http://upload;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_next_upstream error timeout http_404;
       }
       location /static/ {
          proxy_pass http://static;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_next_upstream error timeout http_404;
       }
    }
}

  1. 网站服务根据客户端agent不同,显示不同页面


    image.png
   iphone   -->  wap集群      web01    index.html   iphone page 
   Chrome   -->  web集群      web02    index.html   chrome page 
   other    -->  default 集群 web03    index.html   default page     

第一个历程: 部署web集群服务
web01:
echo iphone page > /html/www/index.html
web02:
echo chrome page > /html/www/index.html
web03:
echo "default page" >/html/www/index.html
   
第二个历程: 修改负载均衡配置文件
[root@lb01 nginx]# cat nginx.conf
user  www;
worker_processes  2;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    keepalive_timeout  0;
    upstream iphone {
        server  10.0.0.7:80;
    }  
    upstream chrome {
        server  10.0.0.8:80;
    }  
    upstream default {
        server  10.0.0.9:80;
    }  
    server {
       listen         80;
       server_name    localhost;
       location / {
          proxy_pass http://default;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_next_upstream error timeout http_404;
#如果是手机访问
#http_user_agent是nginx的内置变量
#固定语法:$http_user_agent ~* iphone
#$http_user_agent ~* Chrome
          if ($http_user_agent ~* iphone) {
              proxy_pass http://iphone;
          }
#如果是谷歌浏览器访问
          if ($http_user_agent ~* Chrome) {
             proxy_pass http://chrome;
          }
       }
    }
}


nginx -t  检查配置语法是否正确
systemctl reload  nginx
访问测试:

你可能感兴趣的:(25.LNMP/负载均衡/高可用搭建)