1. 准備工作
-
查看系統版本
輸入命令
cat /etc/redhat-release
我的Centos版本
CentOS Linux release 7.5.1804 (Core)
-
安裝nginx所需的依賴
-
gcc安裝
安裝 nginx 需要先將官網下載的源碼進行編譯,而編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:
yum install -y gcc-c++
-
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
-
zlib 安裝
zlib 庫提供了很多種壓縮和解壓縮的方式 nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫
yum install -y zlib zlib-devel
-
OpenSSL 安裝
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。yum install -y openssl openssl-devel
-
2. 下載nginx
-
下載地址:
選擇穩定版本
3. 上傳
-
使用xshell連接linux系統 再使用xftp上傳文件到指定目錄
我的目錄
/app/tool/
4. 解壓
-
解壓到當前目錄
tar zxf nginx-1.14.1.tar.gz
目錄結構
5. 初始化nginx(配置)
-
解壓完成后進入解壓目錄
cd /app/tool/nginx-1.14.1
-
配置
其實在 nginx-1.14.0 版本中你就不需要去配置相關東西,默認就可以了。當然,如果你要自己配置目錄也是可以的。-
默認配置(推薦)
編譯
./configure
完成輸出的結果
-
自定義配置
注:將臨時文件目錄指定為/var/temp/nginx,需要在/var下創建temp及nginx目錄
./configure \ --prefix=/usr/local/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --pid-path=/usr/local/nginx/conf/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi
-
6. 編譯安裝
-
執行
make && make install
-
輸出結果最后一行:
make[1]: Leaving directory `/app/tool/nginx-1.14.1'
-
查找安裝路徑
whereis nginx
輸出結果:
nginx: /usr/local/nginx
7. 啟動與重啟
-
啟動
-
進入sbin目錄
cd /usr/local/nginx/sbin
-
啟動
./nginx
-
-
停止
-
先查出nginx進程id再使用kill命令強制殺掉進程
./nginx -s stop
-
停止:等待nginx進程處理任務完畢進行停止
./nginx -s quit
-
-
重啟
先停止再啟動
./nginx -s quit
./nginx -
重載配置文件
當 ngin x的配置文件 nginx.conf 修改后,要想讓配置生效需要重啟 nginx,使用-s reload不用先停止 ngin x再啟動 nginx 即可將配置信息在 nginx 中生效,如下:
./nginx -s reload
-
開放80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
8. 反向代理
-
編輯配置文件
修改/usr/local/nginx/conf下的 nginx.conf文件
vim /usr/local/nginx/conf/nginx.conf
-
修改配置文件
在nginx.conf的http{}里面添加
server { listen 80; server_name example.com; location / { proxy_pass http://127.0.0.1:8080; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
9. 配置https
-
檢查模塊
進入/usr/local/nginx/sbin/目錄下 查看nginx版本與編譯安裝了哪些模塊
cd /usr/local/nginx/sbin/
./nginx -V
-
添加http_ssl_module模塊
進入nginx解壓目錄下配置nginx
cd /app/tool/nginx-1.14.1/
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
-
編譯
千萬不要make install 否則會覆蓋現有的nginx
make
-
關閉nginx
進入/usr/local/nginx/sbin/目錄下./nginx -s quit
-
復制配置目錄
將/app/tool/nginx-1.14.1/objs/nginx 替換/usr/local/nginx/sbin/nginx
復制前注意先將之前的nginx文件備份
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
復制 如果提示是否覆蓋 鍵入y即可
cp /app/tool/nginx-1.14.1/objs/nginx /usr/local/nginx/sbin/
-
查看編譯安裝的模塊
進入/usr/local/nginx/sbin/目錄下
cd /usr/local/nginx/sbin/
./nginx -V
-
上傳證書
我上傳的目錄為
/usr/local/nginx/conf
我使用的是騰訊雲的免費證書
將1_域名_bundle.crt
和2_域名.key
上傳到 /usr/local/nginx/conf/目錄下 -
修改nginx.conf
vim /usr/local/nginx/conf/nginx.conf
在
http{}
末尾添加以下內容注:example.com應換成自己的域名
server { listen 80; server_name example.com; send_timeout 1800; rewrite ^(.*) https://example.com$1 permanent; } server { listen 443; server_name example.com; #填寫綁定證書的域名 index index.html index.htm index.php default.html default.htm default.php; ssl on; ssl_certificate /usr/local/nginx/conf/1_example.com_bundle.crt; ssl_certificate_key /usr/local/nginx/conf/2_example.com.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照這個協議配置 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #按照這個套件配置 ssl_prefer_server_ciphers on; location / { root html; index index.html index.htm; } }
-
驗證配置是否正確
進入/usr/local/nginx/sbin/目錄下
cd /usr/local/nginx/sbin/
./nginx -t
如下提示表示成功
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
-
開啟443端口
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --reload
- 啟動nginx
./nginx
現在就可以使用https訪問了
10. 配置圖片服務器
-
修改/usr/local/nginx/conf/nginx.conf
vim conf/nginx.conf
修改的位置為server{}里面,配置了https的需要在443端口server下添加如下內容,如果沒有配置https則在默認80端口下添加即可
-
內容
location /images/ { root /app/home/; autoindex on; }
-
修改圖片目錄的權限
我的目錄為/app/home/
chmod 777 -R /app/home/
-
存放圖片需要存入設置目錄的images目錄下
我的圖片路徑為
/app/home/images/
-
重載nginx配置文件
cd /usr/local/nginx/sbin/
./sbin -s reload -
訪問
現在就可以訪問圖片了,訪問地址為域名(iP:端口)/images/圖片名稱.后綴