ubuntu 源码安装nginx

ubuntu 安装 nginx



下载nginx源码包(从http://wiki.nginx.org/NginxChs上)

编译nginx需要指定pcre,zlib,openssl,既然我的系统没有安装这些包,我也不安装deb的包了,直接运行
sudo apt-get install libssl-dev  libepcre3  libepcre3-dev


然后也将nginx-0.6.32的包解压到/opt目录下,进入nginx目录,执行:
#./configure --sbin-path=/usr/local/sbin --with-http_ssl_module --with-pcre=../pcre-7.7 --with-zlib=../zlib-1.2.3 --with-openssl=../openssl-0.9.8g
#make && make install

如果在./configure时出现在错误,依次查询问题,一般情况下不会出现什么错误,但前提需要安装build-essential,如果没有安装,可执行以下命令安装编译环境
#apt-get install build-essential

默认nginx安装的目录在/usr/local/nginx下,包括:

/usr/local/nginx/sbin #控制nginx启动文件
/usr/local/nginx/conf #配置文件
/usr/local/nginx/html #默认网页文件
/usr/local/nginx/logs #日志文件



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

转:http://docs.google.com/View?docid=dhf86kr9_302cssf49dh&hgd=1

Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,相对Apache的臃肿,Nginx绝对是轻量级的,配置简单,本篇文章介绍如何在Ubuntu Hardy 上用源,码来编译Nginx。
一、选择版本

选 择从源码编译Nginx的大多数原因是可以采用最新的版本,而不局限于aptitude安装的旧版本,在写本文的时候,Nginx的最新稳定版本是 0.6.31(你可以到Nginx的官方站点查看最新版本),而aptitude安装的是0.5.33版本。
当然,采用源码编译会比aptitude麻烦一些,以后更新都需要DIY,如果你喜欢方便,且对版本不是很敏感的话,你可以参考前 面一篇文章“使用aptitude安装Nginx”采用aptitude来安装。
二、注意目录结构

需要注意的是,采用aptitude安装Nginx和从源码编译安装的Nginx的时候,目录结构是有 所不同的,使用aptitude安装的Nginx会在其安装目录中创建'sites-available' 和 'sites-enabled'目录(貌似想Ubuntu上的Apache安装目录结构),而从源码编译的时候,并不会自动创建这两个目录的,后面我们会 说如何修改使得其达到一致(虽然可以不用修改,但是把每个web的配置放到一个配置文件中,总比混合在一起好)。
三、依赖关系

采用aptitude安装的时候,系统会将所有依赖的包都一并装上,但是使 用源码编译的时候就没这么智能了,我们需要找到需要的依赖包,然后手工安装,幸运的是,这并不复杂,也不多,例如pcre, ssl 和zlib,安装方法比较简单:

    sudo aptitude install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev

四、安装Nginx

ok,准备工作做完了,是时候开始下载、 安装Nginx了。
1、创建一个目录

    mkdir ~/sources
    cd ~/sources/

2、 下载源代码

    wget http://sysoev.ru/nginx/nginx-0.6.31.tar.gz

3、 解压

    tar -zxvf nginx-0.6.31.tar.gz
    ...
    cd nginx-0.6.31/

4、选择需要的编译选项
Nginx的编译选项还是不少 的,你可以参考Nginx wiki的Install Options 这个页面或者我下面的附录1来了解每个选项的详细信息,基本上都是定制位置和模块的,这里就是要两个模块就够了(要是你有自己的需求,自己DIY),分别 是:
1)--sbin-path=/usr/local/sbin
从源码编 译Nginx会安装在/usr/local/nginx目录下(你可以使用参数--prefix=<path>指定自己需要的位置),然后会 将bin文件放在/usr/local/nginx/sbin/nginx,虽然比较清晰,但是和我们一般的习惯不同(我们习惯在/usr/local /sbin下寻找bin文件);
2)--with-http_ssl_module
这个没啥好说的,主要是提供https访问支持的。

5、 Compile

    ./configure --sbin-path=/usr/local/sbin --with-http_ssl_module

编 译的时间不会很长,完成的时候你会在屏幕上看到类似下面的信息:

    ...
    nginx path prefix: "/usr/local/nginx"
    nginx binary file: "/usr/local/sbin"
    nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
    ...

留 意上面的提示,涉及到nginx的几个重要文件的路径信息。

6、Make && make install
差不多快完成了,现在make一下。

    make
    ...
    sudo make install

7、chmod
还需 要给bin文件有执行权限,也比较简单:

    chmod +x /usr/local/sbin/nginx

五、使用

安装完成后,试着使用下。
1、启动

    sudo /usr/local/sbin/nginx    (启动)
    /usr/local/sbin/nginx -s stop (停止)
    /usr/local/sbin/nginx -s reload (重启)









然后把自己的浏览器导航到http://IP就可 以看到默认的欢迎界面了,如下:


2、停止
如果你使用过apache或者看到前面那篇使用aptitude安装Nginx的话,你可能会对/etc/init.d/下的那 个nginx脚本印象比较深刻,使用/etc/init.d/nginx可以直接start或者stop,但是,从源码编译的Nginx是没有自动创建这 个脚本的(后面会告诉你怎么手工创建),现在看看在没有创建这个脚本前,如何了stop。
或许你已经注意编译的时候,有这 个提示“nginx pid file: "/usr/local/nginx/logs/nginx.pid"”,你可以cat下看看,他记录的就是当前nginx的pid号,于是我们就可以这 样来停止nginx了。

    sudo kill `cat /usr/local/nginx/logs/nginx.pid`

特别注意:这里使用的是反 引号(`)而不是单引号(').
六、小结

这篇 文章介绍了如何在Ubuntu上使用源码按照我们的需要来编译安装nginx,然后介绍了如何手工启动和停止nginx,前面也提到了,后续文章会介绍如 何创建init脚本,以及如何规划调整目录结构(sites-available 和sites-enabled);然后会介绍如何配置nginx使其更能发挥威力。
七、附录:Compile-time options

(原文参考:
http://wiki.codemongers.com/NginxInstallOptions )

configure 脚本确定系统所具有一些特性,特别是 nginx 用来处理连接的方法。然后,它创建 Makefile 文件。

configure 支持下面的选项:

1)目录属性

--prefix=<path> - Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。

--sbin-path=<path> - Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为<prefix>/sbin/nginx。

--conf-path=<path> - 在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为<prefix>/conf/nginx.conf。

--pid-path=<path> - 在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 <prefix>/logs/nginx.pid。

--lock-path=<path> - nginx.lock文件的路径,默认为<prefix>/logs/nginx.lock

--error-log-path=<path> - 在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 <prefix>/logs/error.log。

--http-log-path=<path> - 在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 <prefix>/logs/access.log。

--user=<user> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。

--group=<group> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。

--builddir=DIR - 指定编译的目录

2)模块

--with-rtsig_module - 启用 rtsig 模块

--with-select_module --without-select_module -允许或不允许开启SELECT模式,如果 configure 没有找到更合适的模式,比如:kqueue(sun os),epoll (linux kenel 2.6+), rtsig(实时信号)或者/dev/poll(一种类似select的模式,底层实现与SELECT基本相 同,都是采用轮训方法) SELECT模式将是默认安装模式

--with-poll_module --without-poll_module - Whether or not to enable the poll module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.

--with-http_ssl_module -开启HTTP SSL模块,使NGINX可以支持HTTPS请求。这个模块需要已经安装了OPENSSL,在DEBIAN上是libssl

--with-http_realip_module - 启用 ngx_http_realip_module

--with-http_addition_module - 启用 ngx_http_addition_module

--with-http_sub_module - 启用 ngx_http_sub_module

--with-http_dav_module - 启用 ngx_http_dav_module

--with-http_flv_module - 启用 ngx_http_flv_module

--with-http_stub_status_module - 启用 "server status" 页

--without-http_charset_module - 禁用 ngx_http_charset_module

--without-http_gzip_module - 禁用 ngx_http_gzip_module. 如果启用,需要 zlib 。

--without-http_ssi_module - 禁用 ngx_http_ssi_module

--without-http_userid_module - 禁用 ngx_http_userid_module

--without-http_access_module - 禁用 ngx_http_access_module

--without-http_auth_basic_module - 禁用 ngx_http_auth_basic_module

--without-http_autoindex_module - 禁用 ngx_http_autoindex_module

--without-http_geo_module - 禁用 ngx_http_geo_module

--without-http_map_module - 禁用 ngx_http_map_module

--without-http_referer_module - 禁用 ngx_http_referer_module

--without-http_rewrite_module - 禁用 ngx_http_rewrite_module. 如果启用需要 PCRE 。

--without-http_proxy_module - 禁用 ngx_http_proxy_module

--without-http_fastcgi_module - 禁用 ngx_http_fastcgi_module

--without-http_memcached_module - 禁用 ngx_http_memcached_module

--without-http_limit_zone_module - 禁用 ngx_http_limit_zone_module

--without-http_empty_gif_module - 禁用 ngx_http_empty_gif_module

--without-http_browser_module - 禁用 ngx_http_browser_module

--without-http_upstream_ip_hash_module - 禁用 ngx_http_upstream_ip_hash_module

--with-http_perl_module - 启用 ngx_http_perl_module

--with-perl_modules_path=PATH - 指定 perl 模块的路径

--with-perl=PATH - 指定 perl 执行文件的路径

--http-log-path=PATH - Set path to the http access log

--http-client-body-temp-path=PATH - Set path to the http client request body temporary files

--http-proxy-temp-path=PATH - Set path to the http proxy temporary files

--http-fastcgi-temp-path=PATH - Set path to the http fastcgi temporary files

--without-http - 禁用 HTTP server

--with-mail - 启用 IMAP4/POP3/SMTP 代理模块

--with-mail_ssl_module - 启用 ngx_mail_ssl_module

--with-cc=PATH - 指定 C 编译器的路径

--with-cpp=PATH - 指定 C 预处理器的路径

--with-cc-opt=OPTIONS - Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-cc-opt="-I /usr/local/include". If we are using select() and it is necessary to increase the number of file descriptors, then this also can be assigned here: --with-cc-opt="-D FD_SETSIZE=2048".

--with-ld-opt=OPTIONS - Additional parameters passed to the linker. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-ld-opt="-L /usr/local/lib".

--with-cpu-opt=CPU - 为特定的 CPU 编译,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64

--without-pcre - 禁止 PCRE 库的使用。同时也会禁止 HTTP rewrite 模块。在 "location" 配置指令中的正则表达式也需要 PCRE 。

--with-pcre=DIR - 指定 PCRE 库的源代码的路径。

--with-pcre-opt=OPTIONS - Set additional options for PCRE building.

--with-md5=DIR - Set path to md5 library sources.

--with-md5-opt=OPTIONS - Set additional options for md5 building.

--with-md5-asm - Use md5 assembler sources.

--with-sha1=DIR - Set path to sha1 library sources.

--with-sha1-opt=OPTIONS - Set additional options for sha1 building.

--with-sha1-asm - Use sha1 assembler sources.

--with-zlib=DIR - Set path to zlib library sources.

--with-zlib-opt=OPTIONS - Set additional options for zlib building.

--with-zlib-asm=CPU - Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro

--with-openssl=DIR - Set path to OpenSSL library sources

--with-openssl-opt=OPTIONS - Set additional options for OpenSSL building

--with-debug - 启用调试日志

--add-module=PATH - Add in a third-party module found in directory PATH
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=

猜你喜欢

转载自selfcontroller.iteye.com/blog/1673599