天天看點

nginx-1.22.1在linux伺服器上的安裝

作者:小林軟體工作室

一、環境準備

1、gcc安裝

安裝 nginx 需要先将官網下載下傳的源碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:

yum install gcc gcc-c++           

2、pcre pcre-devel 安裝

PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 相容的正規表達式庫。Nginx的http 子產品使用pcre來解析正規表達式,是以需要在Linux上安裝pcre庫,pcre-devel 是使用pcre開發的一個二次開發庫。Nginx也需要此庫。指令:

yum install -y pcre pcre-devel           

3、 zlib安裝

zlib 庫提供了很多種壓縮和解壓縮的方式, Nginx使用zlib對http包的内容進行gzip,是以需要在Linux Centos 7上安裝zlib庫。

yum install -y zlib zlib-devel           

4、openssl安裝

OpenSSL是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協定,并提供豐富的應用程式供測試或其它目的使用。

Nginx不僅支援http協定,還支援https(即在ssl協定上傳輸http),是以需要在Linux Centos 7安裝OpenSSL庫。

yum install -y openssl openssl-devel           

二、安裝Nginx

1、下載下傳

用wget指令下載下傳(推薦)。確定系統已經安裝了wget,如果沒有安裝,執行 yum install wget 安裝。

#下載下傳,這裡也可以去官網下載下傳
wget -c https://nginx.org/download/nginx-1.22.1.tar.gz
#解壓
tar -zxf nginx-1.22.1.tar.gz -C ./
cd nginx-1.22.1           

2、檢查配置并指定安裝參數

./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf  --with-http_stub_status_module --with-http_ssl_module           
  • ./configure 是編譯前檢查的指令,
  • —prefix=/usr/local/nginx 是安裝到指定目錄,
  • —with-http_stub_status_module —with-http_ssl_module 是安裝ssl證書的兩個子產品

3、編譯安裝

make&&make install           

4、操作

#進入目錄
cd /usr/local/nginx/sbin/
./nginx #啟動
./nginx -s quit   # 此方式停止步驟是待nginx程序處理任務完畢進行停止。
./nginx -s stop   # 此方式相當于先查出nginx程序id再使用kill指令強制殺掉程序。
./nginx -t         #校驗配置檔案是否正确
./nginx -s reload #重新加載           

5、檢視程序

ps -ef|grep nginx           
root       4337      1  0 06:13 ?        00:00:00 nginx: master process ./nginx
nobody     4338   4337  0 06:13 ?        00:00:00 nginx: worker process
root       4340   1494  0 06:13 pts/0    00:00:00 grep --color=auto nginx           

6、關閉防火牆或者打開80端口

啟動: systemctl start firewalld
停止: systemctl stop firewalld
檢視所有打開的端口: firewall-cmd --permanent --list-port
開啟端口:firewall-cmd --zone=public --add-port=80/tcp --permanent
關閉端口:firewall-cmd --zone=public --remove-port=80/tcp --permanent
更新防火牆規則: firewall-cmd --reload           

我這裡選擇的是開啟80端口

開啟端口:firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload           
[root@localhost sbin]# firewall-cmd --permanent --list-port
80/tcp           

7、浏覽器通路

這裡我linux伺服器的位址是192.168.192.11 ,通路

nginx-1.22.1在linux伺服器上的安裝

三、nginx使用

待續…

繼續閱讀