查看系統版本php
輸入命令html
cat /etc/redhat-releaselinux
個人Centos版本nginx
CentOS Linux release 7.5.1804 (Core)c++
安裝nginx所需的依賴正則表達式
gcc安裝算法
安裝 nginx 須要先將官網下載的源碼進行編譯,而編譯依賴 gcc 環境,若是沒有 gcc 環境,則須要安裝:shell
yum install -y gcc-c++vim
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
下載地址:
選擇穩定版本
使用xshell鏈接linux系統 再使用xftp上傳文件到指定目錄
個人目錄
/app/tool/
解壓到當前目錄
tar zxf nginx-1.14.1.tar.gz
目錄結構
解壓完成後進入解壓目錄
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
執行
make && make install
輸出結果最後一行:
make[1]: Leaving directory `/app/tool/nginx-1.14.1'
查找安裝路徑
whereis nginx
輸出結果:
nginx: /usr/local/nginx
進入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
編輯配置文件
修改/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; } }
檢查模塊
進入/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訪問了
修改/usr/local/nginx/conf/nginx.conf
修改的位置爲server{}裏面,配置了https的須要在443端口server下添加以下內容,若是沒有配置https則在默認80端口下添加便可vim conf/nginx.conf
內容
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/圖片名稱.後綴