站長資訊網
        最全最豐富的資訊網站

        memcache緩存應用(LNMP+memcache)

        環境:

        memcache:192.168.154.131
        nginx:192.168.154.132
        php:192.168.154.133
        mysql:192.168.154.134

        軟件:

        memcache上:libevent-2.0.22-stable.tar.gz、memcached-1.4.33.tar.gz
        下載地址:http://libevent.org/
        下載地址:http://memcached.org/downloads
        nginx上:nginx-1.14.0.tar.gz
        下載地址:http://nginx.org/en/download.html
        php上:libmcrypt-2.5.7.tar.gz、php-5.6.27.tar.gz、memcache-3.0.8.tgz
        下載地址:https://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/
        下載地址:http://php.net/downloads.php
        mysql上:mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz(二進制包)
        下載地址:https://dev.mysql.com/downloads/mysql/

        步驟

        nginx部分

        groupadd -r www

        useradd -r -g www -s /sbin/nologin www

        tar zxf nginx-1.14.0.tar.gz

        cd nginx-1.14.0

        ./configure –prefix=/usr/local/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=/root/pcre-8.39 –with-zlib=/root/zlib-1.2.8
        –with-http_ssl_module –with-http_gzip_static_module –user=www –group=www

        make && make install

        ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin

        nginx

        firewall-cmd –permanent –add-port=80/tcp

        firewall-cmd –reload

        nginx配置文件/usr/local/nginx/conf/nginx.conf

        user www www;
        worker_processes 4;
        worker_cpu_affinity 0001 0010 0100 1000;
        error_log logs/error.log;
        #error_log logs/error.log notice;
        #error_log logs/error.log info;pid logs/nginx.pid;
        events {
         use epoll;
         worker_connections 65535;
         multi_accept on;
        }
        http {
         include mime.types;
         default_type application/octet-stream;
         #log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
         # ‘$status $body_bytes_sent “$http_referer” ‘
         # ‘”$http_user_agent” “$http_x_forwarded_for”‘;
         #access_log logs/access.log main;
         sendfile on;
         tcp_nopush on;
         keepalive_timeout 65;
         tcp_nodelay on;
         client_header_buffer_size 4k;
         open_file_cache max=102400 inactive=20s;
         open_file_cache_valid 30s;
         open_file_cache_min_uses 1;
         client_header_timeout 15;
         client_body_timeout 15;
         reset_timedout_connection on;
         send_timeout 15;
         server_tokens off;
         client_max_body_size 10m;
         fastcgi_connect_timeout 600;
         fastcgi_send_timeout 600;
         fastcgi_read_timeout 600;
         fastcgi_buffer_size 64k;
         fastcgi_buffers 4 64k;
         fastcgi_busy_buffers_size 128k;
         fastcgi_temp_file_write_size 128k;
         fastcgi_temp_path /usr/local/nginx/nginx_tmp;
         fastcgi_intercept_errors on;
         fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;
         gzip on;
         gzip_min_length 2k;
         gzip_buffers 4 32k;
         gzip_http_version 1.1;
         gzip_comp_level 6;
         gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
         gzip_vary on;
         gzip_proxied any;
         server {
          listen 80;
          server_name localhost;
          #charset koi8-r;
          #access_log logs/host.access.log main;
          location ~* ^.+.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {
           valid_referers none blocked 192.168.154.132;
            if ($invalid_referer) {
            return 404;
            break;
           }
           access_log off;
          }
          location / {
           root html;
           index index.php index.html index.htm;
          }
          location ~* .(ico|jpe?g|gif|png|bmp|swf|flv)$ {
           expires 30d;
           #log_not_found off;
           access_log off;
          }
          location ~* .(js|css)$ {
           expires 7d;
           log_not_found off;
           access_log off;
          }
          location = /(favicon.ico|roboots.txt) {
           access_log off;
          log_not_found off;
          }
          location /status {
           stub_status on;
          }
          location ~ .*.(php|php5)?$ {
           root html;
           fastcgi_pass 192.168.154.133:9000;
           fastcgi_index index.php;
           include fastcgi.conf;
        #關閉fastcgi的緩存   fastcgi_cache cache_fastcgi;
           fastcgi_cache_valid 200 302 1h;
           fastcgi_cache_valid 301 1d;
           fastcgi_cache_valid any 1m;
           fastcgi_cache_min_uses 1;
           fastcgi_cache_use_stale error timeout invalid_header http_500;
           fastcgi_cache_key http://$host$request_uri;
          }
          #error_page 404 /404.html;
          # redirect server error pages to the static page /50x.html
          #
          error_page 500 502 503 504 /50x.html;
          location = /50x.html {
           root html;
          }
         }
        }

        nginx -s reload

        php部分

        tar zxf libmcrypt-2.5.7.tar.gz

        cd libmcrypt-2.5.7

        ./configure –prefix=/usr/local/libmcrypt && make && make install

        yum -y install libxml2-devel libcurl-devel openssl-devel bzip2-devel

        tar zxf php-5.6.27.tar.gz

        cd php-5.6.27

        ./configure –prefix=/usr/local/php –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

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

        cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

        cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

        chmod +x /etc/init.d/php-fpm

        chkconfig –add php-fpm

        chkconfig php-fpm on

        sed -i ‘s#;pid = run/php-fpm.pid#pid = run/php-fpm.pid#g’ /usr/local/php/etc/php-fpm.conf

        sed -i ‘s/listen = 127.0.0.1:9000/listen = 0.0.0.0:9000/g’ /usr/local/php/etc/php-fpm.conf

        sed -i ‘s/pm.max_children = 5/pm.max_children = 300/g’ /usr/local/php/etc/php-fpm.conf

        sed -i ‘s/pm.start_servers = 2/pm.start_servers = 10/g’ /usr/local/php/etc/php-fpm.conf

        sed -i ‘s/pm.min_spare_servers = 1/pm.min_spare_servers = 10/g’ /usr/local/php/etc/php-fpm.conf

        sed -i ‘s/pm.max_spare_servers = 3/pm.max_spare_servers = 50/g’ /usr/local/php/etc/php-fpm.conf

        service php-fpm start

        mysql部分

        rpm -e –nodeps mariadb-libs

        groupadd -r mysql

        useradd -r -g mysql -s /bin/false -M mysql
         
        tar zxf mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz

        mv mysql-8.0.11-linux-glibc2.12-x86_64 /usr/local/mysql

        mkdir /usr/local/mysql/data

        chown -R mysql:mysql /usr/local/mysql/

        ln -s /usr/local/mysql/bin/* /usr/local/bin

        cat >> /etc/my.cnf << EOF
        [client]
        socket=/usr/local/mysql/data/mysql.sock
        [mysqld]
        basedir=/usr/local/mysql
        datadir=/usr/local/mysql/data
        port=3306
        pid-file=/usr/local/mysql/data/mysql.pid
        server_id=1
        socket=/usr/local/mysql/data/mysql.sock
        log-error=/usr/local/mysql/data/mysql.err
        EOF

        mysqld –initialize –user=mysql –basedir=/usr/local/mysql/ –datadir=/usr/local/mysql/data

        cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

        chkconfig –add mysqld

        chkconfig on mysqld

        systemctl daemon-reload

        systemctl start mysqld

        memcache部分

        tar zxf libevent-2.0.22-stable.tar.gz

        cd libevent-2.0.22-stable/

        ./configure && make&& make install

        tar zxf memcached-1.4.33.tar.gz

        cd memcached-1.4.33/

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

        make && make install

        vi ~/.bash_profile

        MEMCACHED_HOME=/usr/local/memcached
        LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MEMCACHED_HOME/lib

        memcached -d -m 2048 -l 192.168.154.131 -p 11211 -u root -c 10240 -P /usr/local/memcached/memcached.pid

        firewall-cmd –permanent –add-port=11211/tcp

        firewall-cmd –reload

        在php上

        加載memcache.so(使php作為memcache的客戶端)

        tar zxf memcache-3.0.8.tgz

        cd memcache-3.0.8/

        /usr/local/php/bin/phpize

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

        make && make install

        注意:安裝完后會有類似這樣的提示: Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20131226/;記住這個路徑,需要加載到php的配置文件中。

        vim /etc/php.ini

        ##查找extension并在對應位置添加
        extension=/usr/local/php/lib/php/extensions/no-debug-zts-20131226/memcache.so
        ##在末尾添加
        session.save_handler = memcache
        session.save_path = “tcp://192.168.154.131:11211?persistent=1&weight=1&timeout=1&retry_interval=15”

        service php-fpm restart

        在nginx上

        編寫php測試頁面。vim /usr/local/nginx/html/index.php

        <?php
        phpinfo();
        ?>

        訪問http://192.168.154.132,應當可以看到memcache和session字樣。

        (成功表示php上安裝好了memcache.so)

        編寫memcache測試頁面。vim /usr/local/nginx/html/test.php

        <?php
        $memcache = new Memcache;
        $memcache->connect(‘192.168.154.131’, 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, 10) or die (“Failed to save data at the server”);
        echo “Store data in the cache (data will expire in 10 seconds)
        “;
        $get_result = $memcache->get(‘key’);
        echo “Data from the cache:
        “;
        var_dump($get_result);
        ?>

        預計會輸出四行字符,有memcache版本信息等

        (成功表示連接到了memcache服務器)

        編寫測試memcache的session共享腳本,vim /usr/local/nginx/html/session.php

        <?php
        session_start();
        if (!isset($_SESSION[‘session_time’]))
        {
          $_SESSION[‘session_time’] = time();
        }
        echo “now_time:”.time().”
        “;
        echo “session_id:”.session_id().”
        “;
        ?>

        預計輸出session_time、now_time、session_id

        使用telnet連接memcache

        telnet 192.168.154.131 11211

        然后get session_id的值,如果得到的session_time和網頁上的一樣表示session共享成功。

        在mysql上

        mysql> create database testdb1;

        mysql> use testdb1;

        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)

        mysql> grant select on testdb1.* to user@’%’ identified by ‘123456’;

        在nginx上

        編輯memcache緩存mysql測試頁面,vim /usr/local/nginx/html/test_db.php

        <?php
        $memcachehost = ‘192.168.154.131’;
        $memcacheport = 11211;
        $memcachelife = 60;
        $memcache = new Memcache;
        $memcache->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.154.134″,”user”,”123456″);
            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 “<br>”
        echo “$key”;
        echo “<br>”
        //print_r($data);
        foreach($data as $a)
        {
        echo “number is <b><font color=#FF0000>$a[id]</font></b>”;
        echo “<br>”;
        echo “name is <b><font color=#FF0000>$a[name]</font></b>”;
        echo “<br>”;
        }
        ?>

        預計會輸出memcache的key,和我們在mysql創建的數據表的數據

        (成功即表示memcache緩存到mysql的數據)

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 国产精品一二区| 久久精品无码一区二区日韩AV| 国产精品免费看久久久香蕉| 青青草原精品国产亚洲av| 久久精品国产精品亜洲毛片| 亚洲国产精品久久久久婷婷老年 | 国产精品视频免费观看| 亚洲精品无码久久久久sm| 国产韩国精品一区二区三区| 1000部精品久久久久久久久| 无码精品人妻一区二区三区免费看| 久久精品成人| 国产精品你懂的在线播放| 五月花精品视频在线观看 | 久久久精品国产sm调教网站| 欧美精品高清在线xxxx| 精品国产黑色丝袜高跟鞋| 国产成人久久久精品二区三区 | 久久99热这里只有精品国产| 国产欧美日韩综合精品一区二区三区| 四虎国产精品永久地址51| 国产精品你懂的| 国产精品美女久久久久久2018| 无码国产69精品久久久久网站 | 四虎国产精品免费久久久| 精品一区二区三区中文字幕| 国产午夜无码精品免费看动漫| 亚洲码国产精品高潮在线 | 久久精品国产国产精品四凭| 国产乱子伦精品无码专区| 国产精品亚洲专区无码WEB| 国产精品JIZZ在线观看老狼| 91精品国产麻豆国产自产在线 | 亚洲日韩国产AV无码无码精品| 久久国产乱子伦精品免费午夜| 精品久久久无码中文字幕| 久久精品亚洲福利| 中日韩产精品1卡二卡三卡| 亚洲精品无码AV中文字幕电影网站| 日韩一区二区精品观看| 宅男宅女精品国产AV天堂|