CentOS6.5 64位 nginx安装与配置

http://jingyan.baidu.com/article/219f4bf7ec67fbde442d38b8.html


linux下nginx的安装与配置过程.

百度经验:jingyan.baidu.com

工具/原料

  • GCC,PCRE,zlib,OpenSSL等安装包
  • nginx-1.6.2.tar.gz openssl-1.0.1c.tar.gz pcre-8.35.tar.gz zlib-1.2.8.tar.gz
百度经验:jingyan.baidu.com

步骤1 安装编译工具及库文件

  1. 1

    1)需要安装软件:GCC,PCRE,zlib,OpenSSL。

         Nginx是C写的,需要用GCC编译;Nginx的Rewrite和HTTP模块会用到PCRE;Nginx中的Gzip用到zlib;

  2. 2

    2)用命令“# gcc”,查看gcc是否安装;如果出现“gcc: no input files”信息,说明已经安装好了。命令:[[email protected] soft]# gcc

    gcc: fatal error: no input files

    否则,执行命令:[[email protected] soft]# yum install gcc”,一直按y确认即可.

  3. 3

    3)gcc测试:[[email protected] soft]# gcc -v

    Using built-in specs.

    COLLECT_GCC=gcc

    COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.2/lto-wrapper

    Target: x86_64-redhat-linux

    Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info 

    --with-bugurl=http://bugzilla.redhat.com/bugzilla 

    --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release 

    --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions 

    --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu 

    --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array 

    --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-x86_64-redhat-linux/isl-install 

    --with-cloog=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-x86_64-redhat-linux/cloog-install 

    --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux

    Thread model: posix

    gcc version 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC) 

  4. 4

    4)同样方法,用如下命令安装PCRE,zlib,OpenSSL;

    注:安装包下安装,即执行命令如下

    tar -zxvf  zlib-1.2.8.tar.gz

    cd zlib-1.2.8

    ./configure

    make 

    make install

    tar -zxvf openssl-1.0.1c.tar.gz

    cd openssl-1.0.1c

    ./configure

    make 

    make install

    END
百度经验:jingyan.baidu.com

步骤2 下载PCRE安装包,下载地址: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

  1. 1

    注:PCRE 作用是让 Ngnix 支持 Rewrite 功能。

    下载并解压 PCRE 安装包;

    [[email protected] soft]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

    [[email protected] soft]# chmod 777 pcre-8.35.tar.gz 

    [[email protected] soft]# tar -zxvf pcre-8.35.tar.gz

  2. 2

    进入安装目录,编译并安装;

    [[email protected] soft]# pwd

    /home/sonar/soft

    [[email protected] soft]# cd pcre-8.35/

    [[email protected] pcre-8.35]# ./configure 

    [[email protected] pcre-8.35]# make && make install

  3. 3

    查看pcre版本;

    [[email protected] pcre-8.35]# pcre-config --version

    8.35

    说明:安装成功!

    END
百度经验:jingyan.baidu.com

步骤3 安装 Nginx

  1. 1

    1)下载 Nginx,下载地址:http://nginx.org/download/nginx-1.6.2.tar.gz;

    [[email protected] soft]# pwd

    /home/sonar/soft

    [[email protected] soft]# wget http://nginx.org/download/nginx-1.6.2.tar.gz

  2. 2

    2)解压安装包;

    [[email protected] soft]# chmod 777 nginx-1.6.2.tar.gz

    [[email protected] soft]# tar -zxvf nginx-1.6.2.tar.gz

  3. 3

    3)进入安装目录,编译并安装;

    [[email protected] soft]# cd nginx-1.6.2

    [[email protected] nginx-1.6.2]# ./configure --prefix=/home/sonar/soft/nginx --with-pcre=/home/sonar/soft/pcre-8.35 --with-http_stub_status_module --with-debug

    [[email protected] nginx-1.6.2]# make 

    [[email protected] nginx-1.6.2]# make install

  4. 4

    4)查看版本;

    [[email protected] nginx-1.6.2]# nginx -v

    END
百度经验:jingyan.baidu.com

步骤4 Nginx 配置

  1. 1)创建 Nginx 运行使用的用户 nginx:

    [[email protected] conf]# /usr/sbin/groupadd nginx

    [[email protected] conf]# /usr/sbin/useradd -g nginx nginx

  2. 2)配置nginx.conf ,将/home/sonar/soft/nginx/conf/nginx.conf替换为以下内容:

    user  root;

    worker_processes 2; #设置值和CPU核心数一致

    error_log /home/sonar/soft/nginx/logs/nginx_error.log crit; #日志位置和日志级别

    pid /home/sonar/soft/nginx/nginx.pid;

    #Specifies the value for maximum file descriptors that can be opened by this process.

    worker_rlimit_nofile 65535;

    events

    {

      use epoll;

      worker_connections 65535;

    }

    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';

      

    #charset gb2312;

         

      server_names_hash_bucket_size 128;

      client_header_buffer_size 32k;

      large_client_header_buffers 4 32k;

      client_max_body_size 8m;

         

      sendfile on;

      tcp_nopush on;

      keepalive_timeout 60;

      tcp_nodelay on;

      fastcgi_connect_timeout 300;

      fastcgi_send_timeout 300;

      fastcgi_read_timeout 300;

      fastcgi_buffer_size 64k;

      fastcgi_buffers 4 64k;

      fastcgi_busy_buffers_size 128k;

      fastcgi_temp_file_write_size 128k;

      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;

     

      #limit_zone crawler $binary_remote_addr 10m;

     #下面是server虚拟主机的配置

     server

      {

        listen 8080;#监听端口

        server_name localhost;#域名

        index index.html index.htm index.php;

        root /home/sonar/soft/nginx/html;#站点目录

          location ~ .*\.(php|php5)?$

        {

          #fastcgi_pass unix:/tmp/php-cgi.sock;

          fastcgi_pass 127.0.0.1:9000;

          fastcgi_index index.php;

          include fastcgi.conf;

        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$

        {

          expires 30d;

      # access_log off;

        }

        location ~ .*\.(js|css)?$

        {

          expires 15d;

       # access_log off;

        }

        access_log off;

      }

    }

  3. 3)检查配置文件ngnix.conf的正确性命令;

    [[email protected] sbin]# pwd

    /home/sonar/soft/nginx/sbin

    [[email protected] sbin]# ./nginx -t

    nginx: the configuration file /home/sonar/soft/nginx/conf/nginx.conf syntax is ok

    nginx: configuration file /home/sonar/soft/nginx/conf/nginx.conf test is successful

  4. 4)nginx启动命令:

    [[email protected] sbin]# pwd

    /home/sonar/soft/nginx/sbin

    [[email protected] sbin]# ./nginx 

       见图;

  5. 5)访问:http://localhost;

       如图;

    Nginx 其他命令:

    /home/sonar/soft/nginx/sbin/nginx -s reload            # 重新载入配置文件

    /home/sonar/soft/nginx/sbin/nginx -s reopen            # 重启 Nginx

    /home/sonar/soft/nginx/sbin/nginx -s stop              # 停止 Nginx

    END
百度经验:jingyan.baidu.com

注意事项

  • 1)远程执行wget命令后,下载的文件需要执行有+X权限;
  • 2)注意查看防火墙,是否启用nginx配置的端口;
  • 3)如果出现403 forbidden,则说是权限异常,在nginx.conf中,指定下user ,如上步骤中user root;