• 1、编译安装Nginx


    1.1 如何选择web服务器

    在实际工作中,我们需要根据业务需求来选择合适的业务服务软件,有关web服务,选择建议如下:

    • 静态业务:若是高并发场景,尽量采用nginx或lighttpd,二者首选nginx
    • 动态业务:理论上采用nginx和apache均可,建议选择nginx,避免相同的业务服务软件多样化,增加额外维护成本。动态业务可以由nginx兼职做前端代理,在根据页面元素的类型或目录,转发到后端相应的服务器处理
    • 既有静态业务又有动态业务:采用nginx

    1.2 安装nginx所需要的pcre库

    安装pcre库是为了使nginx支持具备URI重写功能的rewrite模块,如果不安装pcre库,则nginx无法使用rewrite模块功能,nginx的rewrite模块功能几乎是企业应用必须的。安装pcre库的过程如下。 采用yum安装方式,推荐方法:

    # yum install pcre pcre-devel -y
    

    yum安装后检查版本

    # rpm -qa pcre pcre-devel
    pcre-devel-8.32-15.el7_2.1.x86_64
    pcre-8.32-15.el7_2.1.x86_64
    

    1.3 安装openssl-devel

    nginx在使用HTTPS服务的时候要用到此模块,如果不安装openssl相关包,安装nginx的过程中会报错。

    # yum install -y openssl openssl-devel
    

    1.4 安装nginx

    在实际工作中,选择稳定版时,尽量避免使用最新版本,选择比已出来的最新晚6-10个月的版本比较好。编译nginx软件时,可以使用./configure --help查看相关参数帮助。

    # useradd nginx -s /sbin/nologin -M
    # tar xf nginx-1.12.0.tar.gz 
    # cd nginx-1.12.0
    # ./configure 
    --user=nginx --group=nginx 
    --prefix=/application/nginx-1.12.0 
    --with-http_stub_status_module 
    --with-http_ssl_module
    # make
    # make install
    
    # ln -s /application/nginx-1.12.0 /application/nginx
    

    1.5 启动并检查安装结果

    启动前检查配置文件语法

    # /application/nginx/sbin/nginx -t
    nginx: the configuration file /application/nginx-1.12.0/conf/nginx.conf syntax is ok
    nginx: configuration file /application/nginx-1.12.0/conf/nginx.conf test is successful
    

    在启动前检查语法非常重要,可以防止因配置错误导致网站重启或重新加载配置等对用户的影响 启动nginx服务

    # /application/nginx/sbin/nginx
    

    可以通过curl命令检车是否浏览正常

    # curl 127.0.0.1
    

    也可以通过netstat -lnt|grep 80、lsof -i:80检查nginx服务器端口(默认80)来判断nginx是否已启动,或者通过ps -ef|grep nginx检查nginx服务进程来判断,当然最稳妥的方法还是通过url地址来检查。 

    1.6 查看nginx编译时的参数

    # /application/nginx/sbin/nginx -V
    nginx version: nginx/1.12.0
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) 
    built with OpenSSL 1.0.1e-fips 11 Feb 2013
    TLS SNI support enabled
    configure arguments: --user=nginx --group=nginx --prefix=/application/nginx-1.12.0 --with-http_stub_status_module --with-http_ssl_module
    

    1.7 systemctl启动配置

    # vim /usr/lib/systemd/system/nginx.service
    [Unit]
    Description=nginx - high performance web server
    Documentation=http://nginx.org/en/docs/
    After=network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=forking
    PIDFile=/application/nginx/logs/nginx.pid
    ExecStartPre=/application/nginx/sbin/nginx -t -c /application/nginx/conf/nginx.conf
    ExecStart=/application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    

    管理

    # chmod +x /usr/lib/systemd/system/nginx.service
    # systemctl enable nginx.service
    # systemctl start nginx.service
    # systemctl stop nginx.service
    # systemctl reload nginx.service
    

    2.1 配置虚拟主机的步骤

    1. 增加一个完整的server标签段到结尾处。注意要放在http的结束大括号前,也就是将server标签段放入http标签。
    2. 更改server_name及对应网页的root根目录,如果需要其他参数,可以增加或修改。
    3. 创建server_name域名对应网页的根目录,并建立测试文件,如果没有index首页,访问会出现403错误。
    4. 检查nginx配置文件语法,平滑重启nginx服务,快速检查启动结果。
    5. 域名做dns解析,并检查(ping域名看返回的IP是否正确)。
    6. 在浏览器中输入地址访问,用wget或curl接地址访问。

    2.2 规范优化nginx配置文件

    nginx的主配置文件为nginx.conf,主配置文件包含的所有虚拟主机的子配置文件会统一放入到vhosts目录中,虚拟主机的配置文件安装网站的域名或功能取名,例如www.conf、bbs.conf、blog.conf等。

    这里使用参数include,把它放置在nginx中的http区块中,用法如下:

    http{
        ...
        include  vhosts/*.conf;  #包含vhosts下所有以conf结尾的文件
    }
    

    实施步骤如下:

    # cd /application/nginx/conf/
    # mkdir vhosts
    # vim nginx.conf
    ...
    http{
        ...
        include vhosts/*.conf;
    }
    
    # cd vhosts
    # vim www.heboan.conf
    server {
        listen  80;
        server_name  www.heboan.com;
        location / {
            root  html/www;
            index index.html index.htm;
        }
    }
    
    server {
        listen  80;
        server_name  bbs.heboan.com;
        location / {
            root  html/bbs;
            index index.html index.htm;
        }
    }
    

    2.3 虚拟主机的别名配置

    虚拟主机别名,就是为了虚拟主机设置 除了主域名以外的一个或多个域名名字,这样就能实现用户访问的多个域名对应同一个虚拟主机网站的功能。以www.heboan.com域名的虚拟主机为例,为其增加一个别名heboan.com,使得访问heboan.com时,在该域名出现的网站内容和访问www.heboan.com得到的结果是一样的,这是企业场景中活生生的基本需求配置:

    server {
        listen  80;
        server_name  www.heboan.com heboan.com;
        location / {
            root  html/www;
            index index.html index.htm;
        }
    }
    

    3.1 添加模块(非覆盖安装)

    nginx已经安装好了,现在需要添加一个未被编译安装的模块,需要重新编译,这里以nginx-module-vts模块为例

    nginx-module-vts可查询配置的虚拟主机通讯状态模块,类似stub_status_module模块,并且比这个统计的粒度更细,默认未包含在nginx的发布包中,需要单独下载:

    # git clone git://github.com/vozlt/nginx-module-vts.git
    

    查看原来编译时都带了那些参数

    # /application/nginx/sbin/nginx -V
    nginx version: nginx/1.12.0
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) 
    built with OpenSSL 1.0.1e-fips 11 Feb 2013
    TLS SNI support enabled
    configure arguments: --user=nginx --group=nginx
    --prefix=/application/nginx-1.12.0
    --with-http_stub_status_module
    --with-http_ssl_module
    

    添加模块nginx-module-vts

    # cd nginx-1.12.0
    # ./configure --user=nginx --group=nginx 
    --prefix=/application/nginx-1.12.0 
    --with-http_stub_status_module 
    --with-http_ssl_module 
    --add-module=/root/tools/nginx-module-vts
    
    # make
    # 不要make install,否则会覆盖安装
    

    关闭nginx

    # systemctl stop nginx.service
    

    替换二进制文件

    # cp /application/nginx/sbin/nginx /application/nginx/sbin/nginx.bak
    # cp ./objs/nginx /application/nginx/sbin/
    

    启动nginx,查看模块

    # systemctl start nginx.service
    # /application/nginx/sbin/nginx -V
    nginx version: nginx/1.12.0
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) 
    built with OpenSSL 1.0.1e-fips 11 Feb 2013
    TLS SNI support enabled
    configure arguments: --user=nginx --group=nginx
    --prefix=/application/nginx-1.12.0
    --with-http_stub_status_module
    --with-http_ssl_module
    --add-module=/root/tools/nginx-module-vts
    

    模块添加成功

    配置nginx-module-vts

    # vim /application/nginx/conf/nginx.conf
    http {
        ...
        vhost_traffic_status_zone;
    }
    
    # vim /application/nginx/conf/vhosts/status.heboan.conf
    server {
        listen 80;
        server_name  status.heboan.com;
    
        location /{
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
            access_log off;
        }
    }
    

    检查配置是否正确,重载配置

    # /application/nginx/sbin/nginx -t
    nginx: the configuration file /application/nginx-1.12.0/conf/nginx.conf syntax is ok
    nginx: configuration file /application/nginx-1.12.0/conf/nginx.conf test is successful
    
    # /application/nginx/sbin/nginx -s reload
    

    浏览器访问http://status.heboan.com

    如果我们只是想使用官方的status,只需要改下status.heboan.conf

    server {
        listen 80;
        server_name  status.heboan.com;
    
        location /{
            stub_status on;
            access_log off;
        }
    }
    

      

  • 相关阅读:
    桌面图标背景透明
    如何做好一个中小型企业计算机网络管理员
    打开IE8总是提示欢迎使用?怎样使它不提示?
    js 操作select和option,添加select列
    bios 被加密,怎么进入bios
    Foxmail自动收取新邮件
    代码片段
    提高生活幸福感的13个方法
    水晶报表的真实体验
    游标替代
  • 原文地址:https://www.cnblogs.com/sellsa/p/7787756.html
Copyright © 2020-2023  润新知