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

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中搜索下載
yum install oniguruma-6.8.2-1.el7.x86_64.rpm
yum install 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 高可用
1、在server2 與 server3 安裝 jdk 與 tomcat



向 server3 部署 tomcat 與 jdk 的操作
bin/shutdown.sh ##關閉服務進行scp
netstat -antlp
cd /usr/local/
scp -r tomcat/ server3:/usr/local/
scp jdk-8u121-linux-x64.rpm server3:
在server3的操作
rpm -ivh jdk-8u121-linux-x64.rpm ##安裝
cd /usr/local/
cd tomcat/
bin/startup.sh ##開啟服務
netstat -antlp ##查看埠

2、重新啟動server2 的 tomcat
在瀏覽器使用172.25.73.2:8080訪問
在瀏覽器使用172.25.73.3:8080訪問

3、tomcat+Nginx
在server1中
cd /usr/local/nginx/conf
vim nginx.conf
http {
upstream tomcat {
server 172.25.73.2:8080;
server 172.25.73.3:8080;
}
}
location / {
root html;
index index.html index.htm index.php index.jsp;
}
server{
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
}
nginx -t
nginx -s reload
在瀏覽器使用172.25.73.1/index.jsp訪問到此頁面

在server2 的==/usr/local/tomcat/webapps/ROOT添加一個test.jsp頁面
在server3 的/usr/local/tomcat/webapps/ROOT==添加一個test.jsp頁面
使用瀏覽器訪問,并添加資料,查看日志
cd /usr/local/tomcat/logs
cat catalina.out
可以看到剛剛在網頁添加的資料,此時將server2的 tomcat 關閉,繼續輸入新的資料,Nginx的健康檢測會快速切換到server3,不會影響輸入資料,但是剛剛在網頁輸入的資料丟失


3、tomcat結合memcache實作實作session共享
為了解決上述問題,在實際生產中都是tomcat結合memcache實作實作session共享

當服務開啟時:
server2的tomcat將資料保存在server3的session中
server3的tomcat將資料保存在server2的session中
當server2的tomcat關閉,server3將資料保存在server3的session中
當server3的tomcat關閉,server2將資料保存在server2的session中
1、在server2與server3安裝memcached
yum install -y memcached
systemctl start memcached
2、配置server2 與 server3
server2中
cd /usr/local/tomcat/conf
vim context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.73.2:11211,n2:172.25.72.3:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
/
下載jar包 如圖下
cd /usr/local/tomcat/lib

cd /usr/local/tomcat ##關閉重啟服務
bin/shutdown.sh ##關閉服務
bin/startup.sh ## 在開啟服務
tail -f logs/catalina.out ##查看日志 出現組態檔[n1]代表成功


在server3中
cd /usr/local/tomcat/conf
vim context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.73.2:11211,n2:172.25.72.3:11211"
failoverNodes="n2"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
/
下載jar包 如圖上
cd /usr/local/tomcat/lib
cd /usr/local/tomcat ##關閉重啟服務
bin/shutdown.sh ##關閉服務
bin/startup.sh ## 在開啟服務
tail -f logs/catalina.out ##查看日志 出現[n2]代表成功


訪問頁面建立user1、user2、、、、紅色標記的ID可以用telnet查詢
要在server2、server3中下載
cd /usr/local/tomcat
yum install -y telnet
telnet localhost 11211
get ID 查詢

此時關閉server2的tomcat,在網頁繼續輸入資料,然后查詢,資料沒有丟失并且在寫入

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/297973.html
標籤:其他
