LNMP架構負載均衡
nginx的搭建
1、安裝
安裝前註意:
模塊依賴性:Nginx的一些模塊需要其他第三方庫的支持,例如gzip模塊需要zlib 庫,rewrite模塊需要pcre庫,ssl功能需要openssl庫等。
(1) 確保底層庫都已安裝
運行 yum install zlib* pcre* openssl*
(2) 創建一個用戶和用戶組
創建www組與www用戶 ,理論上來講用戶與用戶組的名稱無所謂,但盡量要有意義
groupadd www
useradd -g www -s /usr/sbin/nologin www
(3) 編譯安裝nginx
第一步:下載nginx
wget
第二步:解壓並進入目錄
tar ….
Cd …
第三步:配置
先查看configure的配置項,並保存成一個幫助文檔
將常用的配置項保存成一個安裝腳本文件
Vim nginx_install.sh
內容如下:
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_image_filter_module --with-pcre --http-client-body-temp-path=/usr/local/nginx/tmp/client_body_temp --http-fastcgi-temp-path=/usr/local/nginx/tmp/fastcgi_temp --http-proxy-temp-path=/usr/local/nginx/tmp/proxy_temp --http-uwsgi-temp-path=/usr/local/nginx/tmp/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/tmp/scgi_temp
給sh文件加上可執行的權限
然後執行
第四步:編譯安裝
make && make install
2、管理
(1)查看幫助文檔
Cd /usr/local/nginx/sbin
./nginx -h
(2)啟動nginx
直接執行安裝目錄下的sbin目錄下的nginx
(3)關閉nginx
可以執行 nginx -s stop 或者 執行 pkill nginx
(4)熱啟動
可以執行 nginx -s reload (前提是nginx已經在運行中)
擴展:如果想用service 服務名 選項來重啟或停止nginx,可以按以下步驟操作:
知識點:service 服務名 能操作成功的前提是 /etc/rc.d/init.d目錄下要有與服務名同名的可執行文件
所以我們可以在這個目錄下新建nginx文件
vim /etc/rc.d/init.d/nginx
內容:
#!/bin/sh
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
start() {
echo ‘Starting Nginx ...‘
/usr/local/nginx/sbin/nginx > /dev/null 2>&1 &
}
stop() {
echo ‘Stoping Nginx ...‘
/usr/local/nginx/sbin/nginx -s stop > /dev/null 2>&1 &
}
reload() {
echo ‘Reloading Nginx ...‘
/usr/local/nginx/sbin/nginx -s reload
}
if [ $# -ne 1 ]
then
echo ‘please input one params like start|restart|stop|reload‘
exit 1
fi
case "$1" in
‘start‘)
start
;;
‘stop‘)
stop
;;
‘restart‘)
stop
sleep 2
start
;;
‘reload‘)
reload
;;
‘*‘)
echo ‘please input one params like start|restart|stop|reload‘
;;
esac
記得給這個文件添加可執行權限
chmod +x /etc/rc.d/init.d/nginx
加入到開機自啟動:
chkconfig --add nginx
chkconfig --level 345 nginx on
五、虛擬主機配置
1、配置文件介紹
Nginx的配置文件位於安裝目錄下的conf目錄下
可以用以下命令查看非註釋部分的內容
egrep -v ‘#|^$‘ nginx.conf
-v 代表取反(去掉匹配到的內容)
在配置文件中,http段裏面的server段是配置虛擬主機使用的。如果想要配置多個虛擬主機,則就在http段裏面創建多個 server段即可。
註意配置文件中,每一行要使用分號結束,指令與{之間有空格。
Listen 後面可以是端口號,也可以是IP:端口號
Server_name 後面可以是域名,多個域名之間空格隔開,也可以是IP地址
在 http{}代碼段裏添加 client_max_body_size 200m; 以支持 php 上傳大文件。(請根據自己項目需求來定值)
六、編譯 php與nginx整合
1、編譯安裝php
(1) 第一步:安裝PHP
1) 下載php
shell># cd ~
shell># wget
2) 解壓並進入目錄
shell># tar zxf php-5.6.24.tar.gz
shell># cd php-5.6.24
3) 將configure參數及詳情解析另存為一個文件,以供學習參考用:
shell># ./configure --help > php_configure.txt
4) 編寫一個輔助shell文件,幫我們配置php
shell># vim php_install.sh
為了避免寫錯,建議大家直接復制以下內容(都寫在一行上,不要換行):
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --enable-opcache --with-zlib-dir --with-bz2 --with-libxml-dir=/usr --with-gd --with-freetype-dir --with-jpeg-dir --with-png-dir --enable-mbstring --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-iconv --disable-ipv6 --enable-static --enable-inline-optimization --enable-sockets --enable-soap --with-openssl --with-curl
以上內容的解釋如圖
把剛剛的shell文件加上可執行權限:
shell># chmod +x ./php_install.sh
5) 執行shell文件進行軟件的配置和環境檢測
shell># ./php_install.sh
6) 編譯軟件並且進行安裝
shell># make && make install
(2) 第二步:配置
1) 復制配置文件
shell># cp php.ini-production /usr/local/php/etc/php.ini
shell># cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
賦予其可執行權限
shell># chmod +x /etc/rc.d/init.d/php-fpm
拷貝產生php-fpm的配置文件
shell># cd /usr/local/php/etc
shell># cp php-fpm.conf.default php-fpm.conf
2) 配置php.ini
shell># vim php.ini
· 找到;date.timezone = 修改為 date.timezone = Asia/Shanghai
· 根據自己的需求調整以下選項的值
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
display_errors = On
max_execution_time = 60
max_input_time = 60
memory_limit = 256M
post_max_size = 256M
upload_max_filesize = 256M
3) 配置php-fpm.conf
shell># vim php-fpm.conf
· 找到user = nobody 和 group = nobody,將nobody改成www
· 找到listen.owner=nobody和listen.group= nobody,將nobody改成www
4) 將php-fpm加入服務並自動啟動
shell># service php-fpm start
shell># chkconfig --add php-fpm
shell># chkconfig --level 345 php-fpm on
2、配置nginx支持php
第一步:在nginx.conf中找到location ~ .php$
復制並去掉註釋,將root改成你的虛擬主機的路徑
保存並重載配置文件,
shell># /usr/local/nginx/sbin/nginx -s reload
在html目錄下新建一個PHP文件,代碼如下:
在瀏覽器輸入php頁面的網址,可以發現並不能訪問
原因是官方給的默認配置文件中,
要求要把php文件放到/scripts目錄下。其實這個要求我們不一定要遵從。
改進的方案如下:
保存並重載
就可以讓nginx支持訪問類似http://www.a.com/index.php以及http://www.a.com/index.php?id=5這樣的頁面了。
3、優化nginx配置文件
第二步:優化
如果把所有的server代碼段都放在nginx.conf裏面的話,會讓nginx.conf顯得又臃腫又亂。建議把它們分離去。
例如,直接把所有的server代碼段都剪切到/usr/local/nginx/conf/vhost.conf裏面
然後在nginx.conf裏面換成
include vhost.conf
虛擬主機的代碼都移到了vhost.conf裏面了
至此,nginx.conf已經很優化了,我們再來優化vhost.conf
第三步:優化vhost.conf
將root語句和 index語句都提取到location外面
第四步:再一次優化vhost.conf
把
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
這兩個語句都剪切到fastcgi.conf裏面去
只留下include fastcgi.conf;
fastcgi.conf文件中新增了以下兩句:
至此,nginx已經支持訪問php頁面,並且nginx.conf和vhost.conf已經很優化了。
只是還無法支持像thinkphp的pathinfo形式的訪問
http://www.a.com/index.php/Admin/public/login
4、與ThinkPHP整合
第一步:將vhost.conf的location ~ .php$ { 的$去掉
可以發現能支持pathinfo的訪問方式,但是無法識別出模塊、控制器和方法
然後將location代碼段寫成
location ~ .php {
fastcgi_split_path_info ^(.+.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi.conf;
保存並重載nginx
就可以支持thinkphp的pathinfo形式的訪問
第二步:優化vhost.conf
將這兩句
fastcgi_split_path_info ^(.+.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
都剪切到 fastcgi.conf裏面,只留下include fastcgi.conf; 語句即可
第三步:開啟偽靜態功能
在server裏面的location / { 代碼段裏,寫上
try_files $uri /index.php$uri;
保存並重載nginx
就可以支持thinkphp的rewrite訪問
http://www.a.com/Admin/public/login
七、負載均衡
1、負載均衡原理圖
2、實現一個簡單的負載均衡
服務器列表
本機 192.168.81.1
VM 192.168.81.6
騰訊雲 123.207.231.180
負載均衡器的配置
upstream demo {
server 192.168.81.1;
server 123.207.231.180;
}
server {
listen 80;
server_name fzjh.com;
location / {
proxy_pass http://demo;
proxy_set_header Host $host;
proxy_set_header X-Peal-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}}
並且要確保 fzjh.com要解析到負載均衡器的IP地址
保存配置文件,並且重載
訪問效果:
LNMP架構負載均衡