部署LNMP環境、構建LNMP平臺、地址重寫

工具—遠程桌面查看器--VNC  172.40.50.118:8php

51CTO博客、百度html

nginx的statusjava

###################################################mysql

opration  linux高級運維linux

day 03nginx

 

LVS[沒有健康檢查的功能]web

 

while :正則表達式

dosql

ping 192.168.2.100shell

if [ $?  -ne 0  ];then

sed  -i  ‘/2.100/  s/(.*)/#\1/’  nginx.conf

sleep  1

done

 

 

who

pgrep -t nginx

kill -9 24158

pstree -ap  | grep nginx

killall -9  nginx

 

配置 rsync 自動將虛擬機 3 上的頁面自動同步到虛擬機 4,確保兩臺主機的頁面一致:

 

 

#####################################

 

 

動態網站

Nginx[靜態,動態]

 

頁面是htmlmp3,mp4,txt,doc,pdf

動態腳本語言:shell,PHP簡單、專門針對網站、只能作網站、javajsp)、Python豆瓣網

 

LNMP== ( Linux操做系統(底層)  +  Nginx網站服務軟件(web+

 MariaDBmysql數據庫  +  PHP|Python|Perl網站開發語言 )

 

192.168.4.5

軟件包列表:LNMP

 

實驗1:部署LNMP環境

nginx[web服務,接收用戶的請求] 源碼包

gccopenssl-develpcre-develzlib-devel

php  [解釋器]

yum -y localinstall php-fpm-5.4.16<tab> [服務、監聽9000端口]  rpm

mariadb [數據庫客戶端]

mariadb-server  [數據庫服務器]

mariadb-devel   [依賴包]

php-mysql  [php鏈接mysql的擴展庫文件]

 

vim  1.php

<?php      /開頭

$i  = 33;    /變量都有個$

$j = 44;

if  ($i > $j ){

echo 「i  is bigger」;

}

else{

echo 「 j is bigger」;

}

 

echo $i;      /echo 默認不換行

echo  「hi」;     /每行以分號結尾

?>   /結尾

#php  1.php

 

啓動全部服務:

nginx 啓動[80]

systemctl start php-fpm 啓動[9000]

systemctl start mariadb        啓動[3306]

 

netstat -antpu |grep 80 | 9000 |3306

##########################################

 

實驗2Nginx動靜分離  (Nginx + FastCGI)

 

    nginx[靜態]

root html

nginx[動態]

nginx轉發給PHP:9000

 

判斷,若是用戶訪問的是靜態頁面,則找到,直接返回

若是用戶訪問的是動態頁面,則轉發給9000

location匹配用戶的地址欄,至關於一個if。支持正則表達式【~+正則】

# vim /usr/local/nginx/conf/nginx.conf

……

location ~ \.php${                /不論目錄

root  html;

fastcgi_pass  127.0.0.1:9000;

fastcgi_index  index.php;

include fastcgi.conf;

}

 

vim  /usr/local/nginx/html/test.php

<?php

phpinfo();

?>

 

# nginx -s reload

# curl http://192.168.4.6/test.php

 

 

常見問題:

1.出現下載頁面 【配置文件中沒有php轉發】

nginx沒有作動靜分離的location和對應的fastcgi_pass

2.出現空白頁面----檢查php頁面文件的語法格式

a) tailf nginx/logs/error.log 沒有錯誤

b)   tailf /var/logphp-fpm/www-error.log

有錯誤提示!

3.File not found. 【轉發設置不正確】防火牆[SELinux]

4.An error occurred(看日誌error.log Connection refused)【轉發給9000後,無響應,php未啓動】

檢查php-fpm是否啓動成功,ip是否正確

##########################################

 

fastcgi是一種常駐型(long-live)的CGI

CGI解釋器進程保持在內存中,進行維護與調度。目前支持語言有PHPC/C++JavaPerlPythonRuby

fastcgi缺點

內存消耗大,應爲是多進程

php配置文件(很耗內存)/etc/php-fpm.d/www.conf

# grep -v "^;"  /etc/php-fpm.d/www.conf | grep -v "^$" /etc/php-

# ps aux | grep fpm

最少5個進程,最多50php進程

每一個進程最少7-25兆內存

 

mairadb性能更好的execl65536

cp  /opt/lnmp_soft/php_scripts/mysql.php   /usr/local/nginx/html/

 

LNMP    1.nginx接受用戶的請求

2.判斷請求的是靜態仍是動態的頁面 location

若是請求是靜態頁面,則nginx找到該頁面直接給用戶

若是請求的是php頁面,則找到該頁面,交給9000執行

執行到中間的某一行,可能須要鏈接數據庫

new mysqli('localhost','root','','mysql')

 

手機上的數據庫sqllite

 

##########################################

 

地址重寫

  rewrite  正則   跳轉後的URL [選項];

 

案例1:訪問a.html跳轉到b.html

 

vim /usr/local/nginx/conf/nginx.conf

... ...

server {

listen 80;

server_name localhost;

location / {

root  html;

rewrite  /a.html  /b.html  redirect;    

}

#echo "BB" > /usr/local/nginx/html/b.html

#nginx -s reload

#curl http://192.168.4.6/a.html

 

案例2:訪問192.168.4.5跳轉到www.tmooc.cn

#curl http://192.168.4.6/a.html

 

vim /usr/local/nginx/conf/nginx.conf

... ...

server {

listen 80

server_name localhost;

location / {

root  html;

rewrite   ^/     http://www.tmooc.cn;

}

 

附加:

訪問舊的網站頁面,跳轉到新的網站相同頁面

rewrite   ^/(.*)     http://www.jd.com/$1;

 保留和粘貼

 

 

案例3:不一樣瀏覽器訪問相同頁面返回結果不一樣

 

 

IE  (msie)  http://192.168.4.5/test.html

 

firefox  http://192.168.4.5/test.html

 

UC   http://192.168.4.5/test.html

 

nginx【內置變量】

vim /usr/local/nginx/conf/nginx.conf

  server {

... ...

if  ($http_user_agent   ~*   curl){        /*表示包含curl,不區分大小寫

rewrite    ^/(.*)    /curl/$1;

}   

#cd  /usr/local/nginx/html

#echo "1" >test.html

#mkdir  curl

#echo "2" >curl/test.html

#nginx -s reload

firefox http://192.168.4.5/test.html

curl http://192.168.4.5/test.html

 

案例4:若是用戶訪問的頁面不存則轉到首頁

vim /usr/local/nginx/conf/nginx.conf

 server {

... ...

if  (!-e   $request_filename){

rewrite   ^/    http://192.168.4.5;

}  

#nginx -s reload

 

 

rewrite  正則   URL   [選項]

rewrite  選項:

last 中止執行其餘rewrite

break 中止執行其餘rewrite,並結束請求

redirect 臨時重定向

permament 永久重定向

 

 

 

 

 

 

 

 

 

Top 

 

案例1:部署LNMP環境 

案例2:構建LNMP平臺 

案例3:地址重寫 

1 案例1:部署LNMP環境

1.1 問題

安裝部署Nginx、MariaDB、PHP環境

安裝部署NginxMariaDBPHPPHP-FPM

啓動NginxMariaDBFPM服務;

並測試LNMP是否工做正常。

1.2 方案

RHEL7系統中,源碼安裝Nginx,使用RPM包安裝MariaDB、PHP、PHP-FPM軟件。

操做過程當中須要安裝的軟件列表以下:

nginx

mariadbmariadb-servermariadb-devel

phpphp-fpmphp-mysql

1.3 步驟

實現此案例須要按照以下步驟進行。

步驟一:安裝軟件

1)使用yum安裝基礎依賴包

[root@svr5 ~]# yum -y groupinstall "Development tools"  "Additional Development"

[root@svr5 ~]# yum -y install gcc openssl-devel pcre-devel zlib-devel

.. ..

2)源碼安裝Nginx

[root@svr5 ~]# useradd –s /sbin/nologin  nginx

[root@svr5 ~]# tar -zxvf nginx-1.8.0.tar.gz

[root@svr5 ~]# cd nginx-1.8.0

[root@svr5 nginx-1.8.0]# ./configure   \

> --prefix=/usr/local/nginx    \

> --user=nginx   --group=nginx \

> --with-http_ssl_module

[root@svr5 ~]# make && make install

.. ..

3)安裝MariaDB

Mariadb在新版RHEL7光盤中包含有該軟件,配置yum源後能夠直接使用yum安裝:

[root@svr5 ~]# yum –y install   mariadb   mariadb-server   mariadb-devel

4)php和php-fpm

[root@svr5 ~]# yum –y  install  php

[root@svr5 ~]# tar  –xf  lnmp_soft-2017-03-28.tar.gz

[root@svr5 ~]# cd  lnmp_soft

[root@svr5 ~]# yum –y  localinstall php-fpm-5.4.16-36.el7_1.x86_64.rpm

[root@svr5 ~]# yum –y  install  php-mysql

步驟二:啓動服務

1)啓動Nginx服務

這裏須要注意的是,若是服務器上已經啓動了其餘監聽80端口的服務軟件(如httpd),則須要先關閉該服務,不然會出現衝突。

[root@svr5 ~]# service httpd stop                //若是該服務存在則關閉該服務

[root@svr5 ~]# chkconfig httpd off

[root@svr5 ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

[root@svr5 ~]# netstat -utnlp | grep :80

tcp    0    0 0.0.0.0:80        0.0.0.0:*        LISTEN        32428/nginx         

2)啓動MySQL服務

[root@svr5 ~]# systemctl start mariadb

[root@svr5 ~]# systemctl status mariadb

[root@svr5 ~]# systemctl enable mariadb

3)啓動PHP-FPM服務

[root@svr5 ~]# systemctl start php-fpm

[root@svr5 ~]# systemctl status php-fpm

[root@svr5 ~]# systemctl enable php-fpm

2 案例2:構建LNMP平臺

2.1 問題

沿用練習一,經過調整Nginx服務端配置,實現如下目標:

配置Fast-CGI支持PHP網頁

建立PHP測試頁面,測試使用PHP鏈接數據庫的效果

2.2 方案

使用2臺RHEL7虛擬機,其中一臺做爲LNMP服務器(192.168.4.5)、另一臺做爲測試用的Linux客戶機(192.168.4.100),如圖-1所示。

 

-1

Nginx結合FastCGI技術便可支持PHP頁面架構,所以本案例,須要延續練習一的實驗內容,經過修改Nginx及php-fpm配置文件實現對PHP頁面的支持。

php-fpm須要修改的常見配置以下:

listen = 127.0.0.1:9000

pm.max_children = 32

pm.start_servers = 15

pm.min_spare_servers = 5

pm.max_spare_servers = 32

2.3 步驟

實現此案例須要按照以下步驟進行。

步驟一:建立並修改php-fpm配置文件

1)查看php-fpm配置文件

[root@svr5 etc]# vim /etc/php-fpm.d/www.conf

[www]

listen = 127.0.0.1:9000

listen.allowed_clients = 127.0.0.1

user = apache

group = apache

pm = dynamic

pm.max_children = 50

pm.start_servers = 5

pm.min_spare_servers = 5

pm.max_spare_servers = 35

2)確認php-fpm服務已經啓動

[root@svr5 ~]# systemctl restart php-fpm

[root@svr5 ~]# systemctl status php-fpm

步驟二:修改Nginx配置文件並啓動服務

[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf

location / {

            root   html;

            index  index.php  index.html   index.htm;

        }

 location  ~  \.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;

        }

[root@svr5 ~]# /usr/local/nginx/sbin/nginx -s reload

步驟三:建立PHP頁面,測試LNMP架構可否解析PHP頁面

1)建立PHP測試頁面1:

[root@svr5 ~]# vim /usr/local/nginx/html/test1.php

<?php

phpinfo();

?>

2)建立PHP測試頁面,鏈接MariaDB數據庫:

 [root@svr5 ~]# vim /usr/local/nginx/html/test2.php

<?php

$links=mysql_connect("localhost","root","密碼");        

//注意:rootmysql帳戶名稱,密碼須要修改成實際mysql密碼,無密碼則留空便可

if($links){

        echo "link db ok!!!";

}

else{

        echo "link db no!!!";

}

?>

3)建立PHP測試頁面,鏈接並查詢MariaDB數據庫:

[root@svr5 ~]# vim /usr/local/nginx/html/test3.php

<?php

$mysqli = new mysqli('localhost','root','','mysql');

if (mysqli_connect_errno()){

    die('Unable to connect!'). mysqli_connect_error();

}

$sql = "select * from user";

$result = $mysqli->query($sql);

while($row = $result->fetch_array()){

    printf("Host:%s",$row[0]);

    printf("</br>");

    printf("Name:%s",$row[1]);

    printf("</br>");

}

?>

4)客戶端使用瀏覽器訪問服務器PHP首頁文檔,檢驗是否成功:

[root@client ~]# firefox http://192.168.4.5/test1.php

[root@client ~]# firefox http://192.168.4.5/test2.php

[root@client ~]# firefox http://192.168.4.5/test3.php

3 案例3:地址重寫

3.1 問題

沿用練習一,經過調整Nginx服務端配置,實現如下目標:

全部訪問/image目錄下資源的請求,重定向至/picture目錄;

全部訪問www.tarena.com的訪問重定向至bbs.tarena.com

實現curl訪問不一樣的頁面。

3.2 方案

關於Nginx服務器的地址重寫,主要用到的配置參數是rewrite:

rewrite regex replacement flag

3.3 步驟

實現此案例須要按照以下步驟進行。

步驟一:修改配置文件

1)修改Nginx服務配置:

[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf

.. ..

server {

        listen       80;

        server_name  www.tarena.com;

location / {

    root   html;

index  index.html index.htm;

rewrite ^/ http://bbs.tarena.com/;            

//地址重寫,訪問www.tarena.com將被重定向至bbs.tarena.com

rewrite ^/image/(.*)$ /picture/$1 break;    

//全部訪問/image目錄下資源的請求,重定向至/picture目錄

if ($http_user_agent ~* url) {                    //識別客戶端curl瀏覽器

rewrite ^(.*)$ /curl/$1 break;

}

}

}

2)從新加載配置文件

[root@svr5 ~]# /usr/local/nginx/sbin/nginx  -s  reload

3)建立網頁目錄以及對應的頁面文件:

[root@svr5 ~]# mkdir  -p  /usr/local/nginx/html/curl/picture

[root@svr5 ~]# echo "I am is curl page" > /usr/local/nginx/html/curl/test.html

[root@svr5 ~]# cp /usr/share/backgrounds/gnome/Road.jpg \

> /usr/local/nginx/curl/picture/test.jpg

步驟二:客戶端測試

客戶端使用瀏覽器測試各個頁面的訪問是否被重定向。

[root@svr5 ~]# curl http://192.168.4.5/test.html

[root@svr5 ~]# firefox http://192.168.4.5/test.html

[root@svr5 ~]# firefox http://192.168.4.5/images/test.jpg

相關文章
相關標籤/搜索