LNMP架构

LNMP架构

动态数据需要Nginx+PHP+MySQL实现

LNMP架构 Linux Nginx MySQL PHP

LNMP架构 Linux Nginx MySQL Python

LNMT架构 Linux Nginx MySQL Tomcat

【1】、LNMP架构概述

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

❓LNMP是如何工作的

首先Nginx服务是不能处理动态请求,那么当用户发起动态请求时,Nginx又是如何进行处理的。

当用户发起http请求,请求会被Nginx处理,如果是静态资源请求Nginx则直接返回,如果是动态请求Nginx则通过fastcgi协议转交给后端的PHP程序处理,具体如下图所示

image-20241210160048645

nginx与fastCGO详细工作流程

1、用户通过 http 协议发起请求,请求会先抵达 LNMP 架构中的 Nginx

2、Nginx 会根据用户的请求进行判断,这个判断是由 location 完成

3、如果用户请求的是静态页面,Nginx 直接进行处理

4、判断用户请求的是动态页面,Nginx 会将请求交给 fastcgi 协议下发

5、fastcgi 会将请求交给 php-fpm 管理进程,php-fpm 管理进程接收到后会调用具体的工作进程 warrap

6、warrap 进程会调用 php 程序进行解析,如果只是解析代码 php 就直接返回

7、如果有查询数据库操作,则有 php 连接数据库用户发起查询操作

8、最终数据由 mysql->php->php-fpm->fastcgi->nginx->http->user

【2】、LNMP架构环境部署

image-20241210163033964

1、部署nginx

# 基于官网仓库的安装方式,版本较新,配置易读
# 准备软件仓库
[root@web01 ~]# cat /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

# 安装nginx
[root@web01 ~]# yum install -y nginx
[root@web01 ~]# nginx -v
nginx version: nginx/1.26.1

2、部署PHP

1.安装php服务
[root@web01 ~]# yum -y install php php-bcmath php-cli php-common php-devel php-embedded php-fpm php-gd php-intl php-mbstring php-mysqlnd php-opcache php-pdo   php-process php-xml php-json

2.配置PHP服务 修改默认启动用户
a.修改启动用户

b.修改监听方式
[root@web01 ~]# grep 127.0.0.1:9000 /etc/php-fpm.d/www.conf  -n
38:listen = 127.0.0.1:9000


3.启动PHP服务
[root@web01 ~]# systemctl start php-fpm
[root@web01 ~]# systemctl enable php-fpm


4.检查PHP服务
[root@web01 ~]# netstat -tnulp
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:80              0.0.0.0:*               LISTEN      4557/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      989/sshd: /usr/sbin 
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      24981/php-fpm: mast 
tcp6       0      0 :::22                   :::*                    LISTEN      989/sshd: /usr/sbin 

[root@web01 ~]# php-fpm -t
[10-Dec-2024 10:38:27] NOTICE: configuration file /etc/php-fpm.conf test is successful

3、安装mariadb数据库

1.安装mariadb
[root@web01 ~]# yum  -y install mariadb-server

2.启动mariadb服务
[root@web01 ~]# systemctl enable mariadb --now
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.

3.配置密码
[root@web01 ~]# mysqladmin password 'lzy123.com'

4.测试登录
[root@web01 ~]# mysql -uroot -plzy123.com
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.39-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)]> quit
Bye

4、nginx连接php

fastcgi原理

$document_root:网站根目录

$fastcgi_script_name:URI

$document_root和$fastcgi_script_name结合起来就是要解析的文件的绝对路径,将文件的路径传给SCRIPT_FILENAME变量

1.写配置文件打通和PHP连接
[root@web01 conf.d]# cat php.conf 
server {
        listen 80;
        server_name php.xu.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;
        }
}
2.写一个php文件测试是否可以正常解析
[root@web01 conf.d]# cat /code/info.php
<?php
        phpinfo();
?>

3.检查配置文件并重启
[root@web01 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@web01 conf.d]# systemctl restart nginx

4.浏览器测试访问
windowshosts解析
10.0.0.7 php.oldboy.com
php.oldboy.com/info.php


出现以下页面则正常

image-20241210170749600

5、配置php连接MySQL

# 需要再php的配置文件中写入数据库的IP+端口+用户名+密码可以测试是否连接数据库
[root@web01 ~]# vim /code/mysql.php
<?php
    $servername = "localhost";
    $username = "root";
    $password = "lzy123.com";

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

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

image-20241210171720926

【3】、部署wordpress

1.创建nginx配置文件
[root@web01 ~]# cat /etc/nginx/conf.d/wordpress.conf
server {
        listen 80;
        server_name www.wp.com;
        root /code/wp;

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

2.测试nginx
[root@web01 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

3.重启生效
[root@web01 conf.d]# systemctl restart nginx

4.创建代码目录
[root@web01 conf.d]# mkdir /code/wp

5.下载、解压wordpress代码
[root@web01 ~]# cd /code/wp
[root@web01 wp]# wget https://cn.wordpress.org/wordpress-5.0.3-zh_CN.tar.gz
--2024-12-10 17:24:22--  https://cn.wordpress.org/wordpress-5.0.3-zh_CN.tar.gz
Resolving cn.wordpress.org (cn.wordpress.org)... 198.143.164.252
Connecting to cn.wordpress.org (cn.wordpress.org)|198.143.164.252|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11098483 (11M) [application/octet-stream]
Saving to: ‘wordpress-5.0.3-zh_CN.tar.gz’

wordpress-5.0.3-zh_CN.tar.gz      100%[==========================================================>]  10.58M   672KB/s    in 17s     

2024-12-10 17:24:41 (626 KB/s) - ‘wordpress-5.0.3-zh_CN.tar.gz’ saved [11098483/11098483]

[root@web01 wp]# ll
total 10844
drwxr-xr-x 5 1006 1006     4096 Jan 11  2019 wordpress

6.hosts解析
192.168.121.7 www.wp.com
浏览器访问业务

image-20241210172719785

需要在数据库中创建wordpress库

[root@web01 wp]# mysql -uroot -plzy123.com -e "create  database wordpress;"
[root@web01 wp]# mysql -uroot -plzy123.com -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| wordpress          |
+--------------------+

image-20241210173102967

image-20241210173150521

统一用户
1.创建用户uid gid 666 的www虚拟账号
[root@web01 nginx]# groupadd -g666 www
[root@web01 nginx]# useradd -u666 -g666 -M -s /sbin/nologin www

2.修改nginx启动用户
[root@web01 nginx]# grep www nginx.conf  -n
2:user  www;
[root@web01 nginx]# systemctl restart nginx

3.修改php启动用户
[root@web01 wp]# sed -rn '/^user|^group/p' /etc/php-fpm.d/www.conf  
user = www
group = www

[root@web01 nginx]# systemctl restart php-fpm

4.修改代码目录属主属组为www
[root@web01 wp]# chown -R www.www /code/wp/

image-20241210173747329

image-20241210173828660

在我们上传主题时可能会出现413报错,这是由于上传文件大小超过了限制,先修改nginx服务器的限制


http {
    ...
    client_max_body_size 100M;
    ...
}
systemctl reload nginx


【4】、部署wecenter、知乎

1.配置Nginx
[root@web01 conf.d]# cat zh.conf 
server {
        listen 80;
        server_name www.zh.com;
        root /code/zh;

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

[root@web01 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@web01 conf.d]# systemctl restart nginx

2.创建代码目录
[root@web01 conf.d]# mkdir /code/zh
[root@web01 conf.d]# cd /code/zh

3.上传代码
[root@web01 zh]# ll
total 24648
-rw-r--r-- 1 root root 25238972 Aug  7 09:28 WeCenter_V3.6.2.zip
[root@web01 zh]# unzip WeCenter_V3.6.2.zip
4.解压代码
[root@web01 zh]# chown -R www.www ../zh

5.安装部署
windows-hosts解析
10.0.0.7 www.zh.com

6.创建数据库zh
[root@web01 ~]# mysql -uroot -plzy123.com -e "create database zh;"
[root@web01 ~]# mysql -uroot -plzy123.com -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| wordpress          |
| zh                 |
+--------------------+

image-20241210174842804

【5】、拆分数据库至独立的服务器

由于单台服务器运行LNMP架构会导致网站访问缓慢,当内存被占满时,很容易导致系统出现oom从而kill掉MySQL数据库,所以要将web和数据库进行独立部署。

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

1.缓解web网站的压力

2.增强数据库读写性能

3.提高用户访问速度

image-20241211142503288

1.准备51服务器,安装mysql服务
[root@db01 ~]# yum makecache
[root@db01 ~]# yum install -y mariadb-server

2.启动数据库
[root@db01 ~]# systemctl enable mariadb --now

3.web01导出数据库所有数据
[root@web01 wp]# mysqldump -uroot -p'lzy123.com' -A > all.sql
检查导出的库是否正确
vim all.sql  # 搜索下你发布的博文是否在sql语句

4.将all.sql拷贝到51服务器
[root@web01 wp]# scp all.sql root@192.168.121.51:~/

5.db51服务器将all.sql导入数据库
[root@db01 ~]# mysql -uroot < admin.sql 
[root@db01 ~]# systemctl restart mariadb.service 
[root@db01 ~]# mysql -uroot -plzy123.com
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.39-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)]> exit
Bye
[root@db01 ~]# mysql -uroot -plzy123.com -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| wordpress          |
+--------------------+

6.数据库为了安全,禁止root远程连接.必须使用普通账号进行远程连接
[root@web01 wp]# mysql -h172.16.1.51 -uroot -plzy123.com
ERROR 1130 (HY000): Host '172.16.1.7' is not allowed to connect to this MariaDB serve


# 授权lzy普通账号密码为lzy123.com 管理所有库和所有的表
[root@db01 ~]# mysql -uroot -plzy123.com
MariaDB [(none)]> grant all on  *.* to lzy@'%' identified by 'lzy123.com';
Query OK, 0 rows affected (0.001 sec)

测试lzy普通账号远程连接
[root@web01 ~]# mysql -h 172.16.1.51 -ulzy -plzy123.com

7.直接修改业务代码的数据信息指向到172.16.1.51
[root@web01 wp]# grep -l -r "lzy123.com" /code/wp
/code/wp/wp-config.php
[root@web01 wp]# vim /code/wp/wp-config.php
/** MySQL数据库用户名 */
define('DB_USER', 'lzy');
/** MySQL主机 */
define('DB_HOST', '172.16.1.51');



8.停止web01的数据库
[root@web01 wp]# systemctl stop mariadb.service 
[root@web01 wp]# systemctl disable mariadb.service 


【6】、扩展多台相同的web服务器

1.准备web02服务器
2.安装nginx
[root@web02 ~]# scp root@192.168.121.7/etc/yum.repos.d/nginx.repo /etc/yum.repos.d/
[root@web02 ~]# yum -y install nginx

3.安装php
[root@web02 ~]# yum -y install php php-bcmath php-cli php-common php-devel php-embedded php-fpm php-gd php-intl php-mbstring php-mysqlnd php-opcache php-pdo   php-process php-xml php-json

4.创建虚拟用户uid gid 666 www
[root@web02 ~]# groupadd -g666 www
[root@web02 ~]# useradd -u666 -g666 -M -s /sbin/nologin www

5.将web01的nginx配置和web02进行同步
[root@web02 ~]# rsync -avz --delete 192.168.121.7:/etc/nginx/ /etc/nginx/

6.将web01的代码拷贝到web02
[root@web01 ~]# tar zcvf code.tar.gz /code
[root@web01 ~]# scp code.tar.gz 10.0.0.8:/root/
[root@web02 ~]# ll
total 60676
-rw-r--r-- 1 root root 62129149 Dec 11 10:09 code.tar.gz
[root@web02 ~]# tar xf code.tar.gz -C /

手动修改php启动用户和监听方式
[root@web02 ~]# egrep "^user|^group" /etc/php-fpm.d/www.conf -n
24:user = www
26:group = www
[root@web02 ~]# grep 'listen = 127.0.0.1:9000' /etc/php-fpm.d/www.conf -n
38:listen = 127.0.0.1:9000

启动nginx和php服务
[root@web02 ~]# systemctl start nginx php-fpm
[root@web02 ~]# systemctl enable nginx php-fpm

【7】、解决web服务器静态数据一致性问题

1.安装nfs服务
yum -y install nfs-utils
2.配置nfs服务
vim /etc/exports
/data/wp  172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
创建目录和用户
groupadd -g666 www
useradd -u666 -g666 -M -s /sbin/nologin www
mkdir /code/wp -p
chown www.www /data/wp

3.启动nfs服务
systemctl start nfs
systemctl enable nfs

4.将完整的图片拷贝到31服务器
[root@web02 ~]# scp -r /code/wordpress/wp-content/uploads/* 10.0.0.31:/data/wp/


web服务器挂载nfs
web01:
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/wp /code/wordpress/wp-content/uploads/
[root@web01 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
devtmpfs              459M     0  459M   0% /dev
tmpfs                 475M     0  475M   0% /dev/shm
tmpfs                 475M   43M  432M   9% /run
tmpfs                 475M     0  475M   0% /sys/fs/cgroup
/dev/sda3              48G  4.3G   44G   9% /
tmpfs                 475M     0  475M   0% /tmp
/dev/sda1             195M  122M   74M  63% /boot
tmpfs                  95M     0   95M   0% /run/user/0
172.16.1.31:/data/wp   48G  3.8G   45G   8% /code/wordpress/wp-content/uploads
加入开机自动挂载

WEB02:
[root@web02 ~]# mount -t nfs 172.16.1.31:/data/wp /code/wordpress/wp-content/uploads/
[root@web02 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
devtmpfs              459M     0  459M   0% /dev
tmpfs                 475M     0  475M   0% /dev/shm
tmpfs                 475M  6.8M  468M   2% /run
tmpfs                 475M     0  475M   0% /sys/fs/cgroup
/dev/sda3              48G  4.1G   44G   9% /
tmpfs                 475M     0  475M   0% /tmp
/dev/sda1             195M  122M   74M  63% /boot
tmpfs                  95M     0   95M   0% /run/user/0
172.16.1.31:/data/wp   48G  3.8G   45G   8% /code/wordpress/wp-content/uploads
加入开机自动挂载

【8】、配置反向代理

[root@lb01 conf.d]# cat proxy.conf
server {
        listen 80;
        server_name www.wp.com;
        location / {
                proxy_pass http://192.168.121.7;
        }
}

[root@lb01 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@lb01 conf.d]# systemctl restart nginx

【9】、配置负载均衡

[root@lb01 nginx]# vim conf.d/lb.conf 
upstream webs {
        server 172.16.1.7;
        server 172.16.1.8;
}
server {
        listen 80;
        server_name www.wp.com;
        
        location / {
        proxy_pass http://webs;
        include proxy_params;
        }
}
                                          
[root@lb01 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 nginx]# systemctl restart nginx

浏览器访问测试
[root@lb01 conf.d]# cat lb.conf
upstream webs {
	server 172.16.1.7;
	server 172.16.1.8;
}
server {
	listen 80;
	server_name www.wp.com;

	location / {
	proxy_pass http://webs;
	include proxy_params;
	}
}

server {
	listen 80;
	server_name www.test.com;
	location / {
	proxy_pass http://webs;
	include proxy_params;
	}
}
[root@lb01 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@lb01 conf.d]# systemctl restart nginx



web01配置静态页面
[root@web01 conf.d]# cat test.conf
server {
	listen 80;
	server_name www.test.com;

	location / {
	root /code/test;
	index index.html;
	}
}
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
snginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
[root@web01 conf.d]# mkdir /code/test
[root@web01 conf.d]# echo web01..... > /code/test/index.html



web02配置静态页面
[root@web02 conf.d]# cat test.conf
server {
	listen 80;
	server_name www.test.com;

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

[root@web02 conf.d]# mkdir /code/test
[root@web02 conf.d]# echo web02.... >/code/test/index.html
[root@web02 conf.d]# cat /code/test/index.html
web02....
[root@web02 conf.d]# nginx -t
sysnginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 conf.d]# systemctl restart nginx



最后hosts指向到10.0.0.5
10.0.0.5 www.test.com
posted @ 2024-12-19 21:33  Linux小菜鸟  阅读(36)  评论(0编辑  收藏  举报