lnmp,fastcgi,rewrite地址重写

LNMP主流的企业网站平台之一

——L:Linux操作系统

——N:Nginx网站服务软件 端口80

——M:Mysql、Mariadb数据库 端口3306

——P:网站开发语言(php、perl、python) 端口9000

案例1:部署LNMP环境

1.1 问题

安装部署Nginx、MariaDB、PHP环境

  • 安装部署Nginx、MariaDB、PHP、PHP-FPM;
  • 启动Nginx、MariaDB、FPM服务;
  • 并测试LNMP是否工作正常。

安装软件

nginx

mariadb、mariadb-server、mariadb-devel

php、php-fpm、php-mysql

1)使用yum安装基础依赖包

# yum - y install gcc openssl- devel pcre- devel zlib- devel

2)源码安装Nginx

# useradd - s /sbin/nologin nginx

# tar - xvf nginx- 1.12.2.tar.gz

# cd nginx- 1.12.2

[ root@proxy nginx- 1.12.2] # ./configure \

> - - prefix=/usr/local/nginx \

> - - user=nginx - - group=nginx \

> - - with- http_ssl_module

# make && make install

3)安装MariaDB

# yum - y install mariadb mariadb- server mariadb- devel

4)php和php-fpm(该软件包在lnmp_soft中提供)

# yum - y install php php- mysql

# yum - y install php- fpm- 5.4.16- 42.el7.x86_64.rpm

启动服务

1)启动Nginx服务(如果已经启动nginx,则可以忽略这一步)

这里需要注意的是,如果服务器上已经启动了其他监听80端口的服务软件(如httpd),则需要先关闭该服务,否则会出现冲突。

# systemctl stop httpd //如果该服务存在则关闭该服务

# /usr/local/nginx/sbin/nginx //启动Nginx服务

# netstat - utnlp | grep : 80

2)启动MySQL服务

# systemctl start mariadb //启动服务器

# systemctl status mariadb //查看服务状态

# systemctl enable mariadb //设置开机启动

3)启动PHP-FPM服务

# systemctl start php- fpm

4)设置防火墙与SELinux

# firewall- cmd - - set- def ault- zone=trusteddevel

# setenf orce 0

################################

案例2 构建LNMP平台

FastCGI工作原理

工作流程

1、Web Server 启动时载入fastcgi 进程管理器

2、fastcgi 进程管理器初始化,启动多个解释器进程

3、当客户端请求到达web server 时,fasrcgi 进程管理器选择并连接到一个解释器

4、fastcgi 子进程完成处理后返回结果,将标准输出和错误信息从同fastcgi一连接返回web server

FastCGI简介

CGI全称是“通用网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序一般运行在网络服务器上。 CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量。如php,perl,tcl等

fastcgi 技术目前支持语言有 php、c/c++、java、perl、python、ruby等

 

2.1 问题

通过调整Nginx服务端配置,实现以下目标:

  • 配置Fast-CGI支持PHP网页
  • 创建PHP测试页面,测试使用PHP连接数据库的效果

使用2台RHEL7虚拟机,其中一台作为LNMP服务器(192.168.4.5)、另外一台作为测试用的Linux客户机(192.168.4.100),如图-1所示。

 

 

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

步骤一: php-fpm配置文件

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

# vim /etc/php- fpm.d/www.conf

[ www]

12行 listen = 127.0.0.1: 9000 //PHP端口号

70行 pm.max_children = 32 //最大进程数量

75行 pm.start_serv ers = 15 //最小进程数量

80行 pm.min_spare_serv ers = 5 //最少需要几个空闲着的进程fastcgi

85行 pm.max_spare_serv ers = 32 //最多允许几个进程处于空闲状态

步骤二:修改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 ;

}

[ root@proxy ~] # /usr/local/nginx/sbin/nginx - s reload

//请先确保nginx是启动状态才可以执行命令成功,否则报错,报错信息如下:

//[ error] open( ) "/usr/local/nginx/logs/nginx.pid" f ailed ( 2: No such file or directory )

步骤三:创建PHP页面,测试LNMP架构能否解析PHP页面

1)创建PHP测试页面1,可以参考lnmp_soft/php_scripts/test.php:

# vim /usr/local/nginx/html/test1.php

<?php

$i="This is a test Page";

echo $i;

?>devel

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

可以参考lnmp_soft/php_scripts/mysql.php:

# vim /usr/local/nginx/html/test2.php

<?php

$mysqli = new mysqli( 'localhost','root','密码','mysql') ;

//注意:root为mysql账户名称,密码需要修改为实际mysql密码,无密码则留空即可

if ( mysqli_connect_errno( ) ) {

die( 'Unable to connect! ') . mysqli_connect_error( ) ;

}

$sql = "select * f rom user";

$result = $my sqli- >query ( $sql) ;

while( $row = $result- >f etch_array ( ) ) {

printf ( "Host: %s",$row[ 0] ) ;

printf ( "</br>") ;

printf ( "Name: %s",$row[ 1] ) ;

printf ( "</br>") ;

}

?>

3)客户端使用浏览器访问服务器PHP首页文档,检验是否成功

# firefox http: //192.168.4.5/test1.php

# firefox http: //192.168.4.5/test2.php

4)LNMP常见问题

Nginx的默认访问日志文件为/usr/local/nginx/logs/access.log

Nginx的默认错误日志文件为/usr/local/nginx/logs/error.log

PHP默认错误日志文件为/var/log/php-fpm/www-error.log

如果动态网站访问失败,可用参考错误日志,查找错误信息。

##############################

地址重写

什么是地址重写

——获得一个来访的URL请求,然后改写成服务器可以处理的另一个URL的过程

地址重写的好处

——缩短URL,隐藏实际路径提高安全性

——易于用户记忆和键入

——易于被搜索引擎收录

关于Nginx服务器的地址重写,主要用到的配置参数是rewrite:

rewrite regex replacement flag

rewrite 旧地址 新地址 [选项]

flag : break、last、redirect、permanent

break 不再读其他语句,结束请求

last 不再读其他rewrite

redirect 302临时重定向,地址栏改变,爬虫不更新URI

permament 301永久重定向,地址栏改变,爬虫更新URI

 

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

1)修改Nginx服务配置:

# v im /usr/local/nginx/conf /nginx.conf

server {

listen 80;

server_name localhost;

location / {

root html;

index index.html index.htm;

rewrite /a.html /b.html;

}

}

# echo "BB" > /usr/local/nginx/html/b.html

2)重新加载配置文件

# /usr/local/nginx/sbin/nginx -s reload

3)客户端测试

# firefox http: //192.168.4.5/a.html

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

1)修改Nginx服务配置

# vim /usr/local/nginx/conf /nginx.conf

server {

listen 80;

server_name localhost;

location / {

root html;

index index.html index.htm;

rewrite /a.html /b.html redirect;

}

}

2)重新加载配置文件

# /usr/local/nginx/sbin/nginx - s reload

3)客户端测试(仔细观察浏览器地址栏的变化)

# firefox http: //192.168.4.5/a.html

步骤三:修改配置文件(访问192.168.4.5的请求重定向至www.tmooc.cn)

1) 修改Nginx服务配置

# vim /usr/local/nginx/conf /nginx.conf

server {

listen 80;

server_name localhost;

rewrite ^/http://www.tmooc.cn/;

location / {

root html;

index index.html index.htm;

# rewrite /a.html /b.html redirect;

}

}

2)重新加载配置文件

# /usr/local/nginx/sbin/nginx - s reload

3)客户端测试(真实机测试,真实机才可以连接tmooc)

# firefox http: //192.168.4.5

步骤四:修改配置文件(访问192.168.4.5/下面子页面,重定向至www.tmooc.cn/下相同的页面)

1) 修改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;

#rewrite /a.html /b.html redirect;

}

}

2)重新加载配置文件

# /usr/local/nginx/sbin/nginx - s reload

3)客户端测试(真实机测试,真实机才可以连接tmooc)

# firefox http://192.168.4.5

# firefox http://192.168.4.5/test

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

1) 创建网页目录以及对应的页面文件:

# 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

2) 修改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;

}

}

3)重新加载配置文件

# /usr/local/nginx/sbin/nginx - s reload

4)客户端测试

# firefox http: //192.168.4.5/test.html

# curl http: //192.168.4.5/test.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值