文章目錄
- NGINX源碼安裝
- 安裝依賴包
- 獲取源碼
- NGINX官方網站
- 各個目錄的用途
- 編譯安裝
- 安裝結束后的文件
- 設置為服務
NGINX源碼安裝
安裝依賴包
root執行
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
這些包是用于開發和構建軟件的一些常見庫和工具。
1.gcc: GNU Compiler Collection 的縮寫,是一個用于編譯源代碼的編譯器套件。它可以將高級編程語言(如C、C++等)編譯為目標機器的可執行二進制代碼。NGINX就是用C寫的,要編譯成二進制的文件。
2. gcc-c++: 這是 GCC 編譯器的 C++ 支持,它允許您編譯和構建使用 C++ 編程語言編寫的軟件。NGINX雖然是C語言寫的,但不可避免的使用到C++的一些特性。
3. make: 是一個自動化構建工具,用于處理源代碼和構建過程。通過 make
命令,您可以根據預定義的規則和指令自動執行編譯、鏈接和安裝等任務。
4. libtool: 是一個用于處理共享庫的工具,它允許開發者在不同的操作系統上創建和使用共享庫。
5. zlib 和 zlib-devel: zlib 是一個用于數據壓縮和解壓縮的庫,廣泛用于許多軟件中以減小數據的存儲空間和傳輸帶寬。zlib-devel 包含用于開發和編譯 zlib 相關程序的頭文件和庫文件。
6. openssl 和 openssl-devel: OpenSSL 是一個開源的加密庫,提供了許多加密和安全相關的功能,如 SSL/TLS 協議、數字證書管理等。openssl-devel 包含了用于開發和編譯使用 OpenSSL 的程序的頭文件和庫文件。
7. pcre 和 pcre-devel: PCRE(Perl Compatible Regular Expressions)是一個正則表達式庫,用于處理文本匹配和替換操作。pcre-devel 包含用于開發和編譯使用 PCRE 的程序的頭文件和庫文件。
以上軟件是必要的軟件包,如果不安裝以上包可能會存在某些配置或功能不能使用的情況。
獲取源碼
NGINX官方網站
NGINX有兩個版本,社區版和商業版
我們使用社區版進行研究,一般情況下社區版是能滿足我們的需求的
NGINX社區版又分mainline版本和stable版本, 想嘗鮮用mainline,想穩定用stable,一般我們線上選擇stable版本,即中間位數是偶數的版本。
### 獲取步驟
# 創建升級的文件夾
mkdir -p /opt/darren/upgrade
cd /opt/darren/upgrade
# 獲取安裝的源碼
wget -c https://nginx.org/download/nginx-1.24.0.tar.gz
# 解壓包
tar -zxvf nginx-1.24.0.tar.gz
cd /opt/darren/upgrade/nginx-1.24.0
各個目錄的用途
[root@WDQCVM nginx-1.24.0]# ll
total 816
drwxr-xr-x 6 wdq wdq 326 Aug 14 18:39 auto
-rw-r--r-- 1 wdq wdq 323312 Apr 11 09:45 CHANGES
-rw-r--r-- 1 wdq wdq 494234 Apr 11 09:45 CHANGES.ru
drwxr-xr-x 2 wdq wdq 168 Aug 14 18:39 conf
-rwxr-xr-x 1 wdq wdq 2611 Apr 11 09:45 configure
drwxr-xr-x 4 wdq wdq 72 Aug 14 18:39 contrib
drwxr-xr-x 2 wdq wdq 40 Aug 14 18:39 html
-rw-r--r-- 1 wdq wdq 1397 Apr 11 09:45 LICENSE
-rw-r--r-- 1 root root 458 Aug 14 18:53 Makefile
drwxr-xr-x 2 wdq wdq 21 Aug 14 18:39 man
drwxr-xr-x 3 root root 125 Aug 14 18:53 objs
-rw-r--r-- 1 wdq wdq 49 Apr 11 09:45 README
drwxr-xr-x 9 wdq wdq 91 Aug 14 18:39 src
[root@WDQCVM nginx-1.24.0]#
- auto目錄,編譯、支持哪些模塊、當前系統有哪些特性給NGINX使用
- CHANGES 提供了哪些特性,和BugFix,.ru是因為NGINX之父是俄羅斯人
- conf 示例文件
- configure編譯文件
- contrib 該文件中是一些對NGINX支持高亮顯示的文件,這里的內容拷貝到VIM中可以高亮
- html 提供了500 和index文件
- man NGINX的幫助文件
- src NGINX源代碼
- objs 是生成中間文件的目錄
- 其中有個ngx_modules.c的文件,這里記錄所有進去的NGINX模塊
編譯安裝
查看configure支持的參數
這里有很多參數,如果不需要變動則只需指定--prefix
指定編譯后的路徑即可
還有帶有--with-……
這種默認是不會編譯進NGINX的,相反 --without-……
這種默認是編譯進NGINX中的
[root@WDQCVM nginx-1.24.0]# ./configure --help--help print this message--prefix=PATH set installation prefix--sbin-path=PATH set nginx binary pathname--modules-path=PATH set modules path--conf-path=PATH set nginx.conf pathname--error-log-path=PATH set error log pathname--pid-path=PATH set nginx.pid pathname--lock-path=PATH set nginx.lock pathname--user=USER set non-privileged user forworker processes--group=GROUP set non-privileged group forworker processes--build=NAME set build name--builddir=DIR set build directory--with-select_module enable select module--without-select_module disable select module--with-poll_module enable poll module--without-poll_module disable poll module--with-threads enable thread pool support--with-file-aio enable file AIO support--with-http_ssl_module enable ngx_http_ssl_module
……--with-http_stub_status_module enable ngx_http_stub_status_module--without-http_charset_module disable ngx_http_charset_module--without-http_gzip_module disable ngx_http_gzip_module
……--without-http_upstream_zone_moduledisable ngx_http_upstream_zone_module--with-http_perl_module enable ngx_http_perl_module
……--with-perl=PATH set perl binary pathname--http-log-path=PATH set http access log pathname--http-client-body-temp-path=PATH set path to storehttp client request body temporary files--http-proxy-temp-path=PATH set path to storehttp proxy temporary files--http-fastcgi-temp-path=PATH set path to storehttp fastcgi temporary files--http-uwsgi-temp-path=PATH set path to storehttp uwsgi temporary files--http-scgi-temp-path=PATH set path to storehttp scgi temporary files--without-http disable HTTP server--without-http-cache disable HTTP cache--with-mail enable POP3/IMAP4/SMTP proxy module……--with-stream_ssl_preread_module enable ngx_stream_ssl_preread_module--without-stream_limit_conn_module disable ngx_stream_limit_conn_module……--without-stream_upstream_zone_moduledisable ngx_stream_upstream_zone_module--with-google_perftools_module enable ngx_google_perftools_module
……--with-openssl-opt=OPTIONS set additional build options for OpenSSL--with-debug enable debug logging[root@WDQCVM nginx-1.24.0]#
編譯
cd /opt/darren/upgrade/nginx-1.24.0
mkdir -p /opt/darren/dev/
./configure --prefix=/opt/darren/dev/nginx --with-http_ssl_module --with-http_stub_status_module --with-stream --with-http_gzip_static_module --with-pcre
make
make install
./configure
:這是運行 NGINX 配置腳本的命令。--prefix=/opt/darren/dev/nginx
:指定 NGINX 的安裝路徑為 /opt/darren/dev/nginx,這是 NGINX 被安裝到的主目錄。--with-http_ssl_module
:啟用 HTTP SSL 模塊,允許 NGINX 支持通過 HTTPS 提供加密的安全連接。--with-http_stub_status_module
:啟用 HTTP Stub Status 模塊,該模塊提供了一個簡單的狀態頁面,用于監控 NGINX 的運行狀態和統計信息。--with-stream
:啟用 Stream 模塊,使 NGINX 能夠處理基于 TCP 或 UDP 的流量。--with-http_gzip_static_module
:啟用 HTTP Gzip Static 模塊,該模塊允許 NGINX 在服務器端對靜態文件進行 Gzip 壓縮,以提高傳輸效率。--with-pcre
:啟用 PCRE(Perl Compatible Regular Expressions)模塊,該模塊用于支持正則表達式,通常用于 URI 路徑匹配等。- make 和 make install 來執行完成安裝操作
在編譯過程中有很多信息,這些信息只要沒有報錯信息即可,如果有報錯信息則需要排查錯誤后再編譯安裝。
安裝結束后的文件
編譯的文件是
[root@WDQCVM nginx]# cd /opt/darren/dev/nginx
[root@WDQCVM nginx]# ll
total 0
drwxr-xr-x 2 root root 333 Aug 14 19:30 conf
drwxr-xr-x 2 root root 40 Aug 14 19:30 html
drwxr-xr-x 2 root root 6 Aug 14 19:30 logs
drwxr-xr-x 2 root root 19 Aug 14 19:30 sbin
[root@WDQCVM nginx]#
- conf配置文件
- html 靜態頁面
- logs存放日志的目錄
- sbin二進制文件存放的地方
設置為服務
服務啟動
# 測試語法是否有問題
[root@WDQCVM sbin]# /opt/darren/dev/nginx/sbin/nginx -t -c /opt/darren/dev/nginx/conf/nginx.conf
nginx: the configuration file /opt/darren/dev/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/darren/dev/nginx/conf/nginx.conf test is successful
# 啟動
[root@WDQCVM sbin]# /opt/darren/dev/nginx/sbin/nginx -c /opt/darren/dev/nginx/conf/nginx.conf
[root@WDQCVM sbin]# ps -ef|grep nginx
root 6677 1 0 19:35 ? 00:00:00 nginx: master process /opt/darren/dev/nginx/sbin/nginx -c /opt/darren/dev/nginx/conf/nginx.conf
nobody 6678 6677 0 19:35 ? 00:00:00 nginx: worker process
root 6680 1148 0 19:35 pts/0 00:00:00 grep --color=auto nginx
[root@WDQCVM sbin]#
將NGINX設置為服務
[root@WDQCVM sbin]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network. target remote-fs.target nss -lookup. target[Service]
Type=forking
PIDFile=/opt/darren/dev/nginx/logs/nginx.pid
ExecStartPre=/opt/darren/dev/nginx/sbin/nginx -t -c /opt/darren/dev/nginx/conf/nginx.conf
ExecStart=/opt/darren/dev/nginx/sbin/nginx -c /opt/darren/dev/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true[Install]
WantedBy=multi-user.target
服務重載
systemctl daemon-reload
以服務的方式啟動
[root@WDQCVM sbin]# systemctl start nginx
[root@WDQCVM sbin]# systemctl status nginx
● nginx.service - nginx - high performance web serverLoaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)Active: active (running) since Mon 2023-08-14 19:40:15 CST; 1s agoDocs: https://nginx.org/en/docs/Process: 6737 ExecStart=/opt/darren/dev/nginx/sbin/nginx -c /opt/darren/dev/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)Process: 6736 ExecStartPre=/opt/darren/dev/nginx/sbin/nginx -t -c /opt/darren/dev/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)Main PID: 6739 (nginx)CGroup: /system.slice/nginx.service├─6739 nginx: master process /opt/darren/dev/nginx/sbin/nginx -c /opt/darren/dev/nginx/conf/nginx.conf└─6740 nginx: worker processAug 14 19:40:15 WDQCVM systemd[1]: [/usr/lib/systemd/system/nginx.service:6] Failed to add dependency on -lookup., ignoring: Invalid argument
Aug 14 19:40:15 WDQCVM systemd[1]: [/usr/lib/systemd/system/nginx.service:6] Failed to add dependency on target, ignoring: Invalid argument
Aug 14 19:40:15 WDQCVM systemd[1]: Starting nginx - high performance web server...
Aug 14 19:40:15 WDQCVM nginx[6736]: nginx: the configuration file /opt/darren/dev/nginx/conf/nginx.conf syntax is ok
Aug 14 19:40:15 WDQCVM nginx[6736]: nginx: configuration file /opt/darren/dev/nginx/conf/nginx.conf test is successful
Aug 14 19:40:15 WDQCVM systemd[1]: Started nginx - high performance web server.
[root@WDQCVM sbin]#
設置為開機啟動
[root@WDQCVM sbin]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@WDQCVM sbin]