天天看點

Nginx安裝教程

Nginx安裝

nginx可以使用各平台的預設包來安裝,本文是介紹使用源碼編譯安裝,包括具體的編譯參數資訊。

正式開始前,編譯環境gcc g++ 開發庫之類的需要提前裝好,這裡預設你已經裝好。

ububtu平台編譯環境可以使用以下指令

apt-get install build-essential apt-get install libtool

1

2

apt - get install build - essential

apt - get install libtool

centos平台編譯環境使用如下指令

安裝make:

yum -y install gcc automake autoconf libtool make

yum - y install gcc automake autoconf libtool make

安裝g++:

yum install gcc gcc-c++

yum install gcc gcc - c ++

下面正式開始

---------------------------------------------------------------------------

一般我們都需要先裝pcre, zlib,前者為了重寫rewrite,後者為了gzip壓縮。

1.標明源碼目錄

可以是任何目錄,本文標明的是/usr/local/src

cd /usr/local/src

cd / usr / local / src

2.安裝PCRE庫

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下載下傳最新的 PCRE 源碼包,使用下面指令下載下傳編譯和安裝 PCRE 包:

Press CTRL+C to Copy, CTRL+V to Paste

cd /usr/local/src wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz tar -zxvf pcre-8.37.tar.gz cd pcre-8.34 ./configure make make install

3

4

5

6

7

wget ftp : //ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz

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包:

cd /usr/local/src wget http://zlib.net/zlib-1.2.8.tar.gz tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8 ./configure make make install

8

wget http : //zlib.net/zlib-1.2.8.tar.gz

tar - zxvf zlib - 1.2.8.tar.gz

cd zlib - 1.2.8

4.安裝ssl(某些vps預設沒裝ssl)

cd /usr/local/src wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz tar -zxvf openssl-1.0.1t.tar.gz

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 目錄下的詳細步驟:

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

9

10

11

12

13

14

15

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

--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 目錄下如下

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

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,

netstat -ano|grep 80

netstat - ano | grep 80

如果查不到結果後執行,有結果則忽略此步驟(ubuntu下必須用sudo啟動,不然隻能在前台運作)

sudo /usr/local/nginx/nginx

sudo  / usr / local / nginx / nginx

打開浏覽器通路此機器的 IP,如果浏覽器出現 Welcome to nginx! 則表示 Nginx 已經安裝并運作成功。

Nginx安裝教程

-----------------------------------------------------

到這裡nginx就安裝完成了,如果隻是處理靜态html就不用繼續安裝了

如果你需要處理php腳本的話,還需要安裝php-fpm。

下面安裝排錯

附:可能遇到的錯誤和一些幫助資訊

1.1編譯pcre錯誤

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

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

Nginx安裝教程

解決辦法:安裝g++,别忘了重新configure

apt-get install g++ apt-get install build-essential make clean ./configure make

apt - get install g ++

make clean

1.2 make出錯

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.

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下

apt-get install openssl apt-get install libssl-dev

apt - get  install  openssl

apt - get  install  libssl - dev

centos下

yum -y install openssl openssl-devel

yum - y install openssl openssl - devel

2.nginx編譯選項

make是用來編譯的,它從Makefile中讀取指令,然後編譯。

make install是用來安裝的,它也從Makefile中讀取指令,安裝到指定的位置。

configure指令是用來檢測你的安裝平台的目标特征的。它定義了系統的各個方面,包括nginx的被允許使用的連接配接處理的方法,比如它會檢測你是不是有CC或GCC,并不是需要CC或GCC,它是個shell腳本,執行結束時,它會建立一個Makefile檔案。nginx的configure指令支援以下參數:

<code>--prefix=<code>path</code></code>    定義一個目錄,存放伺服器上的檔案 ,也就是nginx的安裝目錄。預設使用 <code>/usr/local/nginx。</code>

<code>--sbin-path=<code>path</code></code> 設定nginx的可執行檔案的路徑,預設為  <code><code>prefix</code>/sbin/nginx</code>.

<code>--conf-path=<code>path</code></code>  設定在nginx.conf配置檔案的路徑。nginx允許使用不同的配置檔案啟動,通過指令行中的-c選項。預設為<code><code>prefix</code>/conf/nginx.conf</code>.

<code>--pid-path=<code>path  設定nginx.pid檔案,将存儲的主程序的程序号。安裝完成後,可以随時改變的檔案名 , 在nginx.conf配置檔案中使用 PID指令。預設情況下,檔案名 為</code></code><code><code>prefix</code>/logs/nginx.pid</code>.

<code>--error-log-path=<code>path</code></code> 設定主錯誤,警告,和診斷檔案的名稱。安裝完成後,可以随時改變的檔案名 ,在nginx.conf配置檔案中 使用 的error_log指令。預設情況下,檔案名 為<code><code>prefix</code>/logs/error.log</code>.

<code>--http-log-path=<code>path</code></code>  設定主請求的HTTP伺服器的日志檔案的名稱。安裝完成後,可以随時改變的檔案名 ,在nginx.conf配置檔案中 使用 的access_log指令。預設情況下,檔案名 為<code><code>prefix</code>/logs/access.log</code>.

<code>--user=<code>name</code></code>  設定nginx工作程序的使用者。安裝完成後,可以随時更改的名稱在nginx.conf配置檔案中 使用的 user指令。預設的使用者名是nobody。

<code>--group=<code>name</code></code>  設定nginx工作程序的使用者組。安裝完成後,可以随時更改的名稱在nginx.conf配置檔案中 使用的 user指令。預設的為非特權使用者。

<code>--with-select_module</code> <code>--without-select_module 啟用或禁用建構一個子產品來允許伺服器使用select()方法。該子產品将自動建立,如果平台不支援的kqueue,epoll,rtsig或/dev/poll。</code>

<code>--with-poll_module</code> <code>--without-poll_module</code> 啟用或禁用建構一個子產品來允許伺服器使用poll()方法。該子產品将自動建立,如果平台不支援的kqueue,epoll,rtsig或/dev/poll。

<code>--without-http_gzip_module</code> — 不編譯壓縮的HTTP伺服器的響應子產品。編譯并運作此子產品需要zlib庫。

<code>--without-http_rewrite_module</code>  不編譯重寫子產品。編譯并運作此子產品需要PCRE庫支援。

<code>--without-http_proxy_module</code> — 不編譯http_proxy子產品。

<code>--with-http_ssl_module</code> — 使用https協定子產品。預設情況下,該子產品沒有被建構。建立并運作此子產品的OpenSSL庫是必需的。

<code>--with-pcre=<code>path</code></code> — 設定PCRE庫的源碼路徑。PCRE庫的源碼(版本4.4 - 8.30)需要從PCRE網站下載下傳并解壓。其餘的工作是Nginx的./ configure和make來完成。正規表達式使用在location指令和 ngx_http_rewrite_module 子產品中。

<code>--with-pcre-jit</code> —編譯PCRE包含“just-in-time compilation”(1.1.12中, pcre_jit指令)。

<code>--with-zlib=<code>path</code></code> —設定的zlib庫的源碼路徑。要下載下傳從 zlib(版本1.1.3 - 1.2.5)的并解壓。其餘的工作是Nginx的./ configure和make完成。ngx_http_gzip_module子產品需要使用zlib 。

<code>--with-cc-opt=<code>parameters</code></code> — 設定額外的參數将被添加到CFLAGS變量。例如,當你在FreeBSD上使用PCRE庫時需要使用:<code>--with-cc-opt="-I /usr/local/include。</code>.如需要需要增加 <code>select()支援的檔案數量</code>:<code>--with-cc-opt="-D FD_SETSIZE=2048".</code>

<code>--with-ld-opt=<code>parameters</code></code> —設定附加的參數,将用于在連結期間。例如,當在FreeBSD下使用該系統的PCRE庫,應指定:<code>--with-ld-opt="-L /usr/local/lib".</code>

典型執行個體(下面為了展示需要寫在多行,執行時内容需要在同一行)

./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

     -- 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

繼續閱讀