目錄
1、memadmin工具簡介
2、nginx及php環境安裝
3、memadmin工具部署
4、memadmin工具的簡單運用
5、額外的配置優化
6、總結
1、memadmin工具簡介
MemAdmin是一款可視化的Memcached管理與監控工具,使用PHP開發,體積小,操作簡單。
主要功能:
服務器參數監控:STATS、SETTINGS、ITEMS、SLABS、SIZES實時刷新
服務器性能監控:GET、DELETE、INCR、DECR、CAS等常用操作命中率實時監控
支持數據遍歷,方便對存儲內容進行監視
支持條件查詢,篩選出滿足條件的KEY或VALUE
數組、JSON等序列化字符反序列顯示
兼容memcache協議的其他服務,如Tokyo Tyrant (遍歷功能除外)
支持服務器連接池,多服務器管理切換方便簡潔
2、nginx及php環境安裝
2.1、安裝nginx
root@com:~# uname -r 3.16.0-4-amd64 root@com:~# cat /etc/issue Debian GNU/Linux 8 \n \l root@test1:~# apt-get install nginx root@test1:~# chkconfig --list nginx nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
瀏覽器訪問可見nginx的默認頁面,如下:
2.2、php環境部署
root@test1:~# apt-get install php5-fpm root@test1:~# dpkg -L php5-fpm /. /etc /etc/php5 /etc/php5/fpm /etc/php5/fpm/conf.d /etc/php5/fpm/pool.d /etc/php5/fpm/pool.d/www.conf #這個是php5-fpm的配置文件 /etc/php5/fpm/php-fpm.conf /etc/logrotate.d /etc/logrotate.d/php5-fpm /etc/init /etc/init/php5-fpm.conf /etc/init.d /etc/init.d/php5-fpm /usr /usr/sbin /usr/sbin/php5-fpm /usr/lib /usr/lib/php5 /usr/lib/php5/20131226 /usr/lib/php5/php5-fpm-checkconf /usr/lib/php5/php5-fpm-reopenlogs /usr/share /usr/share/man /usr/share/man/man8 /usr/share/man/man8/php5-fpm.8.gz /usr/share/lintian /usr/share/lintian/overrides /usr/share/lintian/overrides/php5-fpm /usr/share/php5 /usr/share/php5/sapi /usr/share/php5/sapi/fpm /usr/share/bug /usr/share/bug/php5-fpm /usr/share/bug/php5-fpm/script /usr/share/bug/php5-fpm/control /usr/share/doc /lib /lib/systemd /lib/systemd/system /lib/systemd/system/php5-fpm.service /usr/share/doc/php5-fpm
root@test1:~# vim /etc/php5/fpm/pool.d/www.conf #listen = /var/run/php5-fpm.sock 註釋此行,這不是必須的,只是我習慣讓php5-fpm監聽在一個端口而不是sock上 listen = 9000 增加此行使php5-fpm監聽在9000端口 root@test1:~# /etc/init.d/php5-fpm restart [ ok ] Restarting php5-fpm (via systemctl): php5-fpm.service. root@test1:~# ss -tnl | grep 9000 LISTEN 0 128 *:9000 *:*
root@test1:~# vim /etc/nginx/sites-enabled/default #修改nginx配置,使其能把php的請求轉發到php5-fpm,在“#location ~ \.php$ {”代碼塊的下方增加如下代碼: location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; include fastcgi_params; } nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful root@test1:~# vim /var/www/html/info.php <?php phpinfo(); ?> root@test1:~# nginx -t root@test1:~# /etc/init.d/nginx restart [ ok ] Restarting nginx (via systemctl): nginx.service.
瀏覽器打開php的測試面試,如下:
3、memadmin工具部署及簡單運用
root@test1:~# ls memadmin-1.0.12.tar.gz root@test1:~# tar xf memadmin-1.0.12.tar.gz -C /var/www/html/ root@test1:~# cd /var/www/html/ root@test1:/var/www/html# ls index.nginx-debian.html info.php memadmin
訪問memadmin測試,如下:
報錯了,根據錯誤提示安裝memcache的擴展支持:
root@test1:/var/www/html# apt-get install php5-memcache root@test1:/var/www/html# dpkg -L php5-memcache /. /usr /usr/lib /usr/lib/php5 /usr/lib/php5/20131226 /usr/lib/php5/20131226/memcache.so #庫文件在這裏 /usr/share /usr/share/doc /usr/share/doc/php5-memcache /usr/share/doc/php5-memcache/examples /usr/share/doc/php5-memcache/examples/memcache.php.gz /usr/share/doc/php5-memcache/examples/example.php /usr/share/doc/php5-memcache/changelog.Debian.gz /usr/share/doc/php5-memcache/copyright /usr/share/doc/php5-memcache/memcache.php.gz /usr/share/doc/php5-memcache/changelog.gz /usr/share/doc/php5-memcache/package.xml.gz /usr/share/doc/php5-memcache/README.gz /usr/share/doc/php5-memcache/CREDITS /usr/share/doc/php5-memcache/example.php /usr/share/php /usr/share/php/.registry /usr/share/php/.registry/.channel.pecl.php.net /usr/share/php/.registry/.channel.pecl.php.net/memcache.reg /etc /etc/php5 /etc/php5/mods-available /etc/php5/mods-available/memcache.ini
root@test1:/var/www/html# vim /etc/php5/fpm/php.ini #搜索“extension”關鍵字,在“; extension_dir = "ext" ”下增加下邊的代碼 extension_dir = "/usr/lib/php5/20131226" extension = memcache.so root@test1:/var/www/html# /etc/init.d/php5-fpm restart [ ok ] Restarting php5-fpm (via systemctl): php5-fpm.service.
再次訪問測試頁面,如下,有memcache的相關信息:
至此,memadmin工具部署完成。
4、memadmin工具的簡單運用
默認時memadmin工具的登陸用戶名及密碼都爲admin,可以在配置文件中進行更改,如下:
root@test1:/var/www/html# pwd /var/www/html root@test1:/var/www/html# vim memadmin/config.php <?php if (!defined('IN_MADM')) exit(); $config['user'] = "admin"; // your username $config['passwd'] = "admin"; // your password .........
登陸memadmin後是如下頁面:
點擊“添加”後,memcached就被添加到“服務器連接列表”中,如下:
再點擊“開始管理”後,就可察看memcached服務器的監控信息,如下圖:
接下來就摸索着使用這工具吧!
5、額外的配置優化
現在,我們是可以盡情的使用memadmin這工具了,但此服務器的主要功能是提供memcached服務,而memadmin只是一個對memcached的管理工具而已,使用頻率並不是特別高,所以,分配給nginx,php5-fpm的資源只需要一點即可,特別是在你服務器資源很緊張的情況下更應該對nginx和php5-fpm這兩個服務進行優化。
5.1、nginx優化
先來看看默認時nginx的工作特性,如下:
root@test1:~# pgrep nginx 11748 11750 11751 11752 11753 root@test1:~# ps aux | grep nginx root 11748 0.0 0.3 91172 3064 ? Ss 12:25 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; www-data 11750 0.0 0.4 91816 4776 ? S 12:25 0:01 nginx: worker process www-data 11751 0.0 0.4 91828 5028 ? S 12:25 0:00 nginx: worker process www-data 11752 0.0 0.3 91476 3752 ? S 12:25 0:01 nginx: worker process www-data 11753 0.0 0.3 91476 3752 ? S 12:25 0:01 nginx: worker process root 12909 0.0 0.2 12948 2196 pts/1 S+ 15:53 0:00 grep nginx #默認時,nginx有一個主進程,4個worker進程
root@test1:~# ss -tnl | grep 80 LISTEN 0 128 *:80 *:* LISTEN 0 128 :::80 :::* #默認時,nginx監聽了IPV6的地址
針對我這裏的環境,nginx只需要開啓一個worker進程即可,對IPV6地址的監聽也沒有必要,所以做如下修改:
root@test1:~# find /etc/nginx/* -type f | xargs grep "worker_processes" /etc/nginx/nginx.conf:worker_processes 4; root@test1:~# vim /etc/nginx/nginx.conf worker_processes 1; #修改爲“1” root@test1:~# find /etc/nginx/* -type f | xargs grep "listen 80" /etc/nginx/sites-available/default:listen 80 default_server; /etc/nginx/sites-available/default:#listen 80; root@test1:~# vim /etc/nginx/sites-available/default ..... server { listen 80 default_server; #listen [::]:80 default_server; #註釋此行 root@test1:~# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful root@test1:~# /etc/init.d/nginx reload [ ok ] Reloading nginx configuration (via systemctl): nginx.service. 優化後的結果如下: root@test1:~# ps aux | grep nginx root 11748 0.0 0.4 91280 5000 ? Ss 12:25 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; www-data 13031 0.0 0.3 91600 3808 ? S 16:00 0:00 nginx: worker process root 13052 0.0 0.2 12948 2064 pts/1 S+ 16:01 0:00 grep nginx root@test1:~# ss -tnl | grep 80 LISTEN 0 128 *:80 *:*
5.2、php優化
在這裏針對php的優化,我們可以控制php5-fpm的工作進程數來達到節約資源的目的,最開始在“/etc/php5/fpm/php-fpm.conf”文件是找了半天都沒有找到相關的配置選項,最後還是動用強大的find命令找到了相關的配置文件。
先來看看默認時php的啓動進程數:
root@test1:~# ps aux | grep php root 11946 0.0 1.3 93540 13996 ? Ss 12:32 0:01 php-fpm: master process (/etc/php5/fpm/php-fpm.conf) www-data 11949 0.0 1.0 94124 10720 ? S 12:32 0:00 php-fpm: pool www www-data 11950 0.0 1.1 93980 12088 ? S 12:32 0:00 php-fpm: pool www root 13124 0.0 0.2 12948 2204 pts/1 S+ 16:06 0:00 grep php
默認時php也開啓了兩個工作進程,這裏只需要一個即可,做如下操作:
root@test1:~# find /etc/php5/* -type f | xargs grep "max_children" #查找有"max_children"關鍵字的文件 /etc/php5/fpm/pool.d/www.conf:; static - a fixed number (pm.max_children) of child processes; /etc/php5/fpm/pool.d/www.conf:; pm.max_children - the maximum number of children that can /etc/php5/fpm/pool.d/www.conf:; pm.max_children - the maximum number of children that /etc/php5/fpm/pool.d/www.conf:pm.max_children = 5
關於Php的優化參數有如下幾個:
pm、pm.max_children、pm.start_servers、pm.min_spare_servers、pm.max_spare_servers
pm = dynamic #對於專用服務器,pm可以設置爲static。
#如何控制子進程,選項有static和dynamic。如果選擇static,則由pm.max_children指定固定的子進程數。如果選擇dynamic,則由下開參數決定:
pm.max_children #,子進程最大數
pm.start_servers #,啓動時的進程數
pm.min_spare_servers #,保證空閒進程數最小值,如果空閒進程小於此值,則創建新的子進程
pm.max_spare_servers #,保證空閒進程數最大值,如果空閒進程大於此值,此進行清理
在這裏,我設置的取值如下:
root@test1:~# vim /etc/php5/fpm/pool.d/www.conf ..... pm = static #把pm值改爲static,默認是dynamic pm.max_children = 1 #把pm.max_clildren的值修改爲“1”,默認是“5" root@test1:~# /etc/init.d/php5-fpm restart [ ok ] Restarting php5-fpm (via systemctl): php5-fpm.service. root@test1:~# ps aux | grep php-fpm root 13235 0.0 1.3 93524 14016 ? Ss 16:29 0:00 php-fpm: master process (/etc/php5/fpm/php-fpm.conf) www-data 13238 0.0 0.6 93524 6428 ? S 16:29 0:00 php-fpm: pool www root 13258 0.0 0.1 12944 1984 pts/1 S+ 16:29 0:00 grep php-fpm #現在就只有一個php-fpm的工作進程了
通過觀察,一個nginx的worker進程佔用約4MB內存,一個php-fpm佔用約6.5MB內存,通過計算優化後可節約內存=4MB*4+6.5=22.5MB的內存大小。
6、總結
現在公司的系統環境慢慢在從Centos向Debian上遷移,對從沒有接觸過Debian系統的我來說,還需要一個適應過程,通過這段時間的使用,發現Debian與Centos在使用上還是有許多的不同,這次搭建php環境時就遇到一些問題。
MemAdmin中文界面,操作十分簡單,但我在使用中發現幾個問題,比如:只能寫入數據,而不能讀取數據,對狀態監控時的刷新時間間隔不生效等。
還有一款簡潔的memcached的狀態監視工具也不錯,叫memcachephp-master,能實現獲取KEY所對應的VALUE值,正好彌補我這裏MemAdmin不能獲取值的不足。此工具部署也很簡單,此處就不再演示。