LNMP架构部署

LNMP架构部署

LNMP架构介绍

L	---	linux系统	
N	---	Nginx服务	---	处理用户的静态请求
M	---	mysql服务	---	存储用户的字符串数据信息
P	---	PHP服务	---	处理动态的页面请求,负载和数据库建立关系

LNMP部署

nginx服务部署

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

mysql服务部署

第一个历程:安装数据库软件

[root@web01 www]# yum -y install mariadb mariadb-server

第二个历程:数据库初始化

[root@web01 www]# mysql_install_db --help
--basedir=path		--- 指定mysql程序目录
--datadir=path		--- 指定数据信息保存的目录
--user=user_name	 -- 指定用户管理数据目录
PS:yum安装不需要初始化

第三个历程:启动数据库服务

[root@web01 www]# systemctl start mariadb
[root@web01 www]# systemctl enable mariadb

第三个历程:给数据库设置密码

[root@web01 www]# mysqladmin -u root password '123456'
[root@web01 www]# mysql -u root -p123456
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 6
Server version: 5.5.60-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)]> 

PHP服务部署

第一个历程:更新yum源

[root@web01 tools]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@web01 tools]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

第二个历程:安装PHP软件

[root@web01 tools]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-fpm php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb

第三个历程:编写配置文件

[root@web01 tools]# vim /etc/php-fpm.d/www.conf
user = www
group = www
#PS:保证nginx进行的管理用户和php服务进程的管理用户保持一致

第四个历程:启动PHP服务

[root@web01 tools]# systemctl start php-fpm.service
[root@web01 tools]# systemctl enable php-fpm.service

LNMP架构原理

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

Nginx(fastcgi_pass) → FastCGI → (PHP-fpm → wrapper)php (php解析器)→mysql(读取或写入)

1.用户通过 http 协议发起请求,请求会先抵达 LNMP 架构中的 Nginx
2.Nginx 会根据用户的请求进行判断,这个判断是有 Location 进行完成
3.判断用户请求的是静态页面, Nginx 直接进行处理
4.判断用户请求的是动态页面, Nginx 会将该请求交给 fastcgi 协议下发
5.fastgi 会将请求交给 php-fpm 管理进程, php-fpm 管理进程接收到后会调用具体的工作进程 warrap
6.warrap 进程会调用 php 程序进行解析,如果只是解析代码 php 直接返回
7.如果有查询数据库操作,则由 php 连接数据库(用户 密码 IP)发起查询的操作
8.最终数据由 mysql->php->php-fpm->fastcgi->nginx->http->user

LNMP之间建立关系

实现nginx+php建立关系

编写nginx配置文件

[root@web01 tools]# vim /etc/nginx/conf.d/bbb.conf 
server {
  listen  80;
  server_name bbb.test.com;
  location / {
    root /html/bbb;
    index index.html;
  }
  location ~ .php$ {
    root /html/bbb;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi_params;
  }
}
[root@web01 tools]# systemctl restart nginx

编写动态资源文件

[root@web01 bbb]# vim index.php
<?php
phpinfo();
?>

浏览器访问

实现php+mysql建立关系

编写代码文件

[root@web01 bbb]# vim mysql.php 
<?php
  $servername = "localhost";
  $username = "root";
  $password = "123456";
  $conn = mysqli_connect($servername, $username, $password);
  if ($conn) {
     echo "mysql successful by test !
";
    }else{
        die("Connection failed: " . mysqli_connect_error());
  }
?>

浏览器访问

部署搭建博客wordpress

第一个历程:获取代码信息

[root@web01 tools]# rz

[root@web01 tools]# ll
total 10940
-rw-r--r-- 1 root root 11199196 Jul 27 17:05 wordpress-5.2.1.tar.gz
[root@web01 tools]# 

第二个历程:将代码解压,解压后信息放入到站点目录中

[root@web01 tools]# tar xf wordpress-5.2.1.tar.gz 
[root@web01 tools]# mv wordpress /html/

第三个历程:修改站点目录权限

[root@web01 html]# chown -R www.www wordpress/
[root@web01 ~]# cd /etc/nginx/conf.d
[root@web01 conf.d]# vim blog.conf 
server {
  listen  80;
  server_name blog.test.com;
  location / {
    root /html/wordpress;
    index index.php index.html;
  }
  location ~ .php$ {
    root /html/wordpress;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi_params;
  }
}

第五个历程:对数据库服务进行配置

[root@web01 conf.d]# mysql -u root -p123456
MariaDB [(none)]> create database wordpress;
MariaDB [(none)]> grant all on wordpress.* to 'wordpress'@'localhost' identified by '123456'; 

第四个历程:安装wordpress

http://blog.test.com/index.php

上传wordpress主题报413错误解决

第一个历程:修改nginx配置文件

[root@web01 conf.d]# vim blog.conf 
server {
  client_max_body_size 50m;			---	指定用户上传数据的大小限制(默认1m)
}

第二个历程:修改php.ini配置文件

[root@web01 conf.d]# vim /etc/php.ini 
upload_max_filesize = 50M			---	使php接收用户上传的更大的数据(默认2m)
[root@web01 conf.d]# systemctl restart php-fpm.service 

部署搭建博客Wecenter

第一个历程:编写配置文件

[root@web01 conf.d]# cat /etc/nginx/conf.d/zh.conf 
server {
    listen       80;
    server_name  zh.test.com;
    location / {
        root   /html/zh;
        index  index.php index.htm;
    }
 location ~ .php$ {
    root /html/zh;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi_params;
  }
}

[root@web01 conf.d]# 

第二个历程:将代码解压到站点目录

[root@web01 html]# ll
total 10160
-rw-r--r--  1 root root 10393660 Aug  1 11:57 WeCenter_3-3-2.zip
[root@web01 html]# unzip WeCenter_3-3-2.zip -d /html/zh
[root@web01 html]# systemctl restart nginx

第三个历程:对数据库进行配置

[root@web01 html]# mysql -u root -p123456
MariaDB [(none)]> create database zh;
MariaDB [(none)]> grant all on zh.* to 'zh'@'localhost' identified by '123456'; 

第三个历程:安装Wecenter

http://zh.test.com/

如何让LNMP架构和数据库服务器建立关系

数据库(db01) web01
172.16.1.51 172.16.1.9

01 为什么要进行数据库的拆分
由于单台服务器运行 LNMP 架构会导致网站访问缓慢,当内存被吃满时,很容易导致系统出现 oom,从而 kill 掉 MySQL数据库,所以需要将 web 和数据库进行独立部署
02 数据库拆分后解决了什么问题
1.缓解 web 网站的压力
2.增强数据库读写性能
3.提高用户访问的速度

第一个历程:将web服务器本地数据库数据进行备份

[root@web01 07]# mysqldump -uroot -p123456 --all-database >/mnt/web_bak.sql

第二个历程:将备份数据进行迁移

[root@web01 ~]# rsync -azvP /mnt/web_bak.sql 172.16.1.51:/root

第三个历程:恢复数据信息

[root@db01 ~]# yum -y install mariadb mariadb-server
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# mysql -uroot -p123456 </root/web_bak.sql 
[root@db01 ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 7
Server version: 5.5.60-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          |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> 

第四个历程:修改数据库服务器中数据库用户信息

MariaDB [(none)]> select user,host from mysql.user;
+-----------+-----------+
| user      | host      |
+-----------+-----------+
| root      | 127.0.0.1 |
| root      | ::1       |
|           | localhost |
| root      | localhost |
| wordpress | localhost |
|           | web01     |
| root      | web01     |
+-----------+-----------+
7 rows in set (0.00 sec)

MariaDB [(none)]> delete from mysql.user where user="" and host="localhost";
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> delete from mysql.user where user="" and host="web01";
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> select user,host from mysql.user;
+-----------+-----------+
| user      | host      |
+-----------+-----------+
| root      | 127.0.0.1 |
| root      | ::1       |
| root      | localhost |
| wordpress | localhost |
| root      | web01     |
+-----------+-----------+
5 rows in set (0.00 sec)

MariaDB [(none)]> 

第五个历程:添加新的用户信息

[root@db01 ~]# mysql -uroot -p123456
MariaDB [(none)]> grant all on wordpress.* to 'wordpress'@'172.16.1.%' identified by '123456';
MariaDB [(none)]> flush privileges;

#web01连接
[root@web01 ~]# mysql -uwordpress -p123456 -h 172.16.1.51
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 8
Server version: 5.5.60-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)]> 

第六个历程:修改web服务器代码文件信息

[root@web01 ~]# cd /html/wordpress/
[root@web01 wordpress]# vim wp-config.php
define( 'DB_HOST', '172.16.1.51' );

第六个历程:停止web服务器数据库服务

[root@web01 wordpress]# systemctl stop mariadb.service
[root@web01 wordpress]# systemctl disable mariadb.service

拓展多台web服务节点

web01 web02
172.16.1.7 172.16.1.8

01 为什么要拓展多台web节点
单台 web 服务器能抗住的访问量是有限的,配置多台 web 服务器能提升更高的访问速度
02 拓展多台web解决了什么问题
1.单台 web 节点如果故障,会导致业务 down 机
2.多台 web 节点能保证业务的持续稳定,扩展性高
3.多台 web 节点能有效的提升用户访问网站的速度

第一个历程:打包网站代码

[root@web01 html]# tar -zcvf wordpress.tar.gz wordpress

第二个历程:分发打包代码

[root@web01 html]# rsync -azvP wordpress.tar.gz 172.16.1.8:/html

第三个历程:web02解压网站代码

[root@web02 html]# tar xf wordpress.tar.gz

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

存储(nfs) web
172.16.1.31 172.16.1.31

01.为什么要拆分静态资源到独立服务器
当后端的 web 节点出现多台时,会导致用户上传的图片、视频附件等内容仅上传至一台 web 服务器,那么其他的web 服务器则无法访问到该图片
02.新增一台nfs存储解决了什么问题
1.保证了多台 web 节点静态资源一致。
2.有效节省多台 web 节点的存储空间。
3.统一管理静态资源,便于后期推送至 CDN 进行静态资源加速

第一个方法:根据图片链接地址获取图片存储位置

第二种方法:先定位数据存放的站点目录中

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

存储和web服务器需要有相同用户

存储
[root@nfs01 /]# id www
uid=1001(www) gid=1001(www) groups=1001(www)

web
[root@web01 /]# id www
uid=1001(www) gid=1001(www) groups=1001(www)

配置存储服务器nfs

[root@nfs01 /]# vim /etc/exports
/data   172.16.1.0/24(rw,sync,anonuid=1001,anongid=1001)

在web服务器进行挂载

[root@web01 /]# cd /html/wordpress/wp-content/
[root@web01 wp-content]# mount -t 172.16.1.31:/data/blog uploads/
[root@web01 wp-content]# cd uploads/
[root@web01 uploads]# mv /mnt/2019/ .
[root@web01 uploads]# ll
total 0
drwxr-xr-x 3 www www 16 Jul 27 18:32 2019
[root@web01 uploads]# 

web02端操作

web02端操作和web01端一样

原文地址:https://www.cnblogs.com/opesn/p/11638386.html