设为首页 收藏本站
查看: 1332|回复: 0

[经验分享] CentOS 6.5 编译安装Nginx

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-5-9 10:00:15 | 显示全部楼层 |阅读模式
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
wKioL1NrnBKyQV7eAAF9reXcLXc864.jpg 1、从源代码编译 Nginx
解压下载下好的源码包
tar -xzvf nginx-1.6.0.tar.gz
进入解压出的源码文件夹
cd nginx-1.6.0
wKioL1NrnHyxQB0MAACSXf_SL3Y636.jpg
命令行执行如下命令:
./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 isnamedprefix/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_modulemodule.
·--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 install1.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
-------------------------------------
2、yum 安装 Nginx为了追加 nginx 的 yum 仓库,需要创建一个文件 /etc/yum.repos.d/nginx.repo,并将下面的其中一个内容复制进去:[nginx]name=nginx repobaseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/gpgcheck=0enabled=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 repobaseurl=http://nginx.org/packages/centos/6/$basearch/gpgcheck=0enabled=1then run the following:yum install nginx wKiom1NrnNnwbppVAAOEKbeW8v0850.jpg 完成安装3、配置Nginx3.1、Serving 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.2、Setting 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.3、Setting 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
wKiom1NrnReDVJgyAAE_XUb9Tx4614.jpg
控制命令:
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 nginxkill -s QUIT 1628  with the help of Unix tools such as the kill utility,send 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




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-18967-1-1.html 上篇帖子: nginx一些参数配置详解 下篇帖子: nginx配置403错误页面
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表