How to build Memcache service
Install Memcache
Memcache needs to be built with the help of LAMP or LNMP. This blog uses LNMP structure.
The installation environment is as follows: 192.168.148.129 Memcache192.168.148.130 php192.168.148.131 nginx192.168.148.136 mysql install nginx
Download the nginx package
[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 80tcp 00 0.0.0.0 .0.0: * LISTEN 8460/nginx: master installs the PHP server
Download the php package
[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 is modified as follows: pid = run/php-fpm.pid listen = 192.168.31.141vim 9000\\ Local ip address (do not use 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 startStarting php-fpm done [root@localhost php-5.6.27] # netstat-anpt | grep php-fpmtcp 00 192.168.148.130 root@localhost php-5.6.27 9000 0.0.0.0 anpt * LISTEN 130988/php-fpm: mas installs the MySQL database
Download the mysql script and install it with one click
[root@bogon ~] # mysql-u root-p123mysql > create database testdb1;mysql > use testdb1;mysql > grant all on *. * to xws@'192.168.148.%' identified by '123456 create table test1 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) nginx operation is as follows: [root@bogon nginx-1.14.0] # vim / usr/local/nginx/conf/nginx.conf / / modified (about 43 lines) Location / {root html Index index.php index.html index.htm;} location ~\ .php$ {root / var/www/html; fastcgi_pass 192.168.148.130 root 9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME / scripts$fastcgi_script_name; include fastcgi.conf } [root@bogon nginx-1.14.0] # nginx- tnginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful [root@bogon nginx-1.14.0] # nginx- s reload
The php operation is as follows:
[root@bogon ~] # mkdir-p / var/www/html [root@bogon ~] # vim / var/www/html/test.php [root@bogon ~] # vim / var/www/html/test1.php
The client access is as follows:
Install Memecache
Download 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 11211php operates as follows (install Memecache client) [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 After install// is executed, it will show the path where memcache.so is stored [root@PHP ~] # echo "extension = / usr/local/php/lib/php/extensions/no-debug-zts-20131226/memcache.so" > > / etc/php.ini//. Fill in the path where memcache.so modules are stored in the PHP main configuration file. [root@PHP ~] # systemctl restart php-fpm [root@PHP ~] # vim / var/www/html/test2.php// this test script is Displays the version of memcached / / and inserts a key-value pair with a cache time of 600 seconds to "test=123" Its ID is "key"
The client access is as follows:
Install the telnet tool test on the PHP server
[root@PHP ~] # yum-y install telnet [root@PHP ~] # telnet 192.168.148.129 11211 / / Log in to port 11211 of memcached Trying 192.168.148.129...Connected to 192.168.148.129.Escape character is'^] .get key / / query the key-value pair whose ID is "key" You can see the "test=123" VALUE key 1 66O:8: "stdClass": 2: {Sv8: "str_attr" written by our test script SRV 4: "test"; SRV 8: "int_attr"; iRV 123 } END// needs to increase / / or revisit the save time value of the key-value pair inserted in the test2.php file when performing the above get verification, so as to avoid cache invalidation Unable to query quit / / exit the current environment Connection closed by foreign host. [root@PHP ~] # Test Memcache cache database [root@PHP ~] # vim / var/www/html/test4.php / / areas that often need to be modified have been marked! And this test script is accessed for the first time in Memcache software.
Second access (after refresh)