且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Nginx实用指南V1 (连载之二:Nginx安装与编译优化)

更新时间:2022-07-01 06:30:43

一、Nginx安装配置

1、 安装依赖库及pcre
2、 编译参数及优化编译
A、 CPU优化 
B、 Tcmalloc优化 
C、 系统优化
D、 减肥
 

1、安装依赖和pcre

本文以centos 为例。采用yum的安装方法

yum -y install ntp vim-enhanced gcc gcc-c++gcc-g77 flex bison autoconf automake \  bzip2-devel ncurses-devel openssl-devel libtool*zlib-devel libxml2-devel libjpeg-devel \  libpng-devel libtiff-devel fontconfig-devel freetype-devel libXpm-develgettext-devel \  curl curl-devel pam-devel e2fsprogs-devel krb5-devel libidn libidn-devel

#Download pcre

官方下载地址: 下载最新版本
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

wget pcre-laster.tar.gz  tar zxvf pcre-laster.tar.gz  cd pcre-laster  ./configure && make && makeinstall  2、编译参数

#Download nginx 最版本下载地址: http://wiki.nginx.org/NginxInstall

Wget http://sysoev.ru/nginx/nginx-0.8.35.tar.gz  tar zxvf nginx-0.8.35.tar.gz  cd nginx-0.8.35  /configure --user=www --group=www --prefix=/usr/local/nginx/  --with-http_stub_status_module --with-http_ssl_module  --with-md5=/usr/lib --with-sha1=/usr/lib  make && make install


更详细的解释请参与官方WIKI

3、优化

A、 为特定的CPU指定CPU类型编译优化.

默认nginx使用的GCC编译参数是-O
需要更加优化可以使用以下两个参数
--with-cc-opt='-O3' \
--with-cpu-opt=opteron \
使得编译针对特定CPU以及增加GCC的优化.
此方法仅对性能有所改善并不会有很大的性能提升,供朋友们参考.
CPUD类型确定: # cat /proc/cpuinfo | grep "model name"
编译优化参数参考:http://en.gentoo-wiki.com/wiki/Safe_Cflags

B、Tcmalloc优化Nginx 性能

wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-alpha.tar.gz

编译nginx 加载google_perftools_module: 
./configure --with-google_perftools_module 
在主配置文件加入nginx.conf 添加: 
google_perftools_profiles /path/to/profile;


C、内核参数优化

# vi /etc/sysctl.conf   #在末尾增加以下内容:

#使用配置生效 
sysctl -p


D、减肥

减小nginx编译后的文件大小 (Reduce file size of nginx)

默认的nginx编译选项里居然是用debug模式(-g)的(debug模式会插入很多跟踪和ASSERT之类),编译以后一个nginx有好几兆。去掉nginx的debug模式编译,编译以后只有几百K

在 auto/cc/gcc,最后几行有:

# debug

CFLAGS=”$CFLAGS -g”

注释掉或删掉这几行,重新编译即可。

 注: 我这里没有写伪装服务器的版本。大家可以上网搜索进行配置安装。



本文转自守住每一天51CTO博客,原文链接:http://blog.51cto.com/liuyu/293947,如需转载请自行联系原作者