日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LNMP架构(二)

發布時間:2023/12/9 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LNMP架构(二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

一? Nginx安裝

1、切換目錄

? ? # cd /usr/local/src

2、下載

? ? # wget http://nginx.org/download/nginx-1.12.1.tar.gz

3、解壓

? ? # tar xzvf nginx-1.12.1.tar.gz

4、切換到nginx目錄下

? ? # cd?nginx-1.12.1/

5、編譯

? ? # ./configure --prefix=/usr/local/nginx

以上編譯參數是一般情況下使用,在實際編譯時,需要根據需求添加參數,比如網站添加了ssl證書時,就需要將ssl相關的模塊給nginx配置上,因此我們需要注意后續將下載的源碼包保留,可能后續會用到里面包含的某個模塊

6、make && make install

? ? # make && make install

安裝完成后,我們來看下/usr/local/nginx目錄下的文件

其中conf下是nginx的配置文件

html下是樣例文件

logs目錄存放日志文件,sbin下面是進程,也就是核心文件

支持 -t檢查配置文件語法

7、編輯啟動腳本

? ? 啟動腳本需要放在/etc/init.d/目錄下

? ? # vim /etc/init.d/nginx? ? ? ?//創建并編輯啟動腳本

? ? 在啟動腳本中加入以下內容(參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx):

#!/bin/bash

# chkconfig: - 30 21

# description: http service.

# Source Function Library

. /etc/init.d/functions

# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"

NGINX_CONF="/usr/local/nginx/conf/nginx.conf"

NGINX_PID="/usr/local/nginx/logs/nginx.pid"

RETVAL=0

prog="Nginx"

start()

{

echo -n $"Starting $prog: "

mkdir -p /dev/shm/nginx_temp

daemon $NGINX_SBIN -c $NGINX_CONF

RETVAL=$?

echo

return $RETVAL

}

stop()

{

echo -n $"Stopping $prog: "

killproc -p $NGINX_PID $NGINX_SBIN -TERM

rm -rf /dev/shm/nginx_temp

RETVAL=$?

echo

return $RETVAL

}

reload()

{

echo -n $"Reloading $prog: "

killproc -p $NGINX_PID $NGINX_SBIN -HUP

RETVAL=$?

echo

return $RETVAL

}

restart()

{

stop

start

}

configtest()

{

$NGINX_SBIN -c $NGINX_CONF -t

return 0

}

case "$1" in

start)

start

;;

stop)

stop

;;

reload)

reload

;;

restart)

restart

;;

configtest)

configtest

;;

*)

echo $"Usage: $0 {start|stop|reload|restart|configtest}"

RETVAL=1

esac

exit $RETVAL

8、修改啟動腳本權限

? ? # chmod 755 /etc/init.d/nginx

9、將nginx加入啟動服務列表

? ? # chkconfig --add nginx

10、編輯配置文件

? ? 此時/usr/local/nginx/conf/目錄下已經有一個nginx.conf配置文件,

但是我們不用這個配置文件,我們用自己寫的配置文件,所有我們先將原來的nginx.conf備份

? ? # mv nginx.conf nginx.conf.bak

接下來創建并編輯我們自己的nginx.conf配置文件

? ? # vim nginx.conf

在配置文件中加入以下內容(參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf):

user nobody nobody;? //user用于定義啟動nginx的是哪個用戶,也就是如果要讓nginx去做讀寫操作時是使用誰的身份去操作的

worker_processes 2;? //定義子進程有幾個

error_log /usr/local/nginx/logs/nginx_error.log crit;

pid /usr/local/nginx/logs/nginx.pid;

worker_rlimit_nofile 51200;? ? //定義nginx最多可以打開多少個文件

events

{

use epoll;? ?//使用epoll模式

worker_connections 6000;? //進程最大連接數

}

http

{

include mime.types;

default_type application/octet-stream;

server_names_hash_bucket_size 3526;

server_names_hash_max_size 4096;

log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'

' $host "$request_uri" $status'

' "$http_referer" "$http_user_agent"';

sendfile on;

tcp_nopush on;

keepalive_timeout 30;

client_header_timeout 3m;

client_body_timeout 3m;

send_timeout 3m;

connection_pool_size 256;

client_header_buffer_size 1k;

large_client_header_buffers 8 4k;

request_pool_size 4k;

output_buffers 4 32k;

postpone_output 1460;

client_max_body_size 10m;

client_body_buffer_size 256k;

client_body_temp_path /usr/local/nginx/client_body_temp;

proxy_temp_path /usr/local/nginx/proxy_temp;

fastcgi_temp_path /usr/local/nginx/fastcgi_temp;

fastcgi_intercept_errors on;

tcp_nodelay on;

gzip on;

gzip_min_length 1k;

gzip_buffers 4 8k;

gzip_comp_level 5;

gzip_http_version 1.1;

gzip_types text/plain application/x-javascript text/css text/htm

application/xml;

server? ?//每個server對應一個虛擬主機

{

listen 80;? //nginx監聽的端口

server_name localhost;? //網站域名

index index.html index.htm index.php;??

root /usr/local/nginx/html;? ?//網站的根目錄

location ~ \.php$? ? //用于配置解析php的

{

include fastcgi_params;

fastcgi_pass unix:/tmp/php-fcgi.sock;? //指定php-fpm監聽的端口或socket,可替代為fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;

}

}

}

11、檢查配置文件語法錯誤

? ? # /usr/local/nginx/sbin/nginx -t

12、啟動

? ? # /etc/init.d/nginx start? ? //啟動前應確認apache服務已經關閉,如果沒有關閉,出現如下現象

13、查看nginx是否成功啟動

? ? # ps aux |grep nginx? ? //下圖中Ss表示此進程為父進程,下面為2個worker process子進程,一般父進程屬主都是root,子進程屬主為上面我們配置的nobody

? ? # ps aux |grep php-fpm? ?//同樣,php-fpm父進程屬主都是root,子進程屬主為上面我們配置的php-fpm

14、測試訪問默認頁面

????使用下面這兩組命令訪問的結果是一樣的

? ? # curl localhost

? ? # curl 127.0.0.1:80?

上圖中訪問到的頁面實際上就是我們在配置文件中配置的第一個虛擬主機(也是默認虛擬主機)設置的默認訪問頁面/usr/local/nginx/html/index.html

進一步測試,我們在/usr/local/nginx/html/目錄下寫一個1.php的文件,看是否能成功解析,1.php中寫入以下內容:

? ? <?php

? ? echo "This is nginx test page!";

? ? ?>

保存后,我們來訪問這個頁面

? ? # curl localhost/1.php? //下面是訪問成功截圖

?

二 默認虛擬主機

1、修改配置文件

? ? 去掉下圖框中內容:

? ? 在倒數第二行增加如下內容:

????????include vhost/*.conf;? ?//表示虛擬主機配置文件放在當前目錄下的子目錄vhost中;

2、創建虛擬主機配置文件存放目錄

? ? # mkdir vhost? ? //當前目錄為/usr/local/nginx/conf/

3、創建并編輯虛擬主機配置文件

? ? # vim vhost/aaa.com.conf? ? ?

? ? 在文件中寫入以下內容:

server

{ ? ?

????listen 80 default_server; ?// 有default_server這個標記的就是默認虛擬主機 ? ?

????server_name aaa.com; ? ?

????index index.html index.htm index.php;? ? ?//指定索引頁

????root /data/wwwroot/default;? ?//網站所在目錄

}

4、創建網站根目錄

? ? 如果網站指定的目錄還不存在的話,需要創建這個目錄

? ? # mkdir /data/wwwroot/default

5、在網站目錄下寫一個頁面

? ? # cd?/data/wwwroot/default

? ? # vim index.html

????文件中寫入以下內容:

? ?

6、測試nginx配置文件是否有語法錯誤

? ? # /usr/local/nginx/sbin/nginx -t

7、重新加載配置文件

? ? # /etc/init.d/nginx?restart? ? ? //此命令直接通過重啟nginx來重新加載配置文件

? ? # /usr/local/nginx/sbin/nginx -s reload? ? ?//此命令不用重啟nginx服務就可以重新加載配置文件

8、測試訪問我們寫的頁面

? ? # curl localhost

? ? # curl -x127.0.0.1:80 bbb.com? //目前是沒有這個網站的,因此我們訪問到的是默認的虛擬主機

8、如何區分默認虛擬主機和非默認虛擬主機

? ? nginx找server的時候肯定會從第一個開始,因此在沒有設定默認虛擬主機時,nginx會自動將排在第一個的server作為默認虛擬主機

? ? 另外就是找帶有default_server標記的server即為默認虛擬主機

三 Nginx用戶認證

1、創建一個測試用的虛擬主機

? ? 首先切換到vhost虛擬主機配置文件目錄下,新增一個虛擬主機配置文件

? ? # test.com.conf?

? ? 在這個文件里寫入以下內容:

server

{

??? listen 80;

??? server_name test.com;

??? index index.html index.htm index.php;

??? root /data/wwwroot/test.com;

???

location? /

??? {

??????? auth_basic????????????? "Auth"; ? //用于定義用戶認證的名字

??????? auth_basic_user_file?? /usr/local/nginx/conf/htpasswd; ? //用戶名密碼文件

????}

}

2、生成密碼文件

? ? 使用apache2.4自帶的htpsswd命令或文件來生成密碼文件,如果沒有安裝apache,可以使用yum命令安裝一個

? ? #?/usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd lijie
其中-c指創建,/usr/local/nginx/conf/htpasswd是存放路徑 ,lijie是用戶名

按照上圖提示輸入兩次密碼后,我們再來查看密碼文件內容

接下來再來創建一個用戶就不用加-c選項了

3、測試語法并重新加載

? ? #?/usr/local/nginx/sbin/nginx -t
????# /usr/local/nginx/sbin/nginx -s reload
4、訪問這個測試虛擬主機

? ? # curl -x127.0.0.1:80 test.com -I ? ?//提示401錯誤,需要用戶認證

? ? 接下來我們輸入用戶名和密碼來訪問

????# curl -ulj:112233 -x127.0.0.1:80 test.com ? //其中lj是用戶名,112233是密碼

? ? 我們看到上圖提示404錯誤,這是因為我們之前沒有創建訪問的目錄及索引頁,現在我們來創建一個

? ? 這時我們再來訪問就可以看到index.html中的內容了

5、針對目錄的用戶認證

? ? 我們只需要在虛擬主機配置文件/usr/local/nginx/conf/vhost/test.com.conf中作出修改即可

修改前的配置文件內容

修改后的配置文件內容如下,表示是針對admin這個目錄進行用戶認證

? ? 修改后,訪問test.com就不需要做用戶認證,只有訪問test.com/admin時才需要做用戶認證

6、針對某個頁面的用戶認證

? ? 訪問某個頁面才需要用戶認證的話,需要將配置文件/usr/local/nginx/conf/vhost/test.com.conf修改為如下內容:

? ? 上圖中~表示匹配,~ admin.php表示匹配到admin.php這個頁面就需要做用戶認證

四 Nginx域名重定向

1、修改配置文件

? ? 當前我們已經有test.com這個域名來訪問test.com這個網站,為了使另外兩個域名test2.com和test3.com也跳轉到test.com這個網站,我們需要修改配置文件

? ? # vim /usr/local/nginx/conf/vhost/test.com.conf

? ? 以下內容為域名重定向使用的代碼

? ? if ($host != 'test.com' ) {

??????? rewrite? ^/(.*)$? http://test.com/$1? permanent;

??? }

? ? 修改前

? ? 修改后,下圖框中部分為新增內容:

? ? 上面^/(.*)$是http://$host/(.*)$的簡寫,^表示的就是域名,(.*)$代表的是域名后面跟著的一長串,$1代表的就是(.*)

????permanent為永久重定向,狀態碼為301,如果寫redirect則為302

2、檢查語法并重新加載

? ? # /usr/local/nginx/sbin/nginx -t

? ? # /usr/local/nginx/sbin/nginx ?-s reload?

3、測試驗證域名重定向

? ? 驗證訪問test2.com,可以看到狀態碼301重定向到test.com

????驗證訪問test2.com/wrwer/err,可以看到狀態碼301重定向到test.com/wrwer/err

?????驗證訪問test3.com/wrwer/err,可以看到狀態碼301重定向到test.com/wrwer/err

????驗證訪問test4.com/wrwer/err,提示頁面未找到,實際上是跳轉到了默認虛擬主機aaa.com去了。

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

轉載于:https://my.oschina.net/u/3746774/blog/1632648

總結

以上是生活随笔為你收集整理的LNMP架构(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。