LNMP架构概述
什么是LNMP
LNMP是一套技术的组合,L=Linux、N=Nginx、M~=MySQL、P~=PHP
LNMP架构是如何工作的
首先Nginx服务是不能处理动态请求,那么当用户发起动态请求时, Nginx又是如何进行处理的。
当用户发起http请求,请求会被Nginx处理,如果是静态资源请求Nginx则直接返回,如果是动态请求Nginx则通过fastcgi协议转交给后端的PHP程序处理,具体如下图所示
Nginx与Fast-CGO详细工作流程

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
[root@nginx ~]# cat /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
#安装Nginx
[root@nginx ~]# yum install nginx -y
修改nginx用户
[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
启动Nginx加入开机自启
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# systemctl enable nginx
使用第三方扩展源安装php7.1
# 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 ~]# yum remove php-mysql-5.4 php php-fpm php-common
#配置第三方源
[root@nginx ~]# vim /etc/yum.repos.d/php.repo
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0
[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
配置php-fpm用户与Nginx的运行用户保持一致
[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
启动php-fpm加入开机自启
[root@nginx ~]# systemctl start php-fpm
[root@nginx ~]# systemctl enable php-fpm
安装Mariadb数据库
[root@nginx ~]# yum install mariadb-server -y
启动Mariadb加入开机自动
[root@nginx ~]# systemctl start mariadb
[root@nginx ~]# systemctl enable mariadb
给Mariadb配置登陆密码
[root@nginx ~]# mysqladmin password 'Zls123.com'
[root@nginx ~]# mysql -uroot -pZls123.com
LNMP架构环境配置
在将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作用


5.最终Nginx连接Fastcgi服务器配置如下
[root@nginx ~]# cat /etc/nginx/conf.d/php.conf
server {
listen 80;
server_name php.driverzeng.com;
location / {
root /code;
index index.php index.html;
}
location ~ \.php$ {
root /code;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name
}
}
6.在/code目录下创建info.php文件,测试能否通过浏览器访问,访问成功如下图
[root@nginx ~]# cat /code/info.php
phpinfo();
?>

按照刚才的配置,页面打开一片空白。
[root@nginx ~]# cat /etc/nginx/conf.d/php.conf
server {
listen 80;
server_name php.driverzeng.com;
location / {
root /code;
index index.php index.html;
}
location ~ \.php$ {
root /code;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

7.在/code目录下创建mysqli.php文件,填入对应的数据库IP、用户名、密码
[root@nginx ~]# cat /code/mysqli.php
$servername = "localhost";
$username = "root";
$password = "Zls123.com";
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "小哥哥,php可以连接MySQL...";
?>

部署博客产WordPress
#1.nginx具体配置信息
[root@nginx ~]# cat /etc/nginx/conf.d/wordpress.conf
server {
listen 80;
server_name blog.driverzeng.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
[root@nginx ~]# mkdir /code
[root@nginx ~]# cd /code
[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
[root@nginx ~]# tar xf wordpress-5.0.3-zh_CN.tar.gz
[root@nginx ~]# chown -R www.www /code/wordpress/
4)由于wordpress产品需要依赖数据库,所以需要手动建立数据库
[root@nginx ~]# mysql -uroot -pZls123.com
mysql> create database wordpress;
mysql> exit
5)通过浏览器访问wordpress,并部署该产品








思考问题:上传文件报错413,如何解决?
提示:nginx上传大小的限制
部署知乎产品Wecenter
#1.nginx具体配置信息
[root@http-server ~]# cat /etc/nginx/conf.d/zh.conf
server {
listen 80;
server_name zh.driverzeng.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@http-server ~]# systemctl restart nginx
2.下载Wecenter产品,部署Wecenter并授权
官方下载地址:TP
[root@web02 ~]# wget http://ahdx.down.chinaz.com/201605/WeCenter_v3.2.1.zip
[root@web02 ~]# unzip WeCenter_3-2-1.zip
[root@web02 ~]# mv WeCenter_3-2-1/ /code/zh
[root@web02 ~]# chown -R www.www /code/zh/
3.由于wecenter产品需要依赖数据库, 所以需要手动建立数据库
#1.登陆数据库
[root@http-server ~]# mysql -uroot -pZls123.com
#2.创建wordpress数据库
MariaDB [(none)]> create database zh;
MariaDB [(none)]> exit
3.通过浏览器访问网站






当然除了这些产品,还有很多我们可以尝试着搭建的:
phpmyadmin
zblog
discuz
edusoho