一、nginx的一些模块需要第三方库的支持,例如gzip模块需要zlib库,rewrite模块需要pcre库,ssl功能需要openssl模块。所以安装nginx前应安装这些。

安装zlib:sudo apt-get install zlib1g-dev

安装pcre:sudo apt-get update 
                 sudo apt-get install libpcre3 libpcre3-dev 

安装ssl:sudo apt-get install openssl libssl-dev

二、安装nginx,最好到nginx官网http://nginx.org/上下载稳定版本,第一次安装了个最新版nginx,结果设置负载均衡总是不成功,最后下了一个nginx-1.4.4.tar.gz就成功了。

1、nginx一般默认安装在/usr/local/nginx下,如果需要设置,可设置--prefix=<path>---nginx的安装路径。一般默认即可。

2、把nginx安装包放到/usr/local下,然后tar -zxvf nginx-1.4.4.tar.gz解压缩,然后cd到nginx的目录下。

3、./configure,然后观看configure的结果,如果没有显示出少一些第三方库,比如pcre,那么证明可以接下去make

4、make

5、make install

三、安装完nginx后,如果需要设置负载均衡,那么把/usr/local/nginx/conf/nginx.conf的配置文件改为如下即可。

#user www www;

#user  nobody;
worker_processes  1;

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

pid        /usr/local/nginx/logs/nginx.pid;

#制定文件描述符数量
worker_rlimit_nofile 51200;

events {
    use 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  on;

    upstream php_server_pool {
    server 192.168.1.111:80 weight=2 ;
        server 192.168.1.102:80 weight=2 ;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        #access_log  logs/host.access.log  main;

    location / {
        proxy_next_upstream http_502 http_504 error timeout invalid_header;
        proxy_pass         http://php_server_pool;
        proxy_set_header    Host    localhost;
        proxy_set_header    X-Forwarded-For    $remote_addr;
    }

    }

}
View Code

四、修改nginx的配置文件后,必须平滑地让nginx重新运行nginx.conf

①、修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
/usr/local/nginx/sbin/nginx -t
如果屏幕显示以下两行信息,说明配置文件正确:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully

②、这时,输入以下命令查看Nginx主进程号:
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
屏幕显示的即为Nginx主进程号,例如:
6302
这时,执行以下命令即可使修改过的Nginx配置文件生效:
kill -HUP 6302

ps:这时候访问nginx的ip,就可以访问到负载均衡的web服务器池中的服务器了。

posted on 2013-12-09 18:34  cchun  阅读(175)  评论(0)    收藏  举报