WIKI使用導(dǎo)航
站長(zhǎng)百科導(dǎo)航
站長(zhǎng)專題
- 網(wǎng)站推廣
- 網(wǎng)站程序
- 網(wǎng)站賺錢
- 虛擬主機(jī)
- cPanel
- 網(wǎng)址導(dǎo)航專題
- 云計(jì)算
- 微博營(yíng)銷
- 虛擬主機(jī)管理系統(tǒng)
- 開放平臺(tái)
- WIKI程序與應(yīng)用
- 美國(guó)十大主機(jī)
Ubuntu/Ubuntu+Apache+PHP+MySQL+Memcached
安裝apache2和php5[ ]
<? phpinfo(); ?>
保存之后,啟動(dòng)apache服務(wù)器,輸入:
/etc/init.d/apache2 start
服務(wù)器啟動(dòng)成功之后,打開瀏覽器,在地址欄輸入http://127.0.0.1/phpinfo.php,如果顯示出php的信息,說明apache和php安裝成功了。
安裝mysql[ ]
sudo apt-get install mysql-server
安裝完成按提示設(shè)置root密
讓apache、php支持mysql[ ]
sudo apt-get install libapache2-mod-auth-mysql sudo apt-get install php5-mysqlsudo /etc/init.d/apache2 restart
至此apache2+php 5+mysql 5的環(huán)境就完成了。
安裝memcached[ ]
memcached需要使用libevent,所以在安裝memcached之前,首先安裝好libevent。memcached和libevent的安裝過程用經(jīng)典的三步就可以搞定。
/configure make make install
測(cè)試下memcached是否能夠正常運(yùn)行。
memcached -vv
此時(shí)能夠看到很多顯示信息,接下來Telnet到服務(wù)器上。
ecy@ecy-geek:~$ telnet 127.0.0.1 11211 Trying 127.0.0.1… Connected to 127.0.0.1. Escape character is ‘^]’. set foo 0 0 3 bar STORED get foo VALUE foo 0 3 bar END
測(cè)試memcached服務(wù)器運(yùn)行正常.
安裝memcache[ ]
下載地址:http://pecl.php.net/package/memcache
從以上鏈接下載php的memcache extension。將memcache-2.2.5.tgz下載到本地之后,解壓,接下來輸入以下命令:
root@ecy-geek:/home/ecy/memcache-2.2.5# phpize Configuring for: PHP Api Version: 20041225 Zend Module Api No: 20060613 Zend Extension Api No: 220060519 root@ecy-geek:/home/ecy/memcache-2.2.5# make /bin/bash /home/ecy/memcache-2.2.5/libtool –mode=install cp ./memcache.la / /ecy/memcache-2.2.5/modules libtool: install: cp ./.libs/memcache.so /home/ecy/memcache-2.2.5/modules/me he.so libtool: install: cp ./.libs/memcache.lai /home/ecy/memcache-2.2.5/modules/m che.la libtool: finish: PATH=”/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sb bin:/usr/games:/sbin” ldconfig -n /home/ecy/memcache-2.2.5/modules ———————————————————————- …………………………………. root@ecy-geek:/home/ecy/memcache-2.2.5# make install Installing shared extensions: /usr/lib/php5/20060613+lfs/
最后生成的擴(kuò)展動(dòng)態(tài)庫(kù)文件會(huì)放在”/usr/lib/php5/20060613+lfs/”,不要去挪動(dòng)它,php運(yùn)行時(shí)會(huì)到”/usr/lib/php5/”目錄下找這些擴(kuò)展。為了使php能使用memcache,需要修改一個(gè)配置文件。
打開”/etc/php5/conf.d”目錄,新建一個(gè)memcache.ini文件,在文件中輸入”extension=memcache.so”,保存并退出,接下來重啟apache服務(wù)器,然后再刷新下上面那個(gè)phpinfo.php頁(yè)面,此時(shí)將能看到以下內(nèi)容:
這就意味著memcache已經(jīng)能正常工作了,接下來就是測(cè)試使用memcache是否能正常訪問memcached服務(wù)器了。
在”/var/www”目錄下新建一個(gè)testmemcache.php文本文件,輸入以下內(nèi)容:
[codesyntax lang="php"] <?php $memcache = new Memcache; $memcache->connect('127.0.0.1', 11211) or die ("Could not connect"); $version = $memcache->getVersion(); echo "Server's version: ".$version; echo "<br>"; $memcache->set('foo', 'bar');Ok,這就搞定了 echo "<br>"; $get_result = $memcache->get('foo'); echo "Data from the cache:"; echo "<br>"; var_dump($get_result); $memcache->close(); ?> [/codesyntax]
首先啟動(dòng)memcached服務(wù)器,然后在瀏覽器地址欄輸入http://127.0.0.1/testmemcache.php,結(jié)果顯示如下:
Server’s version: 1.2.8 Data from the cache: string(3) “bar”
使用php操作MySQL數(shù)據(jù)庫(kù)[ ]
要使用php操作MySQL數(shù)據(jù)庫(kù),首先必須安裝php5-mysql這個(gè)包,否則連接時(shí)會(huì)提示無法找到mysql_connect這些函數(shù)。安裝成功之后,在”/var/www/”目錄下創(chuàng)建一個(gè)文本文件-testmysql.php,輸入以下內(nèi)容:
[codesyntax lang="php"] <?php $username='root'; //輸入正確的用戶名和密碼 $passwd='dandan'; //連接數(shù)據(jù)庫(kù) $link_mess=mysql_connect('localhost', $username, $passwd); //顯示數(shù)據(jù)庫(kù)連接的返回信息 if (!$link_mess){ echo "failed to connect the server "; echo "<br>"; exit(); } else { echo "connect the server successfully "; echo "<br>"; } //選擇數(shù)據(jù)庫(kù) mysql_select_db("test", $link_mess); //創(chuàng)建一個(gè)新表 $createtable="create table students( stu_no char(9) not null primary key, stu_name char(10) not null, stu_age int)"; mysql_query($createtable, $link_mess); //插入數(shù)據(jù) $insertrec="insert into students (stu_no, stu_name, stu_age) values ('1000', 'ecy fu', 24)"; mysql_query($insertrec, $link_mess); //查詢數(shù)據(jù) $result=mysql_query("select stu_no, stu_name, stu_age from students", $link_mess); $row=mysql_fetch_row($result); echo "No: "; echo $row[0]; echo "<br>"; echo "Name: " ; echo $row[2]; echo "<br>"; echo "Age: "; echo $row[1]; echo "<br>"; ?> [/codesyntax]
在瀏覽器中輸入http://127.0.0.1/testmysql.php,此時(shí)能看見如下內(nèi)容:
connect the server successfully No: 1000 Name: 24 Age: ecy fu
將數(shù)據(jù)緩存在memcache中[ ]
以下的實(shí)例中引用了上面的數(shù)據(jù)庫(kù):
[codesyntax lang="php"] <?php # Connect to memcache: global $memcache; $memcache = new Memcache; $memcache->connect('127.0.0.1', 11211) or die ("Could not connect"); $version = $memcache->getVersion(); echo "Server's version: ".$version; echo "<br>"; //下面兩個(gè)函數(shù)首先都會(huì)判斷是否有使用memcache,如果有使用,就會(huì)調(diào)用memcached的set/get命令來保存和獲取數(shù)據(jù) //否則簡(jiǎn)單地返回false # Gets key / value pair into memcache … called by mysql_query_cache() function getCache($key) { global $memcache; return ($memcache) ? $memcache->get($key) : false; } # Puts key / value pair into memcache … called by mysql_query_cache() function setCache($key, $object, $timeout = 60) { global $memcache; return ($memcache) ? $memcache->set($key,$object,MEMCACHE_COMPRESSED,$timeout) : false; } # Caching version of mysql_query() function mysql_query_cache($sql, $linkIdentifier = false,$timeout = 60) { //首先調(diào)用上面的getCache函數(shù),如果返回值不為false的話,就說明是從memcached服務(wù)器獲取的數(shù)據(jù) //如果返回false,此時(shí)就需要直接從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)了。 //需要注意的是這里使用操作的命令加上sql語(yǔ)句的md5碼作為一個(gè)特定的key,可能大家覺得使用數(shù)據(jù)項(xiàng)的 //名稱作為key會(huì)比較自然一點(diǎn)。運(yùn)行memcached加上"-vv"參數(shù),并且不作為daemon運(yùn)行的話,可以看見 //memcached處理時(shí)輸出的相關(guān)信息 if (!($cache = getCache(md5("mysql_query" . $sql)))) { $cache = false; $r = ($linkIdentifier !== false) ? mysql_query($sql,$linkIdentifier) : mysql_query($sql); //讀取數(shù)據(jù)庫(kù),并將結(jié)果放入$cache數(shù)組中 if (is_resource($r) && (($rows = mysql_num_rows($r)) != 0)) { for ($i=0;$i<$rows;$i++) { $fields = mysql_num_fields($r); $row = mysql_fetch_array($r); for ($j=0;$j<$fields;$j++) { if ($i == 0) { $columns[$j] = mysql_field_name($r,$j); } $cache[$i][$columns[$j]] = $row[$j]; } } //將數(shù)據(jù)放入memcached服務(wù)器中,如果memcached服務(wù)器沒有開的話,此語(yǔ)句什么也不會(huì)做 //如果開啟了服務(wù)器的話,數(shù)據(jù)將會(huì)被緩存到memcached服務(wù)器中 if (!setCache(md5("mysql_query" . $sql), $cache, $timeout)) { # If we get here, there isn’t a memcache daemon running or responding } } } return $cache; } ?> <?php $username='root'; //輸入正確的用戶名和密碼 $passwd='dandan'; //連接數(shù)據(jù)庫(kù) $link_mess=mysql_connect('localhost', $username, $passwd); //顯示數(shù)據(jù)庫(kù)連接的返回信息 if (!$link_mess){ echo "failed to connect the server "; echo "<br>"; exit(); } else { echo "connect the server successfully "; echo "<br>"; } //選擇數(shù)據(jù)庫(kù) mysql_select_db("test", $link_mess); $sql = "select * from students;"; //這里是沒有使用memcached時(shí)的操作,將其注釋掉了,它運(yùn)行不會(huì)有問題的 # Before: [without memcache] /*$rSlowQuery = mysql_query($sql); $row=mysql_fetch_row($rSlowQuery); echo "No: "; echo $row[0]; echo "<br>"; echo "Name: " ; echo $row[2]; echo "<br>"; echo "Age: "; echo $row[1]; echo "<br>";*/ //這里是使用memcached時(shí)的操作 # After: [with memcache] $rSlowQuery = mysql_query_cache($sql); # $rSlowQuery is an array $rows = count($rSlowQuery); for ($i=0; $i<$rows; $i++) { $stu_no = $rSlowQuery[$i]["stu_no"]; $stu_name = $rSlowQuery[$i]["stu_name"]; $stu_age = intval($rSlowQuery[$i]["stu_age"]); echo "No: "; echo $stu_no; echo "<br>"; echo "Name: " ; echo $stu_name; echo "<br>"; echo "Age: "; echo $stu_age; echo "<br>"; } ?> [/codesyntax]
memcached服務(wù)器輸出以下信息:
<10 version >10 VERSION 1.2.8 <10 get a72ce670d58c49583ae9817a40dabda7 >10 sending key a72ce670d58c49583ae9817a40dabda7 >10 END
瀏覽器顯示如下信息:
Server’s version: 1.2.8 connect the server successfully No: 1000 Name: ecy fu Age: 24
到這里,工作基本上都完成了!