1. 程式人生 > 其它 >Nginx記錄

Nginx記錄

Nginx線上安裝

  1. nginx下載地址:http://nginx.org/en/download.html
  1. 安裝前置條件包
#	安裝gcc-c++編譯器
yum install -y zlibyum install gcc-c++
yum install -y openssl openssl-devel zlib-devel

#	安裝pcre包
yum install -y pcre pcre-devel

#	安裝zlib包
yum install -y zlib zlib-devel

  1. 在官網點選下載對應的nginx版本,之後上傳程式到/usr/local目錄下

  2. nginx 安裝

#	進入到/usr/local目錄,解壓nginx安裝包
tar -zxvf nginx-1.14.2.tar.gz

#	進入到剛解壓的nginx目錄
cd nginx-1.14.2

#	編譯nginx
#	--with-http_ssl_module	新增ssl模組
#	--with-stream	新增stream模組用於埠代理
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/pid/nginx.pid --with-http_ssl_module --with-stream

#	安裝nginx
make
make install

#	進入到啟動檔案目錄
cd /usr/local/nginx/sbin

#	啟動nginx
./nginx 

#檢視nginx版本
[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/pid/nginx.pid --with-http_ssl_module --with-stream

#關閉防火牆,或者防火牆放開80埠訪問,nginx預設啟動埠為80埠
systemctl stop firewalld.service #停止firewall
  1. 在網頁訪問 http://ip:80/ 埠測試是否成功,如果訪問提示拒絕許可權不夠等等,可以將目錄nginx許可權提升為755

Ngin離線安裝

  1. 在本地虛擬機器可以連線外網的yum 下進行,安裝前置條件包
#	--downloadonly   只下載安裝,--downloaddir=/tmp/offline_rpm 指定的下載目錄
#	a.首先下載gcc-c++編譯器rpm包
yum install -y zlibyum install gcc-c++ --downloadonly --downloaddir=/tmp/offline_rpm

#	b.第二次在執行方便後面的軟體包下載
yum install -y zlibyum install gcc-c++

#	c.同上a.第一次先下載rpm包
yum install -y openssl openssl-devel zlib-devel --downloadonly --downloaddir=/tmp/offline_rpm

#	d.同上b.
yum install -y openssl openssl-devel zlib-devel

#	e.下載安裝pcre包   
#	a~d都執行的話這裡應該會提示已安裝最新的包
yum install -y pcre pcre-devel --downloadonly --downloaddir=/tmp/offline_rpm
yum install -y pcre pcre-devel

#	f.下載安裝zlib包
#	a~d都執行的話這裡應該會提示已安裝最新的包
yum install -y zlib zlib-devel --downloadonly --downloaddir=/tmp/offline_rpm
yum install -y zlib zlib-devel
  1. 建立本地yum倉庫,使用createrepo來建立repository倉庫,如果系統裡沒有該命令的話需要先進行createrepo的安裝
 #	下載createrepo
 yum install -y createrepo
 
 #	建立本地yum倉庫
 createrepo /tmp/offline_rpm
  1. 離線伺服器yum倉庫配置
#	a.將 offline_rpm 資料夾傳輸至 內網主機 /tmp 下
#  b.將/etc/yum.repos.d 下面所有repo檔案進行轉移至bak資料夾下。(如果沒有就自己新建一個bak目錄)
#  c.新建一個repo檔案,vi /etc/yum.repos.d/new.repo
#  d.寫入如下內容並儲存
[New]
name=New
baseurl=file:///tmp/offline_rpm
gpgcheck=0
enabled=1

#	e.清理快取。yum clean all
  1. 重複Nginx線上安裝的2~5步驟

nginx版本升級

  1. 將最新版本的nginx安裝包放到一個指定目錄/home/ghl,之後解壓檔案。我這裡用的是nginx-1.20.2.tar.gz 版本,解壓完後就是nginx-1.20.2
[root@localhost ghl]# tar -zxvf nginx-1.20.2.tar.gz
[root@localhost ghl]# ls -lrt
總用量 1040
drwxr-xr-x. 8 1001 1001     158 11月 16 22:44 nginx-1.20.2
-rw-r--r--. 1 root root 1062124 3月  18 14:31 nginx-1.20.2.tar.gz
[root@localhost ghl]# 
[root@localhost ghl]# pwd
/home/ghl
[root@localhost ghl]# 
  1. 進入到之前安裝nginx目錄使用./nginx -V儲存編譯執行的命令
[root@localhost ghl]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# 
[root@localhost sbin]# ./nginx  -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/pid/nginx.pid --with-http_ssl_module --with-stream
  1. 之後回到1.20版本的解壓路徑,在啟動命令最後新增:--with-http_sub_module
[root@localhost ghl]# cd /home/ghl/nginx-1.20.2
[root@localhost nginx-1.20.2]# 
[root@localhost nginx-1.20.2]# ls -lrt
總用量 792
-rw-r--r--. 1 1001 1001     49 11月 16 22:44 README
-rw-r--r--. 1 1001 1001   1397 11月 16 22:44 LICENSE
-rwxr-xr-x. 1 1001 1001   2590 11月 16 22:44 configure
-rw-r--r--. 1 1001 1001 476577 11月 16 22:44 CHANGES.ru
-rw-r--r--. 1 1001 1001 312251 11月 16 22:44 CHANGES
drwxr-xr-x. 2 1001 1001     21 3月  18 15:34 man
drwxr-xr-x. 2 1001 1001     40 3月  18 15:34 html
drwxr-xr-x. 9 1001 1001     91 3月  18 15:34 src
drwxr-xr-x. 4 1001 1001     72 3月  18 15:34 contrib
drwxr-xr-x. 2 1001 1001    168 3月  18 15:34 conf
drwxr-xr-x. 6 1001 1001   4096 3月  18 15:34 auto


#	執行nginx編譯
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/pid/nginx.pid --with-http_ssl_module --with-stream --with-http_sub_module
  1. 之後執行make命令,但不要執行make install切記切記。執行完成後會在目錄下生成一個objs檔案,進入到objs目錄下會有一個新的nginx檔案。
  1. 備份舊的1.14.2版本的nginx啟動檔案,使用這個1.20.2版本的nginx啟動檔案替換掉原來的1.14.2版本的nginx啟動檔案重新啟動即可。