If you use google-perftools developed by googler to optimize the memory management of Nginx and MySQL, the performance will be improved to a certain extent. Especially for servers under high concurrency, the effect is more obvious!
Note: This tutorial is for Linux only.
The following is an introduction to the installation of google-perftools, and configuration of Nginx and MySQL to support google-perftools.
First, how to optimize Nginx:
1, first download and install google-perftools:
Note, if it is a 64-bit system:
Then you need to do:
1) Install libunwind first or 2) Add –enable-frame-pointers when configure.
So first talk about how to install libunwind:
wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99.tar.gz
tar zxvf libunwind-0.99.tar.gz
cd libunwind-0.99/
CFLAGS=-fPIC ./configure –prefix=/usr
make CFLAGS=-fPIC
make CFLAGS=-fPIC install
Go here and install libunwind to complete.
If you use the method of adding –enable-frame-pointers, don’t worry about it, let’s go down.
Download and install google-perftools:
wget http://google-perftools.googlecode.com/files/google-perftools-1.7.tar.gz
tar xzvf google-perftools-1.7.tar.gz
cd google-perftools-1.7
Then start configuration:
./configure –prefix=/usr –enable-frame-pointers
Pay attention to this step here, if it is a 32-bit system, you can not add
–enable-frame-pointers, if it is a 64-bit system, and you have not installed libunwind before, then you must add this: –enable-frame-pointers
Compile and install:
make
make install
The installation of google-perftools has been completed here but it has not taken effect. Next, you need to make google-perftools take effect:
echo “/usr/local/lib” > /etc/ld.so.conf.d/usr_local_lib.conf
/sbin/ldconfig
Note that the double quotes here are in English.
Go here and install google-perftools to complete.
In order to make Nginx support google-perftools, you need to add –with-google_perftools_module to recompile Nginx. If you don’t know how to install Nginx, you can check the Nginx installation tutorial from here.
For example:
./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
Add Nginx fast restart script, please download from wpadm.com.
Go here and install Nginx to complete.
Next add the threads directory for google-perftools:
mkdir /tmp/tcmalloc
chmod 0777 /tmp/tcmalloc
Modify /usr/local/nginx/conf/ncing.conf
Add below the pid line
#pid logs/nginx.pid;
google_perftools_profiles /tmp/tcmalloc;
Restart nginx:
service nginx restart
Verify it’s running:
[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
Because my server has 8 cores, 8 Nginx threads are enabled, and each thread will have a similar record.
At this point, the optimized installation of google-perftools under nginx is completed, for your reference.