• 编译安装Nginx


    Linux最小化安装后,还需要进行安装的软件

    yum groupinstall "Compatibility libraries" "Base" "Development tools"
    yum groupinstall "debugging Tools" "Dial-up Networking Support"

    yum install tree telnet dos2unix sysstat lrzsz nc nmap -y

    yum grouplist(查看所有包组名称,安装的和未安装的)
    yum groupinstall "Development Tools"

    关闭防火墙:

    setenforce 0






    1.安装所需的pcre、pcre-devel、openssl、openssl-devel库

    cat /etc/redhat-release
    uname -r

    yum install pcre pcre-devel -y
    rpm -qa pcre pcre-devel

    yum install openssl openssl-devel -y
    rpm -qa openssl openssl-devel

    yum install gcc-c++ -y

    mkdir -p /home/hty/tools
    cd /home/hty/tools/
    wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
    ls -l nginx-1.6.3.tar.gz
    useradd nginx -s /sbin/nologin -M
    tar xf nginx-1.6.3.tar.gz
    ./configure --user=nginx --group=nginx --prefix=/application/nginx --with-http_stub_status_module --with-http_ssl_module
    make
    make install
    ln -s /application/nginx-1.6.3 /application/nginx/
    ls -l /application/nginx/


    检查Nginx配置文件语法:
    /application/nginx/sbin/nginx -t
    启动Nginx服务:
    /application/nginx/sbin/nginx
    查看Nginx端口是否成功启动:
     lsof -i :80
     netstat -lnt|grep 80

     检测Nginx是否启动
     wget 127.0.0.1
     或者curl 127.0.0.1
    或者在Windows浏览器上访问服务器的地址就可以查看是否启动

    eg:当启动Nginx出现如下错误时:
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] still could not bind()
    原因是80端口被占用了
    解决方法:
        sudo fuser -k 80/tcp
        关闭占用80端口的程序,在启动Nginx
    ***
    Nginx服务不能启动时,首先从内部开始查找
    netstat -lntup 查看端口状态
    cd /application/nginx/
    cd conf/
    cat nginx.conf 进入配置文件
    ip a  查看子网地址
    curl 172.16.0.4 ping子网
    ***


    作业:部署一个Web站点:
        cd /application/nginx/html/
        rm -f index.html
        vi index.html
    <html>
    <head>
    <title>hty's Nginx server blog.</title>
    </head>
    <body>
    Hi, I am hty.
    </body>
    </html>

    2.Nginx 主配置文件nginx.conf

    查看Nginx核心配置参数:
    egrep -v "#|^$" nginx.conf.default(去掉所有默认注释行)
    结果:
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }

    3.Nginx其他配置文件
    默认情况下为注释状态:FastCGI相关配置
    查看:cat fastcgi.conf
    *********************************************************************
    4.虚拟主机的配置
    类型:
    a.基于域名的虚拟主机
    (1)基于域名的nginx.conf内容
    cd /application/nginx/conf
    diff nginx.conf.default nginx.conf

    egrep -v "#|^$" nginx.conf.default >nginx.conf
    或者直接创建新的配置文件:
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  www.etiantian.org;
            location / {
                root   html/www;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }

    (2)创建域名对应的站点目录及文件
    [root@instance-yf0xzby9 conf]# mkdir ../html/www -p
    [root@instance-yf0xzby9 conf]# echo "http://www.etiantian.org" >../html/www/index.html
    [root@instance-yf0xzby9 conf]# cat ../html/www/index.html (../表示上级目录)
    http://www.etiantian.org
    作用:创建了一个html/www站点目录,对应于虚拟机配置文件root根目录的
    html/www设置(root html/www),然后生成一个默认的首页文件index.html。
    其内容为http://www.etiantian.org。
    (3)检查语法并重新加载Nginx
    平滑启动:
    ../sbin/nginx -s reload

    (4)测试域名站点的访问结果
    echo "172.16.0.4 www.etiantian.org" >>/etc/hosts
    tail -l /etc/hosts
    curl www.etiantian.org(在监听的端口不是80时,可以在域名后加上端口号
    来进行区分)


    b.基于端口的虚拟主机
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       8089;
            server_name  www.etiantian.org;
            location / {
                root   html/www;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
        server {
            listen       8090;
            server_name  bbs.etiantian.org;
            location / {
                root   html/bbs;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
        server {
            listen       8091;
            server_name  blog.etiantian.org;
            location / {
                root   html/blog;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }


    [root@instance-yf0xzby9 conf]# mkdir ../html/www -p
    [root@instance-yf0xzby9 conf]# echo "http://www.etiantian.org" >../html/www/index.html
    [root@instance-yf0xzby9 conf]# cat ../html/www/index.html (../表示上级目录)
    http://www.etiantian.org


    ../sbin/nginx -s reload
    echo "172.16.0.4 www.etiantian.org" >>/etc/hosts
    tail -l /etc/hosts
    curl www.etiantian.org:8089(在监听的端口不是80时,可以在域名后加上端口号
    来进行区分)




    c.基于IP的虚拟主机
    (1)在服务器上增加多个IP
        ip addr add 10.0.0.9/24 dev eth0
        ip addr add 10.0.0.10/24 dev eth0

    (2)增加虚拟主机配置
    在监听端口设置仅仅基于IP即可,每个主机的server_name字段都换成IP地址

    *********************************************************************

    5.重启Nginx后检测策略

    步骤:
    a.重启Nginx;
    b.调用脚本获取header信息或模拟用户访问指定URL来自动检查Nginx是否正常启动
    c.在非语法问题时,迅速使用上一版备份配置文件覆盖会来

    检查启动是否正常的脚本,可以包含在Nginx启动脚本的start等启动位置


    虚拟主机配置文件和主配置文件分离:
    vi nginx.conf
    sed -n '10,21p' nginx.conf
    sed -n '10,21p' nginx.conf >extra/www.conf
    sed -n '22,33p' nginx.conf
    sed -n '22,33p' nginx.conf >extra/bbs.conf
    sed -n '34,45p' nginx.conf
    sed -n '34,45p' nginx.conf >extra/blog.conf
    sed -i '10,45d' nginx.conf
    cat -n nginx.conf(查看配置文件,袋数字)




    sed -i '10 i include extra/www.conf; include extra/bbs.conf; include extra/blog.conf;' nginx.conf

         1  worker_processes  1;
         2    events {
         3        worker_connections  1024;
         4    }
         5    http {
         6        include       mime.types;
         7        default_type  application/octet-stream;
         8        sendfile        on;
         9        keepalive_timeout  65;
        10    include extra/www.conf;
        11    include extra/bbs.conf;
        12    include extra/blog.conf;
        13    
        14    }

    ./sbin/nginx -t
    ../sbin/nginx -s reload

     curl www.etiantian.org等


     6.域名别名的设置
     在server_name 中添加别名,不同别名之间使用空格隔开
     并在/etc/hosts文件中添加响应的别名
    可以解决的问题:可以通过别名,在监控服务里配置hosts来监控不同域名的等
    地址是否正常,进而判断每台机器上的www.etiantian.org是否正常。

     7.Nginx信息状态功能

     模块:ngx_http_stub_status_module
     功能:记录Nginx的基本访问信息


    (1)查看是否有这个模块:
    /application/nginx/sbin/nginx -V
    (2)配置Nginx status
    ##status
    server{
        listen 80;
        server_name status.etiantian.org;
        location / {
            stub_status on;
            access_log off;
        }
    }
    EOF

  • 相关阅读:
    3-为什么很多 对 1e9+7(100000007)取模
    6-关于#include<bits/stdc++.h>
    7-n!的位数(斯特灵公式)
    5-math中函数汇总
    6-找数的倍数
    6-Collision-hdu5114(小球碰撞)
    5-青蛙的约会(ex_gcd)
    4-圆数Round Numbers(数位dp)
    4-memset函数总结
    一种Furture模式处理请求中循环独立的任务的方法
  • 原文地址:https://www.cnblogs.com/01black-white/p/9720254.html
Copyright © 2020-2023  润新知