Centos 7.x 源码搭建Nginx

环境:

  centos 7

  防火墙关闭

  Selinx关闭

Nginx Web安装
  安装依赖库
    yum install pcre-devel pcre gcc gcc-c++ zlib zlib-devel openssl openssl-devel -y (pcre库主要用于nginx正则表达)
  下载Nginx源码包
    cd /softwares
    wget -c http://nginx.org/download/nginx-1.4.2.tar.gz
    tar -xvzf nginx-1.4.2.tar.gz
  预编译、编译Nginx
    cd nginx-1.4.2
    ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
    make && make install
  检查nginx是否安装正确
    /usr/local/nginx/sbin/nginx  -t    # 返回OK即为安装成功

nginx.conf配置文件详解 (cd /usr/local/nginx/conf) 

#user  nobody;      # Nginx用户及组
worker_processes  1;        # 工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU

#error_log  logs/error.log;         # 错误日志:存放路径
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;         # pid(进程标识符):存放路径


events {
    worker_connections  1024;       
        # 每个工作进程的最大连接数量。根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。
        # 每个进程允许的最多连接数,nginx最大连接数=worker连接数*worker进程数
}

#设定http服务器
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;
        # 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,
        # 如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。
        # 注意:如果图片显示不正常把这个改成off
    #tcp_nopush     on;     # 激活参数可把httpresponse header和文件的开始放在一个文件里发布,减少网络报文段数量,防止网络阻塞

    #keepalive_timeout  0;      
        # 连接超时时间,单位是秒。
        # 客户端到服务器端的连接持续有效时间,当出现对服务器的后继请求时,keepalive-timeout功能可避免建立或重新建立连接
    keepalive_timeout  65;

    #gzip  on;          # 开启gzip压缩功能

    server {
        listen       80;        # 监听端口
        server_name  localhost;         # 域名可以有多个,用空格隔开

        #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;
    #    server_name  localhost;

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

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

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

}

  编辑nginx.conf配置文件
    cd /usr/local/nginx/
    vim 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 {
    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;

    server {
        listen       80;
        server_name  www.myb.com  localhost;

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

  启动nginx
  检查是否有80端口被占用(如若有80端口被启用,首先关掉80端口的相应服务,一般是apach服务)
  netstat -tnl
  netstat -tnl | grep 80    # 没有结果显示,便为正常
  /usr/local/nginx/sbin/nginx    # 启动nginx,此处直接回车即可,没有start
  ps -ef | grep nginx

浏览器输入nginx服务器地址:192.168.168.6

Nginx 虚拟主机配置

 修改nginx.conf配置文件的server段内容如下:

#virtual hosts config 2014/5/18
server {
        listen       80;
        server_name  www.a.com;

        #access_log  logs/host.access.log  main;

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

server {
        listen       80;
        server_name  www.b.com;

        #access_log  logs/host.access.log  main;

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

  mkdir –p /usr/local/nginx/html/{a,b}
  cd /usr/local/nginx/html/a
  vim index.html # 在目录a和b中分别写入不同的index.html

  /usr/local/nginx/sbin/nginx -t
  /usr/local/nginx/sbin/nginx -s reload # 平滑重启

在windows的hosts文件中添加虚拟主机域名
在浏览器中访问虚拟主机

升级Nginx版本

  cd /softwares

  wget http://www.nginx.org/download/nginx-1.6.1.tar.gz

  /usr/local/nginx/sbin/nginx -V # 查看升级之前旧版本的configure选项

  编译新版本的Nginx

    tar zxvf nginx-1.6.1.tar.gz
    cd nginx-1.6.1
    ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
    make
    mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old # 备份旧版本的nginx的执行程序
    cp /usr/src/nginx-1.6.1/objs/nginx /usr/local/nginx/sbin/ # 替换旧的Nginx的执行程序
    kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
      # 使nginx的旧版本停止接收请求,由Nginx新版本接替,且老进程处理完所有请求,关闭所有连接后停止服务
    ls /usr/local/nginx/logs/ # 查看nginx日志目录会生成一个nginx.pid.oldbin文件,存放旧版本nginx 的pid号
    /usr/local/nginx/sbin/nginx -t # 检查新版本Nginx是否正常
    /usr/local/nginx/sbin/nginx -s reload # 平滑重启
    /usr/local/nginx/sbin/nginx -v # 查看升级升级后的版本
    netstat -aupt | grep nginx # 查看服务运行状态

reboot操作系统后,重启nginx服务报错:

报错处理:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

猜你喜欢

转载自www.cnblogs.com/muyaobin/p/10348898.html
今日推荐