Linux(CentOS)下编译安装Nginx及配置多网站

下编译安装Nginx,比起其他程序来说相对简单些。以下是个人编译的一些记录。

Nginx,Mitchell添加了pcre和zlib两个参数,因此需要到官方网站上下载可用版本的源代码,由Nginx编译的时候,一并完成编译。

注意:要让配置文件中的location支持正则表达式或者要启用ngx_http_rewrite_module的时候,就需要使用PCRE。而要让Nginx能够启用gzip选项,就必须添加zlib。版本选择Nginx支持的最新版本即可。

$ ./configure \
   --prefix=/usr/local/nginx-1.6.0 \
   --pid-path=/usr/local/nginx-1.6.0/run \
   --user=nginx \
   --group=nginx \
   --with-http_ssl_module \
   --with-pcre=../pcre/pcre-8.3.2 \
   --with-zlib=../zlib/zlib-1.2.7 &&
make

在configure的参数中我们依次定义了安装路径,pid路径,运行的用户名,用户组名称,启用SSL,使用PCRE,Zlib。

如果./configure没有问题,那么接下来系统就会直接make。如果系统make之后也没有错误,就可以切换到root帐户下,将编译好的Nginx安装。

## 注意:安装必须要切到root帐户下面.
# make install

安装完毕之后,由于,这让我们可以直接先启动Nginx,确认Nginx已经成功安装。如果能够正常启动,我们就可以通过宿主的80端口(HTTP)访问到Nginx的欢迎页面了。

## 启动Nginx
# /usr/local/nginx-1.6.0/sbin/nginx

正常的情况下,此时Nginx是处于工作状态,并且界面没有任何回显。

需要注意:

1. 如果你没有创建./configure参数中的用户组和用户,是无法正常启动的;

2. 请在root下启动Nginx

3. Nginx宿主机的80端口需要确保没有被其他程序占用(如:Apache)

确认能正常启动后,我们可以在/usr/local/bin下创建一个软链接到nginx执行文件上,这样就不用每次都输入一长串的路径了。

# ln -fs /usr/local/bin/nginx /usr/local/nginx-1.6.0/sbin/nginx

创建Nginx软链完成后,我们尝试下关闭Nginx:

#nginx -s quit

Nginx配合-s参数,可以有如下指令:

stop —— 快速关闭;

quit —— 退出(关闭),这个会等待Nginx处理完当前请求;

reload —— 重新加载配置文件;

reopen —— 重新打开日志文件。

在Nginx成功编译安装之后,我们更进一步的可以配置下Nginx的配置文件,默认的配置文件在安装目录下的conf/nginx.conf(添加中文注释):

#user  nobody;
#设置工作进程数
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    #并发连接数,可以启用epoll
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    #开启gzip,如果编译安装的时候没有zlib,此项注释掉
    gzip  on;
    #网站配置
    server {
        # 监听端口,HTTP 80端口
        listen       80;
        # 主机名,有域名,绑定就可以了,多个用空格分隔
        server_name  localhost blog.useasp.net;
        #编码
        #charset koi8-r;
        #访问日志
        #access_log  logs/host.access.log  main;
        #网站位置配置
        location / {
            #根目录
            root   html;
            #默认页
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #错误页定向
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

 如果是单个网站,直接修改server里面的配置就足以完成需要配置。如果涉及到的情况下,直接修改conf文件,在网站数量一多的情况下,可维护和可配置性就很差(上面默认的配置文档中,Nginx配置了三个网站,其中注释掉有两个,但复杂性已经显现)。为了更好的完成多网站的配置,我们可以将每一个网站的配置独立出来,然后以独立文件的形式统一放到一个目录下,在Nginx的配置文件nginx.conf中,只需要加载该目录下的所有配置文件即可。

首先修改Nginx的默认配置文档nginx.conf,将所有的网站配置节提取出来:

worker_processes  1;
events {
    worker_connections  1024;
}


http {
    # 这里我们只存放Nginx全局的配置项
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    gzip_proxied expired no-cache no-store private auth;
    gzip_disable "MSIE [1-6]\.";

    server {
          listen         80;
          server_name     localhost;
          access_log      logs/access.log;
          server_name_in_redirect  off;
          location / {
                root  html;
                index index.html;
          }
    }

    include vhosts/*;
}

 这里只保留了一个本地的测试用例,其余的我们放在同目录下的vhosts下面,通过Nginx将vhosts下的文件全部加载进来作为配置。比如我们在vhosts下创建blog.useasp.net文件,里面保存blog.useasp.net网站的配置:

 server {
        listen       80;
        server_name  blog.useasp.net;

        charset utf-8;
        access_log  logs/blog.useasp.net.access.log;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

 这样配置后,访问blog.useasp.net的效果和直接在nginx.conf配置的效果一样,但却更容易维护,如果有更多的网站,只需要在vhosts文件夹下添加对应的配置文件即可,之后使用nginx -s reload重新加载配置文件。

Thursday, May 01, 2014 | Unix & Linux

文章评论

No comments posted yet.

发表评论

Please add 6 and 8 and type the answer here:

关于博主

  一枚成分复杂的网络IT分子,属于互联网行业分类中的杂牌军。