实操Nginx的LNMP、LNNMP和LNNNMP架构,以及缓存技术的应用
1. 前言
1.1 Nginx简介
Nginx是一款由俄罗斯程序员Igor Sysoev所开发轻量级的网页服务器、反向代理服务器以及电子邮件(IMAP/POP3)代理服务器。可以在目前所有主流的操作系统上运行。
Nginx采用模块化设计架构,易扩展;使用多线程处理客户请求,减少了进程上下文切换的开销;使用epoll或者kqueue事件驱动模型,提高了并发处理性能。
1.2 Tengine介绍
Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。
Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。
特性
-
继承Nginx-1.4.7的所有特性,100%兼容Nginx的配置;
-
动态模块加载(DSO)支持。加入一个模块不再需要重新编译整个Tengine;
-
支持SPDY v3协议,自动检测同一端口的SPDY请求和HTTP请求;
-
流式上传到HTTP后端服务器或FastCGI服务器,大量减少机器的I/O压力;
-
更加强大的负载均衡能力,包括一致性hash模块、会话保持模块,还可以对后端的服务器进行主动健康检查,根据服务器状态自动上线下线;
-
输入过滤器机制支持。通过使用这种机制Web应用防火墙的编写更为方便;
-
支持设置proxy、memcached、fastcgi、scgi、uwsgi在后端失败时的重试次数
-
动态脚本语言Lua支持。扩展功能非常高效简单;
-
支持管道(pipe)和syslog(本地和远端)形式的日志以及日志抽样;
-
支持按指定关键字(域名,url等)收集Tengine运行状态;
-
组合多个CSS、JavaScript文件的访问请求变成一个请求;
-
自动去除空白字符和注释从而减小页面的体积
-
自动根据CPU数目设置进程个数和绑定CPU亲缘性;
-
监控系统的负载和资源占用从而对系统进行保护;
-
显示对运维人员更友好的出错信息,便于定位出错机器;
-
更强大的防攻击(访问速度限制)模块;
-
更方便的命令行参数,如列出编译的模块列表、支持的指令等;
-
可以根据访问文件类型设置过期时间;
今天的实验将使用Tengine替代Nginx来搭建LNMP、LNNMP和LNNNMP。这三种架构后端都使用同样的PHP-FPM、MariaDB,这些服务器搭建参看以前的博文。
PHP-FPM,使用FastCGI协议和Nginx的WEB服务器通信。
MariadB,提供数据库服务器。
2. 环境准备
2.1 PHP-FPM
提供测试页面index.php
# mkdir /web/nginx
# vim /web/nginx/index.php
<html>
<head>
<title>dynamic</title>
</head>
<body>
<table class="tb_show">
<tr>
<td>Client IP is</td>
<td class="content_show"><?php echo getenv('REMOTE_ADDR') . ':' . getenv('REMOTE_PORT') ?></td>
</tr>
<tr>
<td>Server IP is </td>
<td class="content_show"><?php echo getenv('SERVER_ADDR') . ':' . getenv('SERVER_PORT') ?></td>
</tr>
<tr>
<td colspan="2">
<?php
mysql_connect ( "192.168.23.121" , "wp" , "wp" ) or
die( "Could not connect: " . mysql_error ());
printf ( "MySQL server version: %s\n" , mysql_get_server_info ());
?>
</td>
</tr>
</table>
</body>
</html>
2.2 Tengine安装
# yum -y install gcc openssl-devel pcre-devel
# groupadd -r nginx
# useradd -r -g nginx -s /bin/nologin nginx
编译安装前修改版本信息,让客户端无法看到真实的服务器信息和版本号。
# tar xf tengine-2.0.3.tar.gz
# cd tengine-2.0.3
# sed -i \
-e 's/\(#define[[:space:]]\+TENGINE_VER[[:space:]]\+\).*/\1"MeXP\/1.0.1"/' \
-e 's/\(#define[[:space:]]\+NGINX_VER[[:space:]]\+\).*/\1"MeXP\/1.0.0"/' \
./src/core/nginx.h
# ./configure --prefix=/usr/local/nginx \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx --group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/usr/local/nginx/client \
--http-proxy-temp-path=/usr/local/nginx/proxy \
--http-fastcgi-temp-path=/usr/local/nginx/fcgi \
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi \
--http-scgi-temp-path=/usr/local/nginx/scgi \
--with-pcre
# make && make install
# /usr/local/nginx/sbin/nginx -v
Tengine version: MeXP/1.0.1 (MeXP/1.0.0)
导出环境变量
# vim /etc/profile.d/nginx.sh
export PATH=/usr/local/nginx/sbin:$PATH
# source /etc/profile.d/nginx.sh
3. LNMP
3.1 规划
Nginx是WEB服务器。
3.2 提供一个测试用index.html
# vim /usr/local/nginx/html/index.html
<html>
<head>
<title>dynamic</title>
</head>
<body>
<h1 align="left">This is a static page of WEB1</h1>
</body>
</html>
3.3 nginx配置文件
user nginx;
worker_processes auto;
#pid /var/run/nginx.pid;
error_log /var/log/nginx/error.log;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 5;
#gzip on;
server {
listen 80;
server_name WEB1;
add_header X-upS WEB1-$server_addr:$server_port;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
下一篇: 快速安装LNMP环境(版本17.16)