tengine安装及配置

来源:互联网 发布:软件著作权 设计说明书 编辑:程序博客网 时间:2024/05/23 22:17

一。安装过程

  1》下载

        http://tengine.taobao.org/download.html 找到下载包并且下载(Tengine-2.2.0.tar.gz

  2》解压

         tar zxvf Tengine-2.2.0.tar.gz  

  3》配置检查 

       进入解压后的目录  ./configure

异常1:   [root@bogon tengine]# ./configurechecking for OS + Linux 3.10.0-327.el7.x86_64 x86_64checking for C compiler ... not found ./configure: error: C compiler cc is not found也就是c编译器 gcc找不到  安装gcc  yum -y install gcc继续检查异常2: checking for PCRE library ... not foundchecking for PCRE library in /usr/local/ ... not foundchecking for PCRE library in /usr/include/pcre/ ... not foundchecking for PCRE library in /usr/pkg/ ... not foundchecking for PCRE library in /opt/local/ ... not found ./configure: error: the HTTP rewrite module requires the PCRE library.You can either disable the module by using --without-http_rewrite_module 缺少pcre和pcre-devel 安装 yum -y install pcre pcre-devel 这里安装好后 可以通过  Rpm -qa | grep pcre找到所有pcre的包  Rpm -ql 完整包名  查看安装的路径 异常3: checking for OpenSSL library ... not found 缺少openssl和openssl-devel 安装 yum -y install openssl openssl-devel 继续检测成功 可以同构日志看到 需要pcre openssl zlib(安装openssl自动安装)的库checking for PCRE library ... foundchecking for PCRE JIT support ... foundchecking for OpenSSL library ... foundchecking for zlib library ... foundcreating objs/Makefile 

4》安装

    Make && make install 安装完成 安装目录为/usr/local/nginx

找到 /usr/local/nginx/sbin/nginx -V 查看所有加载的模块

[root@bogon sbin]# ./nginx -VTengine version: Tengine/2.2.0 (nginx/1.8.1)built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)TLS SNI support enabledconfigure arguments:nginx: loaded modules:nginx:     ngx_core_module (static)nginx:     ngx_errlog_module (static)nginx:     ngx_conf_module (static)nginx:     ngx_dso_module (static)nginx:     ngx_events_module (static)

 /usr/local/nginx/sbin/nginx start|stop|restart 启动和关闭ngix服务

 访问 http://ip地址

 5》开机启动nginx

  nigix官网上找到  https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/ 查看linux下的启动脚本 

进入官网 www.nginx.com 点击resources-community wiki


进入后 点击 下图 Getting Started按钮进入

点击下界面的Ngix init Scipts

点击 Red hast nginx init script 即可打开脚本



  保存内容到  /etc/init.d/nginx (服务的教程参考http://blog.csdn.net/liaomin416100569/article/details/72876349)

 拷贝后 需要修改其中的两个变量 nginx安装目录的配置文件和nginx程序的目录 

nginx="/usr/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/etc/nginx/nginx.conf"
修改为

nginx="/usr/local/nginx/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"


注意 nginx文件的内容是否完整拷贝 有时拷贝 缺少一些字符导致报错 

 service nginx start  或者 systemctl start nginx

一般启动会卡住  通过 systemctl status nginx 可以看到抛出错误

Jun 08 03:08:55 bogon systemd[1]: PID file /var/run/nginx.pid not readable (yet?) after start.Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
可以看到在nginx 在启动时候 自动会去 /var/run下面去找pid文件所以必须 要将pid文件生成在/var/run下 

修改conf/nginx.conf文件 

将  #pid=logs/nginx.pid(可以看到pid默认生成在logs目录下) 修改为 pid=/var/run/nginx.pid 即可 去掉注释的#   重启即可


添加到系统服务 设置开机启动

[root@bogon init.d]# chkconfig --add nginx
[root@bogon init.d]# chkconfig nginx on

 使用 http://192.168.58.131/访问 看是否出现 欢迎使用tengine的英文

二。配置文件

 默认配置文件位于 

/usr/local/nginx/conf/nginx.conf

 1》常用配置选项

#user  nobody;#工作的cpu的内核数  默认应该是和当前电脑的cpu一致  设置为auto自动获取当前机器worker_processes  auto;#ngx_http_log_module模块功能  #语法为 error_log 存储的log文件路径 [debug | info | notice | warn | error | crit]#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#定义存储nginx主进程ID的file。 nigix可能建立虚拟主机是 会创建多个进程 #pid        logs/nginx.pid;#最顶层的指令 events一般用于指定连接处理参数events {    #设置每个工作进程可以打开的最大并发连接数。(数量包含所有连接(比如,和后端服务器建立的连接,还有其他的), 而不仅仅是和客户端的连接)    worker_connections  1024;}# load modules compiled as Dynamic Shared Object (DSO)##dso {#    load ngx_http_fastcgi_module.so;#    load ngx_http_rewrite_module.so;#}#配置http服务器http {    #包含其他语法正确的配置文件     include       mime.types;    #设置默认的mini类型    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压缩 压缩需要时间 但是压缩后 可以节省带宽    #gzip  on;    #配置虚拟主机    server {        #虚拟主机监听端口        listen       80;        #虚拟主机监听的ip(同一电脑可能多个ip)或者域名        #添加虚拟ip ifconfig 网卡名(可以通过ifconfig查看):1 192.168.58.134 netmask 255.255.255.0 up        #删除虚拟ip ifconfig 网卡名(可以通过ifconfig查看):1 down        server_name  localhost;//当根路径访问时 必须 http://localhost/ 自动进入根目录 html下找 index.html文件        location / {            root   html;            index  index.html index.htm;        }        //出现以下错误状态码 自动进入 /50x.html           error_page   500 502 503 504  /50x.html;        //自动找 html 实际文件路径就是 /html/50x.html        location = /50x.html {            root   html;        }    }}

 2》location的路径匹配

该配置位于 ngx_http_core_module 模块 (http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#location)

nginx中location的路径匹配规则 

 使用正则表达式需要在路径开始添加“~*”前缀 (不区分大小写),或者“~”前缀(区分大小写)
匹配的路径以“^~”开始,那么nginx不再检查后面的所有条件和正则表达式。
使用“=”前缀可以定义URI和路径的精确匹配。如果发现匹配,则终止路径查找
#这个表示 路径精确就是 /就匹配A 比如 http://localhost/
location = / {
    [ configuration A ]
}
location / {
    [ configuration B ]
}
location /documents/ {
    [ configuration C ]
}
location ^~ /images/ {
    [ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ { 
    [ configuration E ] 

请求“/”匹配配置A, 请求“/index.html”匹配配置B, 请求“/documents/document.html”匹配配置C, 请求“/images/1.gif”匹配配置D, 请求“/documents/1.jpg”匹配配置E。
 2》location其他配置

   》ngx_http_access_module模块 限制客户端访问ip(http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_access_module.html)

   配置范例
location / {
    deny  192.168.58.1;
    allow  all;
}
这里 192.168.58.1被禁止访问  除了58.1其他都可以访问

58.1访问 出现 403被禁止  其他ip都可以正常访问(linux访问可以使用 wget 地址 或者 curl地址) 可以通过logs/access.log查看访问服务的客户端信息


  》模块ngx_http_auth_basic_module 允许使用“HTTP基本认证”协议验证用户名和密码来限制对资源的访问。
(http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_auth_basic_module.html)

 location / {    auth_basic           "密码验证";    auth_basic_user_file conf/htpasswd; }


指定保存用户名和密码的文件,格式如下:
  name1:password1  name2:password2:comment  name3:password3


密码应该使用crypt()函数加密。 可以用Apache发行包中的htpasswd命令来创建此类文件。


安装Apache  yum -y install httpd  
[root@bogon logs]# htpasswd   使用语法        htpasswd [-cimBdpsDv] [-C cost] passwordfile username        htpasswd -b[cmBdpsDv] [-C cost] passwordfile username password        htpasswd -n[imBdps] [-C cost] username        htpasswd -nb[mBdps] [-C cost] username password

[root@bogon logs]# htpasswd -nb zs 123456
zs:$apr1$8cAQMH7X$AvjTq5Gon0kiKHR/2LW2g/
通过vi创建文件拷贝内容到该文件 /usr/local/nginx/conf/auth.conf
修改
 
location / {    auth_basic           "密码验证";    auth_basic_user_file /usr/local/nginx/conf/auth.conf; }

效果图