大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Memcache的搭建需要借助于LAMP或LNMP,本篇博文采用LNMP结构
创新互联建站从2013年开始,是专业互联网技术服务公司,拥有项目网站设计制作、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元贵港做网站,已为上家服务,为贵港各地企业和个人服务,联系电话: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
修改内容如下:
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
//执行后会显示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);
?>
//此测试脚本是显示memcached的版本
//并且向里面插入了一个缓存时间为600秒的键值对“test=123”,其ID为“key”
客户端访问如下:
在PHP服务器上安装telnet工具测试
[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”的键值对,可以看到我们测试脚本写入的“test=123”
VALUE key 1 66
O:8:"stdClass":2:{s:8:"str_attr";s:4:"test";s:8:"int_attr";i:123;}
END
//在进行上面的get验证时,需要将test2.php文件中插入的键值对的保存时间值改大一些
//或者重新访问一下,以免缓存失效,查询不到
quit //退出当前环境
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"); //指定数据库服务器的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 "
";
}
?>
//经常需要修改的地方已经标注了!而且这个测试脚本在Memcache软件中也有