码迷,mamicode.com
首页 > 其他好文 > 详细

CentOS 6.5 编译安装Nginx

时间:2014-05-09 06:41:04      阅读:522      评论:0      收藏:0      [点我收藏+]

标签:centos 6.5 nginx

Nginx

Nginx("enginex") 是一个高性能的 HTTP 反向代理服务器,也是一个 IMAP/POP3/SMTP代理服务器 Nginx 是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,它已经在该站点运行超过四年多了。Igor 将源代码以类BSD许可证的形式发布。自Nginx 发布四年来,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。目前国内各大门户网站已经部署了Nginx,如新浪、网易、腾讯等;国内几个重要的视频分享网站也部署了Nginx,如六房间、酷6等。新近发现Nginx 技术在国内日趋火热,越来越多的网站开始部署Nginx

因为 Nginx 的文档大部分是英文的,本维基的目的是帮助使用中文世界的用户安装、配置和交换有关 Nginx 的使用经验。大部分文档源自(al-nginx ATnone.at,Aleksandar Lazic) 的工作,有关本维基本身的问题请直接询问Cliff Wells[1],其他翻译请点击

nginx主要用于反向加速代理而不是像squid那样做为常规代理服务器。Nginx的最大优势在于高负载情况下内存和CPU的低消耗。

下载最新版本Nginx 网址http://nginx.org/en/download.html

bubuko.com,布布扣

1、从源代码编译 Nginx

解压下载下好的源码包

tar -xzvf nginx-1.6.0.tar.gz

进入解压出的源码文件夹

cd nginx-1.6.0

bubuko.com,布布扣

命令行执行如下命令:

./configure

make

sudo make install

默认情况下,Nginx 会被安装在 /usr/local/nginx。通过设定编译选项,你可以改变这个设定。

1.1、编译选项

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

./configure --help 查看有效模块

configure 支持下面的选项:

The build is configured usingthe configure command. It defines various aspects of the system,including the methods nginx is allowed to use for connection processing. At theend it creates a Makefile. The configure command supports thefollowing parameters:

·--prefix=path —defines a directory that will keep server files. This same directory will alsobe used for all relative paths set byconfigure (except for paths tolibraries sources) and in the nginx.conf configuration file. It isset to the /usr/local/nginxdirectoryby default.

·--sbin-path=path —sets the name of an nginx executable file. This name is used only duringinstallation. By default the file is named prefix/sbin/nginx.

·--conf-path=path —sets the name of an nginx.conf configuration file. If needs be, nginxcan always be started with a different configuration file, by specifying it inthe command-line parameter -c file. By default the file isnamed prefix/conf/nginx.conf.

·--pid-path=path —sets the name of an nginx.pid file that will store the process ID of the mainprocess. After installation, the file name can always be changed inthe nginx.conf configuration file using the pid directive. By default the file is namedprefix/logs/nginx.pid.

·--error-log-path=path —sets the name of the primary error, warnings, and diagnostic file. Afterinstallation, the file name can always be changed inthe nginx.conf configuration file using the error_log directive. By default the file is namedprefix/logs/error.log.

·--http-log-path=path —sets the name of the primary request log file of the HTTP server. Afterinstallation, the file name can always be changed inthe nginx.conf configuration file using the access_log directive. By default the file is namedprefix/logs/access.log.

·--user=name —sets the name of an unprivileged user whose credentials will be used by workerprocesses. After installation, the name can always be changed inthe nginx.conf configuration file using the user directive. The default user name is nobody.

·--group=name —sets the name of a group whose credentials will be used by worker processes.After installation, the name can always be changed inthe nginx.conf configuration file using the user directive. By default, a group name is set to the name of anunprivileged user.

·--with-select_module

·--without-select_module —enables or disables building a module that allows the server to work withthe select() method. This module is built automatically if theplatform does not appear to support more appropriate methods such as kqueue,epoll, rtsig, or /dev/poll.

·--with-poll_module

·--without-poll_module —enables or disables building a module that allows the server to work withthe poll() method. This module is built automatically if the platformdoes not appear to support more appropriate methods such as kqueue, epoll,rtsig, or /dev/poll.

·--without-http_gzip_module —disables building a module that compressesresponses of an HTTPserver. The zlib libraryis required to build and run this module.

·--without-http_rewrite_module —disables building a module that allows an HTTP server to redirectrequests and change URI of requests. The PCRE libraryis required to build and run this module.

·--without-http_proxy_module —disables building an HTTP server proxyingmodule.

·--with-http_ssl_module —enables building a module that adds the HTTPSprotocol support to an HTTPserver. This module is not built by default. The OpenSSL library is required to build and run thismodule.

·--with-pcre=path —sets the path to the sources of the PCRE library. The library distribution(version 4.4 — 8.32) needs to be downloaded from the PCRE site and extracted. The rest is done bynginx’s ./configure and make. The library is required forregular expressions support in the location directive and for the ngx_http_rewrite_module module.

·--with-pcre-jit —builds the PCRE library with “just-in-time compilation” support (1.1.12,the pcre_jit directive).

·--with-zlib=path —sets the path to the sources of the zlib library. The library distribution(version 1.1.3 — 1.2.7) needs to be downloaded from the zlib site and extracted. The rest is done bynginx’s ./configure and make. The library is required for thengx_http_gzip_module module.

·--with-cc-opt=parameters —sets additional parameters that will be added to the CFLAGS variable. Whenusing the system PCRE library under FreeBSD, --with-cc-opt="-I/usr/local/include" should be specified. If the number of filessupported byselect() needs to be increased it can also be specified heresuch as this: --with-cc-opt="-D FD_SETSIZE=2048".

·--with-ld-opt=parameters —sets additional parameters that will be used during linking. When using thesystem PCRE library under FreeBSD, --with-ld-opt="-L/usr/local/lib" should be specified.

Example of parameters usage (all of thisneeds to be typed in one line):

./configure \

   --with-pcre=../pcre-4.4 \

--with-zlib=../zlib-1.1.3

After configuration, nginx is compiled andinstalled using make.

在不同版本间,选项可能会有些许变化,请总是使用./configure --help命令来检查一下当前的选项列表。

1.2、编译安装

首先安装需求包:

yum  install  gcc  openssl-devel  pcre-devel  zlib-devel

配置参数:

./configure \

 --sbin-path=/usr/local/sbin


Configuration summary

 + using system PCRE library

 + OpenSSL library is not used

 + md5: using system crypto library

 + sha1: using system cryptolibrary

 + using system zlib library

nginx path prefix: "/usr/local/nginx"

nginx binary file: "/usr/local/sbin/nginx"

nginx configuration prefix: "/usr/local/nginx/conf"

nginx configuration file: "/usr/local/nginx/conf/nginx.conf"

nginx pid file: "/usr/local/nginx/logs/nginx.pid"

nginx error log file: "/usr/local/nginx/logs/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"

最后编译并安装

make && make install

1.3、错误提示

./configure: error: the HTTP rewrite module requires the PCRElibrary.

You can either disable the module by using--without-http_rewrite_module

option, or install the PCRE library into the system, or buildthe PCRE library

statically from the source with nginx by using--with-pcre=<path> option.

解决办法:

从上面的提示可以看出,需要安装PCRE

yum install  pcre-devel

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

2yum 安装 Nginx

为了追加 nginx  yum 仓库,需要创建一个文件 /etc/yum.repos.d/nginx.repo,并将下面的其中一个内容复制进去:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1

Replace “OS” with “rhel” or “centos”, depending on the distributionused, and “OSRELEASE” with “5” or “6”, for 5.x or 6.x versions,respectively.

eg

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
then run the following:
yum install nginx
bubuko.com,布布扣
完成安装

3、配置Nginx

3.1Serving StaticContent

An important web server task is serving out files (such as images orstatic HTML pages). You will implement an example where, depending on therequest, files will be served from different local directories: /data/www (which may contain HTML files) and/data/images (containing images).

http {

              server {

                              location/ {

                                             root/data/www;

                              }


                              location/images/ {

                                             root/data;

                              }

              }

}

This is already a working configuration ofa server that listens on the standardport 80and is accessible onthe local machine athttp://localhost/. In response to requests with URIsstarting with /images/, the server will send files from the /data/imagesdirectory.For example, in response to the http://localhost/images/example.png requestnginx will send the /data/images/example.pngfile. If such file does notexist, nginx will send a response indicating the 404 error. Requests with URIsnot starting with /images/ will be mapped onto the /data/www directory.For example, in response to the http://localhost/some/example.html requestnginx will send the /data/www/some/example.html file.

3.2Setting Up a SimpleProxy Server

One of the frequent uses of nginx issetting it up as a proxy server, which means a server that receives requests,passes them to the proxied servers, retrieves responses from them, and sendsthem to the clients.

We will configure a basic proxy server,which serves requests of images with files from the local directory and sendsall other requests to a proxied server. In this example, both servers will bedefined on a single nginx instance.

First, define the proxied server by addingone more server block to thenginx’s configuration file with the following contents:

server{

   listen 8080;

   root /data/up1;


   location / {

   }

}

This will be a simple server that listenson the port 8080 (previously, the listen directive has not been specifiedsince the standard port 80 was used) and maps all requests to the /data/up1 directory onthe local file system. Create this directory and put theindex.html file intoit. Note that the root directive isplaced in the server context.Such root directive isused when the location blockselected for serving a request does not include own root directive.

When nginxselects a location block to serve a request it first checks location directives that specify prefixes,remembering locationwith the longest prefix, and then checks regularexpressions. If there is a match with a regular expression, nginx picks this locationor,otherwise, it picks the one remembered earlier.

The resulting configuration of a proxyserver will look like this:

server{

   location / {

       proxy_pass http://localhost:8080/;

   }


   location ~ \.(gif|jpg|png)$ {

       root /data/images;

   }

}

3.3Setting Up FastCGIProxying

nginx can beused to route requests to FastCGI servers which run applications built withvarious frameworks and programming languages such as PHP.

The most basic nginx configuration to work with a FastCGI serverincludes using thefastcgi_passdirective instead of theproxy_passdirective,andfastcgi_paramdirectives to set parameters passed toa FastCGI server. Suppose the FastCGI server is accessible onlocalhost:9000.Taking the proxy configuration from the previous section as a basis, replacetheproxy_passdirectivewith thefastcgi_passdirective andchange the parameter tolocalhost:9000.In PHP, theSCRIPT_FILENAMEparameter isused for determining the script name, and theQUERY_STRINGparameter isused to pass request parameters. The resulting configuration would be:

server {
    location / {
        fastcgi_pass  localhost:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param QUERY_STRING    $query_string;
    }
    location ~ \.(gif|jpg|png)$ {
        root /data/images;
    }
}

This will set up a server that will route all requests except forrequests for static images to the proxied server operating onlocalhost:9000through theFastCGI protocol.


4、启动 nginx

启动命令:nginx

bubuko.com,布布扣

控制命令:

nginx -s signal

Wheresignalmay be one of the following:

  • stop — fast shutdown

  • quit — graceful shutdown


This command should be executed under the same user that started nginx.
ps -ax | grep nginx
kill -s QUIT 1628  with the help of Unix tools such as the kill utilitysend the QUIT signal resulting in nginx’s graceful shutdown
  • reload — reloading the configuration     file

nginx -s reload

In case somethingdoes not work as expected, you may try to find out the reasonin access.log and error.log files in thedirectory /usr/local/nginx/logs or /var/log/nginx.

  • reopen — reopening the log     files


查看nginx是否正常启动

ps –ef  | grep nginx


CentOS 6.5 编译安装Nginx,布布扣,bubuko.com

CentOS 6.5 编译安装Nginx

标签:centos 6.5 nginx

原文地址:http://suxiaomeng.blog.51cto.com/4505377/1408642

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!