[toc]

LNMP架构工作流程

简介

  • LNMP含义解析

    1
    2
    3
    4
    L linux
    N nginx
    M mysql
    P php
  • LNMP架构流程描述

    • 静态请求
      • Nginx服务是可以直接处理动态请求,请求会被Nginx处理,如果是静态资源请求Nginx则直接返回
    • 动态请求
      • Nginx服务是不能直接处理动态请求,Nginx通过fastcgi协议转交给后端的PHP程序处理,如下图所示:

image-20230616195818933

image-20230616195832922

  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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 创建nginx官方源
vim /etc/yum.repo.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
module_hotfixes=true

# 安装nginx
yum install -y nginx

# 统一用户 添加www用户和组
groupadd www -g 666 && useradd www -u 666 -g 666 -s /sbin/nologin -M

# 修改nginx启动用户
vim /etc/nginx/nginx.conf
nginx --> www

# 启动nginx并加入开机自启
systemctl start nginx
systemctl enable nginx

# 检查进程
ps -ef | grep [n]ginx

# 检查端口
netstat -lntup | grep 80

部署php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 更改yum源
wget http://us-east.repo.webtatic.com/yum/el7/webtatic-release.rpm
yum localinstall -y webtatic-release.rpm

# 生成缓存
yum makecache

# 安装php
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 php71wopcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb
rpm -ivh *.rpm

# 统一用户
vim /etc/php-fpm.d/www.conf

[www]
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache Choosed to be able to access some dir as httpd
# user = www
; RPM: Keep a group allowed to write in log dir.
# group = www

# 启动php并加入开机自启
systemctl start php-fpm && systemctl enable php-fpm

# 检查进程
ps -ef | grep [p]hp-fpm

# 检查端口
netstat -lntup | grep 9000

安装MySQL数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 安装mariadb
yum install -y mariadb-server

# 启动并加入开机自启
systemctl start mariadb && systemctl enable mariadb

# 检查端口
netstat -lntup |grep 3306

# 设置mariadb的密码123
mysqladmin -u root password '123'

# 客户端连接命令
mysql -uroot -p123

## 显示下列信息表示成功
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.68-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)]>

# create database wp charset utf8;
## 创建数据库(可显示中文)

将nginx和php建立连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 创建并修改配置文件
vim test_php.conf

server {
listen 80;
server_name 10.0.0.7;
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 /etc/nginx/fastcgi_params; # 存放变量文件的路径(fastcgi_params)
}
}

# 创建站点目录
mkdir /code

# 编辑php测试文件
vim /code/info.php

<?php
phpinfo();
?>

# 重启nginx服务
systemctl restart nginx

# 浏览器访问
10.0.0.7/info.php

image-20221214163414380

测试php和数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 创建并修改配置文件
vim /code/mysqli.php

<?php
$servername = "localhost";
$username = "root"; # 用户名(默认root)
$password = "123"; # 数据库密码

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

// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "我要进来咯";
?>

<img style='width:100%;height:100%;'src=http://img.qikula.com/file/image/pic/90a195558070n1999081165c253.jpg>

# 访问远端mariadb数据库
mysql -uroot -p123 -h 远端数据库IP

image-20221214164641886

部署博客

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 创建并修改配置文件
vim /code/test_php.conf

server {
listen 80;
server_name blog.xxx.com; # 修改博客域名
root /code/wordpress; # 博客配置文件路径

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 /etc/nginx/fastcgi_params;
}
}

# 在站点目录部署代码
cd /code
wget http://test.driverzeng.com/Nginx_Code/wordpress-5.0.3-zh_CN.tar.gz

# 解压部署代码文件
tar -xf wordpress-5.0.3-zh_CN.tar.gz

# 授权部署代码目录权限
chown -R www:www /code/wordpress/

# 连接到mysql 给wordpress一个库
mysql -uroot -p123
create database wp charset utf8; # 创建数据库字符集utf8

# 查看库
show databases;

# 创建本地域名解析
10.0.0.7 blog.xxx.com

# 访问域名
blog.xxx.com

image-20221214165943717

image-20221214170033583

image-20221214170036698

image-20221214170201807

image-20221214170355674

wordpress的缺陷

  • 缺陷描述
    • 如果wordpress更改域名本地解析后网页会出现破图
    • 需要在wordpress网页后台修改新的域名url
  • 修复破图
    • 先修改回原来的域名
    • 进入网站后台:域名/wp-admin
    • 手动修改域名

image-20221215160428130

示例:搭建WeCenter博客

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 进入站点目录获取WeCenter代码文件
http://download.driverzeng.com/

# 在站点目录部署代码
cd /code
wget http://test.driverzeng.com/Nginx_Code/WeCenter_3-2-1.zip

# 解压部署代码文件
unzip WeCenter_3-2-1.zip

# 创建并修改配置文件
vim /code/WeCenter.conf

server {
listen 80;
server_name blog.xiaolin.com;
root /code/WeCenter_3-2-1;

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 /etc/nginx/fastcgi_params;
}
}

# 授权部署代码目录权限
chown -R www:www /code/WeCenter_3-2-1/

# 连接到mysql 给wordpress一个库
mysql -uroot -p123
create database WC charset utf8; # 创建数据库字符集utf8

# 查看库
show databases;

# 创建本地域名解析
10.0.0.7 blog.xiaolin.com

# 访问域名
blog.xiaolin.com

image-20221214172020285

image-20221214173127545