首先肯定是從nginx官方網站下載源代碼到本地。
http://nginx.org/en/download.html
如上圖所示,官網提供三種類型的源代碼,分別是:
- Mainline version - 主線版本,這個是當前最新的版本,但是不一定是穩定的;
- Stable version - 穩定版本,這個是當前的穩定版本,一般下載這個版本就可以了;
-
Legacy versions - 歷史版本,這里面都是以前的老版本,可以按照自己的實際需要挑選;
我選擇當前的穩定版本:nginx-1.18.0
我都是直接用root用戶進行操作,下載下來后解壓它:
[root@vb-node02 ~]#tar -zxf nginx-1.18.0.tar.gz
安裝nginx還需要依賴兩個庫,分別是:
- pcre
- zlib
如果需要開啟https的支持,則還需要依賴openssl庫。我們從官方文檔中可以找到如下說明:
--with-pcre=path
sets the path to the sources of the PCRE library. The library distribution (version 4.4 — 8.43) needs to be downloaded from the PCRE site and extracted. The rest is done by nginx’s ./configure and make. The library is required for regular expressions support in the location directive and for the ngx_http_rewrite_module module.
--with-zlib=path
sets the path to the sources of the zlib library. The library distribution (version 1.1.3 — 1.2.11) needs to be downloaded from the zlib site and extracted. The rest is done by nginx’s ./configure and make. The library is required for the ngx_http_gzip_module module.
這里面可以看到所依賴的這兩個庫的版本區間,我們分別去這兩個庫的官網下載符合要求的版本代碼,然后解壓它們。關于openssl的安裝說明中沒有提到版本號,我直接下載了當前最新版本。
openssl-1.1.1k.tar.gz
pcre-8.43.tar.gz
zlib-1.2.11.tar.gz
解壓這三個壓縮包,然后進入nginx的源代碼目錄,執行configure腳本,并且指定這三個庫的路徑。https的支持不是默認開啟的,所以我們除了指定openssl的路徑之外,還需要手動開啟它,開啟它的方式是添加參數:
--with-http_ssl_module
那么完整的命令如下所示:
./configure --with-pcre=../pcre-8.43 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.1.1k --with-http_ssl_module
這里我指定三個庫的路徑用的是相對路徑,你也可以使用絕對路徑。
執行完畢,如果沒有問題的話會在最后看到如下輸出:
Configuration summary
+ using PCRE library: ../pcre-8.43
+ using OpenSSL library: ../openssl-1.1.1k
+ using zlib library: ../zlib-1.2.11
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
這表示Makefile文件已經按照我們指定的參數生成完畢,接下來就可以編譯安裝了。
執行make命令安裝nginx:
make && make install
如果沒有報錯的話就表示nginx安裝成功。我們根據上面的信息進入到/usr/local/nginx
目錄下,可以看到如下四個目錄:
drwxr-xr-x. 2 root root 4096 3月 28 00:05 conf
drwxr-xr-x. 2 root root 40 3月 28 00:05 html
drwxr-xr-x. 2 root root 6 3月 28 00:05 logs
drwxr-xr-x. 2 root root 19 3月 28 00:05 sbin
然后我們執行
[root@vb-node02 nginx]# sbin/nginx
如果沒有報錯的話我們在瀏覽器中嘗試訪問一下。如果打開了如下頁面的話表示nginx啟動成功。
如果頁面打不開的話檢查一下防火墻是否沒有放開80端口的訪問限制。在centos7中可以用如下命令放開80端口:
[root@vb-node02 nginx]# firewall-cmd --add-port=80/tcp --permanent
[root@vb-node02 nginx]# firewall-cmd --reload
至此,從源代碼安裝nginx完畢。