广告
返回顶部
首页 > 资讯 > 数据库 >LNMP架构 LNMP 就是 Linux+Nginx+MySQL+PHP , Linux 作为服务器的操作系统, Nginx 作为 Web 服务器PHP作为解析动态脚本语言 MySQL 即为数据库
  • 338
分享到

LNMP架构 LNMP 就是 Linux+Nginx+MySQL+PHP , Linux 作为服务器的操作系统, Nginx 作为 Web 服务器PHP作为解析动态脚本语言 MySQL 即为数据库

1024程序员节服务器运维云计算架构 2023-10-27 05:10:21 338人浏览 薄情痞子
摘要

LNMP架构 ​ 1.LNMP架构概述 ​ 2.安装LNMP架构 ​ 3.检测LNMP架构 ​ 4.部署博客产品 Wordpress ​ 5.部署知乎产品 Wecenter ​ 6.部署网校产品Edu

LNMP架构

​ 1.LNMP架构概述

​ 2.安装LNMP架构

​ 3.检测LNMP架构

​ 4.部署博客产品 Wordpress

​ 5.部署知乎产品 Wecenter

​ 6.部署网校产品EduSoho

​ 7.迁移数据至独立服务器

​ 8.迁移图片至独立服务器

​ 9.扩展相同的WEB服务器

1.LNMP架构概述

LNMP 就是 linux+Nginx+Mysql+PHP , Linux 作为服务器的操作系统, Nginx 作为 Web 服务器、 php作为解析动态脚本语言、 mysql 即为数据库

​ Linux作为服务器的操作系统。

​ Nginx作为WebServer服务器。

​ PHP 作为动态解析服务(php)。

​ Mysql作为后端存储数据库服务。

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

用户–> http 协议–> Nginx --> fastcgi 协议–> php-fpm

注意: fatcgi 是 nginx 连接 php-fpm 之间的协议。

1.浏览器输入域名,浏览器会拿着域名取DNS服务器解析2.DNS服务器会将域名解析成IP3.浏览器会去与IP对应服务器建立tcp\IP连接4.连接建立完成,会向服务器发起请求,请求nginx5.nginx会判断请求是动态的还是静态的#静态请求location \.jpg$ { root /code;}#动态php请求location \.php$ { fastcgi_pass 127.0.0.1:9000; ......}#动态jsp请求location \.jsp$ { proxy_pass 127.0.0.1:8080; ......}6.如果是静态请求,nginx去code目录获取,直接返回7.如果是动态请求,nginx会通过fastcgi协议连接PHP服务的php-fpm管理进程8.php-fpm管理进程会下发工作给 wrapper工作进程9.wrapper工作进程判断是不是简单的php内容10.如果只是php内容则使用php解析器解析后直接返回11.如果还需要读取数据库,wrapper工作进程会去数据库读取数据,再返回数据12.数据流转过程: 1)请求:浏览器 > 负载均衡 > nginx > php-fpm > wrapper > mysql 2)响应:mysql > wrapper > php-fpm > nginx > 负载均衡 > 浏览器

2.安装LNMP架构

yum安装 nginx1.14 php7.1 mysql5.7

1.安装nginx

#1.使用Nginx官方提供的rpm包[root@nginx ~]# cat /etc/yum.repos.d/nginx.repo [nginx]name=nginx repobaseurl=Http://nginx.org/packages/Centos/7/$basearch/gpGCheck=0enabled=1#2.执行yum安装[root@nginx ~]# yum install nginx -y#3.修改程序用户[root@nginx ~]# groupadd www -g 666[root@nginx ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M#修改nginx配置文件[root@nginx ~]# sed -i '/^user/c user www;' /etc/nginx/nginx.conf#4.启动并加入开机自启动[root@nginx ~]# systemctl start nginx[root@nginx ~]# systemctl enable nginx

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

#1.移除旧版php[root@nginx ~]# yum remove php-mysql-5.4 php php-fpm php-common -y#2.安装扩展源# 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@nginx ~]# vim /etc/yum.repos.d/php.repo[php-webtatic]name = PHP Repositorybaseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/gpgcheck = 0#3.安装php7.1版本[root@nginx ~]# 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#4.替换php-fpm运行的用户和组身份[root@nginx ~]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf [root@nginx ~]# sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf#5.启动php-fpm管理进程, 并加入开机自启[root@nginx ~]# systemctl start php-fpm[root@nginx ~]# systemctl enable php-fpm

3.安装 MySQL 本数据库 ( MySQL 5.7)

#1.下载MySQL官方扩展源[root@nginx ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm#2.安装mysql5.7, 文件过大可能会导致下载缓慢[root@nginx ~]# yum install mysql-community-server -y#3.启动数据库, 并加入开机自启动[root@nginx ~]# systemctl start mysqld[root@nginx ~]# systemctl enable mysqld#4.由于mysql5.7默认配置了默认密码, 需要过滤temporary password关键字查看对应登陆数据库密码[root@nginx ~]# grep "temporary password" /var/log/mysqld.log#5.登陆mysql数据库[password中填写上一步过滤的密码][root@nginx ~]# mysql -uroot -p$(awk '/temporary password/{print $NF}' /var/log/mysqld.log)#6.重新修改数据库密码mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '数据库密码';# 或直接用命令修改mysqladmin -uroot -p$(awk '/temporary password/{print $NF}' /var/log/mysqld.log)password “xxxx'

MySQL5.6

#1.下载MySQL官方扩展源[root@nginx ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.6-commUnity/el/7/x86_64/mysql-community-release-el7-5.noarch.rpm#2.安装mysql5.6, 文件过大可能会导致下载缓慢[root@nginx ~]# yum install mysql-community-server -y#3.启动数据库, 并加入开机自启动[root@nginx ~]# systemctl start mysqld[root@nginx ~]# systemctl enable mysqld#4.由于mysql5.6默认管理员root的密码为空,服务器启动后,可以直接登录[root@web01 ~]# mysql -uroot……mysql> #5.为了安全,必须给root设置密码[root@nginx ~]# mysqladmin -u root password 数据库密码#6.使用密码登陆mysql[root@web01 ~]# mysql -uroot -p数据库密码……mysql>

4.验证 Nginx 是否能正常解析 php 动态请求,以及 php 程序能否正常连接数据库

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

1)设置fastcgi服务器的地址,该地址可以指定为域名或IP地址,以及端口
Syntax: fastcgi_pass address;Default: —Context: location, if in location#语法示例fastcgi_pass localhost:9000;fastcgi_pass unix:/tmp/fastcgi.Socket;
2)设置fastcgi默认的首页文件,需要结合fastcgi_param一起设置
Syntax: fastcgi_index name;Default: —Context: http, server, location
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;
4)通过图形方式展示 fastcgi_index 与 fastcgi_param 作用

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XCFPsPQM-1666542593758)(…/…/图片/image-20221011203900243.png)]

5)最终Nginx连接Fastcgi服务器配置如下
[root@nginx ~]# cat /etc/nginx/conf.d/php.conf server { server_name www.tf.com; listen 80; root /code/www; index index.php index.html;  location ~ \.php$ { root /code/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }}

5.新增 info.php , 测试 php 解析是否工作正常

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

6.测试 php 是否能连接 mysql 数据库服务[无论是本地数据库还是远程数据库,测试方式一致]

[root@nginx ~]# cat /code/www/mysqli.php<?php$servername = "localhost";$username = "root";$password = "数据库密码"; // 创建连接$conn = mysqli_connect($servername, $username, $password); // 检测连接if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }echo "连接成功";?>

3.检测LNMP架构

通过浏览器访问info.php文件。如果出现连接成功表示nginx和php能正常工作

访问mysql.php验证php-mysqli模块是否正常工作

总结LNMP架构访问流程

实现上传大文件。需要如下配置

1.修改nginx的配置文件# vim /etc/nginx/nginx.confhttp { #nginx默认请求文件大小为1M,也可以写在server{}或location{}中 client_max_body_size 10m;}2.修改php的配置文件vim /etc/php.ini#文件上传功能,默认开启file_uploads = On#上传最大文件大小,默认2Mupload_max_filesize = 200M#文件最大尺寸,默认2Mpost_max_size = 200M#一个请求可以上传的最大文件数 ,默认20max_file_uploads = 20

4.部署博客产品 Wordpress

1.配置Nginx虚拟主机站点,域名为blog.tf.com

#1.nginx具体配置信息[root@nginx ~]# cat /etc/nginx/conf.d/wordpress.confserver { listen 80; server_name blog.tf.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; }}

2.重启nginx服务

[root@nginx ~]# systemctl restart nginx

3.下载wordpress产品,部署wordress并授权

#1.获取wordpress代码[root@nginx code]# wget https://cn.wordpress.org/wordpress-5.0.3-zh_CN.tar.gz#永远不要下载最新版[root@nginx code]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz#2.解压网站源码文件,拷贝至对应站点目录,并授权站点目录[root@nginx ~]# tar xf wordpress-5.0.3-zh_CN.tar.gz[root@nginx ~]# cp -r wordpress /code/[root@nginx ~]# chown -R www.www /code/wordpress/

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

#1.登陆数据库[root@nginx ~]# mysql -uroot -p数据库密码#2.创建wordpress数据库mysql> create database wordpress;mysql> exit

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

5.部署知乎产品 Wecenter

1.配置Nginx虚拟主机站点,域名为zh.zmj.com

#1.nginx具体配置信息[root@nginx ~]# cat /etc/nginx/conf.d/zh.confserver { listen 80; server_name zh.tf.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; }}#2.重启nginx服务[root@nginx ~]# systemctl restart nginx

2.下载Wecenter产品,部署Wecenter并授权

[root@nginx ~]# wget http://ahdx.down.chinaz.com/201605/WeCenter_v3.2.1.zip[root@nginx ~]# unzip WeCenter_3-2-1.zip[root@nginx ~]# mv WeCenter_3-2-1/ /code/zh[root@nginx ~]# chown -R www.www /code/zh/

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

#1.登陆数据库[root@nginx ~]# mysql -uroot -p数据库密码#2.创建wordpress数据库mysql> create database zh;mysql> exit

4.通过浏览器访问网站

6.部署网校产品EduSoho

1.配置Nginx虚拟主机站点,域名为edu.zmj.com

#1.nginx具体配置信息[root@nginx ~]# cat /etc/nginx/conf.d/edu.confserver { listen 80; server_name edu.zmj.com; root /code/edu/web; location / { index app.php; try_files $uri @rewriteapp; } location @rewriteapp { rewrite ^(.*)$ /app.php/$1 last; } location ~ ^/udisk { internal; root /code/edu/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/edu/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; }}#2.重启nginx服务[root@nginx ~]# systemctl restart nginx

2.下载edusohu产品,部署edusohu并授权

//获取edusoho代码[root@nginx ~]# cd /soft/[root@nginx soft]# wget http://download.edusoho.com/edusoho-8.2.17.tar.gz//解压软件网站源码文件, 并授权站点目录,不然会导致无法安装[root@nginx soft]# tar xf edusoho-8.2.17.tar.gz[root@nginx soft]# mv edusoho /code/edu[root@nginx soft]# chown -R www.www /code/edu/[root@nginx soft]# chmod -R 777 /code/edu/{app,web}//由于edusohu会自动创建数据库, 所以无需创建数据库

3.通过浏览器访问网站

7.迁移数据至独立服务器

为什么要进行数据库的拆分

由于单台服务器允许LNMP架构会导致网站访问缓慢,当内存被占满时,很容易导致系统出现oom,自动关闭(kill)MySQL数据库,所以要讲web和数据库进行独立部署。

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

​ 1.缓解web网站的压力

​ 2.增强数据库读写性能

​ 3.提高用户访问速度

拆分LNMP的数据库到独立的数据库服务器步骤

1.老服务器操作
  1. 指定导出对应的数据库文件。

    [root@web01 ~]# mysqldump -uroot -p数据库密码 -A > $(date +%F)-mysql-all.sql
  2. 拷贝备份数据库文件至新的数据库服务器上

    [root@web01 zh]# scp 2018-08-09-mysql-all.sql root@10.0.0.51:~
2.新服务器操作
  1. 导入数据库

    [root@db01 ~]# mysql -uroot -p数据库密码 < 2018-08-09-mysql-all.sql 
  2. 登录数据库

    [root@db01 ~]# mysql -uroot -p数据库密码
  3. 检查数据库是否所有的库都被成功导入

    mysql> show databases;
  4. 在新数据库上授权, 允许所有网段, 通过webadm账户连接数据库

    #授权所有权限 grant all privileges#授权所有库所有表 *.* #将授权赋予给哪个用户,这个用户只能通过哪个网段过来(%所有) 'webadm'@'%'#授权该用户登录的密码 identified bymysql> grant all on *.* to webadm@'%' identified by '数据库密码';Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)
  5. 停止web本地数据库

    [root@web01 ~]# systemctl stop mysqld[root@web01 ~]# systemctl disable mysqld
  6. 修改Wordpress产品代码连接数据库的配置文件

    [root@web01 ~]# vim /code/wordpress/wp-config.php# 数据库名称define('DB_NAME', 'wordpress');# 数据库用户define('DB_USER', 'webadm');# 数据库密码define('DB_PASSWORD', '数据库密码');# 数据库地址define('DB_HOST', '10.0.0.51');
  7. 修改wecenter产品代码连接数据库的配置文件

[root@web01 zh]# grep -iR "123.com"|grep -v cachesystem/config/database.php: 'password' => '123.com',[root@web01 zh]# vim /code/zh/system/config/database.php'host' => '10.0.0.51','username' => 'webadm','password' => 'Tl123.com','dbname' => 'zh',
  1. 修改edusoho产品代码连接数据库的配置文件

    [root@web01 edu]# grep -iR "123.com"|grep -v cacheapp/config/parameters.yml: database_password: '123.com'[root@web01 edu]# vim /code/edu/app/config/parameters.yml parameters: database_driver: pdo_mysql database_host: 10.0.0.51 database_port: 3306 database_name: edu database_user: webadm database_password: '123.com'#必须清理缓存[root@web01 edu]# rm -rf /code/edu/app/cache/*

8.迁移图片至独立服务器

1.nfs-server服务端操作

1. 配置nfs共享的目录

[root@nfs ~]# cat /etc/exports/data/blog 10.0.0.0/24(rw,sync,all_squash,anonuid=666,anongid=666)/data/edu 10.0.0.0/24(rw,sync,all_squash,anonuid=666,anongid=666)/data/zh 10.0.0.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

2.创建对应共享的目录

[root@nfs ~]# mkdir /data/{blog,edu,zh} -p[root@nfs ~]# chown -R www.www /data/[root@nfs ~]# systemctl restart nfs-server

2.web01端操作

1.WEB客户端验证NFS是否安装成功

[root@web01 ~]# yum install nfs-utils -y[root@web01 ~]# showmount -e 10.0.0.31Export list for 10.0.0.31:/data/zh 10.0.0.0/24/data/edu 10.0.0.0/24/data/blog 10.0.0.0/24

2.获取Wordpress产品的附件和图片存放的位置

浏览器->右键->检查->Network->选择按钮->点击一下图片

3.备份web服务器上的Wordpress图片和附件

[root@web01 ~]# cd /code/wordpress[root@web01 wordpress]# cp -rp wp-content/ wp-content_bak

4.客户端执行挂载操作[Wordpress]

[root@web01 wordpress]# mount -t nfs 10.0.0.31:/data/blog /code/wordpress/wp-content/[root@web01 wordpress]# cp -rp uploads_bak/* uploads/

5.将挂载信息加入开机自启

[root@web01 wordpress]# tail -1 /etc/fstab 10.0.0.31:/data/blog /code/wordpress/wp-content nfs defaults 0 0[root@web01 wordpress]# mount -a

3.web02端操作

1.挂载nfs存储

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

2.将挂载信息加入开机自启

[root@web02 ~]# tail -1 /etc/fstab 10.0.0.31:/data/blog /code/wordpress/wp-content nfs defaults 0 0[root@web02 wp-content]# mount -a

9.扩展相同的Web服务器

快速的扩展一台相同的web服务器, 数据库一模一样,图片、附件都一样, 准备一台新的服务器
10.0.0.9

1.创建www用户

[root@web03 ~]# groupadd -g666 www[root@web03 ~]# useradd -u666 -g666 www

2.安装LNP

[root@web03 ~]# scp -rp root@10.0.0.7:/etc/yum.repos.d/* /etc/yum.repos.d/[root@web03 ~]# scp -rp root@10.0.0.7:/etc/pki/rpm-gpg/* /etc/pki/rpm-gpg/[root@web03 ~]# yum install nginx -y[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

3.将web01的nginx配置文件导入到web03

[root@web03 ~]# scp -rp root@10.0.0.7:/etc/nginx/* /etc/nginx/

4.将web01的php配置文件导入到web03

[root@web03 ~]# scp -rp root@10.0.0.7:/etc/php-fpm.d/* /etc/php-fpm.d/

5.将web01的产品代码导到web03,在web1上线进行打包操作

[root@web01 ~]# tar czf code.tar.gz /code/#在web3上面拉取web1打包好的内容[root@web03 ~]# scp root@10.0.0.7:/root/code.tar.gz ~#在web3上面解压即可[root@web03 ~]# tar xf code.tar.gz -C /

6.启动相关的服务

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

7.在web02上进行挂载

[root@web02 ~]# mount -t nfs 10.0.0.31:/data/blog /code/wordpress/wp-content# 所有的挂载都应该加入开机自启动

odb

### 3.将web01的nginx配置文件导入到web03

[root@web03 ~]# scp -rp root@10.0.0.7:/etc/nginx/* /etc/nginx/

### 4.将web01的php配置文件导入到web03

[root@web03 ~]# scp -rp root@10.0.0.7:/etc/php-fpm.d/* /etc/php-fpm.d/

### 5.将web01的产品代码导到web03,在web1上线进行打包操作

[root@web01 ~]# tar czf code.tar.gz /code/
#在web3上面拉取web1打包好的内容
[root@web03 ~]# scp root@10.0.0.7:/root/code.tar.gz ~
#在web3上面解压即可
[root@web03 ~]# tar xf code.tar.gz -C /

### 6.启动相关的服务

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

### 7.在web02上进行挂载

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

所有的挂载都应该加入开机自启动

来源地址:https://blog.csdn.net/weixin_48824655/article/details/127483999

您可能感兴趣的文档:

--结束END--

本文标题: LNMP架构 LNMP 就是 Linux+Nginx+MySQL+PHP , Linux 作为服务器的操作系统, Nginx 作为 Web 服务器PHP作为解析动态脚本语言 MySQL 即为数据库

本文链接: https://www.lsjlt.com/news/482638.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作