LNMP建设及地址重写
LNMP部署
FastCGI(快速通用网关接口)是nginx服务器与PHP沟通的一种语言标准
一、安装nginx,mariadb,php
安装nginx参考 web服务器nginx 文档https://blog.csdn.net/yy1506438689/article/details/106838675
[root@proxy ~]# yum install -y php php-mysql php-fpm ==>>安装php相关软件包
[root@proxy ~]# yum install -y mariadb mariadb-server mariadb-devel ==>>安装数据库相关包
mariadb(数据库客户端软件)、mariadb-server(数据库服务器软件)、mariadb-develop(其他客户端软件的依赖包)、php(解释器)、php-fpm(进程管理器服务)、php-mysql(php的数据库扩展包)
[root@proxy ~]# systemctl start mariadb ==>>启动服务
[root@proxy ~]# systemctl enable mariadb ==>>服务加入开机自启动
[root@proxy ~]# systemctl status mariadb ==>>查看服务状态
[root@proxy ~]# systemctl start php-fpm
[root@proxy ~]# systemctl enable php-fpm
[root@proxy ~]# systemctl status mariadb
[root@proxy ~]# netstat -ntulp|grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4266/nginx: master
[root@proxy ~]# netstat -ntulp|grep :3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4917/mysqld
[root@proxy ~]# netstat -ntulp|grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 5021/php-fpm: maste
二、构建LNMP平台
1.php配置
[root@proxy ~]# vim /etc/php-fpm.d/www.conf
listen = 127.0.0.1:9000 ==>>php端口号
pm.max_children = 50 ==>>最大进程数量
pm.start_servers = 5 ==>>最小进程数量
2.nginx配置
[root@proxy ~]# vim /usr/local/nginx/html/test.php ==>>添加测试脚本
<?php
$i=33;
echo $i;
?>
[root@proxy conf]# vim nginx.conf ==>>添加php的location
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf; ==>>访问这个文件,在conf目录下
}
-------------------------------------------------------------------------
location匹配用户的地址栏(从域名后面开始)location /abc { deny all;}
-------------------------------------------------------------------------
3.数据库配置
[root@proxy ~]# cd /usr/local/nginx/html/
[root@proxy html]# vim mysql.php
<?php
$mysqli = new mysqli('localhost','root','','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("</br>");
printf("Name:%s",$row[1]);
printf("</br>");
}
?>
四、地址重写
rewrite 旧地址 新地址 [选项]
last不再读其他rewirte
break不在读其他语句,结束请求
redircet临时重定向
permanent永久重定向
1.将访问a.html重定向到b.html
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
server{
listen 80;
server_name www.a.com;
rewrite /a.html /b.html; ==>>在此处进行重写
location / {
root www;
index index.html index.htm;
}
}
[root@proxy www]# /usr/local/nginx/sbin/nginx -s reload
2.将访问a.html地址栏跳转到b.html
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
server{
listen 80;
server_name www.a.com;
rewrite /a.html /b.html redirect; ==>>在此处进行重写,添加redirect实现地址栏跳转
location / {
root www;
index index.html index.htm;
}
}
[root@proxy www]# /usr/local/nginx/sbin/nginx -s reload
3.访问根下所有页面都跳转到http://www.tmooc.cn
[root@proxy www]# vim /usr/local/nginx/conf/nginx.conf
server{
listen 80;
server_name www.a.com;
rewrite ^/ http://www.baidu.com; ==>>重写根下地址跳转到http://www.baidu.com
location / {
root www;
index index.html index.htm;
}
}
[root@proxy www]# /usr/local/nginx/sbin/nginx -s reload
4.实现www.a.com/a.html跳转到www.b.com/a.html
[root@proxy www]# vim /usr/local/nginx/conf/nginx.conf
server{
listen 80;
server_name www.a.com;
rewrite ^/(.*) http://www.b.com/$1; ==>>重写
location / {
root www;
index index.html index.htm;
}
}
[root@proxy www]# /usr/local/nginx/sbin/nginx -s reload
5.实现不同标识访问相同链接返回页面不同
[root@proxy ~]# mkdir /usr/local/nginx/www/firefox ==>>添加测试数据
[root@proxy ~]# echo firefox >/usr/local/nginx/www/firefox/b.html
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
server{
listen 80;
server_name www.a.com;
if ($http_user_agent ~* windows){ ==>>搜索操作系统为windows的跳转到firefox目录下
rewrite ^/(.*) /firefox/$1;
}
location / {
root www;
index index.html index.htm;
}
}
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload