# Nginx 篇
Nginx 是現如今性能最強勁的 Web 服務器及反向代理服務器
## 構建前
### 查看構建參數
查看當前版本全部構建參數
```sh
$ cd /package/lnmp/nginx-1.20.2
$ ./configure --help
```
### 模塊依賴環境
```sh
$ apt install make g++ libgeoip-dev pkg-config -y
```
查看 geoip 是否存在 pkg-config 列表中
```sh
$ pkg-config --list-all
```
## 構建指令
```sh
$ mkdir /package/lnmp/nginx-1.20.2/build_nginx
$ cd /package/lnmp/nginx-1.20.2
# 構建指令內容如下...
```
### 本次構建指令
```sh
$ ./configure --prefix=/server/nginx \
--builddir=/package/lnmp/nginx-1.20.2/build_nginx \
--user=nginx \
--group=nginx \
--error-log-path=/server/logs/nginx/error.log \
--http-log-path=/server/logs/nginx/access.log \
--pid-path=/server/run/nginx/nginx.pid \
# 核心功能模塊
--with-threads \
--with-file-aio \
# 啟用http功能模塊
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_geoip_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_stub_status_module \
# 禁用http功能模塊
--without-http_upstream_hash_module \
--without-http_upstream_ip_hash_module \
--without-http_upstream_least_conn_module \
--without-http_upstream_random_module \
--without-http_upstream_keepalive_module \
--without-http_upstream_zone_module \
# 外庫路徑
--with-pcre=/package/lnmp/pcre-8.45 \
--with-pcre-jit \
--with-zlib=/package/lnmp/zlib-1.2.11 \
--with-openssl=/package/lnmp/openssl-1.1.1l
```
### 允許構建的全部指令
```sh
$ ./configure --prefix=/server/nginx \
--builddir=/package/lnmp/nginx-1.20.2/build_nginx \
--user=nginx \
--group=nginx \
--error-log-path=/server/logs/nginx/error.log \
--http-log-path=/server/logs/nginx/access.log \
--pid-path=/server/run/nginx/nginx.pid \
# 核心功能模塊
--with-threads \
--with-file-aio \
# 啟用http功能模塊
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
# 啟用郵箱服務
--with-mail \
--with-mail_ssl_module \
# 啟用負載均衡服務
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module \
--with-stream_ssl_preread_module \
# 外庫路徑
--with-pcre=/package/lnmp/pcre-8.45 \
--with-pcre-jit \
--with-zlib=/package/lnmp/zlib-1.2.11 \
--with-openssl=/package/lnmp/openssl-1.1.1l \
# 開啟調試,生產環境下建議禁用
--with-debug
```
親測:nginx-1.20.2 在 `Debian 11` 下,能完成上面兩套指令的構建
### 開始編譯安裝
```sh
# 4核以上可以使用 make -j4 編譯
$ make -j2
# 不掛起,后臺執行
$ nohup make -j2 &
# 安裝
$ make install
```
### 測試
使用 curl 檢測是否成功
```sh
$ /server/nginx/sbin/nginx
$ curl -I 127.0.0.1
```
成功信號:
```sh
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Wed, 15 Sep 2021 12:39:28 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 15 Sep 2021 12:38:19 GMT
Connection: keep-alive
ETag: "6141e93b-264"
Accept-Ranges: bytes
```
失敗信號:
```sh
curl: (7) Failed to connect to 127.0.0.1 port 80: 拒絕連接
```
到此,nginx 構建結束!
## 記錄一次升級
Nginx 平滑升級,具體操作如下:
### 構建指令:
```sh
$ mkdir /package/lnmp/nginx-1.20.2/build_nginx
$ cd /package/lnmp/nginx-1.20.2
$ ./configure --prefix=/server/nginx \
# 構建指令參考前面的「本次構建指令」...
```
### 開始編譯
升級只編譯,不安裝
```sh
$ make
```
備份舊的二進制文件
```sh
$ mv /server/nginx/sbin/nginx{,.v1.20.1-01}
```
拷貝新的二進制文件
```sh
$ cp -p -r /package/lnmp/nginx-1.20.2/bulid_nginx/nginx /server/nginx/sbin/
```
### 平滑升級
nginx 平滑升級步驟如下:
1. 查看舊版 nginx 的 pid
通過 ps 指令查看
```sh
$ ps -ef|grep -E "nginx|PID" |grep -v grep
$ ps aux|grep -E "nginx|PID" |grep -v grep
```
通過 pid 文件查看
```sh
$ cat /server/run/nginx/nginx.pid
```
2. 使用 kill -USR2 <pid> 啟用新的 nginx 可執行文件
```sh
$ kill -USR2 `cat /server/run/nginx/nginx.pid`
```
3. 使用 kill -WINCH <pid> 來關閉舊的進程
指令實現:當進程沒有訪問者時,系統自動關閉當前進程
```sh
$ kill -WINCH old_nginx_pid
```
## 配置 nginx
nginx 的配置原理,在這里不做過多講解,直接給參考文件:
1. [nginx.conf](./nginx/nginx.conf.md)
```text
描述:nginx 主配置文件
數量:1個
路徑:/server/nginx/conf/nginx.conf
操作:替換
```
2. [fastcgi-tp.conf](./nginx/fastcgi-tp.conf.md)
```text
描述:tp6 站點的 fastcgi 模版
數量:按需新建,允許多個,命名需要區分
路徑:/server/nginx/conf/fastcgi-tp.conf
操作:新增,通過 include 加載
```
3. [cache.conf](./nginx/cache.conf.md)
```text
描述:靜態文件緩存模板
數量:按需新建,允許多個,命名需要區分
路徑:/server/nginx/conf/cache.conf
操作:新增,通過 include 加載
```
4. [gzip.conf](./nginx/gzip.conf.md)
```text
描述:靜態文件啟用壓縮
數量:按需新建,允許多個,命名需要區分
路徑:/server/nginx/conf/gzip.conf
操作:新增,通過 include 加載
```
5. [limit_req_http.conf](./nginx/limit_req_http.conf.md)
```text
描述:http 區塊,設置限制請求
數量:按需新建,允許多個,命名需要區分
路徑:/server/nginx/conf/limit_req_http.conf
操作:新增,通過 include 加載
```
6. [limit_req_server.conf](./nginx/limit_req_server.conf.md)
```text
描述:server 區塊,設置限制請求
數量:按需新建,允許多個,命名需要區分
路徑:/server/nginx/conf/limit_req_server.conf
操作:新增,通過 include 加載
```
7. [sites-statics.nginx](./nginx/sites-statics.nginx.md)
```text
描述:靜態站點配置模版
數量:按需新建,允許多個,命名需要區分
路徑:/server/sites/*.nginx
操作:新增
```
8. [sites-tp.nginx](./nginx/sites-tp.nginx.md)
```text
描述:tp6 站點配置
數量:按需新建,允許多個
路徑:/server/sites/*.nginx
操作:新增
```
## 管理 nginx
nginx 常用管理指令
| 操作 | 指令 |
| ------------ | ---------------------------------- |
| 啟動 | /server/nginx/sbin/nginx |
| 正常關閉 | /server/nginx/sbin/nginx -s quit |
| 快速關閉 | /server/nginx/sbin/nginx -s stop |
| 重新載入 | /server/nginx/sbin/nginx -s reload |
| 重新打開日志 | /server/nginx/sbin/nginx -s reopen |
| 檢測配置文件 | /server/nginx/sbin/nginx -t |
| 顯示幫助信息 | /server/nginx/sbin/nginx -h |
| 列出配置信息 | /server/nginx/sbin/nginx -T |
指定配置文件,啟動 Nginx
```sh
$ /server/nginx/sbin/nginx -c /server/nginx/conf/nginx.conf
```
檢測指定的 Nginx 配置文件
```sh
$ /server/nginx/sbin/nginx -t -c /server/nginx/conf/nginx.conf
```
強制停止 Nginx 進程
```sh
$ pkill -9 nginx
```
## Systemd 單元(Unit)
用 Systemd 來管理守護進程更方便,建議為 Nginx 添加 Systemd 單元(Unit)
### 具體操作
將 [nginx.service](./service/nginx.service.md) 拷貝/usr/lib/systemd/system 目錄
```sh
$ mv nginx.service /usr/lib/systemd/system/
```
使用類似如下指令加入開機啟動
```sh
$ systemctl enable nginx
```
重新加載 Systemd 配置文件
```sh
$ systemctl daemon-reload
```
### nginx 單元(Unit)管理
```sh
# 立即激活單元
$ systemctl start nginx.service
# 立即停止單元
$ systemctl stop nginx.service
# 重新加載配置
$ systemctl reload nginx.service
```
- Nginx 篇
- php 多版本
- 主配置文件
- 靜態站點配置模版
- tp6 站點的模版
- php 單版本
- 主配置文件
- 靜態站點配置模版
- tp6 站點的模版
- 靜態文件啟用壓縮
- 限制請求-http 塊
- 限制請求-server 塊
- 緩存配置文件
- tp6 站點的 fastcgi 模版
- MariaDB 篇
- MariaDB 主配置文件
- MariaDB 服務端子配置文件
- MariaDB 密鑰
- redis 篇
- sqlite3 篇
- php
- PHP 單版本篇
- PHP 多版本篇(new)
- php-fpm 主配置文件(php 單版本)
- php-fpm 工作池配置文件(php 單版本)
- '8.0(php 多版本)'
- php-fpm 主配置文件
- php-fpm 工作池進程配置文件
- '8.1(php 多版本)'
- php-fpm 主配置文件
- php-fpm 工作池進程配置文件
- service
- Nginx 系統單元文件
- MariaDB 系統單元文件
- redis 系統單元文件
- php-fpm 系統單元文件(php 單版本)
- phpfpm-8.0 系統單元文件(php 多版本)
- phpfpm-8.1 系統單元文件(php 多版本)
- vsftpd 篇
- pam-mysql 配置文件
- vsftpd 配置文件
- sh
- 刪除 lnmp 目錄腳本
- 創建 lnmp 目錄腳本
- 解壓 lnmp 安裝包腳本