LNMP架构
3027 字
15 分钟
LNMP架构

LNMP架构
[TOC]
架构图

把 静态 的资源都放在我们
NFS里面动态数据 需要Nginx+PHP+MySQL实现
可交互式的,像登录验证、点赞、评论、用户信息…存放在
数据库中Nginx没办法直接和数据库进行交互,需要PHP这个中间件TCP通信,要么用户名+密码验证,要么做免密钥
-
LNMP架构 Linux Nginx MySQL PHP
-
LNMP架构 Linux Nginx MySQL Python
-
LNMT架构 Linux Nginx MySQL Tomcat
Tomcat用来解析JAVA代码

如果请求的是静态的,Nginx直接返回给用户
如果是动态的,转发给fastcgi协议
再交给php-fpm进程--->工作进程warrap
- 如果是php本身的信息,则直接返回
- 如果需要查询数据库,则由
php解析器连接数据库进行查询
- mysql—>php解析器(php.ini)—>php-fpm.conf—>fastcgi—>nginx
部署Nginx
1.配置官网仓库[root@web01 ~]#cat /etc/yum.repos.d/nginx.repo[nginx-stable]name=nginx stable repobaseurl=http://nginx.org/packages/centos/7/$basearch/gpgcheck=0enabled=1
2.安装nginx服务[root@web01 ~]# yum -y install nginx
3.配置Nginx服务 部署博客系统# 具体的配置我们放在后面部署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# 里面有一些插件# 直接yum安装,后面会进行编译安装![root@web01 ~]#rpm -qa|grep php|wc -l15# 验证是否安装成功 15个包
2.配置PHP服务&修改默认启动用户1)PHP配置文件[root@web01 ~]# ll /etc/php.ini /etc/php-fpm.d/www.conf-rw-r--r-- 1 root root 19415 Mar 31 16:54 /etc/php-fpm.d/www.conf# 和web网站相关的配置文件-rw-r--r-- 1 root root 62221 Mar 31 16:54 /etc/php.ini# php的解释器,全局配置文件[root@web01 ~]#grep apache /etc/php-fpm.d/www.confuser = apachegroup = apache# 它这里默认启动用户是apache[root@web01 ~]#id apacheuid=48(apache) gid=48(apache) groups=48(apache)# 我们后面等遇到问题了再去修改它---------------------------'实际做项目,会统一用户为www'# 修改启动用户[root@web01 ~]# groupadd -g666 www[root@web01 ~]# useradd -u666 -g666 -M -s /sbin/nologin www[root@web01 ~]# vim /etc/php-fpm.d/www.conf...user = wwwgroup = www# 你修改完php的配置后,别忘记重启服务!!restart---------------------------2)修改监听方式[root@web01 ~]# grep 127.0.0.1:9000 /etc/php-fpm.d/www.conflisten = 127.0.0.1:9000# 我们php启动起来之后,只会运行在这个IP地址上# 别的网段都没有办法访问我,不需要让别人访问!3)检测配置[root@web01 ~]#php-fpm -t# php-fpm[06-Feb-2026 16:09:44] NOTICE: configuration file /etc/php-fpm.conf test is successful# 他和nginx的配置检测方式一样! -t
3.启动PHP服务[root@web01 ~]# systemctl start php-fpm'这里是php-fpm,并非是php'[root@web01 ~]# systemctl enable php-fpm
4.检查PHP服务和端口[root@web01 ~]#netstat -lntup | grep php-fpmtcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1561/php-fpm: maste安装数据库
1.安装mariadb[root@web01 ~]# yum -y install mariadb-server# 可以将 mariadb 看作“一个开源的、兼容 MySQL 的数据库系统”,在绝大多数传统 LAMP/LNMP 场景下,它完全可以替代 MySQL,且无需修改应用代码# 后面可以直接编译安装MySQL,只是比较繁琐而已[root@web01 ~]#rpm -qa mariadbmariadb-10.3.39-1.p01.01.ky10.x86_64
2.启动mariadb服务[root@web01 ~]# systemctl start mariadb[root@web01 ~]# systemctl enable mariadb
3.配置密码[root@web01 ~]# mysqladmin password 'oldboy123.com'
4.测试登录[root@web01 ~]#mysql -uroot -poldboy123.com# 免交互式的直接登录数据库Welcome to the MariaDB monitor.......MariaDB [(none)]> quit# 退出[root@web01 ~]#mysql -uroot -poldboy123.com -e 'show databases;'# -e 选项:让 MySQL 客户端在连接后立即执行指定的 SQL 语句,然后退出+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema |+--------------------+[root@web01 ~]#netstat -lntup | grep mysqldtcp6 0 0 :::3306 :::* LISTEN 2351/mysqld# 默认端口是3306测试连接

'在讲Nginx与PHP集成的过程中,需要先了解Fastcgi代理配置语法'root /code;fastcgi_pass 127.0.0.1:9000;# 用来设置Fastcgi服务器的地址# 该地址可以指定为 域名或者IP地址,以及端口fastcgi_index index.php;# 默认的首页文件fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;# 设置SCRIPT_FILENAME变量,并将这个变量传递到Fastcgi服务器
假设用户在浏览器上输入:http://kpyun.com/index.php$document_root获取的是我们root指定的代码目录/code$fastcgi_script_name是URI根后面的路径index.php两个拼接组合在一起,赋值给SCRIPT_FILENAME变量# 它就是我们服务器上具体资源的位置/code/index.php测试php连接
1.写子配置文件,并打通PHP连接[root@web01 ~]#cd /etc/nginx/conf.d/[root@web01 conf.d]#vim php.confserver { listen 80; server_name php.kpyun.com; root /code;
location / { index index.php index.html; # 这里是index.php,先匹配php,匹配不到在匹配html }# 上面是静态页面 location ~ \.php$ { # 这里~匹配正则,\撬棍把.打回原形 # 只要匹配到.php结尾的文件,来这里 fastcgi_pass 127.0.0.1:9000; # 如果要访问php文件,转交给本地php服务 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 识别出资源路径 include fastcgi_params; # 把所有的变量包含进来 }}[root@web01 conf.d]#cat /etc/nginx/fastcgi_params.........fastcgi_param QUERY_STRING $query_string;fastcgi_param REQUEST_METHOD $request_method;.........server { listen 80; server_name php.kpyun.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.检查配置文件并重启[root@web01 conf.d]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@web01 conf.d]# systemctl reload nginx
3.写一个php文件测试是否可以正常解析[root@web01 conf.d]# vim /code/info.php<?php phpinfo();?># 看php本身的一个信息,还不是看数据库的信息
# Windows做hosts解析10.0.0.7 php.kpyun.com[root@web01 conf.d]#ll /code-rw-r--r-- 1 root root 28 Feb 6 17:44 info.php# 我这个/code目录下只有一个info.php页面# 并没有index.html页面
4.浏览器测试访问http://php.kpyun.com/# 返回403,主页缺失!# 以为这里/code目录下并没有index主页
http://php.kpyun.com/info.php# 带上路径再次访问'出现以下页面则正常'
"如何让它再简便一点呢?"# 浏览器访问php.kpyun.com直接蹦出来这个页面呢?[root@web01 ~]#mv /code/info.php /code/index.php# 改一下名字index就可以直接访问到了'这个文件只是用来测试是否可以和php正常通信'
测试数据库连接
# 需要在php的配置文件中写入数据库的IP+端口+用户名+密码# 可以测试是否正常连接数据库[root@web01 ~]#vim /code/mysql.php<?php $servername = "localhost"; $username = "root"; $password = "oldboy123.com";
// 创建连接 $conn = mysqli_connect($servername, $username, $password);
// 检测连接 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Successful,PHP可以连接MySQL...";?>
# 直接粘贴,会带格式'进入vim里面 1):set paste 2)再输入i进入编辑模式 3)最后再进行粘贴'
浏览器访问:http://php.kpyun.com/mysql.php'出现以下提示,说明MySQL可以正常连接'
以上两个文件
info.php和mysql.php只是我们用来测试
php和数据库是否能够正常访问 和我们的业务没有太大的关系!后面我们具体搭建WordPress的时候,
/code/wordpress/config.php业务代码里面有专门负责 连接数据库 的配置文件
上面两个文件只是用来测试罢了!
部署WordPress业务
1.创建WordPress子配置文件[root@web01 ~]#cd /etc/nginx/conf.d/[root@web01 conf.d]#cp php.conf wp.conf[root@web01 conf.d]#vim wp.confserver { listen 80; server_name wp.kpyun.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 fastcgi_params; }}
2.测试nginx[root@web01 conf.d]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
3.重启生效[root@web01 conf.d]# systemctl reload nginx
4.创建代码目录[root@web01 conf.d]# mkdir /code/wordpress
5.下载wordpress代码[root@web01 conf.d]#mkdir /server/tmp# 用于存放临时的压缩包和垃圾文件[root@web01 conf.d]#cd /server/tmp/[root@web01 tmp]#wget https://cn.wordpress.org/wordpress-6.0-zh_CN.tar.gz[root@web01 tmp]#lltotal 21372-rw-r--r-- 1 root root 21881579 Jun 16 2022 wordpress-6.0-zh_CN.tar.gz
6.解压代码[root@web01 tmp]#tar -zxf wordpress-6.0-zh_CN.tar.gz -C /code/wordpress/[root@web01 tmp]#ll /code/wordpress/total 4drwxr-xr-x 5 1006 1006 4096 Jun 16 2022 wordpress# 你会发现它自己又创建了一层目录,我们得把它拿出来[root@web01 tmp]#mv /code/wordpress/wordpress/* /code/wordpress/# 移动一下[root@web01 tmp]#ll /code/wordpress/total 212-rw-r--r-- 1 1006 1006 405 Feb 6 2020 index.php-rw-r--r-- 1 1006 1006 19915 Jan 1 2022 license.txt-rw-r--r-- 1 1006 1006 7401 Mar 23 2022 readme.html........[root@web01 tmp]#rm -rf /code/wordpress/wordpress/[root@web01 tmp]#rm -f wordpress-6.0-zh_CN.tar.gz# 删除冗余文件
7.hosts解析10.0.0.7 wp.kpyun.com浏览器访问业务http://wp.kpyun.com/
'企业中配置和数据库连接的业务方式有两种'1)像WordPress一样的开源业务,给我们显示具体的页面# 我们在页面上配置数据库信息# IP+端口+用户名+密码2)直接在配置文件中编辑vim打开# 说明,帮助文档# 像WordPress在/code/wordpress/config.php易错点

[root@web01 ~]#mysql -uroot -poldboy123.com -e 'create database wordpress;'# 创建相应的数据库[root@web01 ~]#mysql -uroot -poldboy123.com -e 'show databases;'+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || wordpress |+--------------------+# 有了对应的数据库,像发表文章,评论,点赞都会存放在这个数据库中
# 看报错,无法写入到对应的配置文件中!'因为它没有权限'# 是哪个用户尝试写入的??'php服务中的默认用户apache,Nginx的默认启动用户为nginx'# 所有的.php文件都交给php服务处理[root@web01 ~]#grep apache /etc/php-fpm.d/www.confuser = apachegroup = apache# php默认启动用户是apache[root@web01 ~]#cat /etc/nginx/nginx.confuser nginx;# Nginx默认用户为nginx[root@web01 ~]#ll -d /code/wordpress/drwxr-xr-x 5 root root 4096 Feb 6 19:34 /code/wordpress/# 而这个代码目录的属主为root[root@web01 ~]#chown -R apache.apache /code/wordpress/[root@web01 ~]#ll -d /code/wordpress/drwxr-xr-x 5 apache apache 4096 Feb 6 19:34 /code/wordpress/# 这样php服务就可以随心所欲的操纵wordpress代码目录了'后期我们会统一nginx和php的启动用户都为www'# 再次提交上面连接数据库的请求------------------------统一用户1.创建用户uid gid 666 的www虚拟账号[root@web01 ~]#cd /etc/nginx/[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 -n2:user www;[root@web01 nginx]# systemctl reload nginx# 也可以用-t检测一下再启动
3.修改php启动用户[root@web01 nginx]# egrep -n '^user|^group' /etc/php-fpm.d/www.conf24:user = www26:group = www[root@web01 nginx]# systemctl restart php-fpm'别忘记重启php服务'
4.修改代码目录属主属组为www[root@web01 nginx]# chown -R www.www /code/wordpress/
# 这次就能够成功的和数据库建立连接了![root@web01 ~]#ll /code/wordpress/wp-config.php-rw-rw-rw- 1 apache apache 3282 Feb 6 20:17 /code/wordpress/wp-config.php# 这就是它连接数据库的配置文件[root@web01 ~]#cat /code/wordpress/wp-config.phpdefine( 'DB_NAME', 'wordpress' );# 数据库的名字define( 'DB_USER', 'root' );# 用户define( 'DB_PASSWORD', 'oldboy123.com' );# 密码define( 'DB_HOST', 'localhost' );# 主机位置
如果打√,那么像百度的搜索引擎可以搜索到!# 小公司自己创建的话,可以打钩# 自己局域网创建,别人也访问不进来,不用打邮箱随便来来一个就行!不然老有广告wp-admin/
'后台管理页面'[root@web01 ~]#ll /code/wordpress/total 216-rw-r--r-- 1 apache apache 405 Feb 6 2020 index.php....drwxr-xr-x 9 apache apache 4096 Jun 16 2022 wp-admin# 就在这个文件夹里面
更换主题

[root@web01 QQ2.8]#head /etc/php.ini[PHP]upload_max_filesize = 64Mpost_max_size = 64Mmax_execution_time = 300memory_limit = 256M# 改一下php的配置文件,也是一种不错的思路!建议 不要通过 WordPress 后台上传 ,而是:
通过 FTP ,将解压后的主题文件夹上传到
/wp-content/themes/your-theme/然后在 WordPress 后台「外观 > 主题」中启用即可。
这种方式绕过所有上传限制,且更稳定

[root@web01 themes]#pwd/code/wordpress/wp-content/themes[root@web01 themes]#lltotal 12-rw-r--r-- 1 apache apache 28 Jun 5 2014 index.phpdrwxr-xr-x 3 root root 19 Feb 6 20:53 QQ2.8.......'权限可不对呀!别忘记改权限!'[root@web01 themes]#chown -R apache.apache QQ2.8/[root@web01 themes]#lltotal 12-rw-r--r-- 1 apache apache 28 Jun 5 2014 index.phpdrwxr-xr-x 3 apache apache 19 Feb 6 20:53 QQ2.8
部署wecenter知乎
# 这个的配置和wordpress差不多,快速的过一遍'这里我们统一用户为www'# 如何创建,如何修改,不过多赘述了[root@web01 ~]#cd /etc/nginx/[root@web01 nginx]#id wwwuid=666(www) gid=666(www) groups=666(www)
1)修改nginx子配置文件[root@web01 nginx]#vim conf.d/zh.confserver { listen 80; server_name zh.kpyun.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; }}
2)检测,启动[root@web01 nginx]#nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@web01 nginx]#systemctl reload nginx
3)修改Windows的hosts映射文件10.0.0.7 zh.kpyun.com
4)创建代码目录[root@web01 nginx]#mkdir /code/zh
5)上传软件包,解压[root@web01 nginx]#cd /server/tmp/[root@web01 tmp]#ll-rw-r--r-- 1 root root 25238972 Feb 7 04:19 'WeCenter V3.6.2.zip'[root@web01 tmp]#unzip 'WeCenter V3.6.2.zip' -d /code/zh[root@web01 tmp]#ll /code/zh/total 96drwxr-xr-x 36 root root 4096 Jan 31 2022 appdrwxr-xr-x 2 root root 6 Jan 31 2022 cache-rw-r--r-- 1 root root 705 Dec 17 2021 index.php.....[root@web01 tmp]#rm -rf 'WeCenter V3.6.2.zip'
6)修改目录属主[root@web01 tmp]#chown -R www.www /code/zh/
7)创建数据库[root@web01 tmp]#mysql -uroot -poldboy123.com -e 'create database zh;'
8)浏览器访问http://zh.kpyun.com/


文章分享
如果这篇文章对你有帮助,欢迎分享给更多人!
相关文章智能推荐
1
LNMP架构拆分
Web服务LNMP架构拆分实战,涵盖数据库分离、Web横向扩展、NFS共享存储及负载均衡代理配置
2
Tomcat动静分离
Web服务Tomcat动静分离与多端适配实战,通过Redis共享Session、Nginx正则分发及User-Agent路由
3
搭建企业内部yum仓库
Web服务企业内网YUM私有仓库搭建,使用createrepo+Nginx实现RPM包统一管理与离线快速分发
4
Keepalived 高可用
Web服务Keepalived高可用集群实战,讲解VRRP原理、主备部署、脑裂处理及Nginx健康检查自动切换
5
https证书部署
Web服务HTTPS证书部署与TLS加密实战,涵盖加密原理、自签名与真实证书配置及SSL参数优化
随机文章随机推荐



