天天看點

安裝nginx+php+fastcgi+memcache+xcache/eaccelerator/apc

rpm -ivh epel-release-5-3.noarch.rpm

yum list

yum install nginx gcc openssl-devel pcre-devel zlib-devel mysql-server mysql-devel libmcrypt-devel libxml2-devel libtool-ltdl-devel

gzip -cd php-5.2.9-fpm-0.5.10-unofficial.diff.gz |patch -d php-5.2.9 -p1

cd php-5.2.9

./configure   --prefix=/usr/local/php --enable-fastcgi   --enable-fpm   --with-mysql   --with-mcrypt   --with-zlib

make && make install

cp /usr/local/php/sbin/php-fpm /etc/init.d

vim /etc/init.d/php-fpm

# chkconfig: - 85 15

# description: php-fpm

vim /usr/local/php/etc/php-fpm.conf

fpm安裝完成後 (其實是php的編譯前打上fpm的更新檔,在編譯的時候增加fpm的參數)

要将                      Unix user of processes

                        <value name="user">nobody</value>                

                        Unix group of processes

                        <value name="group">nobody</value>   

這2個的注釋去掉,不然就會提示

root@yuan:/server/nginx/html# /server/php/sbin/php-fpm start

Starting php_fpm Sep 11 17:12:07.248390 [ERROR] fpm_unix_conf_wp(), line 124: please specify user and group other than root, pool 'default'

 done

vim /etc/nginx/nginx.conf

取消注釋

        location ~ \.php$ {

            root           html;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

            include        fastcgi_params;

        }

vim /etc/nginx/fastcgi_params

加一條fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

service nginx restart

先安裝libevent:

tar zxvf libevent-1.2.tar.gz

cd libevent-1.2

./configure --prefix=/usr

make

make install

Memcached 服務端安裝:(源碼包官方網站下載下傳)

系統指令

tar xvf memcached-1.2.6.tar.gz   

cd memcached-1.2.6   

./configure --prefix=/usr/local/memcached --with-libevent=/usr/

make    

make install  

然後就可以啟動memcached的守護程序了:

/usr/local/memcached/bin/memcached -p 11211 -l 127.0.0.1 -d -u nobody -P /var/run/memcached.pid -m 64M -c 1024  

php中memcache擴充元件的安裝:

tar xvf memcache-3.0.3.tgz   

cd memcache-3.0.3   

/usr/local/php5/bin/phpize   

./configure --with-php-config=/usr/local/php5/bin/php-config --enable-memcache   

make   

/usr/local/php/bin/phpize   

./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache   

    按照我的環境,編譯出來的memcache.so儲存在 /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/ 目錄下,如果你的環境不一樣,你得根據自己情況修改你的php.ini了。

接着要做的工作就是讓php加載這個擴充,編輯你的php.ini,在适當位置(通常是最後,也可以是獨立的一個ini檔案)加入如下行:

 extension=memcache.so

然後重新開機你的phpfastcgi程序或者apache,運作一個phpinfo()來确認一下,正常的話你應該可以看到這個了:memcache

php-memcache的簡單使用舉例:

<?php  

$memcache = new Memcache;  

$memcache->connect('127.0.0.1','11211');  

$memcache->setCompressThreshold(20000, 0.2);   

echo $memcache->getVersion();                  

$test = array(1,2,3,4,5,'abcde');              

if($memcache->get('test')){  

        print_r($memcache->get('test'));      

        echo "\n";  

        echo 'cached';  

}else{  

        $memcache->set('test',$test,0,30);     

        echo 'no cache';  

}  

?>  

四 配置XCache

1、安裝xcache子產品

wget http://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gz

tar -xvzf xcache-1.2.2.tar.gz

cd xcache-1.2.2

/usr/local/php/bin/phpize

./configure --with-php-config=/usr/local/php/bin/php-config --enable-xcache --enable-xcache-optimizer

2、計算密碼的md5值

echo -n "password"|md5sum

5f4dcc3b5aa765d61d8327deb882cf99

3、配置XCache

;注: zend_extension,用來加載zend的擴充,是絕對路徑,

extension是相對路徑,相對于extension_dir的相對路徑,非zend擴充

如果你更改路徑以後,一定要apachectl stop後再start,而不要restart。

vi /usr/local/php/etc/php.ini

添加:

[xcache-common] 

zend_extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xcache.so 

[xcache.admin] 

; Change xcache.admin.user to your preferred login name 

xcache.admin.user = "admin" 

; Change xcache.admin.pass to the MD5 fingerprint of your password 

; Use md5 -s "your_secret_password" to find the fingerprint 

xcache.admin.pass = "5f4dcc3b5aa765d61d8327deb882cf99" 

[xcache] 

; Change xcache.size to tune the size of the opcode cache 

xcache.size = 24M 

xcache.shm_scheme = "mmap" 

xcache.count = 2 

xcache.slots = 8K 

xcache.ttl = 0 

xcache.gc_interval = 0 

; Change xcache.var_size to adjust the size of variable cache 

xcache.var_size = 8M 

xcache.var_count = 1 

xcache.var_slots = 8K 

xcache.var_ttl = 0 

xcache.var_maxttl = 0 

xcache.var_gc_interval = 300 

xcache.test = Off 

xcache.readonly_protection = On 

xcache.mmap_path = "/tmp/xcache" 

xcache.coredump_directory = "" 

xcache.cacher = On 

xcache.stat = On 

xcache.optimizer = Off 

[xcache.coverager] 

xcache.coverager = On 

xcache.coveragedump_directory = ""

5、重新開機PHP子產品

正常load之後,

在phpinfo顯出的資訊内

Zend這快應該會加上XCache的内容

6、另外兩種加速子產品:

在我們的測試中,效果都要好于xcache,這3中加速不能同時存在兩種,有沖突。

6.1 apc

wget http://pecl.php.net/get/APC-3.0.19.tgz

cd APC-3.0.19

/usr/local/php/bin/phpize 

./configure --enable-apc --enable-apc-mmap --with-apxs=/EBS/apache/bin/apxs --with-php-config=/EBS/php/bin/php-config

6.2 eaccelerator

wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.zip

cd eaccelerator-0.9.5.3

./configure --enable-eaccelerator=shared --with-php-config=/EBS/php/bin/php-config

make 

vi php.ini

zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.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"

eaccelerator.compress="1"

eaccelerator.compress_level="9"

五、使用nginx對應多台facgi伺服器

思路:前端一台nginx,用于做為負載均衡和處理靜态頁面。利用nginx的upstream子產品來将php請求分發到後段的php-fpm伺服器上。

後端多台php-fpm的伺服器,隻起php-fpm服務來處理php。

這樣做減少了php-fpm上的nginx服務,相當于少了一層。

本文轉自leonardos51CTO部落格,原文連結: http://blog.51cto.com/leomars/419388,如需轉載請自行聯系原作者