文章目錄
- 1.鏈接nginx與php
- 2.整合資料庫
- 2.1 添加php命令至全域
- 2.2 用phpMyAdmin測驗資料庫
- 3.添加memcache快取模塊
- 4.構建nginx高速快取
- 4.1 配置openresty
- 4.2 構建nginx高效快取
- 4.3結果對比
- 5.nginx反向代理
- 6.nginx平滑升級
- 6.1 升級
- 6.2 降級
- 7.集群
- 8.開啟安全性網頁
- 9. nginx不同server權重
本文需要上節博客內容作為基礎
1.鏈接nginx與php
進入fpm路徑:cd /root/php-7.4.12/sapi/fpm
拷貝模板檔案:cp init.d.php-fpm /etc/init.d/php-fpm
給予執行權限:chmod +x /etc/init.d/php-fpm
執行fpm程式:/etc/init.d/php-fpm
進入目錄:cd /usr/local/php/etc/
復制php-fpm組態檔:cp php-fpm.conf.default php-fpm.conf
修改組態檔:vim php-fpm.conf

進入目錄:/usr/local/php/etc/php-fpm.d
復制:cp www.conf.default www.conf
編輯:vim www.conf

進入目錄:cd php-7.4.12/
拷貝php生產環境檔案:cp php.ini-production /usr/local/php/etc/php.ini
啟動php-fpm:/etc/init.d/php-fpm start
編輯nginx組態檔:
cd /usr/local/nginx/conf/
vim nginx.conf
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html index.htm;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
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;
}
添加nginx到systemd
此處參考官方檔案
進入目錄cd /usr/lib/systemd/system
添加檔案:vim nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重啟deamon:systemctl daemon-reload
啟動nginx:systemctl enable nginx.service
進入目錄:cd /etc/init.d/
以chkcongfig設定啟動(默認35啟動):chkconfig --level 35 php-fpm on

關閉php-fpm:/etc/init.d/php-fpm stop
進入目錄:cd php-7.4.12/sapi/fpm/
復制php服務端檔案模板:cp php-fpm.service /usr/lib/systemd/system
修改檔案vim php-fpm.service:
注銷一行:

撰寫測驗頁:cd /usr/local/nginx/html/
vim index.php
<?php
phpinfo()
?>
重新加載:systemctl daemon-reload
啟動服務:systemctl start php-fpm.service
開機自啟:systemctl enable php-fpm.service
測驗:
2.整合資料庫
2.1 添加php命令至全域
打開資料庫:etc/init.d/mysqld start
將php命令添加至全域:
cd
vim .bash_profile
#添加
PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin:/usr/local/mysql/bin:/usr/local/php/bin
更新命令:source .bash_profile
此時php -m可以顯示相關命令:

2.2 用phpMyAdmin測驗資料庫
進入目錄:cd /usr/local/php/etc/
編輯vim php.ini:
#修改添加
pdo_mysql.default_socket=/usr/local/mysql/data/mysql.sock
mysqli.default_socket = /usr/local/mysql/data/mysql.sock
重啟:systemctl reload php-fpm
安裝zip解壓檔案:yum install unzip -y
準備phpMyAdmin檔案:phpMyAdmin-5.0.2-all-languages.zip
解壓:unzip phpMyAdmin-5.0.2-all-languages.zip
移動至發布頁:mv phpMyAdmin-5.0.2-all-languages /usr/local/nginx/html/phpadmin
進入目錄:cd /usr/local/mysql/
給data添加權限:chmod 755 data
測驗:


3.添加memcache快取模塊
準備檔案:memcache-4.0.5.2.tgz
解壓:tar zxf memcache-4.0.5.2.tgz
安裝環境:yum install -y autoconf
檢測安裝環境:phpize
編譯:./configure --enable-memcache
make
make install

進入目錄:cd /usr/local/php/etc/
添加memcache:vim php.ini

重新加載:systemctl reload php-fpm
此時完成了php與memcache.so模塊之間的鏈接,還需安裝memcache程式:yum install -y memcached
啟動memcache:systemctl start memcached

memcache默認使用64M記憶體作為快取,最大可設定為1024M
進入編譯目錄,復制一個樣本檔案:cp example.php /usr/local/nginx/html/
編輯此檔案vim example.php:

進入編譯目錄,復制主組態檔:cp memcache.php /usr/local/nginx/html
編輯此檔案:
$VERSION='$Id$';
define('ADMIN_USERNAME','admin'); // Admin Username
define('ADMIN_PASSWORD','lee'); // Admin Password
define('DATE_FORMAT','Y/m/d H:i:s');
define('GRAPH_SIZE',200);
define('MAX_ITEM_DUMP',50);
$MEMCACHE_SERVERS[] = 'localhost:11211'; // add more as an array
#$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array
瀏覽器訪問:

4.構建nginx高速快取
以下步驟需完成上步
4.1 配置openresty
準備:oniguruma-6.8.2-1.el7.x86_64.rpm
在根目錄解壓:tar zxf openresty-1.19.3.1.tar.gz
編譯二進制檔案:gmake
安裝:gmake install
此處需要先停止nginx:systemctl stop nginx
確保nginx已經停止后,進入該目錄下,
cd /usr/local/openresty/nginx/conf/
vim nginx.conf
#修改以下部分:
user nginx nginx;
worker_processes auto;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
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;
}
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html index.htm;
}
檢查組態檔有無語法錯誤:/usr/local/openresty/nginx/sbin/nginx -t

開啟openresty:/usr/local/openresty/nginx/sbin/nginx

4.2 構建nginx高效快取

進入openresty發布頁:cd /usr/local/openresty/nginx/html
復制模板檔案cp ~/memcache-4.0.5.2/example.php .
進入配置目錄檔案:cd /usr/local/openresty/nginx/conf
4.3結果對比
未開啟高效快取
首先設定nginx組態檔vim nginx.conf:


在真機上檢測未開啟nginx快取時的處理速度:ab -c10 -n10000 http://172.25.38.1/example.php

可以看到未開啟高效快取時每秒處理只有2300次左右
開啟高效快取
設定nginx組態檔vim nginx.conf:
#此處只列出修改處
# cd /usr/local/openresty/nginx/conf
# vim nginx.conf
upstream memcache {
server 127.0.0.1:11211;
keepalive 512;
}
location /memc {
internal;
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key $query_string;
set $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;
}
檢測語法錯誤:/usr/local/openresty/nginx/sbin/nginx -t
重新加載:/usr/local/openresty/nginx/sbin/nginx -s reload
在真機測驗速度:ab -c10 -n10000 http://172.25.38.1/example.php

5.nginx反向代理
關閉openresty代理的nginx:/usr/local/openresty/nginx/sbin/nginx -s stop
編輯nginx組態檔:vim /etc/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2;
worker_cpu_affinity 01 10;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
upstream westos {
server 172.25.38.2:80;
server 172.25.38.3:80;
}
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
修改系統配置:vim /etc/security/limits.conf

準備兩個虛擬機server2,server3,安裝好httpd;
分別注釋兩個服務端主頁:
echo server2 > /var/www/html/index.html
echo server3 > /var/www/html/index.html
啟動兩個的服務端的httpd:systemctl enable --now httpd
在真機上決議nginx反向代理服務器,查看是否負載均衡:

6.nginx平滑升級
6.1 升級
取消debug除錯vim auto/cc/gcc:
# debug
#CFLAGS="$CFLAGS -g"
配置:./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
編譯:make
查看當前nginx版本:nginx -v

進入目錄:cd /usr/local/nginx/sbin/
備份:mv nginx nginx.old
進入新版本目錄:cd ~/nginx-1.19.1/objs/
轉移新版本至目錄:cp nginx /usr/local/nginx/sbin/
檢測本地nginx版本:curl -I localhost
抓取nginx行程id:ps ax |grep nginx

kill -USR2 18415,此時還未升級,但是多了nginx行程

kill -WINVH 18415,升級完成


6.2 降級
改名:
mv nginx nginx.new
mv nginx.old nginx

kill -HUB 18415,多出了2個子行程:

kill -WINCH 26366,此處的數字為要關閉的新版本,關閉后只剩舊版本:


7.集群
停止所有nginx程式后,進入nginx配置目錄:cd /usr/local/nginx/conf
編輯主組態檔:vim nginx.conf
#打開反向代理功能后,在最后添加
server {
listen 80;
server_name www.westos.org;
location / {
proxy_pass http://westos;
}
}
完成后多載nginx并啟動nginx
在主機上添加決議:vim /etc/hosts
172.25.38.1 server1 www.westos.org
此時,在主機上決議地址即可:

再在組態檔最后添加:
server {
listen 80;
server_name www.linux.org;
location / {
root /web1;
index index.html;
}
}
在根下建立指定的目錄:mkdir /web1
匯入一個首頁資訊:echo web1 > /web1/index.html
在真機上添加決議:vim /etc/hosts
172.25.38.1 server1 www.westos.org www.linux.org
此時的結果為:在外部訪問www.westos.org時,server1上的nginx負載均衡至server2和server3上,而訪問www.linux.org時,指向固定網頁web1,
8.開啟安全性網頁
編輯nginx主組態檔
#取消注釋
# HTTPS server
#
server {
listen 443 ssl;
server_name localhost;
ssl_certificate cert.pem;
ssl_certificate_key cert.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
進入目錄:cd /etc/pki/tls/certs
生成證書:make cert.pem

移動證書:mv cert.pem /usr/local/nginx/conf/
此時在瀏覽器中瀏覽https://172.25.38.1

由于是個人簽名,所以瀏覽器會提示該網頁不受信任
9. nginx不同server權重
編輯nginx主組態檔:
權重默認為1:
http {
upstream westos {
server 172.25.38.2:80 weight=3;
server 172.25.38.3:80;
server 127.0.0.1:80 backup
}
include mime.types;
default_type application/octet-stream;
可以指定nginx本機為備用服務器,在所有節點掛掉時才會呼叫
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/272216.html
標籤:其他
上一篇:MySQL--架構組件極簡總結
