阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS 6.5安装配置Tengine

139次阅读
没有评论

共计 9112 个字符,预计需要花费 23 分钟才能阅读完成。

CentOS 6.5 安装配置 Tengine

一、安装 pcre:

cd /usr/local/src
wget http://downloads.sourceforge.net/project/pcre/pcre/8.34/pcre-8.34.tar.gz

tar zxvf pcre-8.34.tar.gz
cd pcre-8.34
./configure –prefix=/usr/local/pcre
make
make install

二、下载 proxy_cache 插件
cd /usr/local/src

wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz

tar zxvf ngx_cache_purge-2.1.tar.gz 三、安装 tengine

yum install openssl openssl-devel -y

cd /usr/local/src
wget http://tengine.taobao.org/download/tengine-2.0.0.tar.gz
tar zxvf tengine-2.0.0.tar.gz
cd tengine-2.0.0
./configure –add-module=/usr/local/src/ngx_cache_purge-2.1 –prefix=/usr/local/nginx –with-http_stub_status_module –with-pcre=/usr/local/src/pcre-8.34
make
make install
/usr/local/nginx/sbin/nginx  #启动 nginx
chown nobody.nobody -R /usr/local/nginx/html
chmod 700 -R /usr/local/nginx/html 如果编译的问题的话,看看是不是下面的原因:

./configure: error: the HTTP SSL module requires OpenSSL library
  原因:安装 http_ssl_module 模块需要 openssl library
  解决:yum install openssl-devel
./configure: error: the HTTP rewrite module requires the PCRE library.
  原因:安装 http_rewrite_module 模块需要先安装 PCRE 开发包
  解决:yum install pcre-devel

 注意:

–with-pcre=/usr/local/src/pcre-8.21 指向的是源码包解压的路径,而不是安装的路径,否则会报错。

 –add-module=/usr/local/src/ngx_cache_purge-2.1 是指加载缓存的插件模块

四、设置 Tengine 开机启动
vi /etc/rc.d/init.d/nginx  #编辑启动文件添加下面内容

#!/bin/bash
# Tengine Startup script# processname: nginx
# chkconfig: – 85 15
# description: nginx is a World Wide Web server. It is used to serve
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog=”nginx”
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[${NETWORKING} = “no” ] && exit 0
[-x $nginxd] || exit 0
# Start nginx daemons functions.
start() {
if [-e $nginx_pid];then
echo “tengine already running….”
exit 1
fi
echo -n $”Starting $prog: “
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[$RETVAL = 0] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $”Stopping $prog: “
killproc $nginxd
RETVAL=$?
echo
[$RETVAL = 0] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
reload() {
echo -n $”Reloading $prog: “
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case “$1” in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;

status)
status $prog
RETVAL=$?
;;
*)
echo $”Usage: $prog {start|stop|restart|reload|status|help}”
exit 1
esac
exit $RETVAL
 保存退出

chmod 775 /etc/rc.d/init.d/nginx  #赋予文件执行权限
chkconfig  –level 012345 nginx on  #设置开机启动
/etc/rc.d/init.d/nginx restart  四、配置 Tengine
将 nginx 初始配置文件备份,我们要重新创建配置文件.

mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak

创建 nginx 用户 www

groupadd www
useradd -g www www 编辑主配置文件:

vi /usr/local/nginx/conf/nginx.conf 内容如下:

user  www www;
worker_processes  4;  # 工作进程数, 为 CPU 的核心数或者两倍
error_log  logs/error.log  crit; # debug|info|notice|warn|error|crit
pid        logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
 
events {
    use epoll;                            #Linux 最常用支持大并发的事件触发机制
    worker_connections  65535;
}
 
http {
    include      mime.types;            #设定 mime 类型, 类型由 mime.type 文件定义
    default_type  application/octet-stream;

    charset  utf-8;
    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; 
 
    #设定请求缓冲
    server_names_hash_bucket_size 256;    #增加, 原为 128
    client_header_buffer_size 256k;      #增加, 原为 32k
    large_client_header_buffers 4 256k;  #增加,原为 32k
 
    #size limits
    client_max_body_size          50m;    #允许客户端请求的最大的单个文件字节数
    client_header_timeout        3m;
    client_body_timeout          3m;
    send_timeout                  3m;

    sendfile                      on;
    tcp_nopush                    on;
    keepalive_timeout            60;
    tcp_nodelay                  on;
    server_tokens                on;    #不显示 nginx 版本信息
 
    limit_conn_zone $binary_remote_addr zone=perip:10m; #添加 limit_zone, 限制同一 IP 并发数
    #fastcgi_intercept_errors on;        #开启错误页面跳转
 
    include  gzip.conf;                #压缩配置文件
    include  proxy.conf;                  #proxy_cache 参数配置文件
    include  vhost/*.conf;              #nginx 虚拟主机包含文件目录
    include  mysvrhost.conf;              #后端 WEB 服务器列表文件
}
 编辑代理配置文件:

cd /usr/local/nginx/conf/
mkdir vhost
vi /usr/local/nginx/conf/proxy.conf 内容如下:

# 注:proxy_temp_path 和 proxy_cache_path 指定的路径必须在同一分区
proxy_temp_path  /tmp/proxy_temp;

# 设置 Web 缓存区名称为 cache_one,内存缓存空间大小为 500MB,1 天没有被访问的内容自动清除,硬盘缓存空间大小为 30GB。
proxy_cache_path  /tmp/proxy_cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g;
client_body_buffer_size  512k;    #原为 512k
proxy_connect_timeout    50;      #代理连接超时
proxy_read_timeout      600;      #代理发送超时
proxy_send_timeout      600;      #代理接收超时
proxy_buffer_size        128k;    #代理缓冲大小,原为 32k
proxy_buffers          16 256k;  #代理缓冲,原为 4 64k
proxy_busy_buffers_size 512k;      #高负荷下缓冲大小,原为 128k
proxy_temp_file_write_size 1024m;  #proxy 缓存临时文件的大小原为 128k
#proxy_ignore_client_abort  on;    #不允许代理端主动关闭连接
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;
编辑主机配置文件:

vi /usr/local/nginx/conf/mysvrhost.conf 内容如下:

upstream cn100 {
  ip_hash;  #会话保持
  server 127.0.0.1:8080  max_fails=1 fail_timeout=60s; 
  server 127.0.0.1:9080 max_fails=1 fail_timeout=60s;
}编辑压缩配置文件:

vi  /usr/local/nginx/conf/gzip.conf 内容如下:

# 网页 GZIP 压缩设置
#2012.4.2
# 可通过 http://tool.chinaz.com/Gzips/ 检测压缩情况
#
# 启动预压缩功能,对所有类型的文件都有效
#gzip_static on;    #开启 nginx_static 后,对于任何文件都会先查找是否有对应的 gz 文件
 
# 找不到预压缩文件,进行动态压缩
gzip on;
gzip_min_length  1k;  #设置最小的压缩值, 单位为 bytes. 超过设置的 min_length 的值会进行压缩, 小于的不压缩.
gzip_comp_level  3;  #压缩等级设置,1-9,1 是最小压缩, 速度也是最快的;9 刚好相反, 最大的压缩, 速度是最慢的, 消耗的 CPU 资源也多
gzip_buffers      16 64k;  #设置系统的缓存大小, 以存储 GZIP 压缩结果的数据流, 它可以避免 nginx 频烦向系统申请压缩空间大小
gzip_types text/plain application/x-javascript text/css text/javascript;
 
# 关于 gzip_types,如果你想让图片也开启 gzip 压缩,那么用以下这段吧:
#gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php image/jpeg image/gif image/png;
 
#gzip 公共配置
gzip_http_version 1.1;      #识别 http 的协议版本 (1.0/1.1)
gzip_proxied      any;      #设置使用代理时是否进行压缩,默认是 off 的
gzip_vary        on;      #和 http 头有关系, 加个 vary 头, 代理判断是否需要压缩
gzip_disable “MSIE [1-6].”; #禁用 IE6 的 gzip 压缩
编辑配置文件:

vi /usr/local/nginx/conf/vhost/cn100.conf 内容如下:

 server {
listen 80;
server_name localhost;
# 默认启动文件
index index.html index.htm;

 #配置发布目录为 /usr/local/tomcat1/webapps/ROOT
root /usr/local/tomcat1/webapps/ROOT;

 location /
{
 #如果后端的服务器返回 502、504、执行超时等错误,自动将请求转发到 upstream 负载均衡池中的另一台服务器,实现故障转移。
 proxy_next_upstream http_502 http_504 error timeout invalid_header;
 proxy_cache cache_one;
 

 #对不同的 HTTP 状态码设置不同的缓存时间
 proxy_cache_valid  200 304 12h;
 #以域名、URI、参数组合成 Web 缓存的 Key 值,Nginx 根据 Key 值哈希,存储缓存内容到二级缓存目录内
 proxy_cache_key $host$uri$is_args$args;

 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_pass http://cn100;

 proxy_pass_header Set-Cookie;

 #对用户传输 Set-Cookie 的 http 头,不然无法支持一些包含 cookie 的应用,比如我的 typecho

 #过期时间 3 天
 expires 3d;
}

# 用于清除缓存,假设一个 URL 为 http://192.168.8.42/test.txt,通过访问 http://192.168.8.42/purge/test.txt 就可以清除该 URL 的缓存。
    location ~ /purge(/.*)
    {
    #设置只允许指定的 IP 或 IP 段才可以清除 URL 缓存。
    allow            127.0.0.1;
    allow            192.168.0.0/16;
    deny            all;
    proxy_cache_purge    cache_one  $host$1$is_args$args;
    }   

# 查看 nginx 的并发连接数配置
location /NginxStatus
{
stub_status on;
access_log off;
auth_basic “NginxStatus”;
}

 #定义 Nginx 输出日志的路径
#access_log /data/logs/nginx_wugk/access.log main;
#error_log /data/logs/nginx_wugk/error.log crit;
#access_log off; #根据自己的需要选择是否启用 access 日志,注释掉代表启用
error_page 404 /404.html;
error_page 500 502 503 504 /404.html;
location = /404.html {
root html;
}
limit_conn perip 50; #同一 ip 并发数为 50, 超过会返回 503
}

为 Tengine 配置一下系统的 TCP 设置,优化一下:

vi /etc/sysctl.conf 内容如下:

net.ipv4.ip_forward = 0 
net.ipv4.conf.default.rp_filter = 1 
net.ipv4.conf.default.accept_source_route = 0 
kernel.sysrq = 0 
kernel.core_uses_pid = 1 
net.ipv4.tcp_syncookies = 1 
kernel.msgmnb = 65536 
kernel.msgmax = 65536 
kernel.shmmax = 68719476736 
kernel.shmall = 4294967296 
net.ipv4.tcp_max_tw_buckets = 6000 
net.ipv4.tcp_sack = 1 
net.ipv4.tcp_window_scaling = 1 
net.ipv4.tcp_rmem = 4096 87380 4194304 
net.ipv4.tcp_wmem = 4096 16384 4194304 
net.core.wmem_default = 8388608 
net.core.rmem_default = 8388608 
net.core.rmem_max = 16777216 
net.core.wmem_max = 16777216 
net.core.netdev_max_backlog = 262144 
net.core.somaxconn = 262144 
net.ipv4.tcp_max_orphans = 3276800 
net.ipv4.tcp_max_syn_backlog = 262144 
net.ipv4.tcp_timestamps = 0 
net.ipv4.tcp_synack_retries = 1 
net.ipv4.tcp_syn_retries = 1 
net.ipv4.tcp_tw_recycle = 1 
net.ipv4.tcp_tw_reuse = 1 
net.ipv4.tcp_mem = 94500000 915000000 927000000 
net.ipv4.tcp_fin_timeout = 1 
net.ipv4.tcp_keepalive_time = 30 
net.ipv4.ip_local_port_range = 1024 65000

# 允许系统打开的端口范围 
使配置立即生效

/sbin/sysctl - p 制作一个重启全部的脚本

vi /root/restartall

#!/bin/sh

#

# 重启 memcached 进程

service memcached restart

# 清空日志

rm -f /usr/local/tomcat1/logs/*

rm -f /usr/local/tomcat2/logs/*
 
# 清空缓存

rm -rf /tmp/proxy_cache

# 重启动 tomcat

/usr/local/tomcat1/bin/shutdown.sh
/usr/local/tomcat2/bin/shutdown.sh

/usr/local/tomcat1/bin/startup.sh/usr/local/tomcat2/bin/startup.sh #重启 nginx
service nginx restart

给运行权限

chmod 777 /root/restartall

以后重启服务只需要:

/root/restartall

相关阅读

CentOS 6.4 制作 Tengine 的 rpm 包 http://www.linuxidc.com/Linux/2013-12/93786.htm

Tengine 动态开启模块试用 http://www.linuxidc.com/Linux/2012-12/75849.htm

CentOS 6.3 用 ICC 编译 PHP5.4.8+Percona5.5.27+Tengine1.4.1 http://www.linuxidc.com/Linux/2012-12/76636.htm

基于淘宝 Tengine 和 Scribe 的 WEB 日志收集方案 http://www.linuxidc.com/Linux/2012-02/52997.htm

基于 Tengine 部署 LNMP 环境 http://www.linuxidc.com/Linux/2014-01/95148.htm

Tengine 的详细介绍:请点这里
Tengine 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-09/123075.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计9112字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中