linux环境手动编译安装Nginx实践过程 附异常解决

为什么选择Nginx

Nginx 是一个很牛的高性能Web和反向代理服务器, 它具有有很多非常优越的特性:

      • 在高连接并发的情况下,Nginx是Apache服务器不错的替代品:
        Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一. 能够支持高达 50,000 个并发连接数的响应, 感谢Nginx为我们选择了 epoll and kqueue 作为开发模型.
      • Nginx作为负载均衡服务器:
        Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务, 也可以支持作为 HTTP代理 服务器对外进行服务. Nginx采用C进行编写, 不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多.
      • 作为邮件代理服务器:
        Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器), Last.fm 描述了成功并且美妙的使用经验.
      • Nginx 是一个 [#installation 安装] 非常的简单 , 配置文件 非常简洁(还能够支持perl语法), Bugs 非常少的服务器:
        Nginx 启动特别容易, 并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动. 你还能够 不间断服务的情况下进行软件版本的升级

实践安装过程

1、下载nginx源码包并解压

     可在http://nginx.org/en/download.html下载.tar.gz的源码包,如(nginx-1.4.7.tar.gz)

或者使用云盘下载   http://url.cn/5kRqr3n   (密码:f72dcD)

     下载后通过tar -xvzf 进行解压,解压后的nginx目录结构如下:

        

2、为nginx设置安装目录和启用的模块

     切换到解压后的nginx目录中执行:

      ./configure --prefix=/opt/demo/nginx --add-module=/home/fastdfs-nginx-module/src  --with-http_stub_status_module --with-http_ssl_module

      参数说明:

        --prefix 用于指定nginx编译后的安装目录

        --add-module 为添加的第三方模块,此次添加了fdfs的nginx模块

        --with..._module 表示启用的nginx模块,如此处启用了http_ssl_module模块

安装说明:

--add-module=/home/fastdfs-nginx-module/src 指定安装fastdfs文件服务器模块,所以安装先需先安装好fastdfs并指向对应的目录,否则会提示fastdfs安装目录不存在。也可以不安装fastdfs模块,只需要删除这块命令 --prefix=/opt/demo/nginx --add-module=/home/fastdfs-nginx-module/src

--with-http_stub_status_module --with-http_ssl_module 需要启动https模块,所以安装前需要检查是否已经安装好ssl模块,如果尚未安装请执行以下命令:yum install mod_ssl

安装完mod_ssl会创建一个默认的SSL证书,路径位于/etc/pki/tls,安装完成不代表已经配置并生成对应的key,只是表示本地环境支持ssl了.但是这里的安装Nginx命令只需要配置好ssl即可。也可以不安装这个模块,即删除   --with-http_stub_status_module --with-http_ssl_module

 

      可能出现的错误:

        出现:./configure:  error: the HTTP rewrite module requires the PCRE library.

          解决方法:安装pcre-devel解决问题 yum -y install pcre-devel

        出现:SSL modules require the OpenSSL library

          解决方法:yum install openssl-devel 

                            出现:./configure: error: the HTTP cache module requires md5 functions

from OpenSSL library.   You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.

      解决方法:yum  -y install openssl openssl-devel

当无异常并能正常解压后的正常反馈是

Configuration summary  
  + using system PCRE library  
  + using system OpenSSL library  
  + md5: using OpenSSL library  
  + sha1: using OpenSSL library  
  + using system zlib library  
  + jemalloc library is disabled  
  
  nginx path prefix: "/usr/local/nginx"  
  nginx binary file: "/usr/local/nginx/sbin/nginx"  
  nginx configuration prefix: "/usr/local/nginx/conf"  
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"  
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"  
  nginx error log file: "/usr/local/nginx/logs/error.log"  
  nginx http access log file: "/usr/local/nginx/logs/access.log"  
  nginx http client request body temporary files: "client_body_temp"  
  nginx dso module path: "/usr/local/nginx/modules/"  
  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"  

3、编译

    执行make 进行编译,如果编译成功的话会在第一步中objs中出现一个nginx文件

      特别注意:

        在已安装的nginx上进行添加模块的话执行到这里就行了,把objs中的nginx替换掉之前的安装的nginx/sbin/中的nginx文件,然后重启nginx就行了,如果执行下一步的install,会导致之前安装的nginx被覆盖,比如之前配置好的nginx.conf文件)

 

4、安装

    执行make install 进行安装,安装后--prefix 中指定的安装目录下回出现如下目录结构

 

5、启动nginx

    切入到第四步中的sbin目录或是创建一个nginx软链接

        ln -s /opt/demo/nginx/sbin/nginx /usr/bin/nginx

      完成后执行:

        nginx start(如需开机自启,可在/etc/rc.d/rc.local 文件中添此命令)

      如出现:nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"

      则需通过nginx –c ../conf/nginx.conf    命令指定nginx的配置

    nginx的一些常用管理命令

      重启:nginx -s reload

      停止:nginx -s stop或者是通过kill nginx进程号

                  检测配置文件是否合法:nginx -t

      查看版本:nginx –V

关于nginx.conf配置文件

    在安装完nginx后会在conf目录中产生一个nginx.conf的配置文件

    里面有些默认配置,可根据自己的需求进行更改

        示例1:

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
          proxy_pass http://www.baidu.com; #当Nginx监控到80端口有请求过来时进行调整到百度首页去
        }

        #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;
        }

    示例2:

#user  nobody;
worker_processes  1;
events {
     use epoll;
        worker_connections  51200;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    proxy_connect_timeout 600s;
    proxy_read_timeout 600s;
    proxy_send_timeout 600s;
    proxy_buffer_size 64k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    proxy_ignore_client_abort on;
    client_max_body_size 200m;  #此参数在使用fdfs上传可控制上传文件的大小
   #日志的输出格式,如需打印请求的body参数信息,可在$body_bytes_sent后添加 $request_body 
   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; #设置日志输出的位置
  access_log on;  #是否开启日志,开启on,关闭off
  #负载均衡配置
  upstream test.com {
    ip_hash;
    server 192.168.68.9:8080;
    server 192.168.68.72:8080;
   }
    server {
        listen       80; #监听的端口,http默认监听端口为80
        server_name  localhost; #监听的主机名
        location / {
        #设置请求的头部中主机名为请求的主机名,而不是代理的nginx的主机名
        proxy_set_header Host $host:$server_port;
        #代理的目标地址,如果要进行负载均衡,目标地址可添test.com
         proxy_pass http://192.168.68.9:8080;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
   #https配置,https默认监听端口为443
    server {
        listen  443;
        server_name system.test.com;
        ssl on;
        ssl_certificate_key cert/system.key; #ssl key文件的位置,此处使用配置文件的相对路径
        ssl_certificate cert/system.pem; #证书文件,此处为阿里云云盾证书生成的.pem也可修改扩展名为熟悉的.crt
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        location / {
             proxy_pass http://192.168.68.9:8080;
        }
    }
    #以下为我的fdfs文件配置,没有使用fdfs可以不用配置
    server {
        listen       9300;        
        server_name  localhost;   
    #location /group1/M01 {
         #   root   /home/fdfs/storage2/data;
          #  ngx_fastdfs_module;         
        #}
        
        location /group1/M00 {
           root   /home/fdfs/storage1/data;
           ngx_fastdfs_module;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
#include vhost/*.conf;
}

关于https配置ssl

    如果在安装nginx的时候没有安装 --with-http_ssl_module模块要先安装该模块

    nginx –V 可查看已经安装的模块

    如果没有安装,只需执行以上步骤中的2、3步进行手动添加ssl模块

    添加一个https的server大概如下面这个样子

server {
         listen 443;   ##默认的监听端口为443
         server_name  localhost;
         ssl on;
         ssl_certificate_key  xxx.key; ##私钥
         ssl_certificate  xxx.crt; ##证书,证书中包含公钥和私钥加密后的签名信息
 
        location / {
            root   html;
            index  index.html index.htm;
             proxy_pass http://xxx.xxx.xxx.xxx:xxx;
        }
         
    }

私钥和公钥为非对称加密方式加密内容,即公钥加密后的内容只有私钥可解,私钥加密后的内容只有公钥可解;

  大概原理:

    证书中包含公钥和用私钥加密后的签名信息,浏览器请求发出tcp三次握手成功后服务器会将该证书发送给浏览器,浏览器通过证书中的公钥解密私钥加密后的签名,再通过解密后的签名来匹配浏览器中内置的权威签名证书来判断该签名是否权威,不是权威签名会中断访问,并显示警告提示;如果判断为权威机构的签名后会产生一个随机字符串并用证书中的公钥加密发送给服务器端,服务器再通过自己的私钥解密那个随机字符串,将这个字符串作为加密的密码来进行对称加密之后与浏览器交互的数据;

 

原文地址:https://www.cnblogs.com/david97/p/7799121.html