標籤:http io ar os 使用 sp strong on 檔案
安裝nginx的依賴包:pcre, pcre-devel
編譯nginx事實上需要的依賴包是pcre-devel,可以執行yum install pcre-devel 安裝它。不過這個包的編譯安裝很簡單,正好我們拿它練練手,熟悉熟悉linux編譯安裝軟體的一般過程。
[tips] linux下從源碼編譯安裝軟體一般是三步:配置、編譯、安裝。具體一點說就依次是執行三條命令:configure, make, make install. 不多講理論,實際操作一下就明白了。
在build目錄下建立子目錄pcre
[[email protected] build]$ mkdir pcre[[email protected] build]$ cd pcre
使用wget 工具從pcre官方下載 pcre 包,下載連結為 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.zip,將它解壓縮到pcre目錄下
[[email protected] build]$ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.zip
幾秒鐘就可以下載完成。(如果提示 bash: wget: command not found 那是你還沒有安裝wget,切換到root使用者,yum install wget -y 安裝之)
[[email protected] pcre]$ lspcre-8.21.zip
看一下,剛下載的 pcre源碼包,這是個zip包,使用unzip命令解壓縮之
[[email protected] pcre]$ unzip pcre-8.21.zip[[email protected] pcre]$ ll總用量 1680drwxr-xr-x 6 feng feng 4096 12月 12 2011 pcre-8.21-rw-rw-r-- 1 feng feng 1709377 6月 30 13:50 pcre-8.21.zip[[email protected] pcre]$ cd pcre-8.21
進去看看裡面都是些什麼檔案,特別注意一下configure
檔案
[[email protected] pcre-8.21]$ ll總用量 3716-rwxr-xr-x 1 feng feng 6961 10月 5 2009 132html-rw-r--r-- 1 feng feng 338277 12月 11 2011 aclocal.m4........ (略)-rwxr-xr-x 1 feng feng 595700 12月 11 2011 configure........ (略)-rw-r--r-- 1 feng feng 3460 8月 2 2011 ucp.h
這個configure檔案,就是“源碼安裝三步曲”的第一步的configure,它將檢查你的系統是否具備了編譯pcre必要的軟體包,配置出用於編譯pcre的環境,供第二步用。如果缺少某些軟體,它會給出提示。
[tips] 另外,注意這個目錄裡還有幾個檔案README, INSALL,LICENCE,它們都是普通的文字檔,供使用人閱讀,分別是 讀我檔案,安裝說明,授權協議。通常linux/unix世界的源碼包裡都有這幾個檔案(有時作者會把INSTALL並成到README裡),建議閱讀一下,尤其是README與INSTALL。
執行第一步 configure
[[email protected] pcre-8.21]$ ./configure
注意configure命令前面要帶上 ./ ,因為我們要執行的是在目前的目錄下的configure檔案,。
configure過程中可能出現的幾個報錯,及原因:
- 1) ./configure: error: C compiler gcc is not found 原因:你沒有安裝gcc ,這樣可能你也沒安裝下面幾個包,請一併安裝
yum install gcc gcc-c++ autoconf make
- 2) Makefile: 許可權不夠 原因:目前使用者沒有許可權讀寫nginx源碼目錄,請切換到root下運行如下命令,作用是將目前的目錄的所有檔案所有者都設為我們正在使用的普通使用者。
[[email protected] nginx-1.2.1]# chown -R feng:feng ./[[email protected] nginx-1.2.1]# exit
然後exit退出到普通使用者狀態下。 chown 後的 feng:feng 分別是所使用的普通帳號的使用者名稱,及其使用者組名。
開始編譯,這步非常簡單,運行make就可以,可能要花費幾分鐘時間,你可以幹點其它,比如起身活動一下。
[[email protected] pcre-8.21]$ make
完了如果看到下面的訊息:make[1]: Leaving directory `/home/feng/build/pcre/pcre-8.21‘
說明編譯完成了,那麼開始安裝。安裝很簡單,使用root使用者權限,運行make install 就可以了。所以su,按提示輸入root密碼,切換到root身份
[[email protected] pcre-8.21]$ su
[[email protected] pcre-8.21]# make install
完了訊息應該是 make[1]: Leaving directory `/home/feng/build/pcre/pcre-8.21‘
這樣pcre就安裝好了。然後exit退回到普通使用者狀態下。
編譯安裝nginx
接下來才是真正安裝nginx,過程差不多,但我們要多配置幾個參數選項。
到 nginx官網上 http://nginx.org/en/download.html 找最新的穩定版的下載連結,寫本文時2012-06-30 最新版本為 nginx-1.2.1 下載連結為 http://nginx.org/download/nginx-1.2.1.tar.gz 使用wget 將其下載到vps上。
[[email protected] build]$ wget http://nginx.org/download/nginx-1.2.1.tar.gz
幾秒鐘就可以下載完成。(如果提示 bash: wget: command not found 那是你還沒有安裝wget,切換到root使用者,yum install wget -y 安裝之)
解壓縮,然後把壓縮包移動到source目錄,以備不時之需。
[[email protected] build]$ ll總用量 708-rw-r--r-- 1 root root 718161 6月 5 14:10 nginx-1.2.1.tar.gz[[email protected] build]$ tar xf nginx-1.2.1.tar.gz [[email protected] build]$ ll總用量 712drwxr-xr-x 8 1001 1001 4096 6月 5 14:02 nginx-1.2.1-rw-r--r-- 1 root root 718161 6月 5 14:10 nginx-1.2.1.tar.gz[[email protected] build]$ mv nginx-1.2.1.tar.gz ../source/
[tips] 檔案名稱很長,打字太累? 試試tab鍵,比如先打出 mv ngi 然後按tab 鍵一次,沒反應,再按一次,看有什麼變化;然後繼續打一個句點,再按一次tab鍵。相信你已經能悟出點什麼了。
進入nginx源碼目錄,瀏覽一下裡面的檔案
[[email protected] build]$ cd nginx-1.2.1/[[email protected] nginx-1.2.1]$ ll總用量 560drwxr-xr-x 6 1001 1001 4096 6月 30 11:39 auto-rw-r--r-- 1 1001 1001 207988 6月 5 14:02 CHANGES-rw-r--r-- 1 1001 1001 317085 6月 5 14:02 CHANGES.rudrwxr-xr-x 2 1001 1001 4096 6月 30 11:39 conf-rwxr-xr-x 1 1001 1001 2345 1月 18 15:07 configuredrwxr-xr-x 3 1001 1001 4096 6月 30 11:39 contribdrwxr-xr-x 2 1001 1001 4096 6月 30 11:39 html-rw-r--r-- 1 1001 1001 1365 1月 18 15:07 LICENSEdrwxr-xr-x 2 1001 1001 4096 6月 30 11:39 man-rw-r--r-- 1 1001 1001 49 10月 31 2011 READMEdrwxr-xr-x 8 1001 1001 4096 6月 30 11:39 src
運行configure,就是本目錄下的該檔案,上面標紅加;目錄是配置nginx的編譯環境,運行如下命令,參數比較長,可以直接複製帖。
[[email protected] nginx-1.2.1]$ ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/sbin/nginx --conf-path=/usr/local/conf/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_stub_status_module --with-http_gzip_static_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_random_index_module --with-cpu-opt=pentium4
注意configure命令要帶上./ ,上面已經標紅顯示。大致講一下是什麼意思 [注]如果你僅僅想編譯一個web環境用,而不想學習編譯linux程式的一般方法,這部分那可以跳過不用看。
把參數重排下版,一行一個參數,簡單注釋一下
--prefix=/usr/local/nginx #編譯後安裝到目錄 /usr/local/nginx --sbin-path=/usr/local/sbin/nginx #--conf-path=/usr/local/conf/nginx/nginx.conf #主設定檔路徑--error-log-path=/var/log/nginx/error.log #錯誤記錄檔目錄--pid-path=/var/run/nginx.pid #nginx程式啟用後,自動將其進程號寫到該檔案--lock-path=/var/run/nginx.lock #lock檔案路徑--with-http_stub_status_module #通過web查看nginx運行狀態--with-http_gzip_static_module #gzip壓縮支援--with-http_sub_module #加入ngx_http_sub_module模組--with-http_flv_module #加入flv模組--with-http_mp4_module #mp4模組--with-http_random_index_module #http_random_index_module模組--with-cpu-opt=pentium4 #針對intel cpu最佳化
--with-xxxx_module的那些模組,如果你確定
不需要,那就可以不要加入。那就這樣指定參數
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/sbin/nginx --conf-path=/usr/local/conf/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_stub_status_module --with-http_gzip_static_module
只帶兩個比較有用的模組。事實上這些參數一個都不指定,也是可以,這樣就是按nginx的預設配置編譯了。如果你想查看ngix的configure都有哪些參數選項可以運行 ./configure --help 查看,這裡不多講了。
如果報錯:./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
這是因為沒有找到PCRE包,請檢查是否正確安裝了PCRE包:
不出意外,應該能看到這樣報告訊息:
Configuration summary + using system PCRE library + OpenSSL library is not used + md5: using system crypto library + sha1: using system crypto library + using system zlib library nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/sbin/nginx" nginx configuration prefix: "/usr/local/conf/nginx" nginx configuration file: "/usr/local/conf/nginx/nginx.conf" nginx pid file: "/var/run/nginx.pid" nginx error log file: "/var/log/nginx/error.log" nginx http access log file: "/usr/local/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp"
有些軟體的configure完成後會給出類似的一個配置報告,供我們檢查配置選項是否正確。如果有不正確處,或還想改改,那就再運行一次configure --xx --yyy.
確認無誤,開始編譯
[[email protected] nginx-1.2.1]$ make
開始編譯,非常快,可能一分鐘就編譯完成,最後訊息大致如:make[1]: Leaving directory `/home/feng/build/nginx-1.2.1‘
接下來,你應該知道了,切換到root身份,make install 安裝。
nginx運行測試
下面的操作要在root身份下操作。
我們測試一下nginx是否正常工作。因為我們把nginx的二進位執行檔案配置安裝在 /usr/local/sbin/nginx (前面的configure參數 --sbin-path=/usr/local/sbin/nginx ),目錄/usr/local/sbin/預設在linux的PATH環境變數裡,所以我們可以省略路徑執行它:
[[email protected] nginx-1.2.1]# nginx
它不會有任何資訊顯示。開啟瀏覽器,通過IP地址訪問你的nginx網站,如果看到這樣一行大黑字,就證明nginx已經工作了:
Welcome to nginx!
看上去很簡陋,只是因為沒有給它做漂亮的頁面。我們來簡單配置一下它的回合組態。我們configure的參數--conf-path=/usr/local/conf/nginx/nginx.conf 這就是它的設定檔。我們去看看。
[[email protected] nginx-1.2.1]# cd /usr/local/conf/nginx/[[email protected] nginx]# lsfastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_paramsfastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.defaultfastcgi_params koi-win nginx.conf scgi_params.default win-utf
如果這裡沒有nginx.conf,但有個 nginx.conf.default,我們用它複製個副本nginx.conf (早期版本預設是沒有nginx.conf的)
用你熟悉的編輯器開啟它,推薦使用vim;如果不會用,那可以使用nano, 功能簡單,但一看就會用:nano nginx.conf
找到如下的部分,大概在35行,
server { listen 80 default; server_name localhost; root /var/www/html/default; #charset koi8-r; #access_log logs/host.access.log main; location / { # root html; index index.html index.htm; }................}
如上紅色加粗顯示部分,做三處修改:
- listen 80 後插入“空格default”
- 在裡面加入一行 root /var/www/html/default;
- 注釋掉root html;一行
上面的server{....}是定義的一台虛擬機器主機,我們剛加入的一行是定義這個虛假主機的web目錄。listen 行的default是表示這一個server{...}節點是預設的虛假主機(預設網站)
執行 nginx -t 測試剛才的nginx設定檔是否有語法錯誤:
[[email protected] nginx]# nginx -tnginx: the configuration file /usr/local/conf/nginx/nginx.conf syntax is oknginx: configuration file /usr/local/conf/nginx/nginx.conf test is successful
顯示沒有錯誤,檢測通過。如果不是這樣,請返回仔細檢查你的設定檔是否哪裡寫錯了,注意行尾要有英文分號。
我們在硬碟上建立這個目錄:
[[email protected] nginx]# mkdir -p /var/www/html/default
寫一個html檔案index.html 到/var/www/html/default目錄裡,使用你熟悉的編輯器,隨便寫什麼內容、怎麼排版。
然後讓nginx重新載入設定檔,看看剛才創作的html頁面效果如何。
常見錯誤FAQ
1) 如果通常訪問時看到還是 Welcome to nginx! ,請返回檢查,重點在這幾處:
- 請確認/var/www/html/default 目錄下有你創作的index.html 檔案?
- 檢查該檔案許可權,other使用者是否有讀的許可權? 如果不懂linux檔案許可權,請執行 chmod 755 /var/www/html/default/index.html
- 檢查nginx.conf設定檔裡,只否只有一個server{...} 節點,並且該節點裡是否有 listen 80default; 一行,注意其中要有 default 。
- 檢查上述server{...}節點裡是否有 root /var/www/html/default; 一行,注意路徑是拼字是否正確。
- 檢查其中 location / {...} 節點裡的 # root html; 一行,是否注釋掉了。
2) 如果看到的是 404 Not Found 或者“找不到該頁面”類似的提示:
- 檢查上述 location / {...} 節點中是否有這一行 index index.html index.htm;
3) 如果訪問時,顯示“找不到伺服器”、“無法串連到伺服器”這樣的錯誤:
- 運行檢查nginx進程在運行,運行ps aux |grep nginx 命令,正常情況有如下三條:
nginx: master process nginx
nginx: worker process
grep nginx
如果只有第三條,請運行nginx 重新啟用nginx,如有報錯請照說明檢查。一般是設定檔的語法錯誤。
- 請運行nginx -t 檢查設定檔是否有語法錯誤。
[tips] location / {...} 節點裡的 # root html; 一行,不注釋掉也可以:請把你創造的index.html 放到/var/www/html/default/html目錄下。
至此,我們的nginx也可以正常工作了。
在centos下編譯安裝配置高效能Nginx