使用google-perftools优化nginx在高并发时的性能完整教程[第二版]

Nginx

Nginx因为它小巧,占用资源少,非常适合Web站点,越来越多的Linux+Nginx+php+MySQL的站点.在近年逐渐有超过Apache和Lighttpd的趋势.

如果使用googler开发的google-perftools优化Nginx和MySQL的内存管理,性能将会有一定程度的提升.特别是对高并发下的服务器,效果更明显.本教程仅适用于Linux,不适用于Windows.

下面说说如果安装google-perftools,并使Nginx和MySQL支持google-perftools.

今天首先介绍如何优化Nginx:

1,首先下载并安装google-perftools:

注意了,如果是64位系统:

那么你需要做:
1)先安装libunwind或者2)在configure时添加--enable-frame-pointers.

那么首先说说如何安装libunwind:
[php]
wget <a href="http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99.tar.gz" target="_blank">http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99.tar.gz</a>
tar zxvf libunwind-0.99.tar.gz
cd libunwind-0.99/
CFLAGS=-fPIC ./configure --prefix=/usr
make CFLAGS=-fPIC
make CFLAGS=-fPIC install
[/php]

到这里安装libunwind完成.

如果是使用添加--enable-frame-pointers的方式,先不管,咱们往下走.

下载并安装google-perftools:
[php]
wget <a href="http://google-perftools.googlecode.com/files/google-perftools-1.7.tar.gz" target="_blank">http://google-perftools.googlecode.com/files/google-perftools-1.7.tar.gz</a>
tar xzvf google-perftools-1.7.tar.gz
cd google-perftools-1.7
[/php]

然后开始配置:
./configure --prefix=/usr --enable-frame-pointers
在这里注意这步,如果是32位系统,可以不添加 --enable-frame-pointers,如果是64位系统,并且你之前没有安装libunwind,那么你一定要添加这个:--enable-frame-pointers

编译并安装:

make
make install

到这里安装google-perftools完成了但未生效,接下来需要使google-perftools生效:

echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
/sbin/ldconfig

注意,这里的双引号是英文的。

到这里安装google-perftools完成.

为了使Nginx支持google-perftools,需要加上--with-google_perftools_module重新编译Nginx,如果不知道如何安装Nginx,可以从这里查看Nginx安装教程.

例如:
[php]
./configure --user=www --group=www --prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module --with-openssl= \
--with-http_addition_module \
--with-zlib= \
--with-google_perftools_module

make

make install
[/php]

添加Nginx快速重启脚本,请从wpadm.com下载.

到这里安装Nginx完成.

接下来为google-perftools添加线程目录:

mkdir /tmp/tcmalloc
chmod 0777 /tmp/tcmalloc

修改/usr/local/nginx/conf/ncing.conf
在pid这行的下面添加
#pid        logs/nginx.pid;
google_perftools_profiles /tmp/tcmalloc;

重新启动nginx:
service nginx restart

验证是否正在运行:
[php]
[root@localhost ~]# lsof -n | grep tcmalloc
nginx     13101    www   45w      REG                8,1          0    4014748 /tmp/tcmalloc.13101
nginx     13102    www  47w      REG                8,1          0    4014742 /tmp/tcmalloc.13102
nginx     13103    www   49w      REG                8,1          0    4014746 /tmp/tcmalloc.13103
nginx     13105    www   51w      REG                8,1          0    4014745 /tmp/tcmalloc.13105
nginx     13106    www   53w      REG                8,1          0    4014743 /tmp/tcmalloc.13106
nginx     13107    www   55w      REG                8,1          0    4014749 /tmp/tcmalloc.13107
nginx     13108    www   57w      REG                8,1          0    4014754 /tmp/tcmalloc.13108
nginx     13109    www   59w      REG                8,1          0    4014750 /tmp/tcmalloc.13109
nginx     13110    www   61w      REG                8,1          0    4014747 /tmp/tcmalloc.13110
nginx     13111    www   63w      REG                8,1          0    4014755 /tmp/tcmalloc.13111
nginx     13112    www   65w      REG                8,1          0    4014753 /tmp/tcmalloc.13112
nginx     13113    www   67w      REG                8,1          0    4014756 /tmp/tcmalloc.13113
nginx     13114    www  69w      REG                8,1          0    4014757 /tmp/tcmalloc.13114
nginx     13115    www   71w      REG                8,1          0    4014751 /tmp/tcmalloc.13115
nginx     13116    www   73w      REG                8,1          0    4014744 /tmp/tcmalloc.13116
nginx     13117    www   75w      REG                8,1          0    4014752 /tmp/tcmalloc.13117
[/php]

我的服务器因为有8核,所以开启了8个Nginx线程,每个线程会有一行类似的记录.

到这里安装优化完成.

还没有评论,快来抢沙发!

发表评论

  • 😉
  • 😐
  • 😡
  • 😈
  • 🙂
  • 😯
  • 🙁
  • 🙄
  • 😛
  • 😳
  • 😮
  • emoji-mrgree
  • 😆
  • 💡
  • 😀
  • 👿
  • 😥
  • 😎
  • ➡
  • 😕
  • ❓
  • ❗
  • 72 queries in 0.092 seconds