• 构建基于Nginx的web服务器


    一、简介

    Nginx("engine x") 是一个高性能的HTTP 和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。 Nginx 是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,它已经在该站点运行超过四年多了。Igor 将源代码以类BSD许可证的形式发布。自Nginx 发布四年来,Nginx 已经因为它的占有内存少、并发能力强、稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。目前国内各大门户网站已经部署了Nginx,如新浪、网易、腾讯等;国内几个重要的视频分享网站也部署了Nginx,如六房间、酷6等。新近发现Nginx 技术在国内日趋火热,越来越多的网站开始部署Nginx。

    二、系统环境

    系统平台:RHEL 5.4

    Nginx版本:nginx-1.0.15

    三、安装及配置Nginx

    1.安装pcre软件包,pcre的作用为nginx提供兼容perl的正则表达式库。默认情况下,Nginx只处理静态的网页请求,也就是html.如果是来自动态的网页请求,比如*.php,那么Nginx就要根据正则表达式查询路径,然后把*.PHP交给PHP去处理,可以采用RHEL5光盘自带的rpm包进行安装,另外也可下载最新的源码包进行编译安装。

    [root@localhost~]# rpm -ivh pcre-6.6-2.el5_1.7

    [root@localhost~]# rpm -ivh pcre-devel-6.6-2.el5_1.7

    2.安装nginx
    [root@localhost~]# tar zxvf nginx-1.0.15.tar.gz
    [root@localhost~]# cd nginx-1.0.15

    [root@localhost nginx-1.0.15]# ./configure

    更多的安装配置
    ./configure --prefix=/usr/local/nginx
    --with-openssl=/usr/include (启用ssl)

    --with-pcre=/usr/include/pcre/ (启用正规表达式)

          –with-pcre=DIR (set path to PCRE library sources)

      注意:set path to PCRE library sources是让你设置到源码目录,而不是编译安装后的目录。

    --with-http_stub_status_module (安装可以查看nginx状态的程序)
    --with-http_memcached_module (启用memcache缓存)
    --with-http_rewrite_module (启用支持url重写)

    \\其他更多配置选项可以使用./configure --help命令进行查看

    [root@localhost nginx-1.0.15]# make && make install

    四、Nginx服务的运行控制

    1.添加nginx运行的用户组:
    [root@localhost nginx-1.0.15]# useradd -s /sbin/nologin nginx
     
    2.Nginx默认安装在/usr/local/nginx目录下,为了方便应用,可以添加一个nginx主程序的符号链接:
    [root@localhost nginx-1.0.15]# ln -sf /usr/local/nginx/sbin/nginx  /usr/sbin
     
    3.使用nginx -t命令检查nginx配置文件是否有语法错误:
    [root@linux nginx-1.0.15]# nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    [root@linux nginx-1.0.15]#
     执行nginx -t后出现上述提示表示配置文件语法正确。
     
    4.使用nginx启动服务,然后使用netstat命令进行查看:

    [root@linux nginx-1.0.15]# netstat -anpt|grep 80
    tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      16787/nginx         
    [root@linux nginx-1.0.15]#

    5.nginx启动成功后,可以在浏览器中查看初始的web页面:

    在客户端浏览器中执行:http://10.0.0.133(服务器IP地址)进行查看:

    另外在服务器命令行下使用文本浏览器工具elink进行查看:

    [root@localhost nginx-0.8.54]# elinks http://10.0.0.133

    6.使用系统信号控制nginx进程:
    启动:nginx
    重启:kill -HUP `cat /usr/local/nginx/logs/nginx.pid`

    [root@localhost~]# kill -s HUP nginx   //重新加载配置文件,等同于“killall -1 nginx”
    [root@localhost~]# kill -s QUIT nginx  //安全退出,等同于“kill -3 nginx”
    [root@localhost~]# kill -s TERM nginx //快速退出,不等待处理完当前连接
     
    另外,为了方便管理,可以添加一个nginx服务脚本,使用chkconfig和service命令管理nginx服务:
    [root@localhost~]# vi /etc/init.d/nginx
     1 #!/bin/bash
     2 #description: Nginx Service Control Script
     3 case "$1" in
     4   start)
     5          /usr/sbin/nginx    
     6          ;;
     7   stop)
     8          /usr/bin/killall -s QUIT nginx
     9             ;;
    10   restart)
    11          $0 stop
    12          $0 start
    13          ;;
    14   reload)
    15          /usr/bin/killall -s HUP nginx
    16          ;;
    17   *)
    18     echo "Usage:$0 {start|stop|restart|reload}"
    19     exit 1
    20 esac
    21 exit 0
    [root@localhost~]# chmod a+x /etc/init.d/nginx    为nginx脚本赋予可执行权限
    [root@localhost~]# chkconfig --add nginx
    [root@localhost~]# chkconfig --level 2345 nginx on

    接下来就可以使用service nginx stop|start|restart|reload对nginx服务进行控制:

    [root@linux nginx-1.0.15]# service nginx restart

    [root@linux nginx-1.0.15]# !nets
    netstat -anpt|grep 80
    tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      16787/nginx         
    [root@linux nginx-1.0.15]#

  • 相关阅读:
    js如何实现base64转文件下载保存到本地
    安装node及环境配置
    一定需要使用(N)Text吗?
    MS Sql Server EXECUTE
    [teach.net]表组织和索引组织
    Optimization Rules of Thumb[part of Technet Article]
    vs2008的用户自定义控件
    执行计划的缓存和重新使用
    保持索引的健康
    SQLServer性能优化计数器
  • 原文地址:https://www.cnblogs.com/mchina/p/2469680.html
Copyright © 2020-2023  润新知