nginx+php+memcache高速缓存openresty)
一、php源碼編譯
1.軟件下載:軟件下載 (這里使用的時php-7.4.6版本)
如果系統已經裝了php請先卸載,以免與源碼編譯沖突
[root@server1 ~]# yum install bzip2 -y [root@server1 ~]# tar jxf php-7.4.6.tar.bz2 [root@server1 ~]# cd php-7.4.6/ [root@server1 php-7.4.6]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-curl --with-iconv --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-mysqli --with-pdo-mysql --disable-debug --enable-sockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp --enable-gd --enable-exif --enable-mbstring --enable-bcmath --with-fpm-systemd [root@server1 php-7.4.6]# yum install systemd-devel.x86_64 -y [root@server1 php-7.4.6]# yum install libxml2-devel.x86_64 -y [root@server1 php-7.4.6]# yum install sqlite-devel.x86_64 -y [root@server1 php-7.4.6]# yum install libcurl-devel.x86_64 -y [root@server1 php-7.4.6]# yum install /root/gd-devel-2.0.35-26.el7.x86_64.rpm -y [root@server1 php-7.4.6]# yum install /root/oniguruma-devel-6.8.2-1.el7.x86_64.rpm /root/oniguruma-6.8.2-1.el7.x86_64.rpm -y編譯過程中會出現一些依賴性問題一個一個解決即可
編譯
2.拷貝php-fpm配置文件:
[root@server1 php-7.4.6]# cd /usr/local/php/ [root@server1 php]# cd etc/ [root@server1 etc]# cp php-fpm.conf.default php-fpm.conf [root@server1 etc]# vim php-fpm.conf [root@server1 etc]# cd php-fpm.d/ [root@server1 php-fpm.d]# cp www.conf.default www.conf [root@server1 php-7.4.6]# cp php.ini-production /usr/local/php/etc/php.ini [root@server1 php-7.4.6]# cd sapi/fpm/ [root@server1 fpm]# cp php-fpm.service /etc/systemd/system/ [root@server1 fpm]# systemctl daemon-reload [root@server1 etc]# vim /etc/systemd/system/php-fpm.service3.修改php-fpm啟動文件:
二、nginx結合php-fpm
1.修改nginx配置文件:
[root@server1 etc]# vim /usr/local/nginx/conf/nginx.conf location ~ \.php$ {root html;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;include fastcgi.conf;}[root@server1 html]# nginx -s reload
添加環境變量
[root@server1 ~]# vim ~/.bash_profile [root@server1 ~]# source ~/.bash_profile測試:
三、php添加memcache功能模塊(高速緩存)
memcache介紹
memcache是一套分布式的高速緩存系統,由LiveJournal的Brad Fitzpatrick開發,但目前被許多網站使用以提升網站的訪問速度,尤其對于一些大型的、需要頻繁訪問數據庫的網站訪問速度提升效果十分顯著 [1] 。這是一套開放源代碼軟件,以BSD license授權發布。
軟件下載
1.軟件安裝:
這里用到的是memcache-4.0.5.2.tgz
[root@server1 ~]# tar zxf memcache-4.0.5.2.tgz [root@server1 ~]# cd memcache-4.0.5.2/ [root@server1 memcache-4.0.5.2]# phpize [root@server1 memcache-4.0.5.2]# yum install autoconf [root@server1 memcache-4.0.5.2]# phpize [root@server1 memcache-4.0.5.2]# ./configure [root@server1 memcache-4.0.5.2]# make [root@server1 memcache-4.0.5.2]# make installphpize是用來擴展php擴展模塊的,通過phpize可以建立php的外掛模塊
2.配置php加載模塊
[root@server1 no-debug-non-zts-20190902]# cd /usr/local/php/etc/ [root@server1 etc]# vim php.ini extension=memcache.so [root@server1 etc]# systemctl reload php-fpm.service [root@server1 etc]# php -m |grep memcache memcache加入memcache模塊
配置memcache登陸信息
測試
此時的命中率為93,再次訪問時,查看命中率的改變:(即進入example.php界面刷新,然后再次進入memcache.php界面,查看命中率):此時,命中率有了一定的提高,意思是,所讀取的內容是從緩存中進行拿取的,提高了速度,也減輕了后臺服務器的壓力
四、構建nginx高速緩存
使用memc-nginx和srcache-nginx模塊構建高效透明的緩存機制
傳統緩存策略
高效緩存策略
1.軟件下載(openresty)
OpenResty(又稱:ngx_openresty) 是一個基于 NGINX 的可伸縮的 Web 平臺,由中國人章亦春發起,提供了很多高質量的第三方模塊。
OpenResty 是一個強大的 Web 應用服務器,Web 開發人員可以使用 Lua 腳本語言調動 Nginx 支持的各種 C 以及 Lua 模塊,更主要的是在性能方面,OpenResty可以 快速構造出足以勝任 10K 以上并發連接響應的超高性能 Web 應用系統。
360,UPYUN,阿里云,新浪,騰訊網,去哪兒網,酷狗音樂等都是 OpenResty 的深度用戶。
軟件下載
軟件安裝:
# wget https://openresty.org/package/rhel/openresty.repo # mv openresty.repo /etc/yum.repos.d/ # yum install -y openresty # rpm -ql openresty-1.19.3.1-1.el7.x86_64 ##查看文件配置信息 # ps aux | grep nginx ##不能存在nginx 的進程,如果有的話,kill掉軟件配置:
# cd /usr/local/openresty/nginx/conf # vim nginx.conf 其余部分和nginx 配置一樣,以下僅展示不同處 upstream memcache {server 127.0.0.1:11211;keepalive 512; //保持512個不立即關閉的連接用于提升性能} location /memc {internal; //表示只接受內部訪問memc_connect_timeout 100ms;memc_send_timeout 100ms;memc_read_timeout 100ms;set $memc_key $query_string; //使用內置的$query_string來作為keyset $memc_exptime 300; //表示緩存失效時間memc_pass memcache;} location ~ \.php$ {set $key $uri$args; srcache_fetch GET /memc $key; srcache_store PUT /memc $key;root html;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;include fastcgi.conf;} 當所請求的uri以“.php”結尾時,首先到memcache中查詢有沒有以$uri$args為key的數據,如果有則直接返回;否則,執行location的邏輯,如果返回的http狀態碼為200,則在輸出前以$uri$args為key,將輸入結果存入memcache。 # systemctl start openresty # vim /usr/local/openresty/nginx/html/index.php <?php phpinfo() ?>測試:
ab -c 100 -n 10000 http://172.25.1.1/index.php
我們可以看到壓測速度很快,且沒有報錯,速度很快。
通過來回切換觀察兩者速度,及出錯數目。
nginx結合lua
location @client { proxy_pass http://172.25.0.3; } location ~ \.php$ { default_type text/html; content_by_lua 'ngx.say("this is westos.org")'; access_by_lua ' if ngx.var.remote_addr == "172.25.0.250" then ngx.exec("@client") end '; }總結
以上是生活随笔為你收集整理的nginx+php+memcache高速缓存openresty)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HAProxy实现负载均衡及高可用集群(
- 下一篇: Codis安装与部署