轉載)php
nginx可使用各平臺的默認包來安裝,本文是介紹使用源碼編譯安裝,包括具體的編譯參數信息。html
正式開始前,編譯環境gcc g++ 開發庫之類的須要提早裝好,這裏默認你已經裝好。nginx
ububtu平臺編譯環境可使用如下指令c++
1正則表達式 2shell |
apt-get install build-essentialubuntu apt-get install libtoolcentos |
centos平臺編譯環境使用以下指令瀏覽器
安裝make:安全
1 |
yum -y install gcc automake autoconf libtool make |
安裝g++:
1 |
yum install gcc gcc-c++ |
下面正式開始
---------------------------------------------------------------------------
通常咱們都須要先裝pcre, zlib,前者爲了重寫rewrite,後者爲了gzip壓縮。
1.選定源碼目錄
能夠是任何目錄,本文選定的是/usr/local/src
1 |
cd /usr/local/src |
2.安裝PCRE庫
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下載最新的 PCRE 源碼包,使用下面命令下載編譯和安裝 PCRE 包:
1 2 3 4 5 6 7 |
cd /usr/local/src wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz 上面 安裝pcre安裝地址換ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/(打開瀏覽器查看) tar -zxvf pcre-8.37.tar.gz cd pcre-8.34 ./configure make make install |
3.安裝zlib庫
http://zlib.net/zlib-1.2.8.tar.gz 下載最新的 zlib 源碼包,使用下面命令下載編譯和安裝 zlib包:
1 2 3 4 5 6 7 8 |
cd /usr/local/src
wget http://zlib.net/zlib-1.2.8.tar.gz 進入http://zlib.net查看最新版本後綴1.2.8改爲最新版本號 tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8 ./configure make make install |
4.安裝ssl(某些vps默認沒裝ssl)
可跳轉openssl安裝的word文件,查看安裝最新版openssl
安全性更高,php須要高版本openssl支持
1 2 3 |
cd /usr/local/src wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz tar -zxvf openssl-1.0.1t.tar.gz |
|
|
5.安裝nginx
Nginx 通常有兩個版本,分別是穩定版和開發版,您能夠根據您的目的來選擇這兩個版本的其中一個,下面是把 Nginx 安裝到 /usr/local/nginx 目錄下的詳細步驟:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
cd /usr/local/src wget http://nginx.org/download/nginx-1.4.2.tar.gz tar -zxvf nginx-1.4.2.tar.gz cd nginx-1.4.2
./configure --sbin-path=/usr/local/nginx/nginx \ --conf-path=/usr/local/nginx/nginx.conf \ --pid-path=/usr/local/nginx/nginx.pid \ --with-http_ssl_module \ --with-pcre=/opt/app/openet/oetal1/chenhe/pcre-8.37 \ --with-zlib=/opt/app/openet/oetal1/chenhe/zlib-1.2.8 \ --with-openssl=/opt/app/openet/oetal1/chenhe/openssl-1.0.1t
make make install |
--with-pcre=/usr/src/pcre-8.34 指的是pcre-8.34 的源碼路徑。
--with-zlib=/usr/src/zlib-1.2.7 指的是zlib-1.2.7 的源碼路徑。
安裝成功後 /usr/local/nginx 目錄下以下
1 2 3 4 5 6 |
fastcgi.conf koi-win nginx.conf.default fastcgi.conf.default logs scgi_params fastcgi_params mime.types scgi_params.default fastcgi_params.default mime.types.default uwsgi_params html nginx uwsgi_params.default koi-utf nginx.conf win-utf |
6.啓動
確保系統的 80 端口沒被其餘程序佔用,運行/usr/local/nginx/nginx 命令來啓動 Nginx,
1 |
netstat -ano|grep 80 |
若是查不到結果後執行,有結果則忽略此步驟(ubuntu下必須用sudo啓動,否則只能在前臺運行)
1 |
sudo /usr/local/nginx/nginx |
打開瀏覽器訪問此機器的 IP,若是瀏覽器出現 Welcome to nginx! 則表示 Nginx 已經安裝並運行成功。
-----------------------------------------------------
到這裏nginx就安裝完成了,若是隻是處理靜態html就不用繼續安裝了
若是你須要處理php腳本的話,還須要安裝php-fpm。
下面安裝排錯
附:可能遇到的錯誤和一些幫助信息
1.1編譯pcre錯誤
1 2 3 4 5 |
libtool: compile: unrecognized option `-DHAVE_CONFIG_H' libtool: compile: Try `libtool --help' for more information. make[1]: *** [pcrecpp.lo] Error 1 make[1]: Leaving directory `/usr/local/src/pcre-8.34' make: *** [all] Error 2 |
解決辦法:安裝g++,別忘了從新configure
1 2 3 4 5 |
apt-get install g++ apt-get install build-essential make clean ./configure make |
1.2 make出錯
1 2 3 4 5 |
make: *** No rule to make target `build', needed by `default'. Stop. ./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl=<path> option. |
按照第4步的安裝方法或
ubuntu下
1 2 |
apt-get install openssl apt-get install libssl-dev |
centos下
1 |
yum -y install openssl openssl-devel |
2.nginx編譯選項
make是用來編譯的,它從Makefile中讀取指令,而後編譯。
make install是用來安裝的,它也從Makefile中讀取指令,安裝到指定的位置。
configure命令是用來檢測你的安裝平臺的目標特徵的。它定義了系統的各個方面,包括nginx的被容許使用的鏈接處理的方法,好比它會檢測你是否是有CC或GCC,並非須要CC或GCC,它是個shell腳本,執行結束時,它會建立一個Makefile文件。nginx的configure命令支持如下參數:
典型實例(下面爲了展現須要寫在多行,執行時內容須要在同一行)
1 2 3 4 5 6 7 |
./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=../pcre-4.4 --with-zlib=../zlib-1.1.3 |