1. 程式人生 > >企業LNMP環境應用實踐

企業LNMP環境應用實踐

常見web框架結構
比如:LNMP和LAMP
L=linux 、N=nginx A=apache、P=php、T=tomcat

nginx和Apache都是web應用伺服器
nginx處理靜態資料塊,Apache處理動態快
nginx對系統資源佔用小,Apache資源佔用高
nginx用的是epoll模型,Apache用的select模型

LNMP的部署方式分為兩種全都部署在一臺上或全都不在一臺上,N+P+M
只分離mysql, NP+M

LNMP組合的環境過程解析

在LNMP組合工作時,首先是使用者通過瀏覽器輸入域名請求Nginx Web服務,如果請求是靜態資源則由Nginx解析返回給使用者

;如果是動態請求(.php結尾),那麼Nginx就會把它通過FastCGI介面(生產常用方法)傳送給PHP引擎服務FastCGI程序php-fpm)進行解析,如果這個動態請求要讀取資料庫資料,那麼PHP就會繼續向後請求MySQL資料庫,以讀取需要的資料,並最終通過Nginx服務把獲取的資料返回給使用者,這就是LNMP環境的基本請求順序流程。這個請求流程是企業使用LNMP環境的常用流程。

nginx的前面會有個http資料包
http資料包特點格式很鬆散,因此解析的速度慢,但是資料包比fastcgi格式資料包小
nginx到PHP5中間有一個fastcgi介面(介面規則=快速介面)
fastcgi資料包這種規則的資料包格式非常嚴謹,因此,PHP

解析的速度非常快,但是越嚴謹的資料包資料就越大,所以fastcgi資料包大小要絕對大於http資料包

使用者的請求一定是遵循HTTP協議的因此是通過瀏覽器過來的

fastcgi軟體的設計架構分為C/SB/S作為客戶端client的fastcgi_pass和作為server端的php-fpm,因此經常用所以,fastcgi_pass和nginx合併而PHP-fpm和PHP功能合併了

nginx和Apache其實一樣,如果沒有PHP支援,那麼都處理不了所謂的動態請求。他們自身其實都只能處理靜態,只是Apache轉發動態資料包的速度快,但是隻是單個包速度,Apache併發低。

過濾的時候
location ~*. (php|php5)${
fastcgi_pass 127.0.0.1:9000 (如果這裡的ip地址改變的話那就會成分離式)
}

動態網頁的格式 index.php 動態網頁也是有實體檔案。

開始部署LNMP

在這裡插入圖片描述

部署nginx服務
[[email protected] ~]# yum install -y pcre-devel openssl-devel 安裝nginx的關聯包
[[email protected] ~]# tar xf nginx-1.10.2.tar.gz -C /usr/src/ #把包解壓縮之後放到了/usr/src下
[[email protected] nginx-1.10.2]# useradd -M -s /sbin/nologin www
[[email protected] nginx-1.10.2]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module	#原始碼編譯
[[email protected] nginx-1.10.2]# make && make install
[[email protected] nginx-1.10.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/	#做個軟連結
[[email protected] conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf		#去掉沒用的,修飾一下
安裝mysql服務(PHP安裝需要先有mysql環境,不然php編譯報錯)

問題1: 如果mysql在其他伺服器改這麼辦呢?

[[email protected] mysql-5.5.32-linux2.6-x86_64]# /bin/cp support-files/my-small.cnf /etc/my.cnf		#把東西複製過來
[[email protected] mysql-5.5.32-linux2.6-x86_64]# echo "192.168.233.133 LNMP" >> /etc/hosts	#最好做一下本地對映,不然有可能會報錯
[[email protected] mysql-5.5.32-linux2.6-x86_64]# ln -s /usr/local/mysql-5.5.32-linux2.6-x86_64/ /usr/local/mysql	#名字有點長,做個軟連結
[[email protected] mysql-5.5.32-linux2.6-x86_64]# chown mysql.mysql /usr/local/mysql		#給mysql目錄屬組
[[email protected] mysql-5.5.32-linux2.6-x86_64]# yum install -y libaio	光碟源安裝包
[[email protected] mysql]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql	初始化mysql資料庫
[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld	#給mysql做個啟動指令碼
[[email protected] mysql]# chmod +x /etc/init.d/mysqld	#賦予可執行許可權
[[email protected] mysql]# /etc/init.d/mysqld start  啟動mysql伺服器
[[email protected] mysql]# ln -s /usr/local/mysql/bin/* /usr/local/bin/		#做個軟連結
[[email protected] mysql]# mysqladmin -uroot password '123456'		#給mysql設定登入賬號和密碼
檢查PHP函式庫的支援包
[[email protected] mysql]# yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
[[email protected] mysql]# yum -y install freetype-devel libpng-devel gd libcurl-devel libxslt-devel
只有libiconv-devel這個包沒有安裝,因為預設的yum源沒有此包。
[[email protected] mysql]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz	#從網上下載libiconv-devel包
[[email protected] ~]# tar xf libiconv-1.14.tar.gz -C /usr/src/		#然後解壓
[[email protected] ~]# cd /usr/src/libiconv-1.14/
[[email protected] libiconv-1.14]# ./configure --prefix=/usr/local/libiconv && make && make install	#原始碼編譯
----------------------------------------------------------
###安裝著4個rpm包,必須按著個順序來
[[email protected] data]# rpm  -ivh libmcrypt-2.5.8-9.el6.x86_64.rpm 
[[email protected] data]# rpm  -ivh mhash-0.9.9.9-3.el6.x86_64.rpm 
[[email protected] data]# rpm  -ivh libmcrypt-devel-2.5.8-9.el6.x86_64.rpm 
[[email protected] data]# rpm -ivh mcrypt-2.6.8-10.el6.x86_64.rpm 
安裝PHP
[[email protected] ~]# tar xf php-5.3.28.tar.gz -C /usr/src/		#解壓縮
[[email protected] ~]# cd /usr/src/php-5.3.28/	
[[email protected] php-5.3.28]# ./configure \	預配置
> --prefix=/usr/local/php5.3.28 \
> --with-mysql=/usr/local/mysql \
> --with-iconv-dir=/usr/local/libiconv \
> --with-freetype-dir \
> --with-jpeg-dir \
> --with-png-dir \
> --with-zlib \
> --with-libxml-dir=/usr \
> --enable-xml \
> --disable-rpath \
> --enable-safe-mode \
> --enable-bcmath \
> --enable-shmop \
> --enable-sysvsem \
> --enable-inline-optimization \
> --with-curl \
> --with-curlwrappers \
> --enable-mbregex \
> --enable-fpm \
> --enable-mbstring \
> --with-mcrypt \
> --with-gd \
> --enable-gd-native-ttf \
> --with-openssl \
> --with-mhash \
> --enable-pcntl \
> --enable-sockets \
> --with-xmlrpc \
> --enable-zip \
> --enable-soap \
> --enable-short-tags \
> --enable-zend-multibyte \
> --enable-static \
> --with-xsl \
> --with-fpm-user=www \
> --with-fpm-group=www \
> --enable-ftp
上面的這些步驟,必須有mysql環境下才能這麼做
----------------------------------------------------------------------------
如果沒有MySQL軟體包,也可以不單獨安裝,這樣的情況可使用--with-mysql=mysqlnd替代--with-mysql=/usr/local/mysql,因為PHP軟體裡已經自帶了連線MySQL的客戶端工具。
---------------------------------------------------------
#這個是做分離式部署環境    快捷方式
[[email protected] php-5.3.28]# yum -y install openssl-devel openssl		 	安裝著倆個包要不然會報錯

[[email protected] php-5.3.28]# ./configure  --prefix=/usr/local/php5.3.28  --with-mysql=mysqlnd  --with-iconv-dir=/usr/local/libiconv  --with-freetype-dir  --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --disable-rpath  --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem  --enable-inline-optimization  --with-curl  --with-curlwrappers --enable-mbregex  --enable-fpm --enable-mbstring --with-mcrypt  --with-gd  --enable-gd-native-ttf   --with-openssl  --with-mhash  --enable-pcntl  --enable-sockets --with-xmlrpc --enable-zip  --enable-soap  --enable-short-tags --enable-zend-multibyte  --enable-static --with-xsl  --with-fpm-user=www  --with-fpm-group=www  --enable-ftp
編譯PHP
[[email protected] php-5.3.28]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/		#做個軟連結

[[email protected] php-5.3.28]# touch ext/phar/phar.phar		建立這個檔案要不然會報錯

[[email protected] php-5.3.28]# make	#生成

[[email protected] php-5.3.28]# make install
[[email protected] ~]# ln -s /usr/local/php5.3.28/ /usr/local/php	

[[email protected] php-5.3.28]# ls php.ini*		檢視模板檔案
php.ini-development  php.ini-production

#拷貝PHP配置檔案到PHP預設目錄,並更改檔名稱為php.ini
[[email protected] php-5.3.28]# cp php.ini-production /usr/local/php/lib/php.ini

[[email protected] php-5.3.28]# cd /usr/local/php/etc/

[[email protected] etc]# cp php-fpm.conf.default php-fpm.conf		

[[email protected] etc]# /usr/local/php/sbin/php-fpm 		#啟動PHP服務php-fpm
		
[[email protected] etc]# netstat -antup | grep 9000		過濾埠
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      10063/php-fpm       

[[email protected] etc]# vim php-fpm.conf

在這裡插入圖片描述

修改nginx配置檔案(配置Nginx支援PHP)
error_log logs/error.log;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  blog.yunjisuan.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
                }
        location ~.*\.(php|php5)?$ {
        root html/blog;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
                }
        }
}
[[email protected] blog]# /usr/local/nginx/sbin/nginx	#啟動nginx的服務
測試php
[[email protected] blog]# cat test_info.php 
<?php phpinfo(); ?>

在這裡插入圖片描述

PHP連線MySQL的檢查指令碼
[[email protected] blog]# cat test_mysql.php 
<?php
    //$link_id=mysql_connect('主機名','使用者','密碼');
	$link_id=mysql_connect('localhost','root','123456');
    if($link_id){
        echo "mysql successful by Mr.zhuxinwang !\n";
    }else{
        echo mysql_error();
    }
?>

-----------------------------------------------------------------------
[[email protected] blog]# ln -s /usr/local/php/bin/* /usr/local/sbin/		#給命令建立個軟連結
[[email protected] blog]# which php
/usr/local/sbin/php
		
[[email protected] blog]# php test_mysql.php 
mysql successful by Mr.zhuxinwang !
WordPress 部落格搭建準備
[[email protected] blog]# mysql -uroot -p123456			#登入資料庫
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.32 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

mysql> create database wordpress;
mysql> grant all on wordpress.* to [email protected]'localhost' identified by '123123';	#localhost是客戶端的ip地址
mysql> flush privileges;  	#重新整理許可權

(2)對nginx配置檔案

[[email protected] blog]# cat /usr/local/nginx/conf/nginx.conf
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  blog.yunjisuan.com;
        location / {
            root   html/blog;
            index  index.html index.htm index.php;		#新增一個首頁檔案index.php
        	}
	location ~.*\.(php|php5)?$ {
	root html/blog;
	fastcgi_pass 127.0.0.1:9000;
	fastcgi_index index.php;
	include fastcgi.conf;
		}
        } 
}
[[email protected] blog]# /usr/local/nginx/sbin/nginx -s reload	#重啟nginx服務,切記只要修改配置檔案必須要重啟
配置WordPress部落格
[[email protected] ~]# tar -xf wordpress-4.7.4-zh_CN.tar.gz -C /usr/local/nginx/html/blog/		#解壓到 /usr/local/nginx/html/blog/這個目錄下
[[email protected] ~]# cd /usr/local/nginx/html/blog/	#進入到這個目錄下
[[email protected] blog]# rm -f *	#刪除所有檔案
[[email protected] blog]# cd wordpress/	  #進入到這個目錄
[[email protected] wordpress]# mv * .. 	#把目錄裡面的所有東西移動到上級目錄
[[email protected] blog]# rm -rf wordpress/	#然後把這個目錄刪除了
[[email protected] blog]# chown -R www.www /usr/local/nginx/html/blog/		#遞迴授權給blog下的所有檔案或目錄

[[email protected] blog]# ll
total 188
-rw-r--r--  1 www www   418 Sep 25  2013 index.php
-rw-r--r--  1 www www 19935 Jan  3  2017 license.txt
-rw-r--r--  1 www www  6956 Apr 23  2017 readme.html
-rw-r--r--  1 www www  5447 Sep 28  2016 wp-activate.php
drwxr-xr-x  9 www www  4096 Apr 23  2017 wp-admin
-rw-r--r--  1 www www   364 Dec 19  2015 wp-blog-header.php
-rw-r--r--  1 www www  1627 Aug 29  2016 wp-comments-post.php
-rw-r--r--  1 www www  2930 Apr 23  2017 wp-config-sample.php
drwxr-xr-x  5 www www  4096 Apr 23  2017 wp-content
-rw-r--r--  1 www www  3286 May 25  2015 wp-cron.php
drwxr-xr-x 18 www www 12288 Apr 23  2017 wp-includes
-rw-r--r--  1 www www  2422 Nov 21  2016 wp-links-opml.php
-rw-r--r--  1 www www  3301 Oct 25  2016 wp-load.php
-rw-r--r--  1 www www 33939 Nov 21  2016 wp-login.php
-rw-r--r--  1 www www  8048 Jan 11  2017 wp-mail.php
-rw-r--r--  1 www www 16255 Apr  7  2017 wp-settings.php
-rw-r--r--  1 www www 29896 Oct 19  2016 wp-signup.php
-rw-r--r--  1 www www  4513 Oct 15  2016 wp-trackback.php
-rw-r--r--  1 www www  3065 Sep  1  2016 xmlrpc.php

第一步

在這裡插入圖片描述

第二步
在這裡插入圖片描述

第三步
在這裡插入圖片描述

第四步
在這裡插入圖片描述第五步
在這裡插入圖片描述

第六步:登入成功,之後的介面
在這裡插入圖片描述

實現WordPress部落格程式URL偽靜態化
/archives/%post_id%.html
##說明:%post_id%是資料庫對應博文內容的唯一ID,例如423

在這裡插入圖片描述

在Nginx配置檔案的server體裡面中新增程式碼
[[email protected] conf]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections  1024;
}
error_log logs/error.log;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  blog.yunjisuan.com;
        location / {
            root   html/blog;
            index  index.html index.htm index.php;
         if (-f $request_filename/index.html){
            rewrite (.*) $1/index.html break;
                }
        if (-f $request_filename/index.php){
            rewrite (.*) $1/index.php;
                }
        if (!-f $request_filename){
            rewrite (.*) /index.php;
                }
        }
        location ~.*\.(php|php5)?$ {
        root html/blog;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
                }
        }
}

在這裡插入圖片描述

使用者瀏覽器的圖片解析

使用者通過PC上網,當輸入blog.yunjisuan.com時,PC機向Web伺服器發起請求;訪問瀏覽器首頁index.html,web瀏覽器讀取index.html檔案的內容,發給使用者瀏覽器;使用者瀏覽器開始解析,解析到文字時,文字就會出現在瀏覽器上;解析到圖片時,使用者瀏覽器將圖片的地址讀取出來,再次向Web伺服器發起請求,Web伺服器收到請求後,將圖片讀取出來再次發給使用者瀏覽器,使用者瀏覽器再把返還的內容顯示到瀏覽器的頁面上,此時圖片就解析出來了。
上傳圖片時,圖片存在儲存裡(掛載儲存NFS),再把圖片放在儲存裡的位置寫到資料庫裡;
設計上傳時,網頁目錄html由兩個子目錄:static(讀的)和upload(上傳的),NFS儲存既要掛載在static目錄上,還要掛載在upload目錄上;靜態網頁讀取時經過static目錄,php動態網頁讀取時也要經過static目錄,但是,PHP還要經過upload目錄;PHP經過upload目錄後,圖片上傳進upload後,還要將圖片的具體位置寫入MySQL資料庫;這樣,在下次訪問index.php,SQL語句直接到MySQL中查詢。

PHP有兩個作用:
(1)讀動態網頁(讀取時可以不掛載儲存NFS,直接從MySQL裡把URL讀出來反饋給使用者,使用者瀏覽器解析的圖片會再發次請求,找static讀取圖片)
(2)上傳資料,向伺服器寫入資料

圖片加文字的部署

第一步:

[[email protected] conf]# vim nginx.conf

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
server{
listen 80;
server_name xxx.yunjisuan.com;
location / {
root html/xxx;
index index.html index.htm;
                }
        }
}

第二步:

[[email protected] xxx]# vim index.html 

welcome to yunjisuan
<img src="http://192.168.233.133/111.jpg" />

在這裡插入圖片描述