当前位置: 主页 > 服务器技术 > Web服务器 > 用Nginx替代apache实现高性能的Web环境

用Nginx替代apache实现高性能的Web环境

时间:2009-11-16来源:互联网 点击:

Nginx介绍:
Nginx发音为[engine x],是由俄罗斯人Igor Sysoev建立的项目,基于BSD许可。
据说他当初是F5的成员之一,英文主页:http://nginx.net。俄罗斯的一些大网站已经使用它超过两年多了, 一直表现不凡,相信想了解nginx的朋友都读过阿叶大哥的利用nginx实现负载均衡的文章相关链接见(六)。

测试环境:红动中国(redocn)提供运营服务器环境.

关于红动服务环境:
红动中国在早期利用apache环境,再加上一些优化的工作,一直是相对很稳定,但是最近由于网站发展,访问量越来越大,在线人数一多经常出现,负载过高,性能急剧下降,经过双木站长的同意,考虑是否能利用nginx来代替apache,经过长时间的观察目前nginx工作很稳定,系统也不会再说现高负载的状况,占用内存也很低,访问速率从用户体验来看明显有提升.

关于红动中国:
红动中国(redocn)论坛经过近1年的快速发展,目前日均页面访问量超过100万,位居全球设计论坛(中文)第1位,是国内最具影响力的设计论坛之一。目前论坛拥有近20万会员,包括众多设计界领军人物在内的行业中坚力量、相关艺术院校师生以及部分设计爱好者等。

迁移目标:实现网站论坛静态化,防盗链,下载并发数和速率限制,实现原站apache所具有的所有功能,将原apache环境下的站点全部迁移到Nginx

一.PHP(Fastcgi)编译安装
[root@att php-5.2.4]# cat in.sh./configure \

–prefix=/usr/local/php-fcgi \

–enable-fastcgi \

–enable-discard-path \

–enable-force-cgi-redirect \

–with-config-file-path=/usr/local/php-fcgi/etc \

–enable-zend-multibyte \

–with-mysql=/usr/local/mysql \

–with-libxml-dir=/usr/local/libxml2 \

–with-gd=/usr/local/gd2 \

–with-jpeg-dir \

–with-png-dir \

–with-bz2 \

–with-freetype-dir \

–with-iconv-dir \

–with-zlib-dir \

–with-openssl=/usr/local/openssl \

–with-mcrypt=/usr/local/libmcrypt \

–enable-sysvsem \

–enable-inline-optimization \

–enable-soap \

–enable-gd-native-ttf \

–enable-ftp \

–enable-mbstring \

–enable-exif \

–disable-debug \

–disable-ipv6

make

make install

cp php.ini-dist /usr/local/php-fcgi/etc/php.ini
复制代码注:关于如何安装gd库,mysql的编译安装,本文将不介绍,本文重点在于介绍nginx的安装与配置,如想了解其它相关的问题可以到
LinuxPk去找相关的贴子(http://bbs.linuxpk.com)

二.Nginx编译安装
1.创建nginx运行用户和虚拟主机目录groupadd www -g 48

useradd -u 48 -g www www

mkdir -p /data/www/wwwroot

chown -R www:www /data/www/wwwroot
复制代码2.安装lighttpd中附带的spawn-fcgi,用来启动php-cgi
先编译安装lighttpd产生spawn-fcgi二进制文件.cd /usr/local/src/lighttpd-1.4.18
http://www.knowsky.com/
cp src/spawn-fcgi /usr/local/php-fcgi/bin/
复制代码启动php-cgi进程,监听127.0.0.1的8085端口,进程数为250(如果服务器内存小于3GB,可以只开启25个进程),用户为www:
/usr/local/php-fcgi/bin/spawn-fcgi -a 127.0.0.1 -p 8085 -C 250 -u www -f /usr/local/php-fcgi/bin/php-cgi

3.nginx的安装与配置
安装Nginx所需的pcre库:
http://ftp.dk.debian.org/exim/pcre/pcre-7.3.tar.gz tar zxvf pcre-7.2.tar.gz

cd pcre-7.2/

./configure

make && make install

cd ../

http://sysoev.ru/nginx/nginx-0.5.32.tar.gz

tar zxvf nginx-0.5.32.tar.gz

cd nginx-0.5.32

./configure –user=www –group=www –prefix=/usr/local/nginx/ –with-http_stub_status_module –with-

openssl=/usr/local/openssl

make && make install
复制代码此模块非核心模块,需要在编译的时候手动添加编译参数 –with-http_stub_status_module
配置nginx

三.Nginx主配置文件及PHP支持.

1.nginx.conf 主配置文件的配置
#cd /usr/local/nginx/conf/
#cp nginx.conf nginx.conf.cao
#cat /dev/null > nginx.conf
#vi nginx.conf //主配置文件user www www;

worker_processes 10;

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

pid /var/run/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.

worker_rlimit_nofile 51200;

events

{

use epoll;

#maxclient = worker_processes * worker_connections / cpu_number

worker_connections 51200;

}

http

{

include conf/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 /data/www/logs/access.log main;

#sendfile on;

tcp_nopush on;

tcp_nodelay off;

keepalive_timeout 60;

client_header_timeout 3m;

client_body_timeout 3m;

send_timeout 3m;

connection_pool_size 256;

client_header_buffer_size 1k;

large_client_header_buffers 4 2k;

request_pool_size 4k;

output_buffers 4 32k;

postpone_output 1460;

client_max_body_size 10m;

client_body_buffer_size 256k;

client_body_temp_path /dev/shm/client_body_temp;

proxy_temp_path /usr/local/nginx/proxy_temp;

fastcgi_temp_path /usr/local/nginx/fastcgi_temp;

#gzip

gzip on;

gzip_http_version 1.0;

gzip_comp_level 2;

gzip_proxied any;

gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml rss text/javascript;

gzip_min_length 1100;

gzip_buffers 4 8k;

# The following includes are specified for virtual hosts //以下是加载虚拟主机配置.

#[url]www.redocn.com[/url]

include conf/vhosts/www_redocn_com.conf;

#bbs.redocn.com

include conf/vhosts/bbs_redocn_com.conf;

#blog.redocn.com

include conf/vhosts/blog_redocn_com.conf;

#down.redocn.com

include conf/vhosts/down_redocn_com.conf;

}
复制代码2.配置支持Fastcgi模式的PHP
[root@redocn conf]# cat enable_php5.conffastcgi_pass 127.0.0.1:8085;

fastcgi_index index.php;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;

fastcgi_param SERVER_SOFTWARE nginx;

#new ac upload

#fastcgi_pass_request_body off;

#client_body_in_file_only clean;

#fastcgi_param REQUEST_BODY_FILE $request_body_file;

#

fastcgi_param QUERY_STRING $query_string;

fastcgi_param REQUEST_METHOD $request_method;

fastcgi_param CONTENT_TYPE $content_type;

fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;

fastcgi_param REQUEST_URI $request_uri;

fastcgi_param DOCUMENT_URI $document_uri;

fastcgi_param DOCUMENT_ROOT $document_root;

fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param REMOTE_ADDR $remote_addr;

fastcgi_param REMOTE_PORT $remote_port;

fastcgi_param SERVER_ADDR $server_addr;

fastcgi_param SERVER_PORT $server_port;

fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with –enable-force-cgi-redirect

fastcgi_param REDIRECT_STATUS 200;

站长资讯网
.
分页: [1] [2]
TAG: APACHE NGINX WEB 高性能 环境
推荐内容最近更新人气排行
关于我们 | 友情链接 | 网址推荐 | 常用资讯 | 网站地图 | RSS | 留言