9.Nginx搭建流行架构

1. LNMP架构概述

1.1 什么是LNMP

LNMP是一套技术的组合,L=Linux、N=Nginx、M=MySQL、P=PHP

1.2 LNMP架构是如何工作的

首先nginx服务是不能请求动态请求,那么当用户发起动态请求时,nginx又是如何进行处理的。
当用户发起http请求,请求会被nginx处理,如果是静态资源请求nginx则直接返回,如果是动态请求nginx则通过fastcgi协议转交给后端的PHP程序处理,具体如下图所示: -c

1.3 Nginx与fastcgi详细工作流程如下图所示

-c 流程:
1.用户通过http协议发起请求,请求会先抵达LNMP架构中的nginx;
2.nginx会根据用户的请求进行location规则匹配;
3.location如果匹配到请求是静态,则由nginx读取本地直接返回;
4.location如果匹配到请求是动态,则由nginx将请求转发给fastcgi协议;
5.fastcgi收到请求交给php-fpm管理进程,php-fpm管理进程接收到后会调用具体的工作进程wrapper;
6.wrapper进程会调用PHP程序进行解析,如果只是解析代码,php直接返回;
7.如果有查询数据库操作,则由php连接数据库(用户 密码 ip)发起查询的操作;
8.最终数据由mysql-->php-->php-fpm-->fastcgi-->nginx-->http-->user


2. LNMP架构环境部署

2.1 使用官方仓库安装nginx

[root@web03 ~]# cat /etc/yum.repos.d/nginx.repo 
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

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

2.2 修改nginx用户

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

#修改nginx配置文件
[root@web03 ~]# sed -i '/^user/c user www;' /etc/nginx/nginx.conf

2.3 启动nginx并加入开机自启

[root@web03 ~]# systemctl start nginx
[root@web03 ~]# systemctl enable nginx

2.4 使用第三方扩展源安装php7.1

# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

[root@web03 ~]# yum remove php-mysql-5.4 php php-fpm php-common
[root@web03 ~]# cat /etc/yum.repos.d/php.repo
[php]
name = php Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

[root@web03 ~]# yum -y install 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

2.5 配置php-fpm用户与nginx的运行用户保持一致

[root@web03 ~]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf
[root@web03 ~]# sed -i '/^group/c user = www' /etc/php-fpm.d/www.conf

2.6 启动php-fpm并加入开机自启

[root@web03 ~]# systemctl start php-fpm
[root@web03 ~]# systemctl enable php-fpm    

2.7 安装mariadb数据库

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

2.8 启动mariadb数据库并加入开机自启

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

2.9 给mariadb数据库配置登录密码,并登陆数据库

[root@web03 ~]# mysqladmin password 'haoda'
[root@web03 ~]# mysql -uroot -phaoda

3. LNMP架构环境配置

在将nginx与PHP集成的过程中,需要先了解fastcgi代理配置语法

3.1 设置fastcgi服务器的地址,改地址可以指定为域名或IP地址,以及端口

Syntax: fastcgi_pass address;
Default:-
Context:location,if in location

#语法示例
fastcgi_pass location:9000;
fastcgi_pass unix:/tmp/fastcgi.socket;

3.2 设置fastcgi默认的首页文件,需要结合fastcgi_param一起设置

Syntax: fastcgi_index name;
Default:-
Context:http,server,location

3.3 通过fastcgi_param设置变量,并将设置的变量传递到后端的fastcgi服务器

Syntax: fastcgi_param parameter value [if_not_empty];
Default:-
Context:http,server,location

#语法示例
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /code$fastcgi_script_name;

3.4 图形展示fastcgi_index与fastcgi_param的作用

-c

3.5 最终Nginx连接Fastcgi服务器配置如下

[root@web03 ~]# cat /etc/nginx/conf.d/php.conf 
server {
        listen 80;
        server_name php.oldboy.com;;
        root /code;

        location / {
                index index.php index.html;
         }

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

3.6 在/code目录下创建info.php文件,测试能否通过浏览器访问,访问成功如下图

[root@web03 ~]# cat /code/info.php
<?php
        phpinfo();
?>

-c

3.7 在/code目录下创建mysqli.php文件,填入对应的数据库IP、用户名、密码

[root@web03 ~]# cat /code/mysqli.php
<?php
    $servername = "localhost";
    $username = "root";
    $password = "haoda";

    // 创建连接
    $conn = mysqli_connect($servername, $username, $password);

    // 检测连接
    if (!$conn) {
         die("Connection failed: " . mysqli_connect_error());
    }
    echo "连接MySQL...成功!";
?>

4. 部署博客产品WordPress

4.1 部署博客产品WordPress配置Nginx虚拟主机站点,域名为blog.haoda.com

#nginx具体配置信息
[root@web03 ~]# cat /etc/nginx/conf.d/wordpress.conf
server {
        listen 80;
        server_name blog.haoda.com;
        root /code/wordpress;
        index index.php index.html;

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

4.2 修改nginx与php-fpm的运行用户为www,并授权代码属主和属组都为www

#注意:如果没有该用户,启动一定会报错
[root@web03 code]# groupadd -g 666 www
[root@web03 code]# useradd -u666 -g666 www

#修改nginx与php-fpm管理进程,的运行身份为www
[root@web03 code]# sed -i '/^user /c user  www;' /etc/nginx/nginx.conf
[root@web03 code]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf 
[root@web03 code]# sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf 

#一定要重启才生效
[root@web03 code]# systemctl restart nginx
[root@web03 code]# systemctl restart php-fpm

4.3 获取wordpress产品,解压并部署wordress

[root@web03 ~]# mkdir /code
[root@web03 ~]# cd /code
[root@web03 code]# wget https://cn.wordpress.org/wordpress-5.0.3-zh_CN.tar.gz
#永远下载最新版
[root@web03 code]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz
[root@web03 ~]# tar xf wordpress-5.0.3-zh_CN.tar.gz
#最后授权代码为www
[root@web03 code]# chown -R www.www /code/wordpress

4.4 由于wordpress产品需要依赖数据库,所以需要手动建立数据库

[root@web03 ~]# mysql -uroot -phaoda
mysql> create database wordpress;
mysql> exit

4.5 通过浏览器访问wordpress,并部署该产品

-c

-c

-c

-c

-c

-c

4.6 解决nginx上传文件大小限制,413错误

[root@web03 code]# cat /etc/nginx/conf.d/wordpress.conf
server {
        listen 80;
        server_name blog.haoda.com;
        root /code/wordpress;
        index index.php index.html;
        client_max_body_size 100m;
}
[root@web03 code]# systemctl restart nginx

5. 部署知乎产品wecenter

5.1 配置nginx

[root@web03 ~]# cat /etc/nginx/conf.d/zh.conf
server {
        listen 80;
        server_name zh.haoda.com;
        root /code/zh;
        index index.php index.html;

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

5.2 重启nginx服务

[root@web03 ~]# systemctl restart nginx

5.3 下载Wecenter产品,部署Wecenter并授权

Wecenter下载地址:点一下试试

[root@web01 zh]# rz WeCenter_v3.2.1.zip
[root@web01 zh]# unzip WeCenter_3-2-1.zip
[root@web01 zh]# chown -R www.www /code/zh/

5.4 由于wecenter产品需要依赖数据库, 所以需要手动建立数据库

#1.登陆数据库
[root@http-server ~]# mysql -uroot -phaoda

#2.创建wordpress数据库
MariaDB [(none)]> create database zh;
MariaDB [(none)]> exit

5.5 通过浏览器访问网站

-c

-c

-c

-c

-c

-c

-c

-c


6. 部署EduSoho

6.1 配置nginx

[root@web03 ~]# cat /etc/nginx/conf.d/edu.conf
server {
        listen 80;
        server_name edu.haoda.com;
        root /code/edusoho/web;
        client_max_body_size 200m;

        location / {
                index app.php;
                try_files $uri @rewriteapp;
        }
        location @rewriteapp {
                rewrite ^(.*)$ /app.php/$1 last;
        }

        location ~ ^/udisk {
                internal;
                root /code/edusoho/app/data/;
        }

        location ~ ^/(app|app_dev)\.php(/|$) {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                fastcgi_param  HTTPS              off;
                fastcgi_param HTTP_X-Sendfile-Type X-Accel-Redirect;
                fastcgi_param HTTP_X-Accel-Mapping /udisk=/code/edusoho/app/data/udisk;
                fastcgi_buffer_size 128k;
                fastcgi_buffers 8 128k;
        }

        # 配置设置图片格式文件
        location ~* \.(jpg|jpeg|gif|png|ico|swf)$ {
                # 过期时间为3年
                expires 3y;
                # 关闭日志记录
                access_log off;
                # 关闭gzip压缩,减少CPU消耗,因为图片的压缩率不高。
                gzip off;
        }
        # 配置css/js文件
        location ~* \.(css|js)$ {
                access_log off;
                expires 3y;
        }
        # 禁止用户上传目录下所有.php文件的访问,提高安全性
        location ~ ^/files/.*\.(php|php5)$ {
                deny all;
        }

        # 以下配置允许运行.php的程序,方便于其他第三方系统的集成。
        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                fastcgi_param  HTTPS              off;
        }
}

6.2 下载软件,并且授权

[root@web03 code]# wget http://download.edusoho.com/edusoho-8.2.17.tar.gz
[root@web03 code]# tar xf edusoho-8.2.17.tar.gz
[root@web03 code]# chown -R www.www edusoho

6.3 调整php的上传大小

[root@web03 code]# vim /etc/php.ini
post_max_size = 200M
upload_max_filesize = 200M
[root@web03 code]# systemctl restart php-fpm

6.4 通过浏览器访问网站

-c

-c

-c

-c

-c

-c

-c


7. 拆分数据库至独立服务器

7.1 为什么要拆分数据库

由于单台服务器运行LNMP架构胡导致网站访问缓慢,当内存消耗殆尽时,很容易导致系统出现问题oom,从而kill掉消耗内存的服务(mysql),所以需要将web和数据库进行独立部署。

7.2 拆分数据库解决了什么问题

1、缓解web网站的压力2、增强数据库读写性能3、提高用户访问的速度

7.3 数据库拆分架构演变过程,如下图所示

-c

7.4 数据库拆分规划

主机名称 应用环境 外网地址 内网地址
web01 nginx+php 10.0.0.7 172.16.1.7
db01 mysql ----------- 172.16.1.51

7.5 数据库拆分架构详细步骤

7.5.1 web01网站服务器备份web01上的数据库

[root@web01 ~]# mysqldump -uroot -phaoda --all-databases --single-transaction > mysql.sql

7.5.2 db01数据库服务器上装数据库

[root@db01 ~]# yum install -y mariadb-server mariadb
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@db01 ~]#

7.5.3 备份数据传输到拆分的服务器并并停掉数据库

[root@web01 ~]# scp mysql.sql 172.16.1.51:/data
[root@web01 ~]# systemctl stop mariadb

7.5.4 备份数据同步到新数据库并授权外部访问

[root@db01 ~]# mysql < /data/mysql.sql
[root@db01 ~]# systemctl restart mariadb
[root@db01 ~]# mysql -uroot -phaoda
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| edusoho            |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
| zh                 |
+--------------------+
7 rows in set (0.00 sec)

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

7.5.5 修改web01服务器服务连接数据库的信息

#修改blog的配置文件
[root@web01 wordpress]# vim wp-config.php

/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');

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

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

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

#修改wecenter的配置文件
[root@web01 zh]# cat system/config/database.php
<?php

$config['charset'] = 'utf8';
$config['prefix'] = 'haoda_';
$config['driver'] = 'MySQLi';
$config['master'] = array (
    'charset' => 'utf8',
    'host' => '172.16.1.51',
    'username' => 'root',
    'password' => 'haoda',
    'dbname' => 'zh',
);
$config['slave'] = false;

#修改edusoho的配置文件
[root@web01 edusoho]# cat app/config/parameters.yml
parameters:
database_driver: pdo_mysql
database_host: 172.16.1.51
database_port: 3306
database_name: edusoho
database_user: root
database_password: 'haoda'

[root@web01 ~]# rm -rf /code/edusoho/app/cache/*

8. 扩展多台相同的Web服务器

8.1 为什么要扩展多台web节点

单台web服务器能抗住的访问量是有限的,配置多台web服务器能提升更高的访问速度

8.2 扩展多台web解决了什么问题

1、单台web节点如果故障,会导致业务down机
2、多台web节点能保证业务的持续稳定,扩展性搞
3、多台web节点能有效的提升用户访问网站的速度

8.3 多台web节点技术架构组成,如下图所示

-c

8.4 统一web服务器环境

8.4.1 拷贝web01上面的yum仓库到其他web服务器

[root@web02 ~]# scp root@172.16.1.7:/etc/yum.repos.d/*.repo /etc/yum.repos.d/

8.4.2 安装nginx和PHP等服务

[root@web02 ~]# yum -y install nginx  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

8.4.3 创建启动用户

[root@web02 ~]# groupadd -g 666 www
[root@web02 ~]# useradd -u666 -g666 www

8.5 将新加的web服务器配置与web01同步

8.5.1 统一nginx配置

[root@web02 conf.d]# rsync -avz --delete root@172.16.1.7:/etc/nginx/ /etc/nginx/
[root@web02 conf.d]# 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 conf.d]# nginx 
[root@web02 conf.d]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@web02 conf.d]#

8.5.2 统一php配置

[root@web02 etc]# rsync -avz --delete root@172.16.1.7:/etc/php* /etc/
[root@web02 etc]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@web02 etc]# systemctl start php-fpm

8.6 业务统一代码

[root@web01 ~]# tar zcf code.tar.gz /code                   #在web01上打包代码
[root@web01 ~]# scp code.tar.gz root@172.16.1.8:/tmp        #推送代码至其他web服务器
root@172.16.1.8's password: 
code.tar.gz                                          100%  250MB  53.0MB/s   00:04    
[root@web01 ~]#

[root@web02 ~]# tar xf /tmp/code.tar.gz -C /                #在扩展web服务器解压

9. 拆分静态资源至独立服务器

9.1 为什么拆分静态资源值独立存储服务器

当后端的web节点出现多台时,会导致用户上传的图片、视频、附件等静态内容仅上传至一台web服务器,那么其他的web服务器则无法访问到该图片。

9.2 新增一台nfs存储解决了什么问题

1、保证了多台web节点静态资源一致
2、有效节省多台web节点的存储空间
3、统一管理静态资源,便于后期推送至CDN进行静态资源加速

9.3 新增一台nfs存储解决了什么问题

-c

9.4 扩展nfs服务环境规划

主机名称 应用环境 外网地址 内网地址
web01 nginx+php 10.0.0.7 172.16.1.7
web02 nginx+php 10.0.0.8 172.16.1.8
nfs nfs ---------- 172.16.1.31
db01 mysql ---------- 172.16.1.51

9.5 配置nfs共享存储服务器

9.5.1 安装nfs

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

9.5.2 配置目录

[root@nfs ~]# cat /etc/exports
/data/blog 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)
/data/edu 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

9.5.3 创建目录

[root@nfs ~]# mkdir /data/{blog,zh,edu} -p

9.5.4 创建用户

[root@nfs ~]# groupadd -g 666 www
[root@nfs ~]# useradd -u666 -g666 www

9.5.5 授权目录

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

9.5.6 启动服务并加入开机自启

[root@nfs ~]# systemctl enable nfs-server   
[root@nfs ~]# systemctl start nfs-server

9.5.7 将静态文件推送到nfs共享服务器

[root@web02 ~]# cd /code/wordpress/wp-content
[root@web02 wp-content]# scp -rp uploads/* root@172.16.1.31:/data/blog/
#上传失败可能是目录权限问题,推送时属性保留root
[root@nfs ~]# chown -R www.www /data/

9.5.8 web服务器挂载nfs共享服务器

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

9.5.9 最终效果

-c

posted @ 2019-08-30 17:15  _︶"  阅读(720)  评论(0编辑  收藏  举报