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

构建LNMP平台

Nginx结合FastCGI技术即可支持PHP页面架构,如图所示。

在这里插入图片描述


PHP-fpm配置文件(不需要修改文件)
[root@proxy etc]# vim /etc/PHP-fpm.d/www.conf
[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 //最多允许几个进程处于空闲状态
修改Nginx配置文件并启动服务
[root@proxy ~]# vim /usr/local/Nginx/conf/Nginx.conf
location / {
root html;
index index.PHP index.html index.htm;
#设置首页为index.PHP,当用户在浏览器地址栏中只写域名或IP,不说访问什么页面时,服务器会把首页index.PHP返回给用户
}
location ~ .PHP$ {
root html;
fastcgi_pass 127.0.0.1:9000; #将请求转发给本机9000端口,PHP解释器
fastcgi_index index.PHP;
#fastcgi_param SCRIPT_FILENAME documentrootdocument_rootdocumentr​ootfastcgi_script_name;
include fastcgi.conf;
}
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload
#请先确保nginx是启动状态,否则运行该命令会报错,报错信息如下:
#[error] open() “/usr/local/nginx/logs/nginx.pid” failed (2: No such file or directory)
创建PHP页面,测试LNMP架构能否解析PHP页面
[root@proxy ~]# vim /usr/local/nginx/html/test1.php

<?php $i="This is a test Page"; echo $i; ?>

创建PHP测试页面,连接并查询MariaDB数据库。

<?php $mysqli = new mysqli('localhost','root','密码','mysql'); //注意:root为mysql账户名称,密码需要修改为实际mysql密码,无密码则留空即可 //localhost是数据库的域名或IP,mysql是数据库的名称 if (mysqli_connect_errno()){ die('Unable to connect!'). mysqli_connect_error(); } $sql = "select * from user"; $result = $mysqli->query($sql); while($row = $result->fetch_array()){ printf("Host:%s",$row[0]); printf(""); printf("Name:%s",$row[1]); printf(""); } ?>

客户端使用浏览器访问服务器PHP首页文档,检验是否成功:
[root@client ~]# firefox http://192.168.4.5/test1.php
[root@client ~]# firefox http://192.168.4.5/test2.php
LNMP常见问题
Nginx认访问日志文件为/usr/local/Nginx/logs/access.log
Nginx错误日志文件为/usr/local/Nginx/logs/error.log
PHP错误日志文件为/var/log/PHP-fpm/www-error.log
如果动态网站访问失败,可用参考错误日志,查找错误信息。
地址重写
修改配置文件(访问a.html重定向到b.html)
[root@proxy ~]# vim /usr/local/Nginx/conf/Nginx.conf
… …
server {
listen 80;
server_name localhost;
rewrite /a.html /b.html;
location / {
root html;
index index.html index.htm;
}
}
[root@proxy ~]# echo “BB” > /usr/local/Nginx/html/b.html
重新加载配置文件
[root@proxy ~]# /usr/local/Nginx/sbin/Nginx -s reload
客户端测试
[root@client ~]# firefox http://192.168.4.5/a.html
访问a.html重定向到b.html(跳转地址栏)
修改Nginx服务配置:
[root@proxy ~]# vim /usr/local/Nginx/conf/Nginx.conf
… …
server {
listen 80;
server_name localhost;
rewrite /a.html /b.html redirect;
location / {
root html;
index index.html index.htm;
}
}
重新加载配置文件
[root@proxy ~]# /usr/local/Nginx/sbin/Nginx -s reload
#请先确保Nginx是启动状态,否则运行该命令会报错,报错信息如下:
#[error] open() “/usr/local/Nginx/logs/Nginx.pid” Failed (2: No such file or directory)
客户端测试(仔细观察浏览器地址栏的变化)
firefox http://192.168.4.5/a.html
修改配置文件(访问192.168.4.5的请求重定向至www.qq.com)
[root@proxy ~]# vim /usr/local/Nginx/conf/Nginx.conf
… …
server {
listen 80;
server_name localhost;
rewrite ^/ http://www.qq.com/;
location / {
root html;
index index.html index.htm;
}
}
重新加载配置文件
root@proxy ~]# /usr/local/Nginx/sbin/Nginx -s reload
客户端测试
修改配置文件(访问192.168.4.5/下面子页面重定向至www.qq,com/下相同的页面)
修改Nginx服务配置
[root@proxy ~]# vim /usr/local/Nginx/conf/Nginx.conf
… …
server {
listen 80;
server_name localhost;
rewrite ^/(.)$ http://www.qq.com/KaTeX parse error: Expected 'EOF', got '}' at position 65: …l index.htm; } }̲ 重新加载配置文件 [root…http_user_agent ~ firefox) { //识别客户端firefox浏览器
rewrite ^(.*)$ /firefox/$1;
}
}
重新加载配置文件
[root@proxy ~]# /usr/local/Nginx/sbin/Nginx -s reload
#请先确保Nginx是启动状态,否则运行该命令会报错,报错信息如下:
#[error] open() “/usr/local/Nginx/logs/Nginx.pid” Failed (2: No such file or directory)
客户端测试
[root@client ~]# firefox http://192.168.4.5/test.html
[root@client ~]# curl http://192.168.4.5/test.html
地址重写格式【总结】
rewrite 旧地址 新地址 [选项];
last 不再读其他rewrite
break 不再读其他语句,结束请求
redirect 临时重定向
permament 永久重定向

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

相关推荐