Nginx 安裝部署

Nginx 安裝部署

Nginx,一個被貼滿,高性能,低消耗,低成本標籤的web服務器。想必大家都早有耳聞。我是在接觸了公司的圖片服務器的時候,纔開始真正接觸它。本文從Nginx 和傳統項目的區別 和 Nginx的安裝部署兩個方面來了解它。

1 Nginx 和 傳統項目的區別

1.1 傳統項目管理圖片的思路

在傳統項目中,我們一般通過在web項目的根目錄下創建一個用於存儲圖片的images文件夾來方便管理圖片。但隨着業務和規模的逐漸擴大,一臺服務器已經無法滿足我們的需求,我們可以通過搭建服務器集羣來處理高併發的場景。

好景不長,集羣剛搭好,就有用戶反饋,圖片爲什麼時而有,時而沒有? 這是因爲:圖片存儲在 服務器/web根目錄/images文件夾 中,當用戶在上傳圖片的時候,只將圖片傳給了一臺服務器,在獲取圖片時,可能調用了其他服務器。這樣會出現該問題。

解決這個問題很簡單,就是把圖片單獨放在一個服務器。如果選擇Apache的tomcat服務器,在處理業務邏輯簡單的圖片服務器中似乎顯得有些笨重。一款高性能,低成本輕量級web服務器 nginx 脫穎而出。不僅如此它還是一款反向代理服務器和電子郵件代理服務器。

傳統項目管理圖片的思路

2 安裝部署

2.1 理想流程

[root@itdragon ~]# wget http://nginx.org/download/nginx-1.13.6.tar.gz
[root@itdragon ~]# tar -zxvf nginx-1.13.6.tar.gz
[root@itdragon ~]# ll
total 824
drwxr-xr-x 9 1001 1001   4096 Nov 14 14:26 nginx-1.13.6
-rw-r--r-- 1 root root 832104 Nov 14 14:18 nginx-1.13.6.tar.gz
[root@itdragon ~]# cd nginx-1.13.6
[root@itdragon nginx-1.13.6]# ./configure
[root@itdragon nginx-1.13.6]# make
[root@itdragon nginx-1.13.6]# make install
[root@itdragon nginx-1.13.6]# cd /usr/local/nginx/sbin/
[root@itdragon sbin]# ./nginx
[root@itdragon sbin]# ifconfig

第一步:下載Nginx壓縮包
第二步:解壓
第三步:配置,編譯,安裝,啓動
第四步:查看ip地址
第五步:瀏覽器訪問:ip:port
若出現如下圖片則說明安裝成功。
Nginx歡迎頁面
但是,Nginx是調皮的,它不會讓我們如此順利

2.2 常見問題

踩坑?不存在的,我踩過的坑,不允許讓你們再踩。它是我滴!

  • ./configure: error: C compiler cc is not found
  • ./configure: error: the HTTP rewrite module requires the PCRE library.
  • ./configure: error: the HTTP gzip module requires the zlib library
  • OpenSSL library is not used
  • nginx: [emerg] bind() to 0.0.0.0:88 failed (98: Address already in use)

第一個問題,是因爲 nginx 解壓編譯依賴 gcc 環境造成的。

[root@itdragon ~]# yum install gcc-c++

第二個問題,是因爲 nginx 的 http 模塊使用 pcre 來解析正則表達式

[root@itdragon ~]# yum install -y pcre pcre-devel

第三個問題,是因爲 nginx 使用 zlib 對 http 包的內容進行 gzip 操作

[root@itdragon ~]# yum install -y zlib zlib-devel

第四個問題,建議安裝,nginx 它是支持https 協議的

[root@itdragon ~]# yum install -y openssl openssl-devel

第五個問題,是很常見的端口占用,修改 nginx.config 文件中的端口即可。 /port,快速找到端口配置的地方。[Insert] 開啓編輯模式。[Esc] :wq 退出保存

[root@itdragon sbin]# ./nginx 
nginx: [emerg] bind() to 0.0.0.0:88 failed (98: Address already in use)
[root@itdragon sbin]# vim ../conf/nginx.conf
server {
        listen       88;
        server_name  localhost;
[root@itdragon sbin]# ./nginx

若出現 Loaded plugins: fastestmirror 不是問題的問題。可以通過修改fastestmirror.conf 文件,這是一種不負責任的做法,如果自己玩 Nginx 可以這樣做。如果是實際開發,就老老實實的按照提示來做。

[root@plugins ~]# vim /etc/yum/pluginconf.d/fastestmirror.conf
enabled=0
[root@plugins ~]# vim /etc/yum.conf
plugins=0
[root@plugins ~]# yum clean dbcache

到這裏,Nginx的安裝部署就完成了。下一章就利用Nginx搭建圖片服務。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章