微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

LNMP原理及分离部署的实现

1. LNMP理论

在这里插入图片描述


fastcgi的主要优点:
把动态语言和http服务器分离开来,使Nginx可以处理静态请求和向后转发动态请求,而PHP/PHP-fpm服务器转移解析PHP动态请求

使用fastcgi的原因:
Nginx 不支持对外部动态程序的直接调用或者解析 ,所有的外部程序(包括PHP)必须通过FastCGI接口来调用

FastCGI的重要特点总结:

  • 是HTTP服务器和动态脚本语言间通信的接口或者工具
  • 优点就是把动态语言解析和HTTP服务器分离了开来
  • Nginx、Apache、lighttpd以及多数动态语言都支持FastCGI。
  • 接口方式采用C/S结构,分为HTTP服务器(客户端)和动态语言解析服务器(服务端)
  • PHP动态语言服务端可以启动多个FastCGI的守护进程例如:PHP-fpm(fastcgi process mangemnt)
  • http服务器通过例(Nginx fastgi_pass)FastCGI客户端和动态语言FastCGI服务端通信(例如:PHP-fpm)

Nginx - fastcgi - PHP - MysqL 协同下的 请求的完整访问过程

  1. 用户发送http请求报文给Nginx服务器
  2. Nginx会根据文件url和后缀来判断请求
  3. 如果请求的是静态内容,Nginx会将结果直接返回给用户
  4. 如果请求的是动态内容,Nginx会将请求交给fastcgi客户端,通过fastcgi_pass将这个请求发送给PHP-fpm
  5. PHP-fpm会将请求交给wrapper
  6. wrapper收到请求会生成新的线程调用PHP动态程序解析服务器
  7. 如果用户请求的是博文、或者内容PHP会请求MySQL查询结果
  8. 如果用户请求的是图片、附件、PHP会请求nfs存储查询结果
  9. PHP会将查询到的结果交给Nginx
  10. Nginx生成一个响应报文返还给用户

2. LNMP分离部署的实现

主机分配

服务 ip
Nginx 192.168.20.126
PHP 192.168.20.127
MysqL 192.168.20.128

2. 1 Nginx的源码安装

yum -y install gcc gcc-c++ pcre-devel zlib-devel openssl-devel
tar -zxf Nginx-1.15.4.tar.gz 
cd Nginx-1.15.4
./configure && make && make install
ln -s /usr/local/Nginx/sbin/Nginx  /usr/local/sbin/
Nginx -v

2.2 PHP的源码安装

yum -y install gd libxml2-devel libjpeg-devel libpng-devel
tar -zxf PHP-7.2.0.tar.gz 
cd PHP-7.2.0
./configure --prefix=/usr/local/PHP --with-gd --with-zlib --with-pdo-MysqL=MysqLnd --with-MysqLi=MysqLnd --with-config-file-path=/usr/local/PHP --enable-mbstring --enable-fpm --with-jpeg-dir=/usr/lib
make -j4 && make install
cp PHP.ini-development /usr/local/PHP/PHP.ini
cp sapi/fpm/init.d.PHP-fpm /etc/init.d/PHP-fpm
chmod +x /etc/init.d/PHP-fpm
cd /usr/local/PHP/etc
cp PHP-fpm.conf.default PHP-fpm.conf
cp PHP-fpm.d/www.conf.default PHP-fpm.d/www.conf
ln -s /usr/local/PHP/bin/* /usr/local/bin/
ln -s /usr/local/PHP/sbin/* /usr/local/sbin/

关于MysqLnd:

MysqL Native驱动(MysqL Native Driver 简称:MysqLnd )
MysqLnd是MysqL Native Driver的简称,在PHP5.3.0版本中被引入,PHP5.4之后的版本MysqLnd被作为认配置选项。 由zend 公司开发的MysqL数据库驱动,是专门为PHP而写的一个库,用了PHP的内在管理函数以及一些网络流的函数

修改PHP-fpm.conf配置

[root@localhost etc]# vim PHP-fpm.conf
 17 pid = run/PHP-fpm.pid #pid取消注释
 69 process.max = 128 #取消注释 fork出的子进程的最多数量
100 events.mechanism = epoll #使用epoll模型 

2.3 MysqL的源码安装

鉴于前边的文章已经详细说过MysqL的源码安装 可以点击基于LAMP的Discuz论坛的实现(源码安装)自行查看 这里为了节省时间 用一个shell脚本进行MysqL的源码安装 用到的MysqL依然是5.6.46 没有源码包的同学可以点击 【mysql-5.6.46.tar.gz 提取码:nli5】进行下载

[root@localhost ~]# cat my.sh 
#!/bin/bash
yum -y install bison cmake ncurses-devel openssl-devel perl-Data-Dumper 
tar -zxf mysql-5.6.46.tar.gz 
cd mysql-5.6.46
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/MysqL -DSYSconfdIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all
make -j4 && make install
ln -s /usr/local/MysqL/lib/libMysqLclient.so.18 /usr/lib
ln -s /usr/local/MysqL/bin/* /usr/local/bin/
useradd -M -s /sbin/nologin MysqL
chown -R MysqL:MysqL /usr/local/MysqL/
cd support-files/
\cp my-default.cnf /etc/my.cnf
cp MysqL.server /etc/init.d/MysqLd
sed -i '/^basedir=/c basedir=/usr/local/MysqL' /etc/init.d/MysqLd
sed -i '/^datadir=/c datadir=/usr/local/MysqL/data' /etc/init.d/MysqLd
chmod +x /etc/init.d/MysqLd 
/usr/local/MysqL/scripts/MysqL_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/MysqL --datadir=/usr/local/MysqL/data --user=MysqL
[root@localhost ~]# bash my.sh

2.4 在PHP主机上搭建nfs服务

三个主机都添加用户Nginx 命令如下

[root@localhost ~]# useradd -M -s /sbin/nologin -u 1111 Nginx 

三个主机都创建用户nfs的目录并设置属主属组

[root@localhost ~]# mkdir /www
[root@localhost ~]# chown -R Nginx:Nginx /www

PHP主机配置nfs服务

[root@localhost ~]# yum -y install nfs-utils
[root@localhost ~]# vim /etc/exports 
/www 192.168.20.0/24(rw,no_root_squash,sync)
[root@localhost ~]# systemctl start rpcbind 
[root@localhost ~]# systemctl start nfs

Nginx主机和MysqL主机挂载nfs服务

[root@localhost ~]# yum -y install nfs-utils
[root@localhost ~]# mount -t nfs 192.168.20.127:/www /www

2.5 修改Nginx配置

[root@localhost ~]# vim /usr/local/Nginx/conf/Nginx.conf
  2 user  Nginx Nginx;
 43         location / {
 44             root   /www; #改为/www
 45             index  index.PHP index.html index.htm; #添加index.PHP 
 46         }
 65         location ~ \.PHP$ {
 66             root           /www;
 67             fastcgi_pass   192.168.20.127:9000;
 68             fastcgi_index  index.PHP;
 69             fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 70             include        fastcgi.conf;
 71         }

2.6 修改PHP的配置

[root@localhost ~]# vim /usr/local/PHP/etc/PHP-fpm.d/www.conf
 23 user = Nginx
 24 group = Nginx
 36 listen = 192.168.20.127:9000
 47 listen.owner = Nginx
 48 listen.group = Nginx
 49 listen.mode = 0660
 62 listen.allowed_clients = 192.168.20.126 #允许连接的fastcgi 客户端地址 如果有多个 用逗号间隔
 96 pm = dynamic #此行需要注意下 不用改
107 pm.max_children = 128
112 pm.start_servers = 20
117 pm.min_spare_servers = 10
122 pm.max_spare_servers = 30

关于PHP进程管理的几个参数:

pm = dynamic PHP进程的管理方式 dynamic为动态管理 static为静态管理
pm.max_children:静态方式下开启的PHP-fpm进程数量,在动态方式下他限定PHP-fpm的最大进
				 程数(这里要注意pm.max_spare_servers的值只能小于等于pm.max_children)
pm.start_servers:动态方式下的起始PHP-fpm进程数量。
pm.min_spare_servers:动态方式空闲状态下的最小PHP-fpm进程数量。
pm.max_spare_servers:动态方式空闲状态下的最大PHP-fpm进程数量

2.7 各主机启动服务

如果已经启动 则重启

[root@localhost ~]# Nginx -t
Nginx: the configuration file /usr/local/Nginx/conf/Nginx.conf Syntax is ok
Nginx: configuration file /usr/local/Nginx/conf/Nginx.conf test is successful
[root@localhost ~]# Nginx
[root@localhost ~]# netstat -anput | grep LISTEN | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      14210/Nginx: master 

[root@localhost ~]# /etc/init.d/PHP-fpm start
Starting PHP-fpm  done
[root@localhost ~]# netstat -anput | grep LISTEN | grep 9000
tcp        0      0 192.168.20.127:9000     0.0.0.0:*               LISTEN      130876/PHP-fpm: mas 

[root@localhost ~]# /etc/init.d/MysqLd start
Starting MysqL... SUCCESS! 
[root@localhost ~]# netstat -anput | grep LISTEN | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      3274/MysqLd 

2.8 编辑测试页面并测试Nginx - PHP - MysqL 连接性

三个主机的/www目录是完全同步的 在任一主机操作都可

[root@localhost ~]# mkdir /www/test
[root@localhost ~]# vim /www/test/test1.PHP
<?PHP
PHPinfo();
?>
[root@localhost ~]# vim /www/test/test2.PHP
<?PHP
$con = new MysqLi('192.168.20.128','root','123456');
if(!$con)
  die("connect error:".MysqLi_connect_error());
else
  echo "connet MysqL server ok!\n";
?>

MysqL授权PHP主机连接

MysqL> grant all on *.* to 'root'@'192.168.20.127' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MysqL> flush privileges;
Query OK, 0 rows affected (0.00 sec)

访问以下两个url 如下图所示效果 则证明OK
http://Nginx的ip/test/test1.PHP http://Nginx的ip/test/test1.PHP

在这里插入图片描述

在这里插入图片描述

2.9 搭建discuz论坛

论坛安装的操作与LAMP中的基本一致 在这里 web界面的操作 就不在做赘述了 可以参考 基于LAMP的Discuz论坛的实现(源码安装)里论坛的安装 里边有详细步骤和说明

[root@localhost ~]# unzip Comsenzdiscuz-discuzX-master.zip 
[root@localhost ~]# cp -r discuzX/upload/* /www
[root@localhost ~]# chmod -R 777 /www
[root@localhost ~]# firefox http://192.168.20.126/install/index.PHP

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


这个页面 要将数据库的地址写为数据库主机的ip 而不是localhost

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐