web:lnmp,fastCGI,地址重写

部署LNMP

LNMP基础知识

  • L:Linux操作系统
  • N:Nginx网站服务软件
  • M:MySQL、MariaDB数据库
  • P:网站开发语言(PHP、Perl、Python)

LNMP安装

  • 安装基础依赖包
yum -y install gcc openssl-devel pcre-devel
  • 源码安装Nginx
useradd -s /sbin/nologin  nginx
tar -xf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --user=nginx \
--group=nginx \
--with-http_ssl_module
make && make install 
  • 安装MariaDB
    Mariadb在新版RHEL7光盘中包含有该软件,配置yum源后可以直接使用yum安装:
yum -y install   mariadb   mariadb-server   mariadb-devel
  • 安装php
yum -y  install  php   php-mysql php-fpm

启动服务

  • 启动Nginx服务
    这里需要注意的是,如果服务器上已经启动了其他监听80端口的服务软件(如httpd),则需要先关闭该服务,否则会出现冲突。
systemctl stop httpd  #如果该服务存在则关闭该服务
/usr/local/nginx/sbin/nginx   #启动Nginx服务
netstat -utnlp | grep :80
tcp    0    0 0.0.0.0:80        0.0.0.0:*        LISTEN        32428/nginx         
  • 启动MySQL服务
systemctl start  mariadb    #启动服务器
systemctl status mariadb    #查看服务状态
systemctl enable mariadb    #设置开机启动
mysql_secure_installation   #初始化
  • 启动PHP-FPM服务
systemctl start php-fpm     #启动服务
systemctl status php-fpm    #查看服务状态
systemctl enable php-fpm    #设置开机启动

Nginx+FastCGI

FastCGI工作原理

在这里插入图片描述

  • 工作流程
    – Web Server启动时加载FastCGI进程管理器
    – FastCGI进程管理器初始化,启动多个解释器进程
    – 当客户端请求到达Web Server时FastCGI进程管理器选择并连接到一个解释器
    – FastCGI子进程完成处理后返回结果,将标准输出和错误信息从同一连接返回Web Server

FastCGI简介

  • FastCGI技术目前支持语言有PHP、C/C++、Java、Perl、Python、Ruby等

FastCGI缺点

在这里插入图片描述

php-fpm配置文件

查看php-fpm配置文件(实验中不需要修改该文件)

vim /etc/php-fpm.d/www.conf
[www]
listen = 127.0.0.1:9000     #PHP端口号
pm.max_children = 32        #最大进程数量
pm.start_servers = 15       #最小进程数量

修改Nginx配置文件并启动服务

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  $document_root$fastcgi_script_name;
            include        fastcgi.conf;       #加载其他配置文件
        }
        
/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页面

  • 创建PHP测试页面1
vim /usr/local/nginx/html/test.php
<?php
$i="This is a test Page";
echo $i;
?>
  • 创建PHP测试页面,连接并查询MariaDB数据库。
vim /usr/local/nginx/html/mysql.php
<?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("</br>");
    printf("Name:%s",$row[1]);
    printf("</br>");
}
?>
  • 客户端使用浏览器访问服务器PHP首页文档,检验是否成功:
firefox http://192.168.4.5/test.php
firefox http://192.168.4.5/mysql.php

LNMP常见问题

Nginx的默认访问日志文件为/usr/local/nginx/logs/access.log
Nginx的默认错误日志文件为/usr/local/nginx/logs/error.log
PHP默认错误日志文件为/var/log/php-fpm/www-error.log
如果动态网站访问失败,可用参考错误日志,查找错误信息。

地址重写

实现以下目标:
1.所有访问a.html的请求,重定向到b.html;
2.所有访问192.168.4.5的请求重定向至www.baidu.com;
3.所有访问192.168.4.5/下面子页面,重定向至www.baidu.com/下相同的页面;
4.实现firefox与curl访问相同页面文件,返回不同的内容。

  • 关于Nginx服务器的地址重写,主要用到的配置参数是rewrite:
    rewrite regex replacement flag
    rewrite 旧地址 新地址 [选项]

修改配置文件(访问a.html重定向到b.html)

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;
}
}

echo "BB" > /usr/local/nginx/html/b.html
  • 重新加载配置文件
/usr/local/nginx/sbin/nginx  -s  reload
  • 客户端测试
firefox  http://192.168.4.5/a.html

步骤二:访问a.html重定向到b.html(跳转地址栏)

  • 修改Nginx服务配置:
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;
}
}
  • 重新加载配置文件
/usr/local/nginx/sbin/nginx  -s  reload
  • 客户端测试(仔细观察浏览器地址栏的变化)
firefox  http://192.168.4.5/a.html

修改配置文件(访问192.168.4.5的请求重定向至www.baidu.com)

  • 修改Nginx服务配置
vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
        listen       80;
        server_name  localhost;
rewrite ^/  http://www.baidu.com/;
location / {
    root   html;
index  index.html index.htm;
# rewrite /a.html  /b.html  redirect;
}
}
  • 重新加载配置文件
/usr/local/nginx/sbin/nginx  -s  reload
  • 客户端测试
firefox  http://192.168.4.5

修改配置文件(访问192.168.4.5/下面子页面,重定向至www.baidu.com/下相同的页面)

  • 修改Nginx服务配置
vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
        listen       80;
        server_name  localhost;
rewrite ^/(.*)$  http://www.tmooc.cn/$1;
location / {
    root   html;
index  index.html index.htm;
}
}
  • 重新加载配置文件
/usr/local/nginx/sbin/nginx  -s  reload
  • 客户端测试
firefox  http://192.168.4.5
firefox  http://192.168.4.5/test

修改配置文件(实现curl和火狐访问相同链接返回的页面不同)

  • 创建网页目录以及对应的页面文件:
echo "I am Normal page" > /usr/local/nginx/html/test.html
mkdir  -p  /usr/local/nginx/html/firefox/
echo "firefox page" > /usr/local/nginx/html/firefox/test.html
  • 修改Nginx服务配置
vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
        listen       80;
        server_name  localhost;
location / {
    root   html;
index  index.html index.htm;
}
#这里,~符号代表正则匹配,*符号代表不区分大小写
if ($http_user_agent ~* firefox) {   #识别客户端firefox浏览器
rewrite ^(.*)$  /firefox/$1;
}
}
  • 重新加载配置文件
/usr/local/nginx/sbin/nginx  -s  reload
  • 客户端测试
firefox  http://192.168.4.5/test.html
curl     http://192.168.4.5/test.html

地址重写格式【总结】

rewrite 旧地址 新地址 [选项];
last 不再读其他rewrite
break 不再读其他语句,结束请求
redirect 临时重定向
permanent 永久重定向

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值