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

在centos下编译安装配置高性能Nginx

时间:2014-12-13 10:50:51      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:http   io   ar   os   使用   sp   strong   on   文件   

安装nginx的依赖包:pcre, pcre-devel

 

编译nginx事实上需要的依赖包是pcre-devel,可以执行yum install pcre-devel 安装它。不过这个包的编译安装很简单,正好我们拿它练练手,熟悉熟悉linux编译安装软件的一般过程。

[tips] linux下从源码编译安装软件一般是三步:配置、编译、安装。具体一点说就依次是执行三条命令:configure, make, make install. 不多讲理论,实际操作一下就明白了。

 在build目录下创建子目录pcre

[feng@fsvps build]$ mkdir pcre
[feng@fsvps build]$ cd pcre

使用wget 工具从pcre官方下载 pcre 包,下载链接为 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.zip,将它解压缩到pcre目录下

[feng@fsvps build]$ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.zip

几秒钟就可以下载完成。(如果提示 bash: wget: command not found 那是你还没有安装wget,切换到root用户,yum install wget -y 安装之)

[feng@fsvps pcre]$ ls
pcre-8.21.zip

看一下,刚下载的 pcre源码包,这是个zip包,使用unzip命令解压缩之

[feng@fsvps pcre]$ unzip pcre-8.21.zip
[feng@fsvps pcre]$ ll
总用量 1680
drwxr-xr-x 6 feng feng    4096 12月 12 2011 pcre-8.21
-rw-rw-r-- 1 feng feng 1709377  6月 30 13:50 pcre-8.21.zip
[feng@fsvps pcre]$ cd pcre-8.21

进去看看里面都是些什么文件,特别注意一下configure

文件

[feng@fsvps pcre-8.21]$ ll
总用量 3716
-rwxr-xr-x 1 feng feng   6961 10月  5 2009 132html
-rw-r--r-- 1 feng feng 338277 12月 11 2011 aclocal.m4
........  (略)
-rwxr-xr-x 1 feng feng 595700 12月 11 2011 configure
........  (略)
-rw-r--r-- 1 feng feng   3460  8月  2 2011 ucp.h

这个configure文件,就是“源码安装三步曲”的第一步的configure,它将检查你的系统是否具备了编译pcre必要的软件包,配置出用于编译pcre的环境,供第二步用。如果缺少某些软件,它会给出提示。

[tips] 另外,注意这个目录里还有几个文件README, INSALL,LICENCE,它们都是普通的文本文件,供使用人阅读,分别是 自述文件,安装说明,授权协议。通常linux/unix世界的源码包里都有这几个文件(有时作者会把INSTALL并成到README里),建议阅读一下,尤其是README与INSTALL。

执行第一步 configure

[feng@fsvps pcre-8.21]$ ./configure

注意configure命令前面要带上 ./ ,因为我们要执行的是在当前目录下的configure文件,。

configure过程中可能出现的几个报错,及原因:

  • 1) ./configure: error: C compiler gcc is not found 原因:你没有安装gcc ,这样可能你也没安装下面几个包,请一并安装
    yum install gcc gcc-c++ autoconf make
  • 2) Makefile: 权限不够 原因:当前用户没有权限读写nginx源码目录,请切换到root下运行如下命令,作用是将当前目录的所有文件所有者都设为我们正在使用的普通用户。
    [root@fsvps nginx-1.2.1]# chown -R feng:feng  ./
    [root@fsvps nginx-1.2.1]# exit

    然后exit退出到普通用户状态下。 chown 后的 feng:feng 分别是所使用的普通账号的用户名,及其用户组名。

开始编译,这步非常简单,运行make就可以,可能要花费几分钟时间,你可以干点其它,比如起身活动一下。

[feng@fsvps pcre-8.21]$ make

完了如果看到下面的消息:make[1]: Leaving directory `/home/feng/build/pcre/pcre-8.21‘
说明编译完成了,那么开始安装。安装很简单,使用root用户权限,运行make install 就可以了。所以su,按提示输入root密码,切换到root身份

[feng@fsvps pcre-8.21]$ su
[feng@fsvps pcre-8.21]# make install

完了消息应该是 make[1]: Leaving directory `/home/feng/build/pcre/pcre-8.21‘

这样pcre就安装好了。然后exit退回到普通用户状态下。

编译安装nginx

接下来才是真正安装nginx,过程差不多,但我们要多配置几个参数选项。

到 nginx官网上 http://nginx.org/en/download.html 找最新的稳定版的下载链接,写本文时2012-06-30 最新版本为 nginx-1.2.1  下载链接为 http://nginx.org/download/nginx-1.2.1.tar.gz 使用wget 将其下载到vps上。

[root@fsvps build]$ wget http://nginx.org/download/nginx-1.2.1.tar.gz

几秒钟就可以下载完成。(如果提示 bash: wget: command not found 那是你还没有安装wget,切换到root用户,yum install wget -y 安装之)

解压缩,然后把压缩包移动到source目录,以备不时之需。

[root@fsvps build]$ ll
总用量 708
-rw-r--r-- 1 root root 718161  6月  5 14:10 nginx-1.2.1.tar.gz
[root@fsvps build]$ tar xf nginx-1.2.1.tar.gz 
[root@fsvps build]$ ll
总用量 712
drwxr-xr-x 8 1001 1001   4096  6月  5 14:02 nginx-1.2.1
-rw-r--r-- 1 root root 718161  6月  5 14:10 nginx-1.2.1.tar.gz
[root@fsvps build]$ mv nginx-1.2.1.tar.gz ../source/

[tips] 文件名很长,打字太累? 试试tab键,比如先打出 mv ngi 然后按tab 键一次,没反应,再按一次,看有什么变化;然后继续打一个句点,再按一次tab键。相信你已经能悟出点什么了。

进入nginx源码目录,浏览一下里面的文件

[root@fsvps build]$ cd nginx-1.2.1/
[root@fsvps nginx-1.2.1]$ ll
总用量 560
drwxr-xr-x 6 1001 1001   4096  6月 30 11:39 auto
-rw-r--r-- 1 1001 1001 207988  6月  5 14:02 CHANGES
-rw-r--r-- 1 1001 1001 317085  6月  5 14:02 CHANGES.ru
drwxr-xr-x 2 1001 1001   4096  6月 30 11:39 conf
-rwxr-xr-x 1 1001 1001   2345  1月 18 15:07 configure
drwxr-xr-x 3 1001 1001   4096  6月 30 11:39 contrib
drwxr-xr-x 2 1001 1001   4096  6月 30 11:39 html
-rw-r--r-- 1 1001 1001   1365  1月 18 15:07 LICENSE
drwxr-xr-x 2 1001 1001   4096  6月 30 11:39 man
-rw-r--r-- 1 1001 1001     49 10月 31 2011 README
drwxr-xr-x 8 1001 1001   4096  6月 30 11:39 src

运行configure,就是本目录下的该文件,上面标红加;目录是配置nginx的编译环境,运行如下命令,参数比较长,可以直接复制帖。

[root@fsvps nginx-1.2.1]$ ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/sbin/nginx --conf-path=/usr/local/conf/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_stub_status_module --with-http_gzip_static_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_random_index_module --with-cpu-opt=pentium4

注意configure命令要带上./ ,上面已经标红显示。大致讲一下是什么意思 [注]如果你仅仅想编译一个web环境用,而不想学习编译linux程序的一般方法,这部分那可以跳过不用看。

把参数重排下版,一行一个参数,简单注释一下

--prefix=/usr/local/nginx    #编译后安装到目录 /usr/local/nginx 
--sbin-path=/usr/local/sbin/nginx #
--conf-path=/usr/local/conf/nginx/nginx.conf #主配置文件路径
--error-log-path=/var/log/nginx/error.log  #错误日志目录
--pid-path=/var/run/nginx.pid    #nginx程序启用后,自动将其进程号写到该文件
--lock-path=/var/run/nginx.lock  #lock文件路径
--with-http_stub_status_module   #通过web查看nginx运行状态
--with-http_gzip_static_module   #gzip压缩支持
--with-http_sub_module     #加入ngx_http_sub_module模块
--with-http_flv_module     #加入flv模块
--with-http_mp4_module     #mp4模块
--with-http_random_index_module   #http_random_index_module模块
--with-cpu-opt=pentium4    #针对intel cpu优化

--with-xxxx_module的那些模块,如果你确定

不需要,那就可以不要加入。那就这样指定参数

./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/sbin/nginx --conf-path=/usr/local/conf/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock  --with-http_stub_status_module --with-http_gzip_static_module

只带两个比较有用的模块。事实上这些参数一个都不指定,也是可以,这样就是按nginx的默认配置编译了。如果你想查看ngix的configure都有哪些参数选项可以运行 ./configure --help 查看,这里不多讲了。

如果报错:./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

这是因为没有找到PCRE包,请检查是否正确安装了PCRE包:

不出意外,应该能看到这样报告消息:

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + md5: using system crypto library
  + sha1: using system crypto library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/sbin/nginx"
  nginx configuration prefix: "/usr/local/conf/nginx"
  nginx configuration file: "/usr/local/conf/nginx/nginx.conf"
  nginx pid file: "/var/run/nginx.pid"
  nginx error log file: "/var/log/nginx/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"

有些软件的configure完成后会给出类似的一个配置报告,供我们检查配置选项是否正确。如果有不正确处,或还想改改,那就再运行一次configure --xx --yyy.

确认无误,开始编译

[feng@fsvps nginx-1.2.1]$ make

开始编译,非常快,可能一分钟就编译完成,最后消息大致如:make[1]: Leaving directory `/home/feng/build/nginx-1.2.1‘

接下来,你应该知道了,切换到root身份,make install 安装。

nginx运行测试

下面的操作要在root身份下操作。

我们测试一下nginx是否正常工作。因为我们把nginx的二进制执行文件配置安装在 /usr/local/sbin/nginx  (前面的configure参数 --sbin-path=/usr/local/sbin/nginx ),目录/usr/local/sbin/默认在linux的PATH环境变量里,所以我们可以省略路径执行它:

[root@fsvps nginx-1.2.1]# nginx

它不会有任何信息显示。打开浏览器,通过IP地址访问你的nginx站点,如果看到这样一行大黑字,就证明nginx已经工作了:

Welcome to nginx!

 

看上去很简陋,只是因为没有给它做漂亮的页面。我们来简单配置一下它的运行配置。我们configure的参数--conf-path=/usr/local/conf/nginx/nginx.conf 这就是它的配置文件。我们去看看。

[root@fsvps nginx-1.2.1]# cd /usr/local/conf/nginx/
[root@fsvps nginx]# ls
fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_params
fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
fastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf

如果这里没有nginx.conf,但有个 nginx.conf.default,我们用它复制个副本nginx.conf (早期版本默认是没有nginx.conf的)

用你熟悉的编辑器打开它,推荐使用vim;如果不会用,那可以使用nano, 功能简单,但一看就会用:nano nginx.conf

找到如下的部分,大概在35行,

server {
        listen       80 default;
        server_name  localhost;
        root /var/www/html/default;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
        #    root   html;
            index  index.html index.htm;
        }
................
}

如上红色加粗显示部分,做三处修改:

  • listen 80 后插入“空格default”
  • 在里面加入一行 root /var/www/html/default;
  • 注释掉root html;一行

上面的server{....}是定义的一台虚拟主机,我们刚加入的一行是定义这个虚假主机的web目录。listen 行的default是表示这一个server{...}节点是默认的虚假主机(默认站点)

执行 nginx -t 测试刚才的nginx配置文件是否有语法错误:

[root@fsvps nginx]# nginx -t
nginx: the configuration file /usr/local/conf/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/conf/nginx/nginx.conf test is successful

显示没有错误,检测通过。如果不是这样,请返回仔细检查你的配置文件是否哪里写错了,注意行尾要有英文分号。

我们在硬盘上创建这个目录:

[root@fsvps nginx]# mkdir -p /var/www/html/default

写一个html文件index.html 到/var/www/html/default目录里,使用你熟悉的编辑器,随便写什么内容、怎么排版。

然后让nginx重新加载配置文件,看看刚才创作的html页面效果如何。

常见错误FAQ

1) 如果通常访问时看到还是 Welcome to nginx! ,请返回检查,重点在这几处:

  • 请确认/var/www/html/default 目录下有你创作的index.html 文件?
  • 检查该文件权限,other用户是否有读的权限? 如果不懂linux文件权限,请执行 chmod 755 /var/www/html/default/index.html
  • 检查nginx.conf配置文件里,只否只有一个server{...} 节点,并且该节点里是否有 listen       80default;   一行,注意其中要有 default 
  • 检查上述server{...}节点里是否有 root /var/www/html/default; 一行,注意路径是拼写是否正确。
  • 检查其中 location / {...} 节点里的 #    root   html;  一行,是否注释掉了。

2) 如果看到的是 404 Not Found 或者“找不到该页面”类似的提示:

  • 检查上述 location / {...} 节点中是否有这一行 index  index.html index.htm;

3) 如果访问时,显示“找不到服务器”、“无法连接到服务器”这样的错误:

  • 运行检查nginx进程在运行,运行ps aux |grep nginx 命令,正常情况有如下三条:
    nginx: master process nginx
    nginx: worker process
    grep nginx
    如果只有第三条,请运行nginx 重新启用nginx,如有报错请照说明检查。一般是配置文件的语法错误。
  • 请运行nginx -t 检查配置文件是否有语法错误。

[tips] location / {...} 节点里的 #    root   html;  一行,不注释掉也可以:请把你创造的index.html 放到/var/www/html/default/html目录下。

至此,我们的nginx也可以正常工作了。

在centos下编译安装配置高性能Nginx

标签:http   io   ar   os   使用   sp   strong   on   文件   

原文地址:http://www.cnblogs.com/studies/p/4161018.html

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