首頁  >  文章  >  運維  >  如何部署Nginx服務

如何部署Nginx服務

王林
王林轉載
2023-05-12 10:13:171463瀏覽

1. Nginx介紹:

1.1 Nginx是什麼?

Nginx(「engine x」)是一個開源的,支援高效能、高並發的www服務和代理服務軟體。

由俄羅斯人Igor Sysoev開發,最初應用於俄羅斯大型網站www.rambler.ru。

Nginx具有高並發、佔用系統資源少等特性。

Nginx可以運行在UNIX、Linux、DSB、Mac OS X、Solaris及Windows等作業系統上。

1.2 Nginx主要特性

支援高並發:能支援數萬並發連線

資源消耗少:三萬並發連線下,開始10個執行緒消耗記憶體不到200MB。

可以做HTTP反向代理及加速緩存,即負載平衡功能,內建對RS節點伺服器健康檢查功能

具備Squid等專業快取軟體的快取功能

支援非同步網路I/O事件模型

1.3 Nginx軟體的主要功能應用

#作為Web服務軟體

反向代理程式及負載平衡服務

前端業務資料快取服務

2. Nginx Web服務

2.1 Nginx作為Web伺服器應用場景

使用Nginx執行HTML、JS、CSS、小圖片等靜態資料

Nginx結合FastCGI運行PHP等動態程式

Nginx結合Tomcat/Resin等支援Java動態程式

2.2 如何選擇Web伺服器

#工作中,根據需求選擇合適的業務服務軟體:

  • 靜態業務:高並發場景,首選採用Nginx

  • 動態業務:Nginx與Apache都可,建議Nginx

  • 靜態動態業務:推薦Nginx

#3 編譯安裝Nginx

安裝方法多種,本文使用編譯安裝方式。如果需要大規模部署,可將業務需求客製化好rpm包,然後透過Ansible安裝。

3.1 安裝pcre庫

查看目前系統版本:

cat /etc/redhat-release
uname -r

結果:

CentOS release 6.10 (Final)
2.6.32-754.el6.x86_64

採用yum方式安裝pcre:

yum -y install pcre pcre-devel
rpm -qa pcre pcre-devel

結果:

pcre-devel-7.8-7.el6.x86_64
pcre-7.8-7.el6.x86_64

#3.2 安裝Nginx

#檢查是否裝有openssl、openssl-devel:

rpm -qa openssl openssl-devel

結果:如果沒有,使用yum安裝

openssl-1.0.1e-57.el6.x86_64
openssl-devel-1.0.1e-57.el6.x86_64

建立nginx套件存放目錄:

mkdir -p /app/nginx-1.8.1
mkdir -p /server/tools
cd /server/tools/

下載nginx軟體套件:

官方位址:www.nginx.rog

wget -q http://nginx.org/download/nginx-1.8.1.tar.gz

建立nginx使用者:

useradd nginx -s /sbin/nologin -M

解壓縮軟體包並進入解壓縮後的目錄:

tar xf nginx-1.8.1.tar.gz
cd nginx-1.8.1

進行編譯:
編譯模組可以透過./configure --help查看

./configure --user=nginx --group=nginx --prefix=/app/nginx-1.8.1/ --with-http_stub_status_module --with-http_ssl_module

安裝:

make
make install

建立軟連結:方便使用以及版本升級

ln -s /app/nginx-1.8.1/ /app/nginx

#啟動前測:

/app/nginx/sbin/nginx -t

結果:

nginx: the configuration file /app/nginx-1.8.1//conf/nginx.conf syntax is oknginx: configuration file /app /nginx-1.8.1//conf/nginx.conf test is successful

啟動Nginx服務並檢查連接埠:

/app/nginx/sbin/nginx
netstat -utpln | grep 80

結果:

tcp        0      0 0.0.0.0:80                  0.0.0.0:*    

##檢查Nginx啟動結果:以下內容代表啟動成功

curl 192.168.1.31

結果:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h2>Welcome to nginx!</h2>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/" rel="external nofollow" >nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/" rel="external nofollow" >nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

4. Nginx目錄結構與設定檔

4.1 Nginx目錄結構說明

tree /app/nginx
/app/nginx
├── client_body_temp
├── conf							#nginx配置文件目录
│   ├── fastcgi.conf				#fastcgi相关参数配置文件
│   ├── fastcgi.conf.default
│   ├── fastcgi_params				#fastcgi参数文件
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types					#媒体类型
│   ├── mime.types.default
│   ├── nginx.conf					#Nginx主配置文件
│   ├── nginx.conf.default
│   ├── scgi_params					#scgi配置文件
│   ├── scgi_params.default
│   ├── uwsgi_params				#uwsgi配置文件
│   ├── uwsgi_params.default
│   └── win-utf
├── fastcgi_temp					#fastcgi临时数据文件
├── html							#默认站点目录
│   ├── 50x.html					#错误页面显示文件
│   └── index.html					#默认的站点首页文件
├── logs							#默认日志路径
│   ├── access.log					#默认访问日志文件
│   ├── error.log					#默认错误日志文件
│   └── nginx.pid					#Nginx的pid文件
├── proxy_temp						#临时目录
├── sbin							#Nginx命令目录
│   ├── nginx						#启动命令
│   └── nginx.old
├── scgi_temp						#临时目录
└── uwsgi_temp						#临时目录

9 directories, 22 files

4.2 Nginx主設定檔

去註解顯示設定檔:

egrep -v "#|^$" /app/nginx/conf/nginx.conf.default

結果:

worker_processes  1;                            #worker进程数量
events {                                        #事件区块开始
    worker_connections  1024;                    #单worker进程支持的最大连接
}                                                #事件区块结束
http {                                            #HTTP区块开始
    include       mime.types;                    #支持的媒体类型库
    default_type  application/octet-stream;        #默认媒体类型
    sendfile        on;                            #开启高效传输模式
    keepalive_timeout  65;                        #连接超时
    server {                                    #server区块开始
        listen       80;                        #服务端口,默认80
        server_name  localhost;                    #域名主机名
        location / {                            #location区块开始
            root   html;                        #站点根目录
            index  index.html index.htm;        #默认首页文件
        }                                        #location区块结束
        error_page   500 502 503 504  /50x.html;#对应状态码及回应
        location = /50x.html {                    #location开始回应50x.html
            root   html;                        #站点目录为html
        }                                        
    }
}                                                #HTTP区块结束

註:server區塊和location區塊可以是多個。

以上是如何部署Nginx服務的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除