1、下載nginx源碼
官網地址: http://nginx.org/en/download.html
選擇 Stable version:最新穩定版本
下載後可以解壓移動到/usr/local/bin目錄下:
mv nginx-1.12.2.tar.gz /usr/local/bin
2、安裝依賴包
OpenSSL
在官網下到最新穩定版。 https://www.openssl.org/source/
PCRE
下載地址: https://ftp.pcre.org/pub/pcre/
注意有兩個大版本:8.x和10.x,nginx依賴的是8.x,所以選擇 pcre-8.*的最高版本。
zlib
下載地址:http://zlib.net/
把以上庫的解壓目錄也移動到/usr/local/bin(和 nginx 同目錄):
mv openssl-1.1.0g pcre-8.41 zlib-1.2.11 /usr/local/bin
3、編譯安裝
進入之前解壓的 nginx 目錄:
cd /usr/local/bin/nginx-1.18.0/
執行配置命令,幾個依賴包的路徑對就可以,官方文檔提示要寫到一行:
參考官方文檔: http://nginx.org/en/docs/configure.html
./configure --sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=../pcre-8.44 \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-1.1.1g
注意以上依賴包要改成自己的實際版本。
一陣 checking 無報錯信息之後配置成功
然後就可以編譯了:
make
一陣編譯無報錯信息之後安裝:
sudo make install
4、使用
/usr/local/nginx/nginx -h
nginx version: nginx/1.18.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: /usr/local/nginx/nginx.conf)
-g directives : set global directives out of configuration file
5、配置簡易操作命令
如果怕nginx命令操作比較麻煩,可以參考lnmp設計的操作腳本
sudo lnmp -h
+-------------------------------------------+
| Manager for LNMP, Written by Licess |
+-------------------------------------------+
| https://lnmp.org |
+-------------------------------------------+
Usage: lnmp {start|stop|reload|restart|kill|status}
Usage: lnmp {nginx|mysql|mariadb|php-fpm|pureftpd} {start|stop|reload|restart|kill|status}
Usage: lnmp vhost {add|list|del}
Usage: lnmp database {add|list|edit|del}
Usage: lnmp ftp {add|list|edit|del|show}
Usage: lnmp ssl add
Usage: lnmp {dnsssl|dns} {cx|ali|cf|dp|he|gd|aws}
Usage: lnmp onlyssl {cx|ali|cf|dp|he|gd|aws}
或配置alias命令別名:
將以下代碼放到 ~/.bash_profile 文件中
alias nginx restart='sudo /usr/local/nginx/nginx -s stop && sudo /usr/local/nginx/nginx'
alias nginx start='sudo /usr/local/nginx/nginx'
alias nginx stop='sudo /usr/local/nginx/nginx -s stop'
source ~/.bash_profile