前因:公司項(xiàng)目用到了淘寶的nginx-http-concat,導(dǎo)致本地PHP跑項(xiàng)目前端頁(yè)面的js/css無(wú)法加載出來(lái)。設(shè)備是mac,當(dāng)初為了省事直接用homebrew 安裝的Nginx,不支持安裝第三方模塊(homebrew有個(gè)nginx-full似乎也是可以實(shí)現(xiàn),后面再驗(yàn)證)。homebrew折騰了半天還是決定放棄,采用nginx編譯安裝來(lái)實(shí)現(xiàn),一套走下來(lái)發(fā)現(xiàn)也沒(méi)想象中麻煩,步驟如下。
準(zhǔn)備工作
卸載homebrew中的nginx
brew list ????????????#查看brew安裝列表
brew uninstall nginx? ?#卸載nginx
安裝包
下載 Nginx 源碼包
下載頁(yè):?http://nginx.org/en/download.html
當(dāng)前穩(wěn)定版本:?http://nginx.org/download/nginx-1.8.1.tar.gz
zlib
下載頁(yè):?http://zlib.net/
當(dāng)前穩(wěn)定版本:?http://zlib.net/zlib-1.2.8.tar.gz
注: Nginx 參考文檔中提到需要 1.1.3 以上版本的 zlib
pcre
下載頁(yè):?http://www.pcre.org/
當(dāng)前穩(wěn)定版本: ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
注: Nginx 參考文檔中提到需要 4.4 以上版本的 pcre
concat
下載頁(yè):https://github.com/alibaba/nginx-http-concat
配置及安裝
First, 進(jìn)入到包所在的文件夾內(nèi)(也可以直接到下載目錄手動(dòng)解壓)
tar zxvf zlib-1.2.8.tar.gz? # 得到 zlib-1.2.8 目錄
tar zxvf pcre-8.36.tar.gz? # 得到 pcre-8.36 目錄
tar?zxvf?nginx-http-concat.tar.gz??# 得到 concat 目錄
編譯安裝 Nginx
這里會(huì)將各依賴的源碼編譯進(jìn) Nginx, 所以 --with-zlib 和 --with-pcre 后為對(duì)應(yīng)的依賴源碼目錄路徑。此外, 編譯選項(xiàng)中還開(kāi)啟了 HTTPS 的協(xié)議支持--with-http_ssl_module, 若不需要 HTTPS, 可取消該選項(xiàng)。
注意:觀察自己下載的版本號(hào),操作的時(shí)候?qū)?yīng)版本號(hào),然后就是make的過(guò)程中如果有error 需要先處理掉。
tar zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --prefix=/usr/local/nginx --with-zlib=../zlib-1.2.8 --with-pcre=../pcre-8.36 --with-openssl=你的openssl文件夾路徑 --add-module=你的concat文件夾路徑 (2種寫法,也可參考下面指令示例)
make
sudo make install
其他安裝指令(可做了解)
nginx大部分常用模塊,編譯時(shí)./configure --help以--without開(kāi)頭的都默認(rèn)安裝。
--prefix=PATH : 指定nginx的安裝目錄。默認(rèn) /usr/local/nginx
--conf-path=PATH : 設(shè)置nginx.conf配置文件的路徑。nginx允許使用不同的配置文件啟動(dòng),通過(guò)命令行中的-c選項(xiàng)。默認(rèn)為prefix/conf/nginx.conf
--user=name: 設(shè)置nginx工作進(jìn)程的用戶。安裝完成后,可以隨時(shí)在nginx.conf配置文件更改user指令。默認(rèn)的用戶名是nobody。--group=name類似
--with-pcre : 設(shè)置PCRE庫(kù)的源碼路徑,如果已通過(guò)yum方式安裝,使用--with-pcre自動(dòng)找到庫(kù)文件。使用--with-pcre=PATH時(shí),需要從PCRE網(wǎng)站下載pcre庫(kù)的源碼(版本4.4 - 8.30)并解壓,剩下的就交給Nginx的./configure和make來(lái)完成。perl正則表達(dá)式使用在location指令和 ngx_http_rewrite_module模塊中。
--with-zlib=PATH : 指定 zlib(版本1.1.3 - 1.2.5)的源碼解壓目錄。在默認(rèn)就啟用的網(wǎng)絡(luò)傳輸壓縮模塊ngx_http_gzip_module時(shí)需要使用zlib 。
--with-http_ssl_module : 使用https協(xié)議模塊。默認(rèn)情況下,該模塊沒(méi)有被構(gòu)建。前提是openssl與openssl-devel已安裝
--with-http_stub_status_module : 用來(lái)監(jiān)控 Nginx 的當(dāng)前狀態(tài)
--with-http_realip_module : 通過(guò)這個(gè)模塊允許我們改變客戶端請(qǐng)求頭中客戶端IP地址值(例如X-Real-IP 或 X-Forwarded-For),意義在于能夠使得后臺(tái)服務(wù)器記錄原始客戶端的IP地址
--add-module=PATH : 添加第三方外部模塊,如nginx-sticky-module-ng或緩存模塊。每次添加新的模塊都要重新編譯(Tengine可以在新加入module時(shí)無(wú)需重新編譯)
比如:
./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid? \
--lock-path=/var/lock/nginx.lock \?
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--with-pcre=../pcre-7.8?\
--with-zlib=../zlib-1.2.3?\
--with-openssl=你的openssl文件夾路徑(部分模塊需要有這個(gè),下載后解壓到一個(gè)后指向即可,下載地址https://www.openssl.org/source/)?\
--add-module=你的concat文件夾路徑?\
設(shè)置監(jiān)聽(tīng)
進(jìn)入配置文件/usr/local/nginx/conf/nginx.conf ,修改listen下參數(shù):
server {
? ? ? ? listen? ? ? 85;? ? ? ? ? ? #監(jiān)聽(tīng)端口
? ? ? ? server_name? localhost;? ? #監(jiān)聽(tīng)服務(wù)器
? ? ? ? #charset koi8-r;
? ? ? ? #access_log? logs/host.access.log? main;
? ? ? ? location / {
? ? ? ? ? ? root? html;
? ? ? ? ? ? index? index.html index.htm;
? ? ? ? }
? ? ? ? #error_page? 404? ? ? ? ? ? ? /404.html;
? ? ? ? # redirect server error pages to the static page /50x.html
? ? ? ? #
? ? ? ? error_page? 500 502 503 504? /50x.html;
? ? ? ? location = /50x.html {
? ? ? ? ? ? root? html;
? ? ? ? }
? ? }
啟動(dòng)nginx
cd /usr/local/nginx
#啟動(dòng)(需要找到正確的nginx啟動(dòng)程序,才能生效)
sudo sbin/nginx? ? #瀏覽器訪問(wèn) 127.0.0.1 測(cè)試是否成功啟動(dòng)
PS:上面路徑如果不對(duì),可以試試 sudo /usr/sbin/nginx
sudo sbin/nginx -s reload??#重啟
sudo sbin/nginx -s stop?????#停止
PS:最后,請(qǐng)注意在nginx配置,檢查是否開(kāi)啟了concat的配置,這里被把我坑到了。
happy work!~