2012年5月28日 星期一

使用 google-perftools ( LNMP 優化 )

使用 google-perftools 優化 Nginx

在網路上看到有許多人在談論如何加速 LNMP 的使用環境.
方式為 google-perftools & ngx_cache_purge
再加上 memcache (php模組)及使用 memcached (php模組) 等

安裝 libunwind

http://download.savannah.gnu.org/releases/libunwind/
http://download.savannah.gnu.org/releases/libunwind/libunwind-1.0.1.tar.gz

#tar -zvxf libunwind-1.0.1.tar.gz
#cd libunwind-1.0.1
#CFLAGS=-fPIC ./configure --prefix=/usr
#make CFLAGS=-fPIC
#make CFLAGS=-fPIC install

安裝 google-perftools

http://code.google.com/p/gperftools/downloads/list
http://gperftools.googlecode.com/files/gperftools-2.0.tar.gz

#tar -zvxf gperftools-2.0.tar.gz
#cd gperftools-2.0
#./configure
#make
#make install

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


#mkdir /tmp/tcmalloc
#chmod 0777 /tmp/tcmalloc
vi /usr/local/nginx/conf/nginx.conf

#pid        logs/nginx.pid;
google_perftools_profiles /tmp/tcmalloc;

====================

ngx_cache_purge
http://labs.frickle.com/nginx_ngx_cache_purge/

http://www.pcre.org/
http://sourceforge.net/projects/pcre/files/pcre/8.30/

Nginx module Source
https://github.com/agentzh/srcache-nginx-module

https://github.com/agentzh/memc-nginx-module

pcre source
http://sourceforge.net/projects/pcre/files/pcre/
http://sourceforge.net/projects/pcre/files/pcre/8.30/pcre-8.30.tar.gz/download


安裝

Nginx

./configure --prefix=/usr/local/nginx3 --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_perl_module --with-google_perftools_module --with-pcre=/usr/local/src/pcre-8.30 --add-module=/root/ngx_cache_purge-1.5 --http-proxy-temp-path=/usr/local/nginx3/proxy_temp  --add-module=/root/agentzh-memc-nginx-module-8befc56 --add-module=/root/agentzh-srcache-nginx-module-027dd06

安裝 memcache

http://pecl.php.net/package/memcache
http://pecl.php.net/get/memcache-2.2.6.tgz

#tar -zvxf memcache-2.2.6.tgz
#cd   memcache-2.2.6
#phpize
#./configure --enable-memcache

Installing shared extensions:     /usr/lib/php/modules/
==> memcache.so



安裝 memcached
yum install --enablerepo=remi  php-pecl-memcached
yum install  --enablerepo=remi   memcached
yum install  --enablerepo=remi  memcached-devel

or

http://libevent.org/
wget https://github.com/downloads/libevent/libevent/libevent-2.0.19-stable.tar.gz
tar -zvxf libevent-2.0.19-stable.tar.gz
cd libevent-2.0.19-stable
./configure
make
make test
meke install

ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5
memcached (Service ( DAEMON ) )

http://memcached.org/

wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
tar -zvxf memcached-1.4.13.tar.gz
cd memcached-1.4.13
./configure
make
make test
meke install

/usr/local/bin/memcached -d -m 128 -u nginx -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid
kill `cat /tmp/memcached.pid`


https://launchpad.net/libmemcached/+download
libmemcached-1.0.8.tar.gz 
./configure --prefix=/usr  --exec-prefix=usr
make
make install


http://pecl.php.net/package/memcached
http://pecl.php.net/get/memcached-2.0.1.tgz
#wget http://pecl.php.net/get/memcached-2.0.1.tgz
#tar -zvxf memcached-2.0.1.tgz
#cd   memcached-2.0.1
#phpize
#./configure --enable-memcached

Installing shared extensions:     /usr/lib/php/modules/

==> memcached.so



啟動

/usr/local/bin/memcached -d -m 128 -u nginx -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid

or
memcached -d -p 11211 -u nginx -m 64 -c 1024
memcached -d -p 11212 -u nginx -m 64 -c 1024
memcached -d -p 11213 -u nginx -m 1024
memcached -d -p 11214 -u nginx -m 1024

vi /etc/php.ini

cgi.fix_pathinfo=1

; zend_optimizer.optimization_level=15
; zend_extension=/usr/lib/php/modules/ZendOptimizer.so

zend_extension=/usr/lib/php/modules/ZendGuardLoader.so
zend_loader.enable=1
zend_loader.disable_licensing=0
zend_loader.obfuscation_level_support=3
zend_loader.license_path=

extension="eaccelerator.so"
extension="memcache.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
[memcache]
memcache.hash_strategy = "consistent"
memcache.default_timeout_ms = 300
[root@nginx modules]#

重啟 php-fpm
#service php-rpm restart

========================
[root@nginx modules]# php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dom
eAccelerator
ereg
exif
fileinfo
filter
ftp
gd
gettext
gmp
hash
iconv
igbinary
imap
json
ldap
libxml
mbstring
mcrypt
memcache
memcached

mhash
mysql
mysqli
odbc
openssl
pcntl
pcre
PDO
pdo_mysql
PDO_ODBC
pdo_sqlite
Phar
readline
Reflection
session
shmop
SimpleXML
snmp
soap
sockets
SPL
standard
tokenizer
wddx
xml
xmlreader
xmlwriter
xsl
Zend Guard Loader
zip
zlib
[Zend Modules]
Zend Guard Loader
eAccelerator
[root@nginx modules]#



[root@nginx conf]# cat nginx.conf
user  nginx nginx;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
google_perftools_profiles /tmp/tcmalloc;
events {
    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  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 16 64k;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    include  /usr/local/nginx3/conf/proxy.conf;

    upstream memcached {
    server 127.0.0.1:11211;
    }

    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htmi index.php;
            # proxy_pass  http://192.168.100.10/;
            proxy_cache             tmpcache;
            proxy_cache_key         $uri$is_args$args;
          
          
            set $key $uri$args;
            srcache_fetch GET /memc $key;
            srcache_store PUT /memc $key;
       
            #set $memcached_key      $host$uri;
            #memcached_pass   memcached;
            #default_type   text/html;
            #error_page 404 = /error_page;
   
        }
        location /memc {
         internal;
         memc_connect_timeout 100ms;
         memc_send_timeout 100ms;
         memc_read_timeout 100ms;
         set $memc_key $query_string;
         set $memc_exptime 300;
         memc_pass memcached;
        }
        #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;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1/;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME   /html$fastcgi_script_name;
            include        fastcgi_params;
            set $key $uri$args;
            srcache_fetch GET /memc $key;        
            srcache_store PUT /memc $key;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
        location /server-status {
         stub_status on;
         access_log off;
         allow 127.0.0.1;
         allow 192.168.100.0/24;
         deny all;
        }
        location /nginx_status {
        stub_status on;
        # disable access_log if requared
        access_log   off;
        allow 127.0.0.1;
        allow 192.168.100.0/24;
        deny all;
        }

        location ~ /purge(/.*) {
        allow                   127.0.0.1;
        deny                    all;
        proxy_cache_purge       tmpcache   $1$is_args$args;
        }

    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;
    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_timeout  5m;
    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}



[root@nginx conf]# cat proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_temp_path /usr/local/nginx3/proxy_temp/temp;
proxy_cache_path /usr/local/nginx3/proxy_temp/proxy levels=1:2 keys_zone=tmpcache:10m inactive=1d max_size=1g; 
proxy_cache tmpcache;
proxy_cache_valid  200 304 12h;
proxy_cache_valid  301 302 1m;
proxy_cache_valid  any 1m;
proxy_cache_key $host$uri$is_args$args;
[root@nginx conf]#


[root@CentOS6 init.d]# cat memcached
#! /bin/sh
#
# chkconfig: - 55 45
# description:  The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached
# pidfile: /var/run/memcached/memcached.pid
# Standard LSB functions
#. /lib/lsb/init-functions
# Source function library.
. /etc/init.d/functions
PORT=11211
USER=root
MAXCONN=1024
CACHESIZE=2048
OPTIONS="-l 127.0.0.1"
if [ -f /etc/sysconfig/memcached ];then
        . /etc/sysconfig/memcached
fi
# Check that networking is up.
. /etc/sysconfig/network
if [ "$NETWORKING" = "no" ]
then
        exit 0
fi
RETVAL=0
prog="memcached"
pidfile=${PIDFILE-/var/run/memcached/memcached.pid}
lockfile=${LOCKFILE-/var/lock/subsys/memcached}
start () {
        echo -n $"Starting $prog: "
        # Ensure that /var/run/memcached has proper permissions
        if [ "`stat -c %U /var/run/memcached`" != "$USER" ]; then
                chown $USER /var/run/memcached
        fi
        daemon --pidfile ${pidfile} /usr/local/memcached/bin/memcached -d -p $PORT -u $USER  -m $CACHESIZE -c $MAXCONN -P ${pidfile} $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch ${lockfile}
}
stop () {
        echo -n $"Stopping $prog: "
        killproc -p ${pidfile} /usr/local/memcached/bin/memcached
        RETVAL=$?
        echo
        if [ $RETVAL -eq 0 ] ; then
                rm -f ${lockfile} ${pidfile}
        fi
}
restart () {
        stop
        start
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status -p ${pidfile} /usr/local/memcached/bin/memcached
        RETVAL=$?
        ;;
  restart|reload|force-reload)
        restart
        ;;
  condrestart|try-restart)
        [ -f ${lockfile} ] && restart || :
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart|try-restart}"
        RETVAL=2
        ;;
esac
exit $RETVAL

===============
mkdir /var/run/memcached
touch /var/run/memcached/memcached.pid

vi vi /etc/sysconfig/memcached

PORT="11211"
USER="root"
# max connection 2048
MAXCONN="2048"
# set ram size to 2048 - 2GiB
CACHESIZE="4096"
# listen to loopback ip 127.0.0.1, for network connection use real ip e.g., 10.0.0.4
OPTIONS="-l 127.0.0.1"





沒有留言:

張貼留言