nginx可以使用各平臺(tái)的默認(rèn)包來(lái)安裝,本文是介紹使用源碼編譯安裝,包括具體的編譯參數(shù)信息。
正式開(kāi)始前,編譯環(huán)境gcc g++ 開(kāi)發(fā)庫(kù)之類的需要提前裝好,這里默認(rèn)你已經(jīng)裝好。
ububtu平臺(tái)編譯環(huán)境可以使用以下指令
apt-get install build-essentialapt-get install libtool
centos平臺(tái)編譯環(huán)境使用如下指令
安裝make:
yum -y install gcc automake autoconf libtool make
安裝g++:
yum install gcc gcc-c++
下面正式開(kāi)始
---------------------------------------------------------------------------
一般我們都需要先裝pcre, zlib,前者為了重寫(xiě)rewrite,后者為了gzip壓縮。
1.選定源碼目錄
可以是任何目錄,本文選定的是/usr/local/src
cd /usr/local/src
2.安裝PCRE庫(kù)
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下載最新的 PCRE 源碼包,使用下面命令下載編譯和安裝 PCRE 包:
cd /usr/local/srcwget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz tar -zxvf pcre-8.34.tar.gzcd pcre-8.34./configuremakemake install
3.安裝zlib庫(kù)
http://zlib.net/zlib-1.2.8.tar.gz 下載最新的 zlib 源碼包,使用下面命令下載編譯和安裝 zlib包:
cd /usr/local/srcwget http://zlib.net/zlib-1.2.8.tar.gztar -zxvf zlib-1.2.8.tar.gzcd zlib-1.2.8./configuremakemake install
4.安裝ssl(某些vps默認(rèn)沒(méi)裝ssl)
cd /usr/local/srcwget http://www.openssl.org/source/openssl-1.0.1c.tar.gztar -zxvf openssl-1.0.1c.tar.gz
5.安裝nginx
Nginx 一般有兩個(gè)版本,分別是穩(wěn)定版和開(kāi)發(fā)版,您可以根據(jù)您的目的來(lái)選擇這兩個(gè)版本的其中一個(gè),下面是把 Nginx 安裝到 /usr/local/nginx 目錄下的詳細(xì)步驟:
cd /usr/local/srcwget http://nginx.org/download/nginx-1.4.2.tar.gztar -zxvf nginx-1.4.2.tar.gzcd 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=/usr/local/src/pcre-8.34 --with-zlib=/usr/local/src/zlib-1.2.8 --with-openssl=/usr/local/src/openssl-1.0.1cmakemake 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 目錄下如下
fastcgi.conf koi-win nginx.conf.defaultfastcgi.conf.default logs scgi_paramsfastcgi_params mime.types scgi_params.defaultfastcgi_params.default mime.types.default uwsgi_paramshtml nginx uwsgi_params.defaultkoi-utf nginx.conf win-utf
6.啟動(dòng)
確保系統(tǒng)的 80 端口沒(méi)被其他程序占用,運(yùn)行/usr/local/nginx/nginx 命令來(lái)啟動(dòng) Nginx,
netstat -ano|grep 80
如果查不到結(jié)果后執(zhí)行,有結(jié)果則忽略此步驟(ubuntu下必須用sudo啟動(dòng),不然只能在前臺(tái)運(yùn)行)
sudo /usr/local/nginx/nginx
打開(kāi)瀏覽器訪問(wèn)此機(jī)器的 IP,如果瀏覽器出現(xiàn) Welcome to nginx! 則表示 Nginx 已經(jīng)安裝并運(yùn)行成功。
-----------------------------------------------------
到這里nginx就安裝完成了,如果只是處理靜態(tài)html就不用繼續(xù)安裝了
如果你需要處理php腳本的話,還需要安裝php-fpm。
下面安裝排錯(cuò)
附:可能遇到的錯(cuò)誤和一些幫助信息
1.1編譯pcre錯(cuò)誤
libtool: compile: unrecognized option `-DHAVE_CONFIG_H'libtool: compile: Try `libtool --help' for more information.make[1]: *** [pcrecpp.lo] Error 1make[1]: Leaving directory `/usr/local/src/pcre-8.34'make: *** [all] Error 2
解決辦法:安裝g++,別忘了重新configure
apt-get install g++apt-get install build-essentialmake clean./configuremake
1.2 make出錯(cuò)
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 libraryinto the system, or build the OpenSSL library statically from the sourcewith nginx by using --with-openssl=option.
按照第4步的安裝方法或
ubuntu下
apt-get install opensslapt-get install libssl-dev
centos下
yum -y install openssl openssl-devel
2.nginx編譯選項(xiàng)
make是用來(lái)編譯的,它從Makefile中讀取指令,然后編譯。
make install是用來(lái)安裝的,它也從Makefile中讀取指令,安裝到指定的位置。
configure命令是用來(lái)檢測(cè)你的安裝平臺(tái)的目標(biāo)特征的。它定義了系統(tǒng)的各個(gè)方面,包括nginx的被允許使用的連接處理的方法,比如它會(huì)檢測(cè)你是不是有CC或GCC,并不是需要CC或GCC,它是個(gè)shell腳本,執(zhí)行結(jié)束時(shí),它會(huì)創(chuàng)建一個(gè)Makefile文件。nginx的configure命令支持以下參數(shù):
--prefix=path
定義一個(gè)目錄,存放服務(wù)器上的文件 ,也就是nginx的安裝目錄。默認(rèn)使用 /usr/local/nginx。
--sbin-path=path
設(shè)置nginx的可執(zhí)行文件的路徑,默認(rèn)為 prefix
/sbin/nginx
.--conf-path=path
設(shè)置在nginx.conf配置文件的路徑。nginx允許使用不同的配置文件啟動(dòng),通過(guò)命令行中的-c選項(xiàng)。默認(rèn)為prefix
/conf/nginx.conf
.--pid-path=path 設(shè)置nginx.pid文件,將存儲(chǔ)的主進(jìn)程的進(jìn)程號(hào)。安裝完成后,可以隨時(shí)改變的文件名 , 在nginx.conf配置文件中使用 PID指令。默認(rèn)情況下,文件名 為
prefix
/logs/nginx.pid
.--error-log-path=path
設(shè)置主錯(cuò)誤,警告,和診斷文件的名稱。安裝完成后,可以隨時(shí)改變的文件名 ,在nginx.conf配置文件中 使用 的error_log指令。默認(rèn)情況下,文件名 為prefix
/logs/error.log
.--http-log-path=path
設(shè)置主請(qǐng)求的HTTP服務(wù)器的日志文件的名稱。安裝完成后,可以隨時(shí)改變的文件名 ,在nginx.conf配置文件中 使用 的access_log指令。默認(rèn)情況下,文件名 為prefix
/logs/access.log
.--user=name
設(shè)置nginx工作進(jìn)程的用戶。安裝完成后,可以隨時(shí)更改的名稱在nginx.conf配置文件中 使用的 user指令。默認(rèn)的用戶名是nobody。--group=name
設(shè)置nginx工作進(jìn)程的用戶組。安裝完成后,可以隨時(shí)更改的名稱在nginx.conf配置文件中 使用的 user指令。默認(rèn)的為非特權(quán)用戶。--with-select_module
--without-select_module 啟用或禁用構(gòu)建一個(gè)模塊來(lái)允許服務(wù)器使用select()方法。該模塊將自動(dòng)建立,如果平臺(tái)不支持的kqueue,epoll,rtsig或/dev/poll。
--with-poll_module
--without-poll_module
啟用或禁用構(gòu)建一個(gè)模塊來(lái)允許服務(wù)器使用poll()方法。該模塊將自動(dòng)建立,如果平臺(tái)不支持的kqueue,epoll,rtsig或/dev/poll。--without-http_gzip_module
— 不編譯壓縮的HTTP服務(wù)器的響應(yīng)模塊。編譯并運(yùn)行此模塊需要zlib庫(kù)。--without-http_rewrite_module
不編譯重寫(xiě)模塊。編譯并運(yùn)行此模塊需要PCRE庫(kù)支持。--without-http_proxy_module
— 不編譯http_proxy模塊。--with-http_ssl_module
— 使用https協(xié)議模塊。默認(rèn)情況下,該模塊沒(méi)有被構(gòu)建。建立并運(yùn)行此模塊的OpenSSL庫(kù)是必需的。--with-pcre=path
— 設(shè)置PCRE庫(kù)的源碼路徑。PCRE庫(kù)的源碼(版本4.4 - 8.30)需要從PCRE網(wǎng)站下載并解壓。其余的工作是Nginx的./ configure和make來(lái)完成。正則表達(dá)式使用在location指令和 ngx_http_rewrite_module 模塊中。--with-pcre-jit
—編譯PCRE包含“just-in-time compilation”(1.1.12中, pcre_jit指令)。--with-zlib=path
—設(shè)置的zlib庫(kù)的源碼路徑。要下載從 zlib(版本1.1.3 - 1.2.5)的并解壓。其余的工作是Nginx的./ configure和make完成。ngx_http_gzip_module模塊需要使用zlib 。--with-cc-opt=parameters
— 設(shè)置額外的參數(shù)將被添加到CFLAGS變量。例如,當(dāng)你在FreeBSD上使用PCRE庫(kù)時(shí)需要使用:--with-cc-opt="-I /usr/local/include。
.如需要需要增加 select()支持的文件數(shù)量
:--with-cc-opt="-D FD_SETSIZE=2048".
--with-ld-opt=parameters
—設(shè)置附加的參數(shù),將用于在鏈接期間。例如,當(dāng)在FreeBSD下使用該系統(tǒng)的PCRE庫(kù),應(yīng)指定:--with-ld-opt="-L /usr/local/lib".
典型實(shí)例(下面為了展示需要寫(xiě)在多行,執(zhí)行時(shí)內(nèi)容需要在同一行)
./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
- [root@localhost home]# tar zxvf pcre-8.02.tar.gz
- [root@localhost home]# cd pcre-8.02
- [root@localhost pcre-8.02]#./configure
- [root@localhost pcre-8.02]#make
- [root@localhost pcre-8.02]#make install
- [root@localhost home]# tar zxvf nginx-0.7.65.tar.gz
- [root@localhost home]#cd nginx-0.7.65
- [root@localhostnginx-0.7.65]#./configure \
- --with-http_stub_status_module --prefix=/opt/nginx
- [root@localhost nginx-0.7.65]#make
- [root@localhost nginx-0.7.65]#make install
- /opt/nginx/sbin/nginx –t 或者
- /opt/nginx/sbin/nginx -t -c /opt/nginx/conf/nginx.conf
- the configuration file /opt/nginx/conf/nginx.conf syntax is ok
- configuration file /opt/nginx/conf/nginx.conf test is successful
- /opt/nginx/sbin/nginx -v
- /opt/nginx/sbin/nginx -V
- /opt/nginx/sbin/nginx
- [root@localhost logs]# ps -ef|grep nginx
- root 16572 1 0 11:14 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx
- nobody 16591 16572 0 11:15 ? 00:00:00 nginx: worker process
- nobody 16592 16572 0 11:15 ? 00:00:00 nginx: worker process
- nobody 16593 16572 0 11:15 ? 00:00:00 nginx: worker process
- nobody 16594 16572 0 11:15 ? 00:00:00 nginx: worker process
- ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
- cat /opt/nginx/logs/nginx.pid
- kill -HUP ‘cat /opt/nginx/logs/nginx.pid‘
- http {
- server {
- listen 80;
- server_name www.domain1.com;
- access_log logs/domain1.access.log main;
- location / {
- index index.html;
- root /web/www/domain1.com/htdocs;
- }
- }
- server {
- listen 80;
- server_name www.domain2.com;
- access_log logs/domain2.access.log main;
- location / {
- index index.html;
- root /web/www/domain2.com/htdocs;
- }
- }
- include /opt/nginx/conf/vhosts/www.domain2.com.conf;
- }
- server {
- listen 80;
- server_name www.domain3.com;
- access_log logs/domain3.access.log main;
- location / {
- index index.html;
- root /web/www/domain3.com/htdocs;
- }
- }
- http
- {
- upstream myserver {
- server 192.168.12.181:80 weight=3 max_fails=3 fail_timeout=20s;
- server 192.168.12.182:80 weight=1 max_fails=3 fail_timeout=20s;
- server 192.168.12.183:80 weight=4 max_fails=3 fail_timeout=20s;
- }
-
- server
- {
- listen 80;
- server_name www.domain.com 192.168.12.189;
- index index.htm index.html;
- root /ixdba/web/wwwroot;
-
- location / {
- proxy_pass http://myserver;
- proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
- include /opt/nginx/conf/proxy.conf;
- }
- }
- }
-
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_connect_timeout 90;
- proxy_send_timeout 90;
- proxy_read_timeout 90;
- proxy_buffer_size 4k;
- proxy_buffers 4 32k;
- proxy_busy_buffers_size 64k;
- proxy_temp_file_write_size 64k;
- location ~* \.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {
- valid_referers none blocked *.ixdba1.net ixdba1.net;
- if ($invalid_referer) {
- rewrite ^/ http://www.ixdba.net/img/error.gif;
- #return 403;
- }
- }
- location /images {
- root /opt/nginx/html;
- valid_referers none blocked *.ixdba1.net ixdba1.net;
- if ($invalid_referer) {
- return 403;
- }
- }
- #/bin/bash
- savepath_log='/home/nginx/logs'
- nglogs='/opt/nginx/logs'
-
- mkdir -p $savepath_log/$(date +%Y)/$(date +%m)
- mv $nglogs/access.log $savepath_log/$(date +%Y)/$(date +%m)/access.$(date +%Y%m%d).log
- mv $nglogs/error.log $savepath_log/$(date +%Y)/$(date +%m)/error.$(date +%Y%m%d).log
- kill -USR1 `cat /opt/nginx/logs/nginx.pid`
-
- # debug
- CFLAGS=”$CFLAGS -g”
- [root@localhost home]#tar zxvf libunwind-0.99-alpha.tar.gz
- [root@localhost home]# cd libunwind-0.99-alpha/
- [root@localhost libunwind-0.99-alpha]#CFLAGS=-fPIC ./configure
- [root@localhost libunwind-0.99-alpha]#make CFLAGS=-fPIC
- [root@localhost libunwind-0.99-alpha]#make CFLAGS=-fPIC install
- [root@localhost home]#tar zxvf google-perftools-1.8.tar.gz
- [root@localhost home]#cd google-perftools-1.8/
- [root@localhost google-perftools-1.8]# ./configure
- [root@localhost google-perftools-1.8]#make && make install
- [root@localhost google-perftools-1.8]#echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
- [root@localhost google-perftools-1.8]# ldconfig
- [root@localhostnginx-0.7.65]#./configure \
- >--with-google_perftools_module --with-http_stub_status_module --prefix=/opt/nginx
- [root@localhost nginx-0.7.65]#make
- [root@localhost nginx-0.7.65]#make install
- [root@localhost home]#mkdir /tmp/tcmalloc
- [root@localhost home]#chmod 0777 /tmp/tcmalloc
- #pid logs/nginx.pid;
- google_perftools_profiles /tmp/tcmalloc;
- [root@ localhost home]# lsof -n | grep tcmalloc
- nginx 2395 nobody 9w REG 8,8 0 1599440 /tmp/tcmalloc.2395
- nginx 2396 nobody 11w REG 8,8 0 1599443 /tmp/tcmalloc.2396
- nginx 2397 nobody 13w REG 8,8 0 1599441 /tmp/tcmalloc.2397
- nginx 2398 nobody 15w REG 8,8 0 1599442 /tmp/tcmalloc.2398
- net.ipv4.tcp_max_tw_buckets = 6000
- net.ipv4.ip_local_port_range = 1024 65000
- net.ipv4.tcp_tw_recycle = 1
- net.ipv4.tcp_tw_reuse = 1
- net.ipv4.tcp_syncookies = 1
- net.core.somaxconn = 262144
- net.core.netdev_max_backlog = 262144
- net.ipv4.tcp_max_orphans = 262144
- net.ipv4.tcp_max_syn_backlog = 262144
- net.ipv4.tcp_synack_retries = 1
- net.ipv4.tcp_syn_retries = 1
- net.ipv4.tcp_fin_timeout = 1
- net.ipv4.tcp_keepalive_time = 30
- gcc gcc-c++ libxml2 libxml2-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel
- [root@localhost local]#tar zxvf php-5.2.13.tar.gz
- [root@localhost local]#gzip -cd php-5.2.13-fpm-0.5.13.diff.gz | patch -d php-5.2.13 -p1
- [root@localhost local]#cd php-5.2.13
- [root@localhost php-5.2.13]#./configure --prefix=/usr/local/php --enable-fastcgi --enable-fpm
- [root@localhost php-5.2.13]#make
- [root@localhost php-5.2.13]#make install
- [root@localhost php-5.2.13]cp php.ini-dist /usr/local/php/lib/php.ini
- /usr/local/php/bin/php-cgi --fpm
- 或者
- /usr/local/php/sbin/php-fpm start
- start,啟動(dòng)PHP的FastCGI進(jìn)程。
- stop,強(qiáng)制終止PHP的FastCGI進(jìn)程。
- quit,平滑終止PHP的FastCGI進(jìn)程。
- restart, 重啟PHP的FastCGI進(jìn)程。
- reload, 重新加載PHP的php.ini。
- logrotate, 重新啟用log文件。
- [root@localhost php]# netstat -antl|grep 9000
- tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN
- [root@localhost php]# ps -ef|grep php-cgi
- root 3567 1 0 17:06 ? 00:00:00 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/php/etc/php-fpm.conf
- nobody 3568 3567 0 17:06 ? 00:00:00 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/php/etc/php-fpm.conf
- nobody 3569 3567 0 17:06 ? 00:00:00 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/php/etc/php-fpm.conf
- nobody 3570 3567 0 17:06 ? 00:00:00 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/php/etc/php-fpm.conf
- nobody 3571 3567 0 17:06 ? 00:00:00 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/php/etc/php-fpm.conf
- nobody 3572 3567 0 17:06 ? 00:00:00 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/php/etc/php-fpm.conf
- root 3583 3524 0 17:09 pts/1 00:00:00 grep php-cgi
- server {
- include port.conf;
- server_name www.ixdba.net ixdba.net;
-
- location / {
- index index.html index.php;
- root /web/www/www.ixdba.net;
- }
-
- location ~ \.php$ {
- root html;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME html$fastcgi_script_name;
- include fastcgi_params;
- }
- }
-
- fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;
- fastcgi_connect_timeout 300;
- fastcgi_send_timeout 300;
- fastcgi_read_timeout 300;
- fastcgi_buffer_size 64k;
- fastcgi_buffers 4 64k;
- fastcgi_busy_buffers_size 128k;
- fastcgi_temp_file_write_size 128k;
- fastcgi_cache TEST;
- fastcgi_cache_valid 200 302 1h;
- fastcgi_cache_valid 301 1d;
- fastcgi_cache_valid any 1m;
聯(lián)系客服