實現FastCGI:
CGI的由來:
最早的Web服務器只能簡單地響應瀏覽器發來的HTTP請求,並將存儲在服務器上的HTML文件返回給瀏覽器,也
就是靜態html文件,但是后期隨着網站功能增多網站開發也越來越復雜,以至於出現動態技術,比如像php(1995
年)、java(1995)、python(1991)語言開發的網站,但是nginx/apache服務器並不能直接運行 php、java這樣的文
件,apache實現的方式是打補丁,但是nginx缺通過與第三方基於協議實現,即通過某種特定協議將客戶端請求轉
發給第三方服務處理,第三方服務器會新建新的進程處理用戶的請求,處理完成后返回數據給Nginx並回收進程,
最后nginx在返回給客戶端,那這個約定就是通用網關接口(common gateway interface,簡稱CGI),CGI(協議)
是web服務器和外部應用程序之間的接口標准,是cgi程序和web服務器之間傳遞信息的標准化接口。
為什么FastCGI?
CGI協議雖然解決了語言解析器和seb server之間通訊的問題,但是它的效率很低,因為web server每收到一個請
求都會創建一個CGI進程,PHP解析器都會解析php.ini文件,初始化環境,請求結束的時候再關閉進程,對於每一
個創建的CGI進程都會執行這些操作,所以效率很低,而FastCGI是用來提高CGI性能的,FastCGI每次處理完請求之
后不會關閉掉進程,而是保留這個進程,使這個進程可以處理多個請求。這樣的話每個請求都不用再重新創建一個
進程了,大大提升了處理效率。
什么是PHP-FPM?
PHP-FPM(FastCGI Process Manager:FastCGI進程管理器)是一個實現了Fastcgi的程序,並且
提供進程管理的功能。進程包括master進程和worker進程。master進程只有一個,負責監聽端口,接受來自web
server的請求。worker進程一般會有多個,每個進程中會嵌入一個PHP解析器,進行PHP代碼的處理。
FastCGI配置指令:
Nginx基於模塊ngx_http_fastcgi_module實現通過fastcgi協議將指定的客戶端請求轉發至php-fpm處理,其配置指
令如下:
fastcgi_pass address;
#轉發請求到后端服務器,address為后端的fastcgi server的地址,可用位置:location, if in location
fastcgi_index name;
#fastcgi默認的主頁資源,示例:fastcgi_index index.php;
fastcgi_param parameter value [if_not_empty];
#設置傳遞給FastCGI服務器的參數值,可以是文本,變量或組合,可用於將Nginx的內置變量賦值給自定義key
fastcgi_param REMOTE_ADDR $remote_addr; #客戶端源IP fastcgi_param REMOTE_PORT $remote_port; #客戶端源端口 fastcgi_param SERVER_ADDR $server_addr; #請求的服務器IP地址 fastcgi_param SERVER_PORT $server_port; #請求的服務器端口 fastcgi_param SERVER_NAME $server_name; #請求的server name
Nginx默認配置示例:
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_params; }
實戰:Nginx與php-fpm在同一服務器
nginx服務端相關配置
1、安裝nginx服務,並修改配置文件。
[root@centos27site1]#yum install nginx -y [root@centos27site1]#vim /etc/nginx/conf.d/test.conf server { listen 80; server_name www.magedu.net; root /data/php/; location ~* \.php$ { root /data/php; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #fastcgi_param SCRIPT_FILENAME /data/php$fastcgi_script_name; include fastcgi_params; } access_log /var/log/nginx/access_json.log access_json; }
修改完配置文件之后,重啟nginx服務:nginx -s reload
2、先安裝php和連接mysql數據庫相關包,並啟動php-fpm服務。
[root@centos27~]#yum install php-fpm php-mysql -y [root@centos27~]#systemctl start php-fpm
3、查看php相關配置文件,並修改所有者和所屬組。
[root@centos27~]#rpm -ql php-fpm 查看php相關包 /etc/logrotate.d/php-fpm /etc/php-fpm.conf /etc/php-fpm.d /etc/php-fpm.d/www.conf /etc/sysconfig/php-fpm /run/php-fpm /usr/lib/systemd/system/php-fpm.service /usr/lib/tmpfiles.d/php-fpm.conf /usr/sbin/php-fpm /usr/share/doc/php-fpm-5.4.16 /usr/share/doc/php-fpm-5.4.16/fpm_LICENSE /usr/share/doc/php-fpm-5.4.16/php-fpm.conf.default /usr/share/fpm /usr/share/fpm/status.html /usr/share/man/man8/php-fpm.8.gz /var/log/php-fpm
[root@centos27~]#vim /etc/php-fpm.d/www.conf 修改配置文件,將所有者和所屬組都改為nginx,避免后面權限問題。 user=nginx group=nginx
查看php配置文件中可以優化配置的含義
cat /etc/php-fpm.d/www.conf [www] listen = 127.0.0.1:9000 #監聽地址及IP listen.allowed_clients = 127.0.0.1 #允許客戶端從哪個源IP地址訪問,要允許所有行首加 ;注釋即可 user = nginx #php-fpm啟動的用戶和組,會涉及到后期文件的權限問題 group = nginx pm = dynamic #動態模式進程管理 pm.max_children = 500 #靜態方式下開啟的php-fpm進程數量,在動態方式下他限定php-fpm的最大進程數 pm.start_servers = 100 #動態模式下初始進程數,必須大於等於pm.min_spare_servers和小於等於pm.max_children的值。 pm.min_spare_servers = 100 #最小空閑進程數 pm.max_spare_servers = 200 #最大空閑進程數 pm.max_requests = 500000 #進程累計請求回收值,會重啟 pm.status_path = /pm_status #狀態訪問URL ping.path = /ping #ping訪問動地址 ping.response = ping-pong #ping返回值 slowlog = /var/log/php-fpm/www-slow.log #慢日志路徑 php_admin_value[error_log] = /var/log/php-fpm/www-error.log #錯誤日志 php_admin_flag[log_errors] = on php_value[session.save_handler] = files #phpsession保存方式及路徑 php_value[session.save_path] = /var/lib/php/session #當時使用file保存session的文件路徑
4、將下載的wordpress解壓到data目錄下,然后移動到php目錄下
[root@centos27~]#mkdir /data/php [root@centos27~]#tar xvf wordpress-4.9.4-zh_CN.tar.gz -C /data/ [root@centos27data]#mv wordpress/* /data/php
5、修改wordpress配置文件,與mysql數據庫進行關聯。
[root@centos27data]#cd php [root@centos27php]#ls index.php wp-activate.php wp-comments-post.php wp-cron.php wp-load.php wp-settings.php xmlrpc.php license.txt wp-admin wp-config-sample.php wp-includes wp-login.php wp-signup.php readme.html wp-blog-header.php wp-content wp-links-opml.php wp-mail.php wp-trackback.php [root@centos27php]#mv wp-config-sample.php wp-config.php [root@centos27php]#chown -R nginx.nginx . 修改目錄權限 [root@centos27php]#vim wp-config.php
mysql服務器端進行相關配置
1、創建數據庫和wordpress用戶賬號
[root@centos37~]#yum install mariadb-server -y [root@centos37~]#systemctl start mariadb [root@centos37~]#mysql -e 'create database wordpress;grant all on wordpress.* to wordpress@"192.168.37.%" identified by "centos"'
查看網頁效果:www.magedu.net/index.php
實戰:Nginx與php不在同一個服務器
在php服務器上配置相關文件
安裝最新版本的php-fpm和php-mysql包
https://mirrors.tuna.tsinghua.edu.cn/remi/
[root@centos27yum.repos.d]#yum install https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
查看安裝后的yum源倉庫路徑
[root@centos27yum.repos.d]#rpm -ql remi-release /etc/pki/rpm-gpg/RPM-GPG-KEY-remi /etc/pki/rpm-gpg/RPM-GPG-KEY-remi2017 /etc/pki/rpm-gpg/RPM-GPG-KEY-remi2018 /etc/pki/rpm-gpg/RPM-GPG-KEY-remi2019 /etc/yum.repos.d/remi-glpi91.repo /etc/yum.repos.d/remi-glpi92.repo /etc/yum.repos.d/remi-glpi93.repo /etc/yum.repos.d/remi-glpi94.repo /etc/yum.repos.d/remi-modular.repo /etc/yum.repos.d/remi-php54.repo /etc/yum.repos.d/remi-php70.repo /etc/yum.repos.d/remi-php71.repo /etc/yum.repos.d/remi-php72.repo /etc/yum.repos.d/remi-php73.repo /etc/yum.repos.d/remi-php74.repo /etc/yum.repos.d/remi-safe.repo /etc/yum.repos.d/remi.repo
在php服務器上啟用yum源倉庫
[root@centos27yum.repos.d]#vim /etc/yum.repos.d/remi-php73.repo [remi-php73] name=Remi's PHP 7.3 RPM repository for Enterprise Linux 7 - $basearch #baseurl=http://rpms.remirepo.net/enterprise/7/php74/$basearch/ #mirrorlist=https://rpms.remirepo.net/enterprise/7/php74/httpsmirror mirrorlist=http://cdn.remirepo.net/enterprise/7/php74/mirror enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
安裝php-fpm和php-mysql包
[root@centos27yum.repos.d]#yum install php73-php-fpm php73-php-mysql -y
查看安裝后的軟件包
[root@centos27~]#rpm -ql php73-php-fpm /etc/logrotate.d/php73-php-fpm /etc/opt/remi/php73/php-fpm.conf /etc/opt/remi/php73/php-fpm.d /etc/opt/remi/php73/php-fpm.d/www.conf /etc/opt/remi/php73/sysconfig/php-fpm /etc/systemd/system/php73-php-fpm.service.d /opt/remi/php73/root/usr/sbin/php-fpm /opt/remi/php73/root/usr/share/doc/php73-php-fpm-7.3.12 /opt/remi/php73/root/usr/share/doc/php73-php-fpm-7.3.12/php-fpm.conf.default /opt/remi/php73/root/usr/share/doc/php73-php-fpm-7.3.12/www.conf.default /opt/remi/php73/root/usr/share/fpm /opt/remi/php73/root/usr/share/fpm/status.html /opt/remi/php73/root/usr/share/licenses/php73-php-fpm-7.3.12 /opt/remi/php73/root/usr/share/licenses/php73-php-fpm-7.3.12/fpm_LICENSE /opt/remi/php73/root/usr/share/man/man8/php-fpm.8.gz /usr/lib/systemd/system/php73-php-fpm.service /var/opt/remi/php73/lib/php/opcache /var/opt/remi/php73/lib/php/session /var/opt/remi/php73/lib/php/wsdlcache /var/opt/remi/php73/log/php-fpm /var/opt/remi/php73/run/php-fpm
如果是nginx服務器和php程序分開,就需要查看nginx服務器的nginx用戶屬性,在php程序新建的用戶要與nginx服務一致,查看nginx服務器的nginx用戶賬號信息。
[root@centos17~]#getent passwd nginx nginx:x:987:981::/home/nginx:/sbin/nologin
在php服務器上創建nginx用戶賬號
[root@centos27~]#groupadd -g 981 nginx [root@centos27~]#useradd -r -u 987 -g nginx -s /sbin/nologin
修改php配置文件,指定nginx用戶連接php程序信息。
[root@centos27php]#vim /etc/opt/remi/php73/php-fpm.d/www.conf user = nginx 用戶名 group = nginx 組 listen = 9000 監聽9000端口,默認所有IP地址都可以連接 ;listen.allowed_clients = 127.0.0.1 注釋掉此行,允許所有人訪問 [root@centos27php]#systemctl start php73-php-fpm.service 啟動php-fpm服務
修改wordpress配置文件,與mysql數據庫進行關聯。
[root@centos27data]#cd php [root@centos27php]#ls index.php wp-activate.php wp-comments-post.php wp-cron.php wp-load.php wp-settings.php xmlrpc.php license.txt wp-admin wp-config-sample.php wp-includes wp-login.php wp-signup.php readme.html wp-blog-header.php wp-content wp-links-opml.php wp-mail.php wp-trackback.php [root@centos27php]#mv wp-config-sample.php wp-config.php [root@centos27php]#chown -R nginx.nginx . 修改目錄權限 [root@centos27php]#vim wp-config.php
nginx服務端修改相關配置
修改nginx服務端的配置文件,vim /etc/nginx/conf.d/test.conf
server { listen 80; server_name www.magedu.net; root /data/php; location ~* \.php$ { root /data/php; fastcgi_pass 192.168.37.27:9000; 指定nginx服務端的IP地址 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #fastcgi_param SCRIPT_FILENAME /data/php$fastcgi_script_name; include fastcgi_params; } }
mysql服務端進行創建用戶賬號
在mysql服務端創建用戶賬號
[root@centos37~]#yum install mariadb-server -y [root@centos37~]#systemctl start mariadb [root@centos37~]#mysql -e 'create database wordpress;grant all on wordpress.* to wordpress@"192.168.37.%" identified by "centos"'
訪問頁面測試效果:www.magedu.net/index.php
實現緩存效果:
緩存定義指令:
fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
定義fastcgi的緩存; path #緩存位置為磁盤上的文件系統路徑 max_size=size #磁盤path路徑中用於緩存數據的緩存空間上限 levels=levels:緩存目錄的層級數量,以及每一級的目錄數量,levels=ONE:TWO:THREE,示例: leves=1:2:2 keys_zone=name:size #設置緩存名稱及k/v映射的內存空間的名稱及大小 inactive=time #緩存有效時間,默認10分鍾,需要在指定時間滿足fastcgi_cache_min_uses 次數被 視為活動緩存。
緩存調用指令:
fastcgi_cache zone | off; #調用指定的緩存空間來緩存數據,可用位置:http, server, location
fastcgi_cache_key string; #定義用作緩存項的key的字符串,示例:fastcgi_cache_key $request_uri;
fastcgi_cache_methods GET | HEAD | POST ...; #為哪些請求方法使用緩存
fastcgi_cache_min_uses number; #緩存空間中的緩存項在inactive定義的非活動時間內至少要被訪問到此處所指定的次數方可被認作活動項
fastcgi_keep_conn on | off; #收到后端服務器響應后,fastcgi服務器是否關閉連接,建議啟用長連接
fastcgi_cache_valid [code ...] time; #不同的響應碼各自的緩存時長
fastcgi_hide_header field; #隱藏響應頭指定信息 fastcgi_pass_header field; #返回響應頭指定信息,默認不會將Status、X-Accel-...返回
示例:
http { fastcgi_cache_path /var/cache/nginx/fcgi_cache levels=1:2:1 keys_zone=fcgicache:20m inactive=120s; ... server { location ~* \.php$ { ... fastcgi_cache fcgicache; fastcgi_cache_key $request_uri; fastcgi_cache_valid 200 302 10m; fastcgi_cache_valid 301 1h; fastcgi_cache_valid any 1m; ... } }
1、在nginx服務端配置文件中添加緩存指令
[root@centos27php]#vim /etc/nginx/nginx.conf fastcgi_cache_path /var/cache/nginx/fcgi_cache levels=1:2:1 keys_zone=fcgicache:20m inactive=120s;
vim /etc/nginx/conf.d/test.conf
server { listen 80; server_name www.magedu.net; root /data/php; location ~* \.php$ { root /data/php; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #fastcgi_param SCRIPT_FILENAME /data/php$fastcgi_script_name; include fastcgi_params; fastcgi_cache fcgicache; fastcgi_cache_key $request_uri; fastcgi_cache_valid 200 302 10m; fastcgi_cache_valid 301 1h; fastcgi_cache_valid any 1m; } }
配置完之后重新加載nginx服務:nginx -s reload
2、在客戶端進行測試效果:
ab -c 10 -n 100 http://www.magedu.net/index.php