前言:
nginx官網下載地址:
地址一:http://nginx.org/en/download.html
地址二:http://nginx.org/download/
以上兩個地址都是nginx官網提供的下載地址,可根據實際情況下載自己需要的版本。
或者通過我的倉庫進行下載,相關安裝包下載地址:
nginx下載
1.安裝gcc
因為linux系統版本是aliyun,已經安裝了gcc 環境。所以不需要安裝,但是centos7等系統需要gcc環境編譯。
掛載鏡像后使用
yum install -y gcc gcc-c++
2.安裝PCRE依賴庫
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫
cd pcre-8.42/ ./configure make && make install
3.安裝zlib 依賴庫
zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。
cd zlib-1.2.11/ ./configure make && make install
4.安裝OpenSSL安全套接字層密碼庫
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。
cd openssl-1.0.2r/ ./config make && make install
5.安裝Nginx
tar -zxvf nginx-1.14.2.tar.gz mv nginx-1.14.2 nginx cd nginx/ ./configure --with-pcre=/usr/local/pcre-8.42/ --with-zlib=/usr/local/zlib-1.2.11/ --with-openssl=/usr/local/openssl-1.0.2r/ --prefix=/usr/local/nginx make && make install
注:
–prefix是將nginx安裝的哪個目錄,如果沒有指定,將安裝到/usr/local/nginx中,如果指定的目錄中沒有nginx文件夾,需要自己創建一個文件夾。
–with-pcre:后面的目錄不能是 pcre 安裝的目錄,一定要是 pcre 的文件目錄才可以,否則有些文件就找不到
啟動Nginx提示報錯
nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)
2021/05/31 18:32:24 [emerg] 41780#0: open() "/usr/local/nginx/logs/access.log" failed (2: No such file or directory)
原因:nginx下沒有logs這些文件夾 。解決發方法:
[root@localhost nginx]# mkdir logs [root@localhost nginx]# touch logs/error.log [root@localhost nginx]# touch logs/access.log [root@localhost nginx]# ./sbin/nginx [root@localhost nginx]#firewall-cmd --zone=public --add-port=80/tcp --permanent [root@localhost nginx]#firewall-cmd --reload
6.測試是否安裝成功
curl IP地址 或者通過瀏覽器輸入IP地址進行訪問 成功的話會出現nginx頁面
拓展
常見問題:
[root@localhost sbin]# ./nginx -s reload nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
解決方法:
使用nginx -c的參數指定nginx.conf文件的位置
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
查看nginx版本
[root@localhost ~]# cd /usr/local/nginx/sbin/ [root@localhost sbin]# ./nginx -V nginx version: nginx/1.14.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) configure arguments: --with-pcre=/usr/local/pcre-8.42/ --with-zlib=/usr/local/zlib-1.2.11/ --with-openssl=/usr/local/openssl-1.0.2r/ --prefix=/usr/local/nginx