LNMP架构
1.简介
1.LNMP是一套技术的组合,L=Linux、N=Nginx、M~=MysqL、P~=PHP
2.不仅仅只有这些服务,还有很多
3.redis\elasticsearch\kibana\logstash\zabbix\git\jenkins\kafka\hbase\hadoop\spark\flink
2.LNMP架构工作方式
首先Nginx服务是不能处理动态请求,那么当用户发起动态请求时, Nginx又是如何进行处理的。
1.静态请求:请求的内容是静态文件就是静态请求
1)静态文件:文件上传到服务器,永远不会改变的文件就是静态文件
2)html就是一个标准的静态文件
2.动态请求:请求的内容是动态的就是动态请求
1)不是真实存在服务器上的内容,是通过数据库或者其他服务拼凑成的数据
当用户发起http请求,请求会被Nginx处理,如果是静态资源请求Nginx则直接返回,如果是动态请求Nginx则通过fastcgi协议转交给后端的PHP程序处理,具体如下图所示
3.访问流程
1.浏览器输入域名,浏览器会拿着域名取DNS服务器解析
2.DNS服务器会将域名解析成IP
3.浏览器会去与IP对应服务器建立TCP\IP连接
4.连接建立完成,会向服务器发起请求,请求Nginx
5.Nginx会判断请求是动态的还是静态的
#静态请求
location \.jpg$ {
root /code;
}
#动态请求
location \.PHP$ {
fastcgi_pass 127.0.0.1:9000;
... ...
}
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 > 负载均衡 > 浏览器
LNMP架构搭建
1.搭建Nginx
1)配置官方源
[root@web01 ~]# vim /etc/yum.repos.d/Nginx.repo
[Nginx-stable]
name=Nginx stable repo
baseurl=http://Nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://Nginx.org/keys/Nginx_signing.key
module_hotfixes=true
2)安装Nginx
[root@web01 ~]# yum install -y Nginx
3)配置Nginx
[root@web01 ~]# vim /etc/Nginx/Nginx.conf
user www;
4)创建用户
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
5)启动Nginx
[root@web01 ~]# systemctl start Nginx
[root@web01 ~]# systemctl enable Nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/Nginx.service to /usr/lib/systemd/system/Nginx.service.
#验证启动
[root@web01 ~]# ps -ef | grep Nginx
root 9953 1 0 11:17 ? 00:00:00 Nginx: master process /usr/sbin/Nginx -c /etc/Nginx/Nginx.conf
www 9954 9953 0 11:17 ? 00:00:00 Nginx: worker process
2.安装PHP
1)安装方式一
#安装yum源
# 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
# 清除之前部署的PHP
[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
#安装PHP
[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
[root@Nginx ~]# PHP -v
2)安装方式二
1.上传包
[root@web01 ~]# rz
[root@web01 ~]# ll
-rw-r--r--. 1 root root 19889622 Nov 22 15:52 p-hp.tar.gz
2.解压包
[root@web01 ~]# tar xf PHP.tar.gz
3.本地安装PHP的rpm包
[root@web01 ~]# yum localinstall -y *.rpm
3)配置PHP
[root@web01 ~]# vim /etc/PHP-fpm.d/www.conf
user = www
group = www
4)启动服务
[root@web01 ~]# systemctl start PHP-fpm
[root@web01 ~]# systemctl enable PHP-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/PHP-fpm.service to /usr/lib/systemd/system/PHP-fpm.service.
5)验证启动
[root@web01 ~]# ps -ef | grep PHP-fpm
root 10195 1 0 11:29 ? 00:00:00 PHP-fpm: master process (/etc/PHP-fpm.conf)
www 10196 10195 0 11:29 ? 00:00:00 PHP-fpm: pool www
www 10197 10195 0 11:29 ? 00:00:00 PHP-fpm: pool www
www 10198 10195 0 11:29 ? 00:00:00 PHP-fpm: pool www
www 10199 10195 0 11:29 ? 00:00:00 PHP-fpm: pool www
www 10200 10195 0 11:29 ? 00:00:00 PHP-fpm: pool www
搭建交作业页面
1)配置Nginx
[root@web01 ~]# vim /etc/Nginx/conf.d/default.conf
server {
listen 80;
server_name linux.zuoye.com;
location / {
root /code/zuoye;
index index.html;
}
}
2)创建站点目录
[root@web01 ~]# mkdir /code/zuoye -p
3)上传代码
[root@web01 ~]# cd /code/zuoye/
[root@web01 zuoye]# rz
[root@web01 zuoye]# ll
-rw-r--r--. 1 root root 26995 Nov 22 16:47 kaoshi.zip
[root@web01 zuoye]# unzip kaoshi.zip
[root@web01 zuoye]# ll
-rw-r--r--. 1 root root 38772 Apr 27 2018 bg.jpg
-rw-r--r--. 1 root root 2633 May 4 2018 index.html
-rw-r--r--. 1 root root 52 May 10 2018 info.PHP
-rw-r--r--. 1 root root 1192 Jan 10 2020 upload_file.PHP
4)修改代码中上传作业位置
[root@web01 ~]# vim /code/zuoye/upload_file.PHP
$wen="/code/zuoye/upload";
5)授权
[root@web01 zuoye]# chown -R www.www /code/
6)重启服务
[root@web01 ~]# systemctl restart Nginx
7)配置hosts访问测试
#配置hosts
10.0.0.7 linux.zuoye.com
#访问
http://linux.zuoye.com/
#测试
上传代码出错,报错405,因为Nginx没办法处理PHP代码程序
关联Nginx与PHP
1)关联语法
#fastcgi_pass,Nginx连接PHP的代理协议
Syntax: fastcgi_pass address;
Default: —
Context: location, if in location
#指定请求的文件
Syntax: fastcgi_param parameter value [if_not_empty];
Default: —
Context: http, server, location
#指定默认的PHP页面
Syntax: fastcgi_index name;
Default: —
Context: http, server, location
2)配置
[root@web01 ~]# vim /etc/Nginx/conf.d/default.conf
server {
listen 80;
server_name linux.zuoye.com;
location / {
root /code/zuoye;
index index.html;
}
location ~* \.PHP$ {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME /code/zuoye/$fastcgi_script_name;
include fastcgi_params;
}
}
3)访问页面测试
1.访问页面
http://linux.zuoye.com/
2.上传图片文件
成功
3.上传一个大文件
失败,413报错,说文件过大
#解决:修改配置文件上传文件大小配置
[root@web01 ~]# vim /etc/Nginx/Nginx.conf
http {
... ...
client_max_body_size 200m;
... ...
}
[root@web01 ~]# systemctl restart Nginx
[root@web01 ~]# vim /etc/PHP.ini
upload_max_filesize = 200M
post_max_size = 200M
[root@web01 ~]# systemctl restart PHP-fpm
4.重新上传文件测试
成功
5.搭建mariadb
1)安装
[root@web01 ~]# yum install -y mariadb-server
2)启动服务
[root@web01 ~]# systemctl start mariadb
[root@web01 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
3)验证启动
[root@web01 ~]# ps -ef | grep mariadb
MysqL 11006 10841 1 12:06 ? 00:00:00 /usr/libexec/MysqLd --basedir=/usr --datadir=/var/lib/MysqL --plugin-dir=/usr/lib64/MysqL/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/MysqL/MysqL.sock
4)连接
[root@web01 ~]# MysqL
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server
copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases; #查看数据库
+--------------------+
| Database |
+--------------------+
| @R_310_4045@ion_schema |
| MysqL |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
5)设置数据库密码
[root@web01 ~]# MysqLadmin -uroot password '123'
#使用密码连接数据库
[root@web01 ~]# MysqL -uroot -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.68-MariaDB MariaDB Server
copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
测试PHP和mariadb关联
1)编写PHP测试连接数据库的代码
[root@web01 ~]# vim /code/zuoye/test.PHP
<?PHP
$servername = "172.16.1.51";
$username = "root";
$password = "123456";
// 创建连接
$conn = MysqLi_connect($servername, $username, $password);
// 检测连接
if (!$conn) {
die("Connection Failed: " . MysqLi_connect_error());
}
echo "小哥哥,PHP可以连接MysqL...";
?>
<img style='width:100%;height:100%;' src=https://blog.drig>
2)访问测试
http://linux.zuoye.com/test.PHP
搭建wordpress博客
1.上传代码
[root@web01 code]# rz
[root@web01 code]# ll
-rw-r--r--. 1 root root 11098483 Sep 12 17:52 wordpress-5.0.3-zh_CN.tar.gz
2.解压代码
[root@web01 code]# tar xf wordpress-5.0.3-zh_CN.tar.gz
3.授权
[root@web01 code]# chown -R www.www wordpress
4.配置Nginx
[root@web01 code]# vim /etc/Nginx/conf.d/linux.wp.com.conf
server {
listen 80;
server_name linux.wp.com;
location / {
root /code/wordpress;
index index.PHP;
}
location ~* \.PHP$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /code/wordpress/$fastcgi_script_name;
include fastcgi_params;
}
}
5.重启访问
#检查配置
[root@web01 code]# 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 code]# systemctl restart Nginx
6.访问测试
#配置hosts
10.0.0.7 linux.wp.com
#访问
http://linux.wp.com/
7.创建数据库
[root@web01 code]# MysqL -uroot -p123
MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| @R_310_4045@ion_schema |
| MysqL |
| performance_schema |
| test |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)
8.根据页面提示操作
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。