天天看點

項目實戰:LAMP環境+Xcache+Redis,另附Memcached配置

由于公司需求,小菜首次做項目:

        程式架構:Linux+Apache+Mysql+php+Xcache+Redis

編譯安裝apr,apr-util

# tar -zxvf apr-1.5.1.tar.gz 

# ls

# cd apr-1.5.1

# ./configure --help  

# ./configure --prefix=/usr/local/apr

# make && make install

# cd ..

# tar -zxvf apr-util-1.5.3.tar.gz 

# cd apr-util-1.5.3

# ./configure  --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/

編譯安裝httpd2.4.9

設定編譯參數:

--prefix    設定安裝目錄

--sysconfdir    設定配置檔案目錄

--enable-so    啟用動态子產品加載

--enable-ssl    啟用SSL

--enable-cgi    啟用cgi功能

--with-pcre    使用擴充的pcre庫

--enable-rewrite    基于基本的URL處理

--with-apr    apr安裝目錄

--with-apr-util    apr-util安裝目錄

--enable-modules=most    啟用大多數空閑分離子產品

--enable-mpms-shared=all    啟用MPM空閑分離子產品動态裝載

--with-mpm=event    給Apache選擇預設程序模式

如果編譯程序模式為work,或event,則在編譯PHP的時候要加上 --enable-maintainer-zts,因為這兩種模式是基于線程工作的。

# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event

連接配接頭檔案,導入庫檔案,修改PATH環境變量,這些步驟我就省略了。

編譯安裝PHP為fpm服務

--with-mysql=    讓PHP支援mysql,指定mysql的位置。

--with-openssl    指定openssl位置

--with-mysqli=/usr/local/mysql/bin/mysql_config    指定mysql的進階接口

--with-freetype-dir    圖檔格式的支援

--with-jpeg-dir    圖檔格式的支援

--with-png-dir      圖檔格式的支援

--enable-mbstring    啟用多位元組字元串的支援

--with-zilb    壓縮庫

--with-libxml-dir    指定xml安裝目錄

--enable-xml    啟用php解釋XML頁面程式

--enable-sockets    啟用套接字通信

--with-apxs2=/usr/local/apache/bin/apxs    apache把php編譯成子產品的接口

--with-mcrypt    增加php加密的功能  依賴庫(libmcrypt,libmcrypt-devel,mhash,mhash-devel)

--with-config-file-path=/etc    php的配置檔案目錄

--with-config-file-scan-dir=/etc/php.d        設定掃描配置檔案的路徑

--with-bz2    壓縮功能

--enable-fpm    啟用fpm服務

 ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --enable-fpm --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2

# cp php.ini-production  /etc/php.ini

# cp /home/php-5.5.13/sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm

# cd /usr/local/php/etc

# cp php-fpm.conf.default  php-fpm.conf

# vim php-fpm.conf

<a href="http://s3.51cto.com/wyfs02/M01/2F/BE/wKioL1OhNCOxTmQJAAN7uIt9sgk477.jpg" target="_blank"></a>

接下來啟動php-fpm

# chkconfig --add php-fpm

# service php-fpm start

編輯apache配置檔案httpd.conf,以apache支援php 

 # vim /etc/httpd/httpd.conf

添加如下二行

   AddType application/x-httpd-php  .php

   AddType application/x-httpd-php-source  .phps

定位至DirectoryIndex index.html 

修改為:

   DirectoryIndex  index.php  index.html

加載子產品:

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

配置虛拟主機支援使用fcgi:

ProxyRequests Off

ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1

編譯Xcache

# tar -xf xcache-3.1.0.tar.gz

# cd xcache-3.1.0

是不是沒有configure腳本,要執行phpize程式檢測一下,就可以configure了。

<a href="http://s3.51cto.com/wyfs02/M00/2F/BF/wKiom1OhOpCSRBS-AAFXdmgmp3k665.jpg" target="_blank"></a>

# /usr/local/php/bin/phpize

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

安裝結束時,會出現類似如下行:

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20100525/

整合php和xcache:

# mkdir /etc/php.d

# cp xcache.ini /etc/php.d

# vim /etc/php.d/xcache.ini

extension =  /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so

編譯安裝redis服務端

# tar -zxvf redis-2.8.9.rar.gz

# cd redis-2.8.9

這裡直接make

# make

編譯完成

<a href="http://s3.51cto.com/wyfs02/M01/2F/BF/wKioL1OhPZ7BXC8BAAEPeCnT67M220.jpg" target="_blank"></a>

# cd src

# make install

<a href="http://s3.51cto.com/wyfs02/M01/2F/BF/wKioL1OhPdPywk_AAADQFRpLhZk749.jpg" target="_blank"></a>

# mkdir /usr/local/redis/etc -pv  &amp;&amp; mkdir /usr/local/redis/bin

# cp redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel  redis-server  mkreleasehdr.sh  /usr/local/redis/bin

# cp ../redis.conf  /usr/local/redis/etc/

# cd /usr/local/redis

# vim etc/redis.conf

<a href="http://s3.51cto.com/wyfs02/M00/2F/C0/wKiom1OhQAfzYimeAAGgkEd0BMU341.jpg" target="_blank"></a>

現在可以啟動redis了,啟動成功後,程式會監聽在6379端口。

# ./bin/redis-server etc/redis.conf

<a href="http://s3.51cto.com/wyfs02/M00/2F/BF/wKioL1OhQNvBK2K3AAGd3Y9Vo9Q812.jpg" target="_blank"></a>

編譯安裝phpredis,方法與安裝Xcache有點類似。

# tar -zxvf  phpredis-master.tar.gz

# cd phpredis-master

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

整合php和redis:

# vim /etc/php.d/redis.conf

extension =  /usr/local/php/lib/php/extensions/no-debug-zts-20100525/redis.so

然後重新開機php就可以識别到redis了。

Memcached 安裝配置:

由于Memcached要依賴于libevent,是以先安裝libevent。

安裝libevent:

# tar -xf libevent-2.0.21-stable.tar.gz 

# cd libevent-2.0.21-stable

# ./configure  --prefix=/usr/local/libevent

# cd /usr/local/libevent

# ln -sv /usr/local/libevent/include/ /usr/include/libevent

# vim /etc/ld.so.conf.d/libevent.conf

# ldconfig

安裝Memcached

# cp ~/Desktop/memcached-1.4.20.tar.gz  .

# tar -xf memcached-1.4.20.tar.gz 

# cd memcached-1.4.20

# ./configure --help

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

# cd /usr/local/memcacheds

# ln -sv /usr/local/memcached/include/ /usr/include/memcached

# cd bin/

# ./memcached -h

<a href="http://s3.51cto.com/wyfs02/M02/2F/C3/wKioL1OhYgyiWDN9AAYPplQdy7Y838.jpg" target="_blank"></a>

使用 # ./memcached -d -u daemon  啟動memcached

啟動後,程式會監聽在tcp11211端口和udp11211端口

<a href="http://s3.51cto.com/wyfs02/M00/2F/C3/wKiom1OhZr7zWE3pAAQczOgfiS4826.jpg" target="_blank"></a>

 Memcached提供了為數不多的幾個指令來完成與伺服器端的互動,這些指令基于memcached協定實作。

存儲類指令:set, add, replace, append, prepend

擷取資料類指令:get, delete, incr/decr

統計類指令:stats, stats items, stats slabs, stats sizes

清空緩存: flush_all

set    設定一個緩存;

add    新增;

replace    替換;

append    在一個已有的緩存後面附加值;

prepend    在緩存前面加值;

get    擷取資料;

delete    清除;

incr    在目前緩存的值加1;

decr    在目前緩存減去值;

stats    

stats items    檢視緩存了多少資料;

stats slabs     檢視slab占用情況;

stats sizes     檢視空間的使用情況;

# telnet 127.0.0.1 11211

# starts

<a href="http://s3.51cto.com/wyfs02/M02/2F/C3/wKiom1Ohakry5XHAAAJ1uqqecUk077.jpg" target="_blank"></a>

add keyname  flag  timeout  datasize

檢測能不能設定緩存

<a href="http://s3.51cto.com/wyfs02/M00/2F/C3/wKioL1OhalyBYYOuAABW_r5p5NA613.jpg" target="_blank"></a>

用add指令也可以

安裝memcache,php連接配接memcached驅動。

# tar -xf memcache-2.2.4.tgz

# cd memcache-2.2.4

# /usr/local/php-fpm/bin/phpize

<a href="http://s3.51cto.com/wyfs02/M02/2F/C3/wKioL1OhbA6i6WdnAAGH2YwW-WM043.jpg" target="_blank"></a>

檢測完成之後,可以開始configure

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

安裝完成!

<a href="http://s3.51cto.com/wyfs02/M01/2F/C3/wKioL1OhbKXzal_vAAJypkGL8xg621.jpg" target="_blank"></a>

整合進php

# vim /etc/php-fpm/php.d/memcache.ini

 寫入:extension = /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20121212/memcache.so

然後重新開機php-fpm

# /etc/init.d/php-fpm restart

再通路下phpinfo();

<a href="http://s3.51cto.com/wyfs02/M02/2F/C3/wKioL1OhbcDjRNL-AAGdIH6hF18401.jpg" target="_blank"></a>

我們測試下Memcached:

建立一個虛拟主機,添加一個index.php頁面檔案,把以下代碼貼進去,再通路這個檔案。

&lt;?php

$mem = new Memcache;

$mem-&gt;connect("127.0.0.1", 11211)  or die("Could not connect");

$version = $mem-&gt;getVersion();

echo "Server's version: ".$version."&lt;br/&gt;\n";

$mem-&gt;set('hellokey', 'Hello World', 0, 600) or die("Failed to save data at the memcached server");

echo "Store data in the cache (data will expire in 600 seconds)&lt;br/&gt;\n";

$get_result = $mem-&gt;get('hellokey');

echo "$get_result is from memcached server.";         

?&gt;

<a href="http://s3.51cto.com/wyfs02/M01/2F/CE/wKiom1OiVVWBTsPuAAE-gMCW8Co684.jpg" target="_blank"></a>

我們再telnet登陸到memcached看看,也是有值的,看來資料确實緩存到memcached裡面了。

<a href="http://s3.51cto.com/wyfs02/M01/2F/CE/wKiom1OiVdvgC01ZAACdm8qLAfA966.jpg" target="_blank"></a>

使用memadmin,提供一個Web接口,管理和監控你的memcached。

過程很簡單:

先解壓程式,然後建一個虛拟主機,把程式移過去,再通路就出現如下頁面:

<a href="http://s3.51cto.com/wyfs02/M02/2F/D2/wKioL1OiXdrQn9LJAAGXeW_uHJ4809.jpg" target="_blank"></a>

預設賬号密碼:admin

<a href="http://s3.51cto.com/wyfs02/M00/2F/D5/wKiom1OiYqChlq4IAARlRzbMnLk220.jpg" target="_blank"></a>

今天就到這裡了,有問題歡迎與我交流QQ:1183710107

本文轉自qw87112 51CTO部落格,原文連結:http://blog.51cto.com/tchuairen/1425053