Memcache的搭建需要借助于LAMP或LNMP,本篇博文采用LNMP結(jié)構(gòu)
創(chuàng)新互聯(lián)建站從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元貴港做網(wǎng)站,已為上家服務(wù),為貴港各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
192.168.148.129 Memcache
192.168.148.130 php
192.168.148.131 nginx
192.168.148.136 MySQL
下載nginx軟件包
[root@bogon ~]# useradd -M -s /sbin/nologin nginx
[root@bogon ~]# yum -y install openssl-devel
[root@bogon ~]# tar zxf pcre-8.39.tar.gz -C /usr/src
[root@bogon ~]# tar zxf zlib-1.2.8.tar.gz -C /usr/src
[root@bogon ~]# tar zxf nginx-1.14.0.tar.gz -C /usr/src
[root@bogon ~]# cd /usr/src/nginx-1.14.0/
[root@bogon nginx-1.14.0]# ./configure --prefix=/usr/local/nginx \
--user=nginx --group=nginx --with-http_dav_module \
--with-http_stub_status_module --with-http_addition_module \
--with-http_sub_module --with-http_flv_module --with-http_mp4_module \
--with-pcre=/usr/src/pcre-8.39 --with-zlib=/usr/src/zlib-1.2.8 \
--with-http_ssl_module --with-http_gzip_static_module && make && make install
[root@bogon ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
[root@bogon ~]# nginx
[root@bogon ~]# netstat -anpt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8460/nginx: master
下載php軟件包
[root@localhost ~]# yum -y install libxml2-devel lzip2-devel libcurl-devel libmcrypt-devel openssl-devel bzip2-devel
[root@localhost ~]# tar zxf libmcrypt-2.5.7.tar.gz
[root@localhost ~]# cd libmcrypt-2.5.7/
[root@localhost libmcrypt-2.5.7]# ./configure --prefix=/usr/local/libmcrypt && make && make install
[root@localhost ~]# tar zxf php-5.6.27.tar.gz
[root@localhost ~]# cd php-5.6.27/
[root@localhost php-5.6.27]# ./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd \
--with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets \
--enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib \
--with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt \
--with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d \
--with-bz2 --enable-maintainer-zts && make && make install
[root@localhost php-5.6.27]# cp php.ini-production /etc/php.ini
[root@localhost php-5.6.27]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-5.6.27]# chmod +x /etc/init.d/php-fpm
[root@localhost php-5.6.27]# chkconfig --add php-fpm
[root@localhost php-5.6.27]# chkconfig php-fpm on
[root@localhost php-5.6.27]# cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf
[root@localhost php-5.6.27]# vim /usr/local/php5.6/etc/php-fpm.conf
修改內(nèi)容如下:
pid = run/php-fpm.pid
listen = 192.168.31.141:9000 \\本地ip地址(千萬不能用127.0.0.1)
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
[root@localhost php-5.6.27]# service php-fpm start
Starting php-fpm done
[root@localhost php-5.6.27]# netstat -anpt | grep php-fpm
tcp 0 0 192.168.148.130:9000 0.0.0.0:* LISTEN 130988/php-fpm: mas
下載mysql腳本一鍵安裝
[root@bogon ~]# mysql -u root -p123
mysql> create database testdb1;
mysql> use testdb1;
mysql> grant all on *.* to xws@'192.168.148.%' identified by '123456';
mysql> create table test1(id int not null auto_increment,name varchar(20) default null,primary key (id)) engine=innodb auto_increment=1 default charset=utf8;
mysql> insert into test1(name) values ('tom1'),('tom2'),('tom3'),('tom4'),('tom5');
mysql> select * from test1;
+----+------+
| id | name |
+----+------+
| 1 | tom1 |
| 2 | tom2 |
| 3 | tom3 |
| 4 | tom4 |
| 5 | tom5 |
+----+------+
5 rows in set (0.00 sec)
[root@bogon nginx-1.14.0]# vim /usr/local/nginx/conf/nginx.conf //修改如下(大約在43行)
location / {
root html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 192.168.148.130:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
[root@bogon nginx-1.14.0]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@bogon nginx-1.14.0]# nginx -s reload
php操作如下:
[root@bogon ~]# mkdir -p /var/www/html
[root@bogon ~]# vim /var/www/html/test.php
[root@bogon ~]# vim /var/www/html/test1.php
客戶端訪問如下:
下載memcache
[root@bogon ~]# tar zxf libevent-2.0.22-stable.tar.gz -C /usr/src/
[root@bogon ~]# cd /usr/src/libevent-2.0.22-stable/
[root@bogon libevent-2.0.22-stable]# ./configure && make && make install
[root@bogon ~]# tar zxf memcached-1.4.33.tar.gz -C /usr/src/
[root@bogon ~]# cd /usr/src/memcached-1.4.33/
[root@bogon memcached-1.4.33]# ./configure --prefix=/usr/local/memcached \
> --with-libevent=/usr/local/ && make && make install
[root@bogon ~]# memcached -d -m 2048 -l 192.168.1.7 -p 11211 -c 10240 -P /usr/local/memcached/memcached.pid -u root
[root@bogon ~]# netstat -anpt | grep 11211
[root@bogon ~]# tar zxf memcache-3.0.8.tgz -C /usr/src/
[root@bogon ~]# cd /usr/src/memcache-3.0.8/
[root@bogon memcache-3.0.8]# /usr/local/php5.6/bin/phpize
[root@PHP memcache-3.0.8]# ./configure --enable-memcache \
--with-php-config=/usr/local/php/bin/php-config && make && make install
//執(zhí)行后會(huì)顯示memcache.so存放的路徑
[root@PHP ~]# echo "extension = /usr/local/php/lib/php/extensions/no-debug-zts-20131226/memcache.so" >> /etc/php.ini
//在PHP主配置文件中填寫memcache.so模塊存放的路徑
[root@PHP ~]# systemctl restart php-fpm
[root@PHP ~]# vim /var/www/html/test2.php
connect('192.168.1.129', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."
";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 600) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 600 seconds)
";
$get_result = $memcache->get('key');
echo "Data from the cache:
";
var_dump($get_result);
?>
//此測(cè)試腳本是顯示memcached的版本
//并且向里面插入了一個(gè)緩存時(shí)間為600秒的鍵值對(duì)“test=123”,其ID為“key”
客戶端訪問如下:
在PHP服務(wù)器上安裝telnet工具測(cè)試
[root@PHP ~]# yum -y install telnet
[root@PHP ~]# telnet 192.168.148.129 11211 //登陸到memcached的11211端口
Trying 192.168.148.129...
Connected to 192.168.148.129.
Escape character is '^]'.
get key //查詢ID為“key”的鍵值對(duì),可以看到我們測(cè)試腳本寫入的“test=123”
VALUE key 1 66
O:8:"stdClass":2:{s:8:"str_attr";s:4:"test";s:8:"int_attr";i:123;}
END
//在進(jìn)行上面的get驗(yàn)證時(shí),需要將test2.php文件中插入的鍵值對(duì)的保存時(shí)間值改大一些
//或者重新訪問一下,以免緩存失效,查詢不到
quit //退出當(dāng)前環(huán)境
Connection closed by foreign host.
[root@PHP ~]#
[root@PHP ~]# vim /var/www/html/test4.php
connect($memcachehost,$memcacheport) or die ("Could not connect");
$query="select * from test1 limit 10";
$key=md5($query);
if(!$memcache->get($key))
{
$conn=mysql_connect("192.168.1.8","lzj","123456"); //指定數(shù)據(jù)庫(kù)服務(wù)器的IP地址、用戶及密碼
mysql_select_db(testdb1);
$result=mysql_query($query);
while ($row=mysql_fetch_assoc($result))
{
$arr[]=$row;
}
$f = 'mysql';
$memcache->add($key,serialize($arr),0,30);
$data = $arr ;
}
else{
$f = 'memcache';
$data_mem=$memcache->get($key);
$data = unserialize($data_mem);
}
echo $f;
echo "
";
echo "$key";
echo "
";
//print_r($data);
foreach($data as $a)
{
echo "number is $a[id]";
echo "
";
echo "name is $a[name]"; //突出顯示信息的字體顏色
echo "
";
}
?>
//經(jīng)常需要修改的地方已經(jīng)標(biāo)注了!而且這個(gè)測(cè)試腳本在Memcache軟件中也有