Nginx中、PHP配置、nginx結合php-fpm、memcache模塊、openresty模塊
一、PHP的原始碼編譯
1、軟體包下載
https://www.php.net/

2、編譯php
configure
./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/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
將缺少的依賴性安裝
yum install -y systemd-devel
yum install -y libxml2-devel
yum install -y sqlite-devel
yum install -y libcurl-devel
yum install libpng-devel -y
oniguruma-devel #此安裝包不在軟體倉庫中,csdn中搜索下載
rpm -ivh oniguruma-6.8.2-1.el7.x86_64.rpm
rpm -ivh oniguruma-devel-6.8.2-1.el7.x86_64.rpm
configure完成
編譯 make
安裝 make install

二、拷貝php-fpm組態檔
1、php-fpm.conf

2、www.conf
進入php-fpm.conf檔案,將注釋打開


3、php.ini

設定地區時間

4、php-fpm.service,讀取并開啟服務

注釋

三、nginx中使用php-fpm
1、修改nginx組態檔
進入nginx的配置目錄,編輯組態檔,注釋之前的設定,取消php的注釋
cd /usr/local/nginx
vim conf/nginx.conf
nginx -s reload


在html中撰寫php發布頁面,重新啟動服務,在真機瀏覽器訪問http://172.25.73.1/index.php



2、添加環境變數

四、php添加memcache功能模塊
1、軟體包準備
http://pecl.php.net/package/memcache ##網址下載

2、軟體安裝
解壓軟體包進入目錄,執行phpize,提醒缺少依賴,phpize是用來擴展php擴展模塊的,通過phpize可以建立php的外掛模塊,

安裝依賴,重新phpize
yum install autoconf -y
yum install automake.noarch -y
phpize ##擴展成功
對memcache進行原始碼編譯、make、mkae install
./configure --enable-debug
make
make install

編輯php.ini ,然后重啟服務,執行php -m可以看到memcache
cd /usr/local/lnmp/php/etc/
ls
vim php.ini
///
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ; ##在這個位置
;;;;;;;;;;;;;;;;;;;;;;
938 extension=memcache ##連接php與memcache服務
///
php -m | grep memcache ##出現memcache表示添加成功
systemctl reload php-fpm.service
php -m ##測驗php服務是否正常,可以看到memcache


3、構建nginx高速快取,添加memcache功能模塊

使用memc-nginx和srcache-nginx模塊構建高效透明的快取機制,先安裝memcached,并開啟服務,查看埠,
yum install -y memcached
systemctl start memcached.service
netstat -antlp ##11211埠
cat /etc/sysconfig/memcached ##11211埠


cd memcache目錄,拷貝檔案并編譯,最后重啟服務
cd
cd memcache-4.0.5.2/
ls
cp example.php /usr/local/nginx/html/
cp memcache.php /usr/local/nginx/html/
cd /usr/local/nginx/html/
ls
vim memcache.php
///
define('ADMIN_PASSWORD','root'); // Admin Password ##設定密碼
$MEMCACHE_SERVERS[] = '172.25.73.1:11211'; // add more as an array ##server1IP
#$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array ##注釋
///
nginx -s reload ##重啟nginx
systemctl start php-fpm.service ##開啟服務
systemctl start memcached.service ##開啟服務

此時服務配置成功,訪問172.25.73.1/memcache.php,使用 用戶名,密碼 登陸

五、配置php加載模塊openresty
基于openresty(構建高效透明的快取機制) 訪問,能將快取放在nginx中,速度更快
1、準備
nginx -s stop ##先停止nginx服務
https://openresty.org/cn/ ##軟體下載

2、軟體安裝
tar zxf openresty-1.19.3.1.tar.gz
ls
cd openresty-1.19.3.1/
ls
./configure --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
make
make install
3、軟體配置
cd /usr/local/openresty/nginx.conf
vim nginx.conf
///
user nginx;
worker_processes auto;
events {
worker_connections 65535;
}
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;
}
///
cd ..
cd html/
cp /usr/local/nginx/html/example.php .
cp /usr/local/nginx/html/index.php .
/usr/local/openresty/nginx/sbin/nginx -t
/usr/local/openresty/nginx/sbin/nginx -s reload
ERROR

/usr/local/openresty/nginx/sbin/nginx -s reload
報錯 nginx: [error] invalid PID number "" in "/usr/local/openresty/nginx/logs/nginx.pid"
解決:使用nginx -c的引數指定nginx.conf檔案的位置,conf檔案的位置在nginx -t的回傳資訊中
/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
/usr/local/openresty/nginx/sbin/nginx -s reload
4、更改配置改進一步來提升性能
cd /usr/local/openresty/nginx/conf
vim nginx.conf
///
http {
upstream memcache {
server 127.0.0.1:11211;
keepalive 512; ##保持512個不立即關閉的連接用于提升性能
}
include mime.types;
default_type application/octet-stream;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /memc {
internal; ##表示只接受內部訪問
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key $query_string; ##使用內置的$query_string來作為key
set $memc_exptime 300; ##表示快取失效時間
memc_pass memcache;
}
///
/usr/local/openresty/nginx/sbin/nginx -t
/usr/local/openresty/nginx/sbin/nginx -s reload


5、在真機進行壓力測驗
ab -c10 -n 5000 http://172.25.73.1/example.php
///
Complete requests: 5000
Failed requests: 0
Total transferred: 1425000 bytes
HTML transferred: 580000 bytes
///
##可以看到壓測速度很快,且沒有報錯,速度很快,
ab -c10 -n 5000 http://172.25.73.1/index.php
///
Complete requests: 5000
Failed requests: 492
(Connect: 0, Receive: 0, Length: 492, Exceptions: 0)
Total transferred: 358824457 bytes
HTML transferred: 357979457 bytes
///
##傳輸量大幅度提升
六、tomcat + memcache 高可用








轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/297800.html
標籤:其他
上一篇:面經: 計算機基礎
