##########################################################################################################
LNMP:
-L :LINUX操作系統
-N :Nginx網站服務軟件
-M :Mysql Mariadb數據庫
-P :PHP Python Perl網站開發語言
#################################################################################################
準備三臺虛擬機:
Web1:
eth1網卡:192.168.2.100
Proxy:
eth0網卡:192.168.4.5
eth1網卡:192.168.2.5
Client:
eth0網卡:192.168.4.100
防火牆設置爲trusted
selinux設置爲permissive
##################################################################################################
LNMP部署:
#安裝Nginx(上篇博客裏面有安裝方法)
#yum -y install mariadb mariadb-server mariadb-devel //安裝Mariadb
#yum -y install php php-mysql //php-mysql用來連接數據庫
#rpm -ivh php-fpm-5.4.16-36.el7_1.x86_64.rpm //安裝php-fpm
#nginx //啓動nginx服務
#systemctl start mariadb //啓動mariadb服務
#systemctl enable mariadb //隨機自啓
#systemctl start php-fpm //啓動php-fpm服務
#systemctl enable php-fpm //隨機自啓
#netstat -anptu | grep "80" //驗證nginx是否啓動
#netstat -anptu | grep "3306" //驗證mariadb是否啓動
#netstat -anptu | grep "9000" //驗證php-fpm是否啓動
####################################################################################################
靜態頁面:不需要服務器進行解釋,直接傳遞給客戶端
動態頁面:需要服務器進行解釋,再傳遞給客戶端。
FastCGI簡介:
FastCGI是一種常駐(Long-live)型的CGI
-將GCI解釋器進程保持在內存中,進行維護和調度
-FastCGI技術目前支持的與語言 PHP C/C++ JAVA perl python 等
FastCGI工作原理:
1.web server 啓動時載入FastCGI進程管理器
2.FastCGI進程管理器初始化,啓動多個解釋器進程
3.當客戶端請求到達爲web server FastCGI進程管理器選擇並連接到一個解釋器。
4,FastCGI子進程完成處理後返回結果,將標準輸出和錯誤信息從同一連接返回web server
FastCGI缺點:
內存消耗大
-因爲多進程,所以比CGI多線程消耗更多的服務器內存,PHP—CGI解釋器每進程消耗7-25M內存,將這個數字乘50或100就是很大的內存數。
#####################################################################################################
php-fpm需要修改的常見配置如下:
#vim /var/
listen = 127.0.0.1:9000 //PHP端口號
pm.max_children = 32 //最大進程數量
pm.start_servers = 15 //最小進程數量
pm.min_spare_servers = 5 //最少需要幾個空閒着的進程
pm.max_spare_servers = 32 //最多允許幾個進程處於空閒狀態
#######################################################################################################
實現頁面的動靜分離:
#vim /usr/local/nginx/html/test.php //書寫簡單的動態頁面
<?php
$i="hello world";
echo $i;
?>
#nginx //啓動nginx服務
客戶端訪問:
#firefox http://192.168.4.5/test.php
結果:會出現下載頁面,因爲這個時候服務器並沒有解釋test.php,所以這屬於靜態頁面。
#vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.php index.html index.htm;
}
location ~ \.php$ { //去掉配置文件中的註釋,實現訪問.php結尾的動態頁面。
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; //將這一句話註釋
include fastcgi.conf; //這裏將源配置文件改爲fastcgi.conf
}
客戶端訪問:
#firefox http://192.168.4.5/test.php
結果:hello world 這個時候服務器已經用php解釋了test.php,所以屬於動態頁面。
#####################################################################################################
測試連接maridb數據庫:
#cp /root/lnmp_soft/php_scripts/mysql.php /usr/local/nginx/html/ //安裝php-fpm會有這個mysql.php的測試頁面,這是我的文件存儲路徑,只需要將mysql.php複製到/usr/local/nginx/html/下來完成測試。
注意:我們要將selinux設置爲permissive,這個和selinx的標籤值有關。
客戶端訪問:
#firefox http://192.168.4.5/mysql.php //關於Host和User的頁面
####################################################################################################
地址重寫:
站內轉換:
#vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.php index.html index.htm;
rewrite /a\.html /b.html; //訪問a.html轉換爲b.html,因爲nginx支持正則表達式,再正則中.表示任意單個字符,所以使用\將.屏蔽。
}
echo "AA" > /usr/local/nginx/html/a.html
echo "BB" > /usr/local/nginx/html/b.html
客戶端訪問:
#firefox http://192.168.4.5/a.html
結果:BB
如果需要訪問a轉到b,並且在我的瀏覽器導航欄中也顯示b.html rewrite /a\.html /b.html redirect;加上redirect。
######################################################################################################
站外轉換:
#vim /usr/local/nginx/conf/nginx.conf
http {
.. ..
server {
listen 80;
server_name localhost;
rewrite ^/(.*) http://www.tmooc.cn/$1; //訪問到 / 時,跳轉到www.tmooc.cn
// (.*)在正則中表示後面的所有內容,並且保留起來
//按照正則當中的理解應該爲\1,但是nginx爲$1.
location / {
root html;
index index.html index.htm;
#rewrite /a\.html /b.html;
}
}
}
客戶端訪問:
#firefox http://192.168.4.5/xxxx
結果:http://www.tmooc.cn/xxxxx
########################################################################################
不同設備訪問,顯示內容相同,格式不同:
#vim /usr/local/nginx/conf/nginx.conf
http {
server {
listen 80;
server_name localhost;
if ($http_user_agent ~* firefox) {
rewrite ^/(.*) /firefox/$1;
}
//$http_user_agent 日至消息中的一段,其中包含瀏覽器信息,設備信息等,匹配到firefox後跳轉到目錄下網頁中 ~模糊匹配,*代表不區分大小寫。
//日誌位置/usrl/local/nginx/logs/access_log,訪問日誌。
#rewrite ^/(.*) http://www.tmooc.cn/$1;
location / {
root html;
index index.html;
#rewrite /a\.html /b\.html;
}
}
}
mkdir /usr/local/nginx/html/firefox //創建目錄
echo "firefox" > /usr/local/nginx/html/firefox/test.html //書寫測試頁面
echo "curl" > /usr/local/nginx/html/test.html //書寫測試頁面
客戶端測試:
#firefox 192.168.4.5/test.html //使用火狐瀏覽器
結果:firefox
#curl 192.168.4.5/test.html //使用curl 瀏覽器
結果:curl
注意:在實際的應用中 兩個訪問的內容是相同的,格式不同。這裏只是爲了測試