1 什么是LNMP
LNMP 是一套技术的组合, L=Linux、 N=Nginx、 M=MySQL、 P=PHP
1.2 LNMP架构是如何工作的
首先 Nginx 服务是不能处理动态请求,那么当用户发起动态请求时, Nginx 又是如何进行处理的。
当用户发起 http 请求,请求会被 Nginx 处理,如果是静态资源请求 Nginx 则直接返回,如果是动态请求 Nginx 则通过 fastcgi 协议转交给后端的 PHP 程序处理,具体如下图所示
1.3 Nginx与Fast-CGI工作流程
比喻:
Nginx: 拉货的货车
FastCGI: 高速公路
php-fpm: 每个路道的收费站
wrapper: 搬货的师傅
systemctl start php-fpm: 管理整个高速工作所有的收费站
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
2 第二章 LNMP安装部署
2.1 创建www统一用户
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -s /sbin/nologin -M -u 666 -g 666
[root@web01 ~]# id www
uid=666(www) gid=666(www) 组=666(www)
2.2 使用官方仓库安装nginx
[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
[root@web01 ~]# yum install nginx -y
2.3 启动Nginx并加入开机自启动
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
2.4 使用第三方拓展源安装php7.1
[root@web01 ~]# yum remove php-mysql-5.4 php php-fpm php-common
[root@web01 ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@web01 ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@web01 ~]# 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
2.5 配置php-fpm用户与nginx用户保持一致
方法一:
[root@web01 ~]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf
[root@web01 ~]# sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf
方法二
[root@web01 /etc/nginx/conf.d]# vim /etc/php-fpm.d/www.conf
user = www
group = www
2.6 启动php-fpm并加入开机自启动
[root@web01 ~]# systemctl start php-fpm.service
[root@web01 ~]# systemctl enable php-fpm.service
3 第三章 安装Mariadb数据库(客户端 服务端都要安装数据库)
[root@web01 ~]# yum install mariadb-server mariadb -y
3.1 启动Mariadb数据库并加入开机自启动
[root@web01 ~]# systemctl start mariadb
[root@web01 ~]# systemctl enable mariadb
3.2 配置Mariadb账号密码
[root@web01 ~]# mysqladmin password 'oldboy123'
[root@web01 ~]# mysql -uroot -poldboy123
方法二 进入数据库 (安全)
[root@web01 ~]# mysql -uroot -p
Enter password:
3.3 mysql基础命令
1.查看当前mysql有哪些用户
MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | web01 |
| root | web01 |
+------+-----------+
2.查看当前有哪些数据库
show databases;
3.进入一个数据库
use mysql;
4.查看一个库下的表
show tables;
5.查看当前数据库里有那些用户
select user,host from mysql.user;
6.mysql 数据库用户的概念
user | host
7.创建一个数据库
create database zhong
3.4 退出
exit
4 第四章 LNMP环境配置
官网:http://nginx.org/
4.1 Fastcgi语法
设置 fastcgi 服务器的地址,该地址可以指定为域名或 IP 地址,以及端口
Syntax: fastcgi_pass address;
Default: —
Context: location, if in location
#语法示例
fastcgi_pass localhost:9000;
fastcgi_pass unix:/tmp/fastcgi.socket;
4.2 设置 fastcgi 默认的首页文件,需要结合 fastcgi_param 一起设置
Syntax: fastcgi_index name;
Default: —
Context: http, server, location
4.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.4 最终Nginx连接FastCGI服务器配置如下
[root@web01 /etc/nginx/conf.d]# vim php.conf
server {
server_name php.mysun.com;
listen 80;
root /code;
index index.php index.html;
location ~ \.php$ {
root /code;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
4.5 在/code目录下创建info.php文件并访问测试 (没有目录就创建)
[root@web01 ~]# mkdir /code
[root@web01 ~]# chown -R www:www /code/
[root@web01 ~]# cat /code/info.php
<?php
phpinfo();
?>
4.6 检查nginx语法并重启
[root@web01 ~]# 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 ~]# systemctl restart nginx
4.7 绑定hosts然后在浏览器访问
5 第五章 测试php和数据库访问是否正常
[root@web01 /code]# vim mysql.php
<?php
$servername = "localhost";
$username = "root";
$password = "oldboy123";
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// // 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "php 连接 MySQL 数据库成功";
?>
[root@web01 ~]# chown -R www:www /code/
5.1 通过浏览器访问数据库页面
666 总结
要想连接PHP和 Mysql 要先启动服务
1. 启动Mariadb数据库并加入开机自启动
[root@web01 ~]# systemctl start mariadb
[root@web01 ~]# systemctl enable mariadb
2.启动php-fpm并加入开机自启动
[root@web01 ~]# systemctl start php-fpm.service
[root@web01 ~]# systemctl enable php-fpm.service
6 第六章 部署博客Wordpress
6.1 配置Nginx虚拟主机站点
[root@web01 /etc/nginx/conf.d]# mv php.conf wordpress.conf
[root@web01 /etc/nginx/conf.d]# vim wordpress.conf
server {
server_name blog.mysun.com;
listen 80;
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;
}
}
6.2 重启nginx
[root@web01 /etc/nginx/conf.d]# systemctl restart nginx
6.3 下载解压wordpress到代码目录
注:代码要找到存放路径,也可以直接拉进来
[root@web01 ~]# mkdir /code
[root@web01 ~]# cd /code/
[root@web01 /code]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@web01 /code]# tar xf wordpress-4.9.4-zh_CN.tar.gz
[root@web01 /code]# chown -R www:www /code/wordpress
6.4 建wordpress数据库
[root@web01 ~]# mysql -uroot -poldboy123
MariaDB [(none)]> create database wordpress;
MariaDB [(none)]> exit
6.5 浏览器访问wordpress并部署
7 部署问答网站Wecenter
7.1 配置Nginx虚拟站点
注明:(wordpress.conf)是已经写好的模板
[root@web01 /etc/nginx/conf.d]# cp wordpress.conf zh.conf
[root@web01 /etc/nginx/conf.d]# vim zh.conf
server {
server_name zh.mysun.com;
listen 80;
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;
}
}
7.2 检测语法 重启服务
[root@web01 /etc/nginx/conf.d]# nginx -t
[root@web01 /etc/nginx/conf.d]# systemctl restart nginx
7.3 下载解压wecenter并授权 注明( zh 代码要下载的)
[root@web01 /code]#
[root@web01 /code]# unzip WeCenter_3-3-2.zip -d zh
[root@web01 /code]# chown -R www:www /code/
7.4 创建wecenter数据库
[root@web01 ~]# mysql -uroot -poldboy123
MariaDB [(none)]> create database zh;
MariaDB [(none)]> show databases; #查看数据库
MariaDB [(none)]> exit
7.5 浏览器访问
8 总结 要想打开网页 要现把相关服务启动起来
启动Nginx并加入开机自启动
[root@web01 ~]# systemctl start nginx
启动php-fpm并加入开机自启动
[root@web01 ~]# systemctl start php-fpm.service
启动Mariadb数据库并加入开机自启动
[root@web01 ~]# systemctl start mariadb