# LNMP(LINUX,Nginx,Mysql,Php)
- 静态网站(图片)html.txt.doc.jpg.mp3.mp4
- 动态网站(执行脚本)代码需要载服务器执行一次
-
client ——> nginx 【走http协议一个共享】
/usr/local/nginx/html
if 如果用户访问的是静态页面,则直接返回
if 如果用户访问的是php$,则转发php,php执行代码,并把结果给nginx,nginx把结果给用户
用户看见的是 data:2020.1.01:xx:xx:xx
模块化
php php-mysql
YUM安装配置的过程
yum -y install gcc openssl-devel pcre-devel zlib-devel0
systemctl restart mariadb
systemctl enable mariadb
systemctl restart php-fpm
systemctl enable php-fpm
firewall-cmd --set-default-zone=trusted
etenforce 0
修改配置文件
grep -v '^\;' /etc/php-fpm.d/www.conf|grep -v "^$" |grep -v "^ "|wc -l #最终的结果为15行
[www]
listen = 127.0.0.1:9000 #PHP端口号
pm.max_children = 32 #最大进程数量
pm.start_servers = 15 #最小进程数量
pm.min_spare_servers = 5 #最少需要几个空闲着的进程
pm.max_spare_servers = 32 #最多允许几个进程处于空闲状态
localtion匹配用户的地址栏(判断),支持正则表达式
localtion /a { #相当于if判断
deny 1.1.1.1;
allow all;
}
localtion /b {
deny all;
}
localtion /c {
allow all;
}
licalation / { #优先级最低,根匹配一切
}
实现动静分离
1.修改配置文件
lcoalation / {
root html;
}
localation ~ \.php${ # ~带边正则表达式
转发给127.0.0.1:9000;
}
2.原理
#例如访问http:192.168.4.5/a.html 找根找到就返回,匹配即停止返回,静态
#例如访问http:192.168.4.5/index.html 动静分离方式
client -----> Nginx[判断] -----> 127.0.0.1:9000
3.实现方式
vim /usr/local/ngnix/conf/ngin.conf
62 location ~ \.php { 63 root html; 64 fastcgi_pass 127.0.0.1:9000; #转发给9000端口 65 fastcgi_index index.php; 66 #fastcgi_param SCRIPT_FILENAME /scriptsfastcgi_script_name;
67 include fastcgi.conf;
68 }
LNMP常见的问题
问题内容 | 解决方法 |
---|---|
动静分离没有实现 | 检查配置文件 |
An error occurred 报错检查你的错误日志 | tailf /usr/local/nginx/logs/error.log #日志排查 |
空白页面(可能是你的php脚本错误) | client —> Nginx —> php-fpm #逐步的筛查日志检查错误 |
Nginx的地址重写
什么是地址重写?
地址重写就是当我们访问某个网址时自动url转发到另外一个网址上
www.360buy.com -----> www.jb.com
什么方式下要使用地址重写?
- 1.文件被删除,移动
- 2.域名被修改了
- 3.路径比较深
地址重写的格式
rewrite 语句 选项 ;
last #不再读其他rewrite
break #不再读其他的语句,结束请求
redirect #临时重定向
permament #永久重定向
执行方案
server {
listen 80;
server_name www.aa.com;
rewrite ^/ http://www.it211.com.cn/; #当访问所有的以/目录的地址全部转发为后载冕
#rewrite ^/(.*) http://要跳转的网址/$1; #^/(.*) #在原来的基础上保留原来的绝对路径
location / {
root html;
index index.php index.html index.htm;
rewrite /a.html /b.html; #访问a.html只显示访问a.html不会显示b.html
#rewrite /ahtml /b.html redirect; #访问a.html会变成b.html
}
优化
网页区分来访问的人的信息,提供更好的用户体验方式
-> 电脑:宽屏幕
-> 手机:窄屏幕
UC --> 设置 --> 浏览器 --> UA(用户标示):电脑,手机
修改配置文件(实现curl和火狐访问相同链接返回的页面不同)
echo "this a page" >/usr/local/nginx/html/test.php
mkdir -p /usr/local/nginx/html/firefox
echo "this firefox page" >/usr/local/nginx/html/firefox/test.php
server {
listen 80;
server_name www.aa.com;
location / {
root html;
index index.html index.htm;
}
if (http_user_agent ~* firefox){ rewrite ^(.*) /firefox/1 break; } if (http_user_agent ~* Chrome){
rewrite ^(.*) /Chrome/1 break;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
Hello, Neat post. There is a problem with your web site in web explorer, would test this… IE nonetheless is the marketplace chief and a large portion of people will miss your magnificent writing because of this problem.
Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how could we communicate?
728032145@qq.com 随时可以联系我
I carry on listening to the news bulletin speak about getting free online grant applications so I have been looking around for the most excellent site to get one. Could you advise me please, where could i find some?