Linux 安裝基於(PHP5.5)memcache擴展


需安裝php7版本的參考:http://www.yiichina.com/topic/6636  yii2 下使用memcached(非memcache) 

http://www.yiichina.com/topic/6635      php7.0下 安裝 memcached 和 php擴展memcached 

---------------------------------------------------------------

 

一. memcache服務器端

下載地址:http://memcached.org/

安裝memcached,同時需要安裝中指定libevent的安裝位置

tar zxvf memcached-1.2.0.tar.gz
cd memcached-1.2.0.tar
./configure –with-libevent=/usr
make
make install

或直接yum apt-get安裝

 啟動服務 :

./memcached -d -m 10    -u root

 

memcached -d -m 256 -p 11211 -u root

 

參數說明

-d選項是啟動一個守護進程,
-m是分配給Memcache使用的內存數量,單位是MB,我這里是10MB,
-u是運行Memcache的用戶,我這里是root,
-l是監聽的服務器IP地址,
-p是設置Memcache監聽的端口,
-c選項是最大運行的並發連接數,默認是1024,
-P是設置保存Memcache的pid文件,


二 . 安裝php5.5 memcache擴展

這里版本為php5.5.25

下載地址 http://pecl.php.net/package/memcache

tar vxzf memcache-2.2.1.tgz
cd memcache-2.2.1
/usr/local/php/bin/phpize ./configure –enable-memcache –with-php-config=/usr/local/php/bin/php-config make make install

問題來了,報錯:

[root@iaaa memcache-2.2.1]# ./configure –enable-memcache –with-php-config=/usr/local/php/bin/php-config
configure: WARNING: you should use --build, --host, --target
configure: WARNING: invalid host type: –enable-memcache
configure: error: invalid variable name: `–with-php-config'

解決辦法: 這行代碼需要你手動輸入,直接復制粘貼就會出現這種錯誤,   參考來自: http://www.wdlinux.cn/bbs/thread-7007-1-1.html

安裝完后會有類似這樣的提示

Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/

然后修改php.ini, 添加extension

extension=memcache.so

查找php.ini  執行phpinfo()

修改完成后重啟nginx

service  nginx restart

問題來了:

重啟nginx 后phpinfo() 顯示沒有加載memcache模塊

google發現要需要 php-fpm 來重新加載配置文件php.ini

然后呢安裝網上說的

/php-fpm 還有其它參數 ,包括start|stop | quit | restart | reload | logrotate 使用 PHP-FPM 來控制 PHP-CGI 的 FastCGI 進程

--start   啟動 PHP 的 fastcgi 進程
--stop   強制終止 PHP 的 fastcgi 進程
--quit    平滑終止 php 的 fastcgi 進程
--restart  重啟 php 的 fastcgi 進程
--reload   平滑重新加載 php 的 php.ini
--logrotate   重新啟用 log 文件

 

php5.5的php-fpm 參數

Usage: php-fpm [-n] [-e] [-h] [-i] [-m] [-v] [-t] [-p <prefix>] [-g <pid>] [-c <file>] [-d foo[=bar]] [-y <file>] [-D] [-F]
  -c <path>|<file> Look for php.ini file in this directory
  -n               No php.ini file will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -h               This help
  -i               PHP information
  -m               Show compiled in modules
  -v               Version number
  -p, --prefix <dir>
                   Specify alternative prefix path to FastCGI process manager (default: /usr/local/php).
  -g, --pid <file>
                   Specify the PID file location.
  -y, --fpm-config <file>
                   Specify alternative path to FastCGI process manager config file.
  -t, --test       Test FPM configuration and exit
  -D, --daemonize  force to run in background, and ignore daemonize option from config file
  -F, --nodaemonize
                   force to stay in foreground, and ignore daemonize option from config file
  -R, --allow-to-run-as-root
                   Allow pool to run as root (disabled by default)

所以應該:

 

service php-fpm stop 
./php-fpm -c /usr/local/php/etc/php2.ini

完成后在看phpinif()

 

顯示的memcache模塊:

 

 

windows中memcached 啟動服務:

 

 

memcache 未授權訪問漏洞

修復方案:

因memcache無權限控制功能,所以需要用戶對訪問來源進行限制。

方案一:

如果memcache沒有在外網開放的必要,可在memcached啟動的時候指定綁定的ip地址為 127.0.0.1。例如:

memcached -d -m 1024 -u root -l 127.0.0.1 -l 192.168.1.102 -p 11211 -c 1024 -P /tmp/memcached.pid

其中 -l 參數指定為地址, 可以指定多個地址用逗號分開也可以使用- l多次

方案二:(注意:請謹慎配置iptables規則)

如果memcache服務需要對外提供服務,則可以通過iptables進行訪問控制。

iptables -A INPUT -p tcp -s 192.168.0.2 --dport 11211 -j ACCEPT

上述規則的意思是只允許192.168.0.2這個ip對11211端口進行訪問。

參考:https://help.aliyun.com/knowledge_detail/37553.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM