看外网ip~~

curl -s ‘http://208.78.70.70/’  | sed ‘s/.*Current IP Address: \([0-9\.]*\).*/\1/g’

No Comments

20个Nginx Web服务器最佳安全实践

Nginx是一个轻量级,高性能的Web服务器/反向代理和电子邮件代理(IMAP/POP3),它可以运行在UNIX,GNU/Linux,BSD变种,MAC OS X,Solaris和Microsoft Windows上。根据Netcraft的调查数据显示,互联网上6%的域名都使用了Nginx Web服务器。Nginx是解决C10K问题的服务器之一,与传统服务器不一样,Nginx不依赖于线程处理请求,相反,它使用了一个更具扩展性的事件驱动(异步)架构。Nginx在很多高流量网站上得到了应用,如WordPress,Hulu,Github和SourceForge。
本文的主要目是介绍如何提高运行在Linux或UNIX类操作系统上的Nginx Web服务器的安全性。
Nginx默认配置文件和默认端口
◆ /usr/local/nginx/conf/ – Nginx服务器配置目录,/usr/local/nginx/conf/nginx.conf 是主配置文件
◆ /usr/local/nginx/html/ – 默认文档位置
◆ /usr/local/nginx/logs/ – 默认日志文件位置
◆ Nginx HTTP默认端口:TCP 80
◆ Nginx HTTPS默认端口:TCP 443
可以使用下面的命令测试Nginx的配置是否正确:
# /usr/local/nginx/sbin/nginx –t
输出示例:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
要让修改后的配置生效,执行下面的命令:
# /usr/local/nginx/sbin/nginx -s reload
如果要停止服务器,运行:
# /usr/local/nginx/sbin/nginx -s stop
1、开启SELinux
SELinux(安全增强的Linux)是一个Linux内核功能,它提供了一个机制支持访问控制安全策略,提供了巨大的安全保护能力,它可以防止大多数系统root级攻击,请参考“如何在CentOS/Red Hat系统上开启SELinux”(http://www.cyberciti.biz/faq/rhel-fedora-redhat-selinux-protection/)。
运行getsebool –a命令查看SELinux设置项:
getsebool -a | less
getsebool -a | grep off
getsebool -a | grep o
然后使用setsebool命令开启需要的配置项,注意:开启SELinux后,在RHEL或CentOS上通常会增加2-8%的系统开销。
2、通过mount参数提供最低权限
为你的/html/php文件创建独立的分区,例如,创建一个/dev/sda5分区挂载在/ngnix上,确定/ngnix使用了noexec,nodev和nosetuid权限挂载。下面是我的一个挂载实例:
LABEL=/nginx    
/nginx         
ext3  
defaults,nosuid,noexec,nodev 1 2
注意你需要使用fdisk和mkfs.ext3命令创建一个新分区。
3、通过/etc/sysctl.conf加固
可以通过/etc/sysctl.conf控制和配置Linux内核及网络设置。
另外,请参考:
# 避免放大攻击
net.ipv4.icmp_echo_ignore_broadcasts = 1
# 开启恶意icmp错误消息保护
net.ipv4.icmp_ignore_bogus_error_responses = 1
# 开启SYN洪水攻击保护
net.ipv4.tcp_syncookies = 1
# 开启并记录欺骗,源路由和重定向包
net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.default.log_martians = 1
# 处理无源路由的包
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0

# 开启反向路径过滤
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# 确保无人能修改路由表
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
# 不充当路由器
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
# 开启execshild
kernel.exec-shield = 1
kernel.randomize_va_space = 1
# IPv6设置
net.ipv6.conf.default.router_solicitations = 0
net.ipv6.conf.default.accept_ra_rtr_pref = 0
net.ipv6.conf.default.accept_ra_pinfo = 0
net.ipv6.conf.default.accept_ra_defrtr = 0
net.ipv6.conf.default.autoconf = 0
net.ipv6.conf.default.dad_transmits = 0
net.ipv6.conf.default.max_addresses = 1
# 优化LB使用的端口
# 增加系统文件描述符限制
fs.file-max = 65535
# 允许更多的PIDs (减少滚动翻转问题); may break some programs 32768
kernel.pid_max = 65536
# 增加系统IP端口限制
net.ipv4.ip_local_port_range = 2000 65000
# 增加TCP最大缓冲区大小
net.ipv4.tcp_rmem = 4096 87380 8388608
net.ipv4.tcp_wmem = 4096 87380 8388608
# 增加Linux自动调整TCP缓冲区限制
# 最小,默认和最大可使用的字节数
# 最大值不低于4MB,如果你使用非常高的BDP路径可以设置得更高
# Tcp窗口等
net.core.rmem_max = 8388608
net.core.wmem_max = 8388608
net.core.netdev_max_backlog = 5000
net.ipv4.tcp_window_scaling = 1
◆ Linux VM调优(内存)子系统(http://www.cyberciti.biz/faq/linux-kernel-tuning-virtual-memory-subsystem/
◆ Linux网络堆栈调优(缓冲区大小)提高网络性能(http://www.cyberciti.biz/faq/linux-tcp-tuning/
4、移除所有不需要的Nginx模块
你需要最大限度地将Nginx加载的模块最小化,我的意思是满足Web服务器需要就可以了,多余的模块一个不留,例如,禁用SSI和autoindex模块的命令如下:
# ./configure –without-http_autoindex_module –without-http_ssi_module
# make
# make install
在编译Nginx服务器时,使用下面的命令查看哪些模块应该启用,哪些模块应该禁用:
# ./configure –help | less
禁用你不需要的Nginx模块。
修改Nginx版本头信息(可选),编辑src/http/ngx_http_header_filter_module.c:
# vi +48 src/http/ngx_http_header_filter_module.c
找到下面两行:
static char ngx_http_server_string[] = “Server: nginx” CRLF;
static char ngx_http_server_full_string[] = “Server: ” NGINX_VER CRLF;
将其修改为:
static char ngx_http_server_string[] = “Server: Ninja Web Server” CRLF;
static char ngx_http_server_full_string[] = “Server: Ninja Web Server” CRLF;
保存并关闭文件。现在可以开始编译服务器了,将下面的配置代码添加到nginx.conf中,禁止在所有自动产生的错误页面中显示Nginx版本号:
server_tokens off
5、使用mod_security(仅适用于后端Apache服务器)
Mod_security为Apache提供了一个应用程序级防火墙,为所有后端Apache Web服务器安装mod_security模块,可以阻止许多注入攻击。
6、配置SELinux策略加固Nginx
默认情况下,SELinux没有保护Nginx Web服务器,可以手动配置进行保护,首先安装SELinux编译时需要的支持包:
# yum -y install selinux-policy-targeted selinux-policy-devel
从项目主页(http://sourceforge.net/projects/selinuxnginx/)下载SELinux策略:
# cd /opt
# wget ‘http://downloads.sourceforge.net/project/selinuxnginx/se-ngix_1_0_10.tar.
gz?use_mirror=nchc’

解压:
# tar -zxvf se-ngix_1_0_10.tar.gz
编译:
# cd se-ngix_1_0_10/nginx
# make
输出示例:
Compiling targeted nginx module
/usr/bin/checkmodule:  loading policy configuration from tmp/nginx.tmp
/usr/bin/checkmodule:  policy configuration loaded
/usr/bin/checkmodule:  writing binary representation (version 6) to tmp/nginx.mod
Creating targeted nginx.pp policy package
rm tmp/nginx.mod.fc tmp/nginx.mod
安装生成的nginx.pp SELinux模块:
# /usr/sbin/semodule -i nginx.pp
7、通过iptables防火墙设置限制
下面的防火墙脚本可以阻止一切请求,只允许:
◆ 入站HTTP请求(TCP 80端口)
◆ 入站ICMP ping请求
◆ 出站NTP请求(端口123)
◆ 出站SMTP请求(TCP端口25)
#!/bin/bash
IPT=”/sbin/iptables”
#### IPS ######
# 获得服务器公共IP
SERVER_IP=$(ifconfig eth0 | grep ‘inet addr:’ | awk -F’inet addr:’ ‘{ print $2}’ | awk ‘{ print $1}’)
LB1_IP=”204.54.1.1″
LB2_IP=”204.54.1.2″
# 实现某些智能逻辑,以便我们可以在LB2上使用damm脚本
OTHER_LB=”"
SERVER_IP=”"
[[ "$SERVER_IP" == "$LB1_IP" ]] && OTHER_LB=”$LB2_IP” || OTHER_LB=”$LB1_IP”
[[ "$OTHER_LB" == "$LB2_IP" ]] && OPP_LB=”$LB1_IP” || OPP_LB=”$LB2_IP”
### IPs ###
PUB_SSH_ONLY=”122.xx.yy.zz/29″ #### 文件 #####
BLOCKED_IP_TDB=/root/.fw/blocked.ip.txt
SPOOFIP=”127.0.0.0/8 192.168.0.0/16 172.16.0.0/12 10.0.0.0/8 169.254.0.0/16 0.0.0.0/8 240.0.0.0/4 255.255.255.255/32 168.254.0.0/16 224.0.0.0/4 240.0.0.0/5 248.0.0.0/5 192.0.2.0/24″
BADIPS=$( [[ -f ${BLOCKED_IP_TDB} ]] && egrep -v “^#|^$” ${BLOCKED_IP_TDB})
### 接口 ###
PUB_IF=”eth0″   # public interface
LO_IF=”lo”      # loopback
VPN_IF=”eth1″   # vpn / private net
### 启动防火墙 ###
echo “Setting LB1 $(hostname) Firewall…”
# 删除和关闭一切
$IPT -P INPUT DROP
$IPT -P OUTPUT DROP
$IPT -P FORWARD DROP
# 不受限制的lo访问
$IPT -A INPUT -i ${LO_IF} -j ACCEPT
$IPT -A OUTPUT -o ${LO_IF} -j ACCEPT
# 不受限制的vpn/pnet访问
$IPT -A INPUT -i ${VPN_IF} -j ACCEPT
$IPT -A OUTPUT -o ${VPN_IF} -j ACCEPT
# 删除sync
$IPT -A INPUT -i ${PUB_IF} -p tcp ! –syn -m state –state NEW -j DROP
# 删除碎片
$IPT -A INPUT -i ${PUB_IF} -f -j DROP
$IPT  -A INPUT -i ${PUB_IF} -p tcp –tcp-flags ALL FIN,URG,PSH -j DROP
$IPT  -A INPUT -i ${PUB_IF} -p tcp –tcp-flags ALL ALL -j DROP
# 删除空包
$IPT  -A INPUT -i ${PUB_IF} -p tcp –tcp-flags ALL NONE -m limit –limit 5/m –limit-burst 7 -j LOG –log-prefix ” NULL Packets ”
$IPT  -A INPUT -i ${PUB_IF} -p tcp –tcp-flags ALL NONE -j DROP
$IPT  -A INPUT -i ${PUB_IF} -p tcp –tcp-flags SYN,RST SYN,RST -j DROP
# 删除XMAS
$IPT  -A INPUT -i ${PUB_IF} -p tcp –tcp-flags SYN,FIN SYN,FIN -m limit –limit 5/m –limit-burst 7 -j LOG –log-prefix ” XMAS Packets ”
$IPT  -A INPUT -i ${PUB_IF} -p tcp –tcp-flags SYN,FIN SYN,FIN -j DROP
# 删除FIN包扫描
$IPT  -A INPUT -i ${PUB_IF} -p tcp –tcp-flags FIN,ACK FIN -m limit –limit 5/m –limit-burst 7 -j LOG –log-prefix ” Fin Packets Scan ”
$IPT  -A INPUT -i ${PUB_IF} -p tcp –tcp-flags FIN,ACK FIN -j DROP
$IPT  -A INPUT -i ${PUB_IF} -p tcp –tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
# 记录并放弃广播/多播和无效数据包
$IPT  -A INPUT -i ${PUB_IF} -m pkttype –pkt-type broadcast -j LOG –log-prefix ” Broadcast ”
$IPT  -A INPUT -i ${PUB_IF} -m pkttype –pkt-type broadcast -j DROP
$IPT  -A INPUT -i ${PUB_IF} -m pkttype –pkt-type multicast -j LOG –log-prefix ” Multicast ”
$IPT  -A INPUT -i ${PUB_IF} -m pkttype –pkt-type multicast -j DROP
$IPT  -A INPUT -i ${PUB_IF} -m state –state INVALID -j LOG –log-prefix ” Invalid ”
$IPT  -A INPUT -i ${PUB_IF} -m state –state INVALID -j DROP
# 记录和阻止欺骗IP
$IPT -N spooflist
for ipblock in $SPOOFIP
do   
$IPT -A spooflist -i ${PUB_IF} -s $ipblock -j LOG –log-prefix ” SPOOF List Block “      
$IPT -A spooflist -i ${PUB_IF} -s $ipblock -j DROP
done
$IPT -I INPUT -j spooflist
$IPT -I OUTPUT -j spooflist
$IPT -I FORWARD -j spooflist
# 只允许从选定的公共IP使用SSH
for ip in ${PUB_SSH_ONLY}
do        $IPT -A INPUT -i ${PUB_IF} -s ${ip} -p tcp -d ${SERVER_IP} –destination-port 22 -j ACCEPT      
$IPT -A OUTPUT -o ${PUB_IF} -d ${ip} -p tcp -s ${SERVER_IP} –sport 22 -j ACCEPT
done
# 允许入站ICMP ping
$IPT -A INPUT -i ${PUB_IF} -p icmp –icmp-type 8 -s 0/0 -m state –state NEW,ESTABLISHED,RELATED -m limit –limit 30/sec  -j ACCEPT
$IPT -A OUTPUT -o ${PUB_IF} -p icmp –icmp-type 0 -d 0/0 -m state –state ESTABLISHED,RELATED -j ACCEPT
# 允许入站HTTP端口80
$IPT -A INPUT -i ${PUB_IF} -p tcp -s 0/0 –sport 1024:65535 –dport 80 -m state –state NEW,ESTABLISHED -j ACCEPT
$IPT -A OUTPUT -o ${PUB_IF} -p tcp –sport 80 -d 0/0 –dport 1024:65535 -m state –state ESTABLISHED -j ACCEPT
# 允许出站NTP
$IPT -A OUTPUT -o ${PUB_IF} -p udp –dport 123 -m state –state NEW,ESTABLISHED -j ACCEPT
$IPT -A INPUT -i ${PUB_IF} -p udp –sport 123 -m state –state ESTABLISHED -j ACCEPT
# 允许出站SMTP
$IPT -A OUTPUT -o ${PUB_IF} -p tcp –dport 25 -m state –state NEW,ESTABLISHED -j ACCEPT
$IPT -A INPUT -i ${PUB_IF} -p tcp –sport 25 -m state –state ESTABLISHED -j ACCEPT
### 在这里添加其他规则 ####
#######################
# 删除并记录其它数据包
$IPT -A INPUT -m limit –limit 5/m –limit-burst 7 -j LOG –log-prefix ” DEFAULT DROP ”
$IPT -A INPUT -j DROP
exit 0
8、控制缓冲区溢出攻击
编辑nginx.conf设置所有客户端可用的缓冲区大小限制:
# vi /usr/local/nginx/conf/nginx.conf
具体设置如下:
## Start: Size Limits & Buffer Overflows ##  client_body_buffer_size  1K;

client_header_buffer_size 1k;  client_max_body_size 1k;  large_client_header_buffers 2 1k;
## END: Size Limits & Buffer Overflows ##
说明:
client_body_buffer_size 1k:默认是8k或16k,指定客户端请求主体缓冲区大小。
client_header_buffer_size 1k:指定来自客户端请求头的headerbuffer大小,对于大多数请求,1k的缓冲区大小已经足够,如果你自定义了消息头或有更大的Cookie,你可以增加其大小。
client_max_body_size 1k:客户端请求中可接受的主体最大大小,由请求头中的Content-Length表示,如果大小大于给定的尺寸,客户端将会收到一条“Request Entity Too Large(413)”的错误,如果你要通过POST方法上传文件,可以将该值设大一些。
large_client_header_buffers 2 1k:为客户端请求中较大的消息头指定的缓存最大数量和大小,默认情况下,一个缓冲区的大小等于页面的大小,根据平台的不同可能是4K或8K,如果在请求连接的末尾状态转换为保持活动(keep-alive),这些缓冲区就被释放,2×1K将可以接收2KB数据的URI,这样有助于打击机器人攻击和DoS攻击。
另外,你还需要控制超时时间,提高服务器性能,降低客户端的等待时间,做如下修改:
## Start: Timeouts
##  client_body_timeout  
10;  client_header_timeout 10;

keepalive_timeout    
5 5;  send_timeout         
10;
## End: Timeouts ##
client_body_timeout 10:设置客户端请求主体读取超时时间,如果在这个时间后客户端还没有发送任何数据,Nginx返回“Request time out(408)”错误,默认值是60。
client_header_timeout 10:设置客户端请求头读取超时时间,如果在这个时间后客户端还没有发送任何数据,Nginx返回“Request time out(408)”错误。
keepalive_timeout 5 5:第一个参数指定客户端连接保持活动的超时时间,在这个时间之后,服务器会关掉连接,第二个参数是可选的,它指定了消息头保持活动的有效时间,即响应中的timeout=time,它可以告诉某些浏览器关闭连接,因此服务器就不必关闭连接了,如果没有这个参数,Nginx不会发送Keep-Alive头。
send_timeout 10:指定响应客户端的超时时间,这个超时仅限于两个阅读活动之间的时间,如果这个时间后客户端没有任何活动,Nginx将会关闭连接。
9、控制并发连接
你可以使用NginxHttpLimitZone模块限制指定会话,或某个IP的并发连接数,编辑nginx.conf:
### Directive describes the zone, in which the session states are stored i.e. store in slimits.
### 1m can handle 32000 sessions with 32 bytes/session, set to 5m x 32000 session ###

limit_zone slimits $binary_remote_addr 5m;

### Control maximum number of simultaneous connections for one session i.e. ###
### restricts the amount of connections from a single ip address ###

limit_conn slimits 5;
上述设置可以限制远程客户端每IP地址不能超过5个同时打开的连接。
10、只允许访问指定的域名
如果有机器人程序在随机扫描所有域,那就阻止它访问,你必须配置只允许虚拟域或反向代理请求。
## Only requests to our Host are allowed i.e. nixcraft.in, images.nixcraft.in and www.nixcraft.in  
if ($host !~ ^(nixcraft.in|www.nixcraft.in|images.nixcraft.in)$ ) {        
return 444;      }
##
11、限制可用的方法
GET和POST是互联网上最常用的方法,RFC 2616定义了Web服务器可用的方法,如果一个Web服务器不要求实现所有方法,那些方法就应该被禁止掉,下面的代码将过滤所有方法,只允许GET,HEAD和POST方法:
## Only allow these request methods ##    
if ($request_method !~ ^(GET|HEAD|POST)$ ) {        
return 444;     }
## Do not accept DELETE, SEARCH and other methods ##
关于HTTP方法的更多信息:
GET方法用于请求文档,如http://www.cyberciti.biz/index.php
HEAD方法与GET相同,但服务器不会在响应中只返回消息主体。
POST方法功能就多了,如通过表单存储或更新数据,订购一个产品,发送电子邮件等,通常使用服务器端脚本(如PHP,Perl,Python等)处理,如果你要上传文件或在服务器上处理表单就必须用它。
12a、如何阻止某些用户代理(User-Agents)?
你可以轻松阻止用户代理,如扫描器,机器人和垃圾邮件,它们可能会滥用你的服务器。
## Block download agents ##    
if ($http_user_agent ~* LWP::Simple|BBBike|wget) {        
return 403;     }
##
阻止msnbot和scrapbot机器人:
## Block some robots ##    
if ($http_user_agent ~* msnbot|scrapbot) {          
return 403;     }
12b、如何阻止被提名的垃圾邮件
被提名的垃圾邮件都很危险,它们可能会损害你的SEO排名,可以使用下面的代码阻止访问垃圾邮件发送者:
## Deny certain Referers ###  
if ( $http_referer ~* (babes|forsale|girl|jewelry|love|nudit|organic|poker|porn|sex|teen) )    
{       
# return 404;       
return 403;     }
##
13、如何停止图片热链
图片或HTML热链是指有人在他们的网站上引用了你网站的图片,你必须为其它网站的流量支付贷款费用,有点象是网站劫持,通常这种情况发生在博
客和论坛中,我强烈建议你在服务器级停止并阻止图片热链。
# Stop deep linking or hot linking
location /images/ {  valid_referers none blocked www.example.com example.com;  
if ($invalid_referer) {    
return   403;   }
}
例子:重写并显示禁令图片:
valid_referers blocked www.example.com example.com;
if ($invalid_referer) { rewrite ^/images/uploads.*\.(gif|jpg|jpeg|png)$
http://www.examples.com/banned.jpg last
}
另外,请参考“How-to:使用Nginx映射阻止图片热链”(http://nginx.org/pipermail/nginx/2007-June/001082.html)。
14、目录限制
你可以为特定目录设置访问控制,所有网页目录都应配置为按需访问。
通过IP地址限制访问,你可以限制访问/docs/目录的IP地址:
location /docs/ {  ## block one workstation  deny    192.168.1.1;
## allow anyone in 192.168.1.0/24  allow   192.168.1.0/24;
## drop rest of the world  deny    all;
}
通过密码保护目录,首先创建一个密码文件,再添加一个用户vivek:
# mkdir /usr/local/nginx/conf/.htpasswd/
# htpasswd -c /usr/local/nginx/conf/.htpasswd/passwd vivek
编辑nginx.conf添加需要保护的目录:
### Password Protect /personal-images/ and /delta/ directories ###
location ~ /(personal-images/.*|delta/.*) {  auth_basic  “Restricted”; 
auth_basic_user_file   /usr/local/nginx/conf/.htpasswd/passwd;
}
创建好密码文件后,后面的用户可以使用下面的命令进行追加:
# htpasswd -s /usr/local/nginx/conf/.htpasswd/passwd userName

15、Nginx SSL配置
HTTP是一个纯文本协议,很容易被窃听,你应该使用SSL加密传输的信息。
首先需要创建一个SSL证书,输入下面的命令:
# cd /usr/local/nginx/conf
# openssl genrsa -des3 -out server.key 1024
# openssl req -new -key server.key -out server.csr
# cp server.key server.key.org
# openssl rsa -in server.key.org -out server.key
# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
编辑nginx.conf,找到对应位置,做如下修改:
server {   
server_name example.com;   
listen 443;   
ssl on;   
ssl_certificate /usr/local/nginx/conf/server.crt;   
ssl_certificate_key /usr/local/nginx/conf/server.key;   
access_log /usr/local/nginx/logs/ssl.access.log;   
error_log /usr/local/nginx/logs/ssl.error.log;
重启Nginx:
# /usr/local/nginx/sbin/nginx -s reload
另外,请参考Nginx SSL文档(http://wiki.nginx.org/NginxHttpSslModule)。
16、Nginx和PHP安全技巧
PHP是流行的服务器端脚本语言,对/etc/php.ini做如下修改:
# 禁用危险的函数
disable_functions = phpinfo, system, mail, exec
## 限制资源  ##
# 每个脚本的最大执行时间,单位秒
max_execution_time = 30
# 每个脚本解析请求数据的最大时间
max_input_time = 60
# 每个脚本可以消耗的最大内存(8MB)
memory_limit = 8M
# PHP要接收的POST数据最大大小
post_max_size = 8M
# 是否允许HTTP文件上传
file_uploads = Off
# 允许上传的最大文件大小
upload_max_filesize = 2M
# 不将PHP错误消息暴露给外部用户
display_errors = Off
# 启用安全模式
safe_mode = On
# 只允许访问隔离目录中的可执行文件
safe_mode_exec_dir = php-required-executables-path
# 限制外部访问PHP资源
safe_mode_allowed_env_vars = PHP_
# 限制泄露PHP信息
expose_php = Off
# 记录所有错误
log_errors = On
# 不为输入数据注册全局
register_globals = Off
# 最小化允许的php post大小
post_max_size = 1K
# 确保PHP重定向正确
cgi.force_redirect = 0
# 禁止上传,除非必要
file_uploads = Off
# 启用SQL安全模式
sql.safe_mode = On
# 避免打开远程文件
allow_url_fopen = Off
另外,请参考“PHP安全:限制脚本使用的资源”(http://www.cyberciti.biz/faq/php-resources-limits/),“PHP.INI:禁用exec,shell_exec,system,popen和其它功能提高安全”(http://www.cyberciti.biz/faq/linux-unix-apache-lighttpd-phpini-disable-functions/)。
17、尽可能在Chroot Jail(容器)中运行Nginx
将Nginx放入Chroot Jail可以最大限度地减少被攻击的危险,它将Web服务器隔离到文件系统的专用区域,注意你不能使用传统的chroot方法设置Nginx,但你可以使用FreeBSD jails,Xen或OpenVZ虚拟化,它们也使用了容器的概念。
18、在防火墙级限制每个IP的连接
Web服务器必须时刻关注连接和每秒的连接限制,pf和iptables都可以在访问Nginx服务器之前卡住最终用户。
Linux iptables:每秒卡住的Nginx连接
下面的例子表示如果某个IP在60秒尝试连接到80端口的次数超过了15,iptables将会丢掉来自它的入站连接:
/sbin/iptables -A INPUT -p tcp –dport 80 -i eth0 -m state –state NEW -m recent –set
/sbin/iptables -A INPUT -p tcp –dport 80 -i eth0 -m state –state NEW -m recent
–update –seconds 60  –hitcount 15 -j DROP
service iptables save
BSD PF:每秒卡住的Nginx连接
编辑/etc/pf.conf,做如下更新,下面的命令限制了每个来源的最大连接数为100,15/5指定某时间跨度内的连接数限制,这里就是5秒内的最大连接数为
15,如果有人违背这条规则,将被加入到abusive_ips表,那么他以后就不能再连接了。最后刷新所有状态。
ebserver_ip=”202.54.1.1″
table  persist
block in quick from
pass in on $ext_if proto tcp to $webserver_ip port www flags S/SA keep state (max-src-conn 100,
max-src-conn-rate 15/5, overload  flush)
请根据你的需要和通信流量调整所有的值(浏览器可能会打开多个连接)。
另外,请参考“PF防火墙脚本示例”(http://bash.cyberciti.biz/firewall/pf-firewall-script/),“iptables防火墙脚本示例”(http://bash.cyberciti.biz/firewall/linux-iptables-firewall-shell-script-for-standalone-server/)。
19、配置操作系统保护Web服务器
除了开启SELinux外,还要给/nginx目录设置正确的权限,运行Nginx的系统用户名是nginx,但在DocumentRoot(/nginx或/usr/local/nginx/html)中的文件不应该
属于该用户,他也不能进行修改。使用下面的命令找出权限设置不当的文件:
# find /nginx -user nginx
# find /usr/local/nginx/html -user nginx
请确保将文件的所有者修改为root或其它用户,一个典型的权限设置如下:
# ls -l /usr/local/nginx/html/
输出示例:
-rw-r–r– 1 root root 925 Jan  3 00:50 error4xx.html
-rw-r–r– 1 root root  52 Jan  3 10:00 error5xx.html
-rw-r–r– 1 root root 134 Jan  3 00:52 index.html
另外,你必须删除由vi或其它文本编辑器创建的不必要的备份文件:
# find /nginx -name ‘.?*’ -not -name .ht* -or -name ‘*~’ -or -name ‘*.bak*’ -or -name ‘*.old*’
# find /usr/local/nginx/html/ -name ‘.?*’ -not -name .ht* -or -name ‘*~’
-or -name ‘*.bak*’ -or -name ‘*.old*’
给find命令传递-delete参数,它就会自动删除这些文件。
20、限制出站Nginx连接
攻击者可能要在你的Web服务器上使用如wget等工具下载文件,使用iptables阻止来自Nginx用户的出站连接,ipt_owner模块可以匹配各种包创建者的特征,只有在OUTPUT链中的才有效,在这里,允许vivek用户使用80端口连接外部资源(对RHN访问或通过仓库抓取CentOS更新特别有用)。
/sbin/iptables -A OUTPUT -o eth0 -m owner –uid-owner vivek -p tcp
–dport 80 -m state –state NEW,ESTABLISHED  -j ACCEPT
将上述规则添加到你的iptables基础shell脚本中,不允许nginx Web服务器用户连接外部资源。
附送技巧:观察日志和审核
检查日志文件,可以从中找到攻击者的一些行踪和攻击手段。
# grep “/login.php??” /usr/local/nginx/logs/access_log
# grep “…etc/passwd” /usr/local/nginx/logs/access_log
# egrep -i “denied|error|warn” /usr/local/nginx/logs/error_log
Auditd服务提供了系统审核功能,启动SELinux事件,认证事件,文件修改,帐户修改等的审核服务,象往常一样首先关闭所有服务,然后打开我在“Linux服务器加固”(http://www.cyberciti.biz/tips/linux-security.html)一文中指出的服务。
总结
通过这些设置,你的Nginx服务器就可以对外提供服务了,但你应该根据应用程序安全需要进一步查看另外的资源。例如,WordPress或其它第三方程序都有其自身的安全要求。

No Comments

php实现验证码的识别(中级篇) 收藏

恩。。写的不错

 php实现验证码的识别(中级篇) 收藏
 在上篇文章 <php实现验证码的识别 (初级篇 ) > 中,讲了如何识别简单的验证,这里的简单只的是验证码有数字和字母组成,格式统一,每次出现位置固定。这篇文章将继续深入研究识别验证码,这次识别的目标是,验证码有字符和数字组成,验证码存在旋转(可能左右都旋转),位置不固定,存在字符与字符之间的粘连,且验证码有更强的干扰素。这篇文章讲解的方法,并不是万能的解决方案,并且提供代码不能直接解决你的问题,这里仅仅是方法,具体需求读者自己解决,需要说明的是,识别验证码与具体的编程语言无关,这里只是使用 php 语言实现,使用这里介绍的方法,你可以使用任何语言实现。

 这篇文章逐步讲解识别验证码过程中的各个步骤。

 
 如上图,随后的讲解我们都围绕此图展开。
 一:拿到一个验证码的,第一眼我们首先要做的工作是,二值化。把验证码的部分用 1 表示,背景部分用 0 表示出来,识别方法很简单,我们打印出验证码正张图片的 RGB ,然后分析其规律即可,通过 RGB 码,我们很容易分辨出上面这张图片的 R 值大于 120 , G 和 B 的值小于 80 ,所以依据这个规则我们很容易把上面的图片二值化。再看初级篇中识别的两张图

 
  刚看上去,感觉很复杂。验证码的图片每次背景色都不相同,且不是单色,各个验证码数字的颜色每次也各不相同。貌似很难二值化,其实我们打印出其 RGB 值很容易就发现。无论验证数字颜色如何变化,该数字的 RGB 值总有一个值小于 125 ,所以通过如下判断

$rgbarray['red'] < 125 || $rgbarray['green']<125|| $rgbarray['blue'] < 125

我们就很容易分辨出哪里是数字,哪里是背景。

  我们能够找到这些规律的因素是,在制作验证码的干扰素时,为了使干扰素不影响数字的显示效果,必须使用干扰素的 RGB 和数字 RGB 相互独立,互不干扰。只要懂得这个规律,我们就很容易实现二值化。

  我们找到的 120 , 80 , 125 等阈值,可能和实际的 RGB 有出入,所以,有时二值化后,会有部分地方出现 1 ,对于验证码上固定位置显示数字,这种干扰没有太大意义。但是对于验证码位置不确定的图片来说,在我们切割字符时,很可能造成干扰。所以,在二值化后要进行去噪出来。

  二:接下来我们进行第二个步骤,出噪。出燥的原理很简单,就是把孤立的有效的值去掉,如果噪点比较高,要求的效率也比较高的话,这里面也有很多工作要做。幸好这里我们不要求这么高深,我们使用最简单的方法就可以,如果一个点为 1 则判断这个点的上下左右上左上右下左下右 8 个方位上数字是否为 1 ,如果不为 1 ,就认为是一个燥点,直接设置为 1 即可。

 
如上图所示,我们使用此方法很容易发现红色方框部分的 1 为燥点,直接设置为 1 即可。

在判断时我们使用了一个技巧,有时候的噪点可能是两个连续的 1 ,所以我们

+ expand sourceview plaincopy to clipboardprint?
$num = 0;  
if($data[$i][$j] == 1)  
{  
    // 上  
    if(isset($data[$i-1][$j])){  
        $num = $num + $data[$i-1][$j];  
    }  
    // 下  
    if(isset($data[$i+1][$j])){  
        $num = $num + $data[$i+1][$j];  
    }  
    // 左  
    if(isset($data[$i][$j-1])){  
        $num = $num + $data[$i][$j-1];  
    }  
    // 右  
    if(isset($data[$i][$j+1])){  
        $num = $num + $data[$i][$j+1];  
    }  
    // 上左  
    if(isset($data[$i-1][$j-1])){  
        $num = $num + $data[$i-1][$j-1];  
    }  
    // 上右  
    if(isset($data[$i-1][$j+1])){  
        $num = $num + $data[$i-1][$j+1];  
    }  
    // 下左  
    if(isset($data[$i+1][$j-1])){  
        $num = $num + $data[$i+1][$j-1];  
    }  
    // 下右  
    if(isset($data[$i+1][$j+1])){  
        $num = $num + $data[$i+1][$j+1];  
    }  
}  
if($num == 0){  
    $data[$i][$j] = 0;  

$num = 0;
if($data[$i][$j] == 1)
{
 // 上
 if(isset($data[$i-1][$j])){
  $num = $num + $data[$i-1][$j];
 }
 // 下
 if(isset($data[$i+1][$j])){
  $num = $num + $data[$i+1][$j];
 }
 // 左
 if(isset($data[$i][$j-1])){
  $num = $num + $data[$i][$j-1];
 }
 // 右
 if(isset($data[$i][$j+1])){
  $num = $num + $data[$i][$j+1];
 }
 // 上左
 if(isset($data[$i-1][$j-1])){
  $num = $num + $data[$i-1][$j-1];
 }
 // 上右
 if(isset($data[$i-1][$j+1])){
  $num = $num + $data[$i-1][$j+1];
 }
 // 下左
 if(isset($data[$i+1][$j-1])){
  $num = $num + $data[$i+1][$j-1];
 }
 // 下右
 if(isset($data[$i+1][$j+1])){
  $num = $num + $data[$i+1][$j+1];
 }
}
if($num == 0){
 $data[$i][$j] = 0;
}
我们计算这个点的 8 个方向上的值之和,最后我们判断他们的和是否小于特定的阈值
 三:经过去噪后,我们就得到干净的二值化的数据,接下来要做的就是切割字符了。切割字符的方法有很多种,这里我采用最简单的一种,先垂直方向切割成为字符,然后在水平方向去掉多于的 0000 ,如下图

 
第一步切割红线部分,第二步切割蓝线部分,这样就可以得到独立的字符了。但是像下面这种情况

 
按上面的方法会把 dw 字符切割成一个字符,这是错误的切割,所以这里我们涉及到粘连字符的切割。
 四:粘连字符切割,制作验证码时,规则字符的粘连很容易分割开,如果字符本身有缩放,变形就很难处理,经过分析,我们可以发现,上面的字符粘连属于很简单的方式,只是规则字符的粘连,所以处理这种情况,我们也使用很简单的处理方式。当完成分割操作后,我们不能马上确定分割的部分就为一个字符,要进行验证,验证的关键因素就是,切割下来的字符的宽是否大于阈值,这个阈值的取舍标准是,一个字符无论怎么旋转变形都不会大于这个阈值,所以,如果我们切割的块大于这个阈值,就可以认为这是一个粘连字符;如果大于两个阈值之和,就认为是三个字符粘连,以此类推。知道这个规则后,切割粘连字符也就很简单了。如果我们发现是粘连字符块,直接平分这个块为两个或者多个新的块就可以。当然为了更好的还原字符,我一般都采用平分 +1 , -1 对字符块的部分进行适当的补充。
 五:经过上面四个步骤,我们就可以提取出比较纯的字符块了,接下来要做就是匹配字符了。对于旋转字符的特征码建立,有很多种方法,这里就不做深入研究了。我这里使用的最简单的方式,为所有字符的所有情况建立匹配库,所以在我提供的代码种增加了 study 操作,其目的就是,先有人手工识别图片的验证码,然后通过 study 方法,写入特征码库。这样写入的图片数据越多,验证识别的准确行也就越高。
 好了,经过以上步骤,我们基本上可以识别现在互联网上大部分的验证码,这里我们都是使用的最简单的方法,没有使用任何 OCR 知识。这些方法,应该属于非 OCR 领域的顶峰了,要想识别更加复杂的验证码,那就需要更多的 OCR 知识了。有机会的话,我会在高级篇中一一做介绍。
 下面是一些容易识别的验证码,希望引起网站管理者的重视。

 
制作验证码的一些建议
 对于识别验证码的程序来说,最难得部分是验证字符的切割和特征码的建立,而国内很多程序员只做验证码时,总是喜欢在验证码加很多干扰素,干扰线,影响效果不说,还达不到很好的效果;所以,要想使自己验证码难于本识别,只做下面两点就够了
1 :字符粘连,最好所有的字符都有粘连的部分;
2 :不要使用规格字符,验证码的各个部分使用不同比例的缩放或者旋转。
只要做到这两点,或者这两点的变形,识别程序就很难识别。我们看看, yahoo 和 google 的验证码就知道,白字黑底,却很难被识别。

Goole:

yahoo:

源文件下:点击下载

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ugg/archive/2009/03/09/3972368.aspx

No Comments

varnish 设置header和forward IP设置问题

What is the purpose of the X-Varnish HTTP header?

The X-Varnish HTTP header allows you to find the correct log-entries for the transaction. For a cache hit, X-Varnish will contain both the ID of the current request and the ID of the request that populated the cache. It makes debugging Varnish a lot easier.

Does Varnish support compression?

This is a simple question with a complicated answer; see WIKI.

How do I add a HTTP header?

To add a HTTP header, unless you want to add something about the client/request, it is best done in vcl_fetch as this means it will only be processed every time the object is fetched:

sub vcl_fetch {
  # Add a unique header containing the cache servers IP address:
  remove obj.http.X-Varnish-IP;
  set    obj.http.X-Varnish-IP = server.ip;
  # Another header:
  set    obj.http.Foo = "bar";
}

How can I log the client IP address on the backend?

All I see is the IP address of the varnish server. How can I log the client IP address?

We will need to add the IP address to a header used for the backend request, and configure the backend to log the content of this header instead of the address of the connecting client (which is the varnish server).

Varnish configuration:

sub vcl_recv {
  # Add a unique header containing the client address
  remove req.http.X-Forwarded-For;
  set    req.http.X-Forwarded-For = client.ip;
  # [...]
}

For the apache configuration, we copy the “combined” log format to a new one we call “varnishcombined”, for instance, and change the client IP field to use the content of the variable we set in the varnish configuration:

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" varnishcombined

And so, in our virtualhost, you need to specify this format instead of “combined” (or “common”, or whatever else you use):

<VirtualHost *:80>
  ServerName www.example.com
  # [...]
  CustomLog /var/log/apache2/www.example.com/access.log varnishcombined
  # [...]
</VirtualHost>

No Comments

php网页抓取+分析


function getAbsolutePath($path,$host,$prefix='') {
                if(strtolower(substr($path,0,7))=='http://') return $path;
        $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
        $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); //过滤掉空。。
        $absolutes = array();
        foreach ($parts as $part) {
            if ('.' == $part) continue;
            if ('..' == $part) {
                array_pop($absolutes);
            } else {
                $absolutes[] = $part;
            }
        }
        $end='';
        if(strlen($path)>1 && substr($path,-1)==DIRECTORY_SEPARATOR) $end=DIRECTORY_SEPARATOR;  //strlen $path >1 是为了针对传入一个/的情况,会返回//,所以限制一下!
        if(strlen($path)>1 && substr($path,1)==DIRECTORY_SEPARATOR) $prefix=''; //不算前缀
        return $host.$prefix.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $absolutes).$end;
}
function getNodes($node,$type='a',$uri)
{
        $GLOBALS['node_tmp']=array();
        _callBackGetNode($node,$type,$uri);
        $tmp=$GLOBALS['node_tmp'];
        unset($GLOBALS['node_tmp']);
        return $tmp;
}
function _callBackGetNode($node,$type,$uri) {
        if($node->name ==$type  ) {
                $n=$type=='a'?strtolower($node->attribute['href']):strtolower($node->attribute['src']);
                $n=getAbsolutePath($n,$uri['scheme'].'://'.$uri['host'],$uri['path']);
                $GLOBALS['node_tmp'][$n]=1;
        }
        if(is_object($node) && $node->hasChildren()) {
                foreach($node->child as $child) {
                        _callBackGetNode($child,$type,$uri);
                }
        }
}
function getHtml($url,$type='a') {
   $getUrl=parse_url($url);
   $html=tidy_parse_string(file_get_contents($url));
   return getNodes($html->html(),$type,$getUrl);
}

No Comments

/usr/bin/ld: cannot find -lltdl php安装错误,libmcrypt编译问题

/usr/bin/ld: cannot find -lltdl

/usr/bin/ld: cannot find -lltdl
安装php make的时候提示如下出错信息:
/usr/bin/ld: cannot find -lltdl
collect2: ld returned 1 exit status
make: *** [libphp5.la] Error 1
解决方法:这是因为找不到:   libltdl库文件

ldconfig -p |grep ltdl
#/software/libmcrypt-2.5.8/libltdl
#./configure  –enable-ltdl-install
#make
#make install
以下是原文:
Then I visited
http://mcrypt.hellug.gr/mcrypt/index.html
an learned that I have to install not only libmcrypt
but also mcrypt and libmhash.
Here is the to do:
libmcrytp:
———–
download libmcrypt-xxx.tar.gz
create the following directory:  / usr / local / libmcrypt
copy the libmcrypt-xxx.tar.gz into that directory and move to it
run the following shell (>) commands:  (‘xxx’ is the version number)
> gunzip -c libmcrypt-xxx.tar.gz | tar xf -
> cd libmcrypt-xxx
> ./configure –disable-posix-threads
> make
> make check  (note:  ‘make check’ is optional)
> make install
then (update your environment) add in / etc / profile the following path:
/usr/local/libmcrypt/libmcrypt-xxxx
(note:  as I run Red Hat 7.3 I  set the line   a f t e r   the if-part
(id -u = 0 …) with: pathmunge /usr/local / libm….)
and add in / etc / ld.so.conf the following path:  /usr/local/lib
then run ldconfig:
> ldconfig
now comes the important part:
> cd /usr/local/libmcrypt/libmcrypt-xxx/libltdl
> ./configure –enable-ltdl-install
> make
> make install
(maybe not needed:  I also added a link in / usr / bin: )
(> cd /usr/bin)
(> ln -s /usr/lib/libltdl.so.3.1.0 ltdl)

No Comments

linux一些常用指令

1.按内存从大到小排列进程:
ps -eo “%C : %p : %z : %a”|sort -k5 -nr

2.查看当前有哪些进程;查看进程打开的文件:
ps -A ;lsof -p PID

3.获取当前IP地址(从中学习grep,awk,cut的作用)
ifconfig eth0 |grep “inet addr:” |awk ‘{print $2}’|cut -c 6-

4.统计每个单词出现的频率,并排序
awk ‘{arr[$1]+=1 }END{for(i in arr){print arr”\t”i}}’ 文件名 | sort -rn

5.显示10条最常用的命令
sed -e “s/| /\n/g” ~/.bash_history | cut -d ‘ ‘ -f 1 | sort | uniq -c | sort -nr | head Read the rest of this entry »

No Comments

varnish 2.x启动指令和配置

Varnish 2.0.3 has just been released. This release contains multiple changes, amongst them:

  • Support for backend timeouts
  • Multiple fixes in how we process ESI
  • restart in vcl_hit is now supported
  • Documentation has been updated
  • Expiry processing is now more scalable
  • The default session workspace is now 16k instead of 8k
  • More graceful handling of too many headers from the client or the server.
  • More expressive purges

之前一直使用的还是2.0的一个trunk的R2860版本,因为只有这个版本我从1.1.2升级上来以后没有慢的问题⊙﹏⊙。但是看着新版却一直不能用,实在是让人心里痒。于是抱着死磕到底的态度,在检查了n+1遍配置文件和修改启动参数重启了n+1遍Varnish以后终于找到了问题的所在,即启动参数的-w这个上面。那么这个参数是干什么用的呢?

    -w int[,int[,int]]           # Number of worker threads
                                 #   -w <fixed_count>
                                 #   -w min,max
                                 #   -w min,max,timeout [default: -w2,500,300]

可以看出这个参数是控制每个进程的线程数的,1.1.2版本的时候这个参数我配置的是-w30000,51200,10,貌似到了2.0版以后这个最小启动的线程数不能设定过大,于是在进行了几次调试以后最终将参数定为了-w5,51200,30

软件列表
pcre-8.02.tar.gz
varnish-2.1.tar.gz

软件存放位置
/data/software

安装过程
# /usr/sbin/groupadd www -g 48
# /usr/sbin/useradd -u 48 -g www www
# mkdir -p /data/vcache
# chmod +w /data/vcache
# chown -R www:www /data/vcache
# mkdir -p /var/log/varnish
# chmod +w /var/log/varnish
# chown -R www:www /var/log/varnish
# cd /data/software/pkg
# tar zxvf ../pcre-8.02.tar.gz
# cd pcre-8.02
# ./configure
# make && make install
# cd ..
# tar zxvf ../varnish-2.1.tar.gz
# cd varnish-2.1
# export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
# ./configure –prefix=/usr/local/varnish
# make && make install

编辑Varnish配置文件
# vi /usr/local/varnish/vcl.conf

backend webserver {
       set backend.host = “10.10.10.8″;
       set backend.port = “80″;
}

acl purge {
       “localhost”;
       “127.0.0.1″;
       “10.10.10.0″/24;
}

sub vcl_recv {
        remove req.http.X-Forwarded-For;
        set    req.http.X-Forwarded-For = client.ip;
        if (req.request == “PURGE”) {
               if (!client.ip ~ purge) {
                       error 405 “Not allowed.”;
               }
               lookup;
       }

       if (req.http.host ~ “(a|b|c).test.com”) {
               set req.backend = webserver;
              if (req.url ~ “\.(png|gif|jpg|swf|css|js)$”) {
                       lookup;
        }
               else {
                       pass;
               }
       }

       else {
               error 404 “Test Cache Server”;
               pipe;
       }
}

sub vcl_hash {
    set req.hash += req.url;
    if (req.http.host) {
        set req.hash += req.http.host;
    } else {
        set req.hash += server.ip;
    }
    hash;
}

sub vcl_pipe {
        set req.http.connection = “close”;
        #pipe;
}

sub vcl_hit {
        if (!obj.cacheable) {
                pass;
        }
       if (req.request == “PURGE”) {
               set obj.ttl = 0s;
               error 200 “Purged.”;
       }
        deliver;
}

sub vcl_miss {
       if (req.request == “PURGE”) {
               error 404 “Not in cache.”;
       }
}

sub vcl_fetch {
               set obj.ttl = 180s;
               #set    obj.http.X-Varnish-IP = server.ip;
               set    obj.http.Varnish = “Tested by Kevin”;
}

启动Varnish
# /usr/local/varnish/sbin/varnishd -n /data/vcache -f /usr/local/varnish/etc/varnish/default.vcl -a 10.10.10.8:80 -s file,/data/vcache/varnish_cache.data,50G -u www -w2,65536,60 -T 127.0.0.1:3600 -p thread_pool_min=200 -p thread_pool_max=4000 -p thread_pools=4 -p thread_pool_add_delay=2 -p listen_depth=4096 -p lru_interval=20

启动日志记录
#/usr/local/varnish/bin/varnishncsa -n /data/vcache -w /var/log/varnish/varnish.log &

No Comments

php-fpm vs nginx vs unix socket

nginx修改
fastcgi_pass unix:/tmp/nginx.socket;

php-fpm.conf修改
listen = /tmp/nginx.socket
重启php-fpm 重启nginx 搞定。。不过不知道能有多大改善。。难说。。。。

No Comments

php-fpm vs php 5.3+ 启动指令

php-5.3.2/sapi/fpm/init.d.php-fpm

这个东西在源始代码里面,copy出来用就可以了,这个脚本可以start stop reload fpm就和5.2+的那个脚本一样了!

No Comments

编译php-fpm cgi模式

./configure –prefix=/usr/local/php –with-mysqli=/usr/local/mysql/bin/mysql_config –with-mysql=/usr/local/mysql –with-config-file-path=/usr/local/php/etc –with-curl –with-zlib –enable-ftp  –with-mcrypt –enable-inline-optimization –disable-debug –with-gd –enable-gd-native-ttf –enable-gd-jis-conv –with-iconv-dir=/usr/local –with-freetype-dir=/usr/local/lib/freetype –with-jpeg-dir=/usr/local –with-png-dir=/usr/local –with-zlib –with-libxml-dir=/usr/local/lib/libxml –enable-xml –enable-exif –enable-soap –enable-zip –with-openssl –enable-mbstring –with-mhash –with-tidy –enable-fastcgi –enable-fpm –enable-force-cgi-redirect –enable-mbstring

make ZEND_EXTRA_LIBS=’-liconv’

make install

No Comments

libtidy 安装

  • wget http://tidy.sourceforge.net/src/old/tidy_src_051026.tgz
  • gunzip tidy-xxxx.tgz
  • tar -xvf tidy-xxxx.tar
  • cd tidy
  • sh build/gnuauto/setup.sh
  • 按照提示装就行了!

    No Comments

    Squid3.0与2.7的配置差异

    Squid3.0最重要的新特性:

    • Code converted to C++, with significant internal restructuring and rewrites.
    • ICAP implementation (RFC 3507 and www.icap-forum.org)
    • Edge Side Includes (ESI) implementation (www.esi.org)

    更多的请参考官方说明

    来说说配置方法的不同之处(只说我自己实际使用的)

    ./configure的配置项

    ./configure –prefix=/usr/local/squid –with-large-files –enable-useragent-log –enable-referer-log –enable-linux-netfilter –enable-x-accelerator-vary –disable-internal-dns –disable-mempools –with-maxfd=65535

    3.0版本去掉了以下选项:

    –enable-dlmalloc    不再需要 Read the rest of this entry »

    No Comments

    如何关闭Squid的Cache功能!

    1、编译的时候添加null的文件系统:
    –enable-storeio=null,(Others)

    2、在squid.conf设置:
    cache_dir null /tmp

    No Comments

    squid 多ip配置

    if (! ($tokens[1] =~ "127.0.0.1")) {

    11        push(@acls, "acl ip$count myip $tokens[1]\n");
    12        push(@tcps, "tcp_outgoing_address $tokens[1] ip$count\n");
    13        $count++;
    14     }

    看懂这个,打在squid.conf里面,就搞定了!!很简单

    No Comments

    关于多ip路由的例子

    http://uddtm.com/os/linux/duoxianluyoucelue.php

    No Comments

    在线的正则表达式检测

    http://www.regextester.com/

    No Comments

    php webdav模块安装

    /root/soft/phpmodel/dav/dav.c:21:23: error: ne_socket.h: No such file or directory
    /root/soft/phpmodel/dav/dav.c:22:24: error: ne_session.h: No such file or directory
    /root/soft/phpmodel/dav/dav.c:23:22: error: ne_utils.h: No such file or directory
    /root/soft/phpmodel/dav/dav.c:24:21: error: ne_auth.h: No such file or directory
    /root/soft/phpmodel/dav/dav.c:25:22: error: ne_basic.h: No such file or directory
    /root/soft/phpmodel/dav/dav.c:26:20: error: ne_207.h: No such file or directory

    http://www.webdav.org/neon/

    需要这个lib

    No Comments

    libmemcached 安装报错 解决

    clients/ms_conn.o: In function `ms_get_udp_request_id’:
    /root/soft/phpmodel/libmemcached-0.42/clients/ms_conn.c:194: undefined reference to `__sync_fetch_and_add_4′
    clients/ms_conn.o: In function `ms_reconn_socks’:
    /root/soft/phpmodel/libmemcached-0.42/clients/ms_conn.c:1051: undefined reference to `__sync_fetch_and_add_4′
    clients/ms_conn.o: In function `ms_reconn’:
    /root/soft/phpmodel/libmemcached-0.42/clients/ms_conn.c:919: undefined reference to `__sync_fetch_and_add_4′
    /root/soft/phpmodel/libmemcached-0.42/clients/ms_conn.c:956: undefined reference to `__sync_fetch_and_add_4′
    clients/ms_thread.o: In function `ms_setup_thread’:
    /root/soft/phpmodel/libmemcached-0.42/clients/ms_thread.c:225: undefined reference to `__sync_fetch_and_add_4′
    clients/ms_thread.o:/root/soft/phpmodel/libmemcached-0.42/clients/ms_thread.c:208: more undefined references to `__sync_fetch_and_add_4′ follow
    collect2: ld returned 1 exit status
    make[2]: *** [clients/memslap] Error 1
    make[2]: Leaving directory `/root/soft/phpmodel/libmemcached-0.42′
    make[1]: *** [all-recursive] Error 1
    make[1]: Leaving directory `/root/soft/phpmodel/libmemcached-0.42′
    make: *** [all] Error 2

    用这个语句

    ./configure -disable-64bit CFLAGS=”-O3 -march=i686″

    就可以了

    No Comments

    ss5架设

    yum -y install pam-devel openldap-devel cyrus-sasl-devel

    需要BerkeleyDb
    openladap
    ss5.tar.gz..

    一个一个编译完毕以后,修改/etc/opt/ss5.conf
    auth    0.0.0.0/0               -              u
    permit u 0.0.0.0/0 – 0.0.0.0/0 – - ulimit – -
    修改ss5.passwd文件加入
    username password
    添加ulimit 文件
    ulimit里面写上username

    mv /usr/lib/ss5/mod_socks4.so /usr/lib/ss5/mod_socks4.so.bk

    启动/etc/rc.d/init.d/ss5 restart

    No Comments