CentOS6.5编译安装Nginx的方法

来源:互联网 发布:不该 周杰伦 知乎 编辑:程序博客网 时间:2024/06/03 19:03

Nginx介绍参考:Nginx介绍

Nginx的官网:http://nginx.org/ ,Nginx有三个版本:稳定版、开发版和历史稳定版。开发版更新快,包含最新的功能和bug修复,但同时也可能会出现新的bug。开发版一旦更新稳定下来,就会被加入稳定版分支,稳定版更新较慢,但bug较少,所以生产环境优先选择稳定版

一、下载Nginx安装文件

目前最新稳定版:http://nginx.org/download/nginx-1.10.1.tar.gz ,可以先下载好安装文件再通过ftp上传的CentOS上,也可以在CentOS上直接通过wget命令下载,这里我将文件下载到了/home/software文件夹下,如下:

[plain] view plain copy
  1. [root@localhost software]# pwd  
  2. /home/software  
  3. [root@localhost software]# wget http://nginx.org/download/nginx-1.10.1.tar.gz  

二、解压安装文件

[plain] view plain copy
  1. [root@songguoliang software]# tar -xzvf nginx-1.10.1.tar.gz   

三、执行configure命令

通过cd命令进入Nginx解压文件目录,执行该目录下的configure命令,--prefix是打算将Nginx安装在哪个目录。在执行configure命令之前,确保安装了gcc、openssl-devel、pcre-devel和zlib-devel软件库(gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库),也可以直接执行configure命令,根据提示缺少的软件库安装,下面有缺少相应库报的错误信息和安装依赖库的方法。

[plain] view plain copy
  1. [root@localhost software]# cd nginx-1.10.1  
  2. [root@localhost nginx-1.10.1]# pwd  
  3. /home/software/nginx-1.10.1  
  4. [root@localhost nginx-1.10.1]# ./configure --prefix=/usr/local/nginx  
1、如果报下面错误,说明还没有安装gcc编译环境,可以通过yum在线安装功能安装gcc,重新执行configure命令。
[plain] view plain copy
  1. [root@localhost nginx-1.10.1]# ./configure --prefix=/usr/local/nginx  
  2. checking for OS  
  3.  + Linux 2.6.32-431.el6.x86_64 x86_64  
  4. checking for C compiler ... not found  
  5.   
  6. ./configure: error: C compiler cc is not found  
在线安装gcc:
[plain] view plain copy
  1. [root@localhost nginx-1.10.1]# yum install gcc  
2、如果报下面的错误,说明没有安装pcre-devel库,通过yum在线安装pcre后,重新执行configure命令。

[plain] view plain copy
  1. ./configure: error: the HTTP rewrite module requires the PCRE library.  
  2. You can either disable the module by using --without-http_rewrite_module  
  3. option, or install the PCRE library into the system, or build the PCRE library  
  4. statically from the source with nginx by using --with-pcre=<path> option.  
在线安装pcre-devel库:
[plain] view plain copy
  1. [root@localhost nginx-1.10.1]# yum -y install pcre-devel  
-y参数表示使用yum在线安装时,如果需要用户输入Y/N时自动输入Y。
3、如果报下面的错误,说明没有安装zlib库,安装zlib库后重新执行configure命令。

[plain] view plain copy
  1. ./configure: error: the HTTP gzip module requires the zlib library.  
  2. You can either disable the module by using --without-http_gzip_module  
  3. option, or install the zlib library into the system, or build the zlib library  
  4. statically from the source with nginx by using --with-zlib=<path> option.  
在线安装zlib库:
[plain] view plain copy
  1. [root@localhost nginx-1.10.1]# yum -y install zlib-devel  

4、如果报以下错误,说明没有安装OpenSSL库,安装OpenSSL库后重新执行configure命令。

[plain] view plain copy
  1. ./configure: error: SSL modules require the OpenSSL library.  
  2. You can either do not enable the modules, or install the OpenSSL library  
  3. into the system, or build the OpenSSL library statically from the source  
  4. with nginx by using --with-openssl=<path> option.  
在线安装openssl库:

[plain] view plain copy
  1. [root@localhost nginx-1.10.1]# yum install openssl-devel  



执行configure命令成功后,显示如下信息:

[plain] view plain copy
  1. checking for zlib library ... found  
  2. creating objs/Makefile  
  3.   
  4. Configuration summary  
  5.   + using system PCRE library  
  6.   + OpenSSL library is not used  
  7.   + using builtin md5 code  
  8.   + sha1 library is not found  
  9.   + using system zlib library  
  10.   
  11.   nginx path prefix: "/usr/local/nginx"  
  12.   nginx binary file: "/usr/local/nginx/sbin/nginx"  
  13.   nginx modules path: "/usr/local/nginx/modules"  
  14.   nginx configuration prefix: "/usr/local/nginx/conf"  
  15.   nginx configuration file: "/usr/local/nginx/conf/nginx.conf"  
  16.   nginx pid file: "/usr/local/nginx/logs/nginx.pid"  
  17.   nginx error log file: "/usr/local/nginx/logs/error.log"  
  18.   nginx http access log file: "/usr/local/nginx/logs/access.log"  
  19.   nginx http client request body temporary files: "client_body_temp"  
  20.   nginx http proxy temporary files: "proxy_temp"  
  21.   nginx http fastcgi temporary files: "fastcgi_temp"  
  22.   nginx http uwsgi temporary files: "uwsgi_temp"  
  23.   nginx http scgi temporary files: "scgi_temp"  

四、执行make命令

[plain] view plain copy
  1. [root@localhost nginx-1.10.1]# make  

五、执行make install命令

[plain] view plain copy
  1. [root@localhost nginx-1.10.1]# make install  

步骤四和步骤五可以合并执行如下命令,连接符 && 代表前面一个命令如果执行成功则继续执行后面的命令,如果前面命令执行失败则不再执行后面的命令。而 || 表示如果前面的命令执行成功则不执行后面的命令,如果前面的命令执行失败则继续执行后面的命令

[plain] view plain copy
  1. [root@localhost nginx-1.10.1]# make && make install  

六、启动Nginx服务

[plain] view plain copy
  1. [root@localhost nginx-1.10.1]# cd /usr/local/nginx/  
  2. [root@localhost nginx]# ll  
  3. 总用量 16  
  4. drwxr-xr-x. 2 root root 4096 10月  1 23:35 conf  
  5. drwxr-xr-x. 2 root root 4096 10月  1 23:35 html  
  6. drwxr-xr-x. 2 root root 4096 10月  1 23:35 logs  
  7. drwxr-xr-x. 2 root root 4096 10月  1 23:35 sbin  
  8. [root@songguoliang nginx]# ./sbin/nginx   

通过浏览器访问Nginx,显示如下welcome to nginx!页面便表示安装成功:




注:

如果80端口被占用,服务将启动失败,关闭其他程序重新启动Nginx即可。

如果开启着防火墙,需要开放80端口,在/etc/sysconfig/iptables文件里添加如下内容:

[plain] view plain copy
  1. -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT   
原创粉丝点击