一些有用apache重写规则
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,NC]
# 将xxx.domain.com/yyy...重定向到www.domain.com/xxx/yyy..., xxx 5-20位, 字母开头只含字母, 数字以及"-"和"_"
RewriteCond %{SERVER_NAME} ^([a-z][a-z0-9\-\_]{4,19})\.domain\.com [NC]
RewriteRule ^(.+)$ %{SERVER_NAME}$1 [C]
RewriteRule ^([a-z][a-z0-9\-\_]{4,19})\.domain\.com(.*)$ http://www.domain.com/$1$2 [R=301,NC]
# 将首页www.domain.com转向www.domain.com/html/index.html
RewriteCond %{HTTP_HOST} www\.domain\.com
RewriteRule ^/$ http://www.domain.com/html/index.html [R=301,L]
lamp 相关配置 [Debian]
编译环境
Debian (Ubuntu)
apt-get install build-essential
apt-get install libncurses5-dev
sudo apt-get install libxml2-dev libcurl3-dev libpng-dev libmhash-dev libmcrypt-dev libxslt-dev libpspell-dev
Mysql编译安装参数
配置成功会提示:
MySQL has a Web site athttp://www.mysql.com/which carries details on the
latest release, upcoming features, and other information to make your
work or play with MySQL more productive. There you can also find
information about mailing lists for MySQL discussion.
Remember to check the platform. specific part of the reference manual for
hints about installing MySQL on your platform. Also have a look at the
files in the Docs directory.
Thank you for choosing MySQL!
make
make install
groupadd mysql //增加mysql组
useradd -g mysql mysql //增加mysql用户,这个用户属于mysql组
cd /usr/local/mysql
bin/mysql_install_db --user=mysql
chown -R root:mysql . //设置权限,注意后面有一个 "."
chown -R mysql /var/lib/mysql //设置 mysql 目录权限
chgrp -R mysql . //注意后面有一个 "."
cp share/mysql/my-medium.cnf /etc/my.cnf
cp share/mysql/mysql.server /etc/init.d/mysqld //开机自动启动 mysql。
chmod 755 /etc/init.d/mysqld
rcconf //开启启动服务设置
/etc/init.d/mysqld start //启动 MySQL
bin/mysqladmin -u root password "password_for_root"
查看mysql编译参数
cat /usr/local/mysql/bin/mysqlbug |grep ./configure
把 mysql 加入环境变量
export PATH="$PATH:/usr/local/mysql/bin"
apache 编译
./configure //配置源代码树
--prefix=/usr/local/apache2 //体系无关文件的顶级安装目录PREFIX ,也就Apache的安装目录。
--enable-module=so //打开 so 模块,so 模块是用来提 DSO 支持的 apache 核心模块
--enable-deflate=shared //支持网页压缩
--enable-expires=shared //支持 HTTP 控制
--enable-rewrite=shared //支持 URL 重写
--enable-cache //支持缓存
--enable-file-cache //支持文件缓存
--enable-mem-cache //支持记忆缓存
--enable-disk-cache //支持磁盘缓存
--enable-static-support //支持静态连接(默认为动态连接)
--enable-static-htpasswd //使用静态连接编译 htpasswd - 管理用于基本认证的用户文件
--enable-static-htdigest //使用静态连接编译 htdigest - 管理用于摘要认证的用户文件
--enable-static-rotatelogs //使用静态连接编译 rotatelogs - 滚动 Apache 日志的管道日志程序
--enable-static-logresolve //使用静态连接编译 logresolve - 解析 Apache 日志中的IP地址为主机名
--enable-static-htdbm //使用静态连接编译 htdbm - 操作 DBM 密码数据库
--enable-static-ab //使用静态连接编译 ab - Apache HTTP 服务器性能测试工具
--enable-static-checkgid //使用静态连接编译 checkgid
--disable-cgid //禁止用一个外部 CGI 守护进程执行CGI脚本
--disable-cgi //禁止编译 CGI 版本的 PHP
--disable-userdir //禁止用户从自己的主目录中提供页面
--with-mpm=worker // 让apache以worker方式运行
--enable-authn-dbm=shared // 对动态数据库进行操作。Rewrite时需要。
make
make install
建立一个符号连接:
ln -s /usr/local/apache2/bin/apachectl /etc/init.d/httpd
rcconf //加入自动启动
php 编译
CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" ./configure --prefix=/usr/local/php5 --with-mysql=/usr/local/mysql --with-gd --enable-calendar --with-zlib --with-curl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-apxs2=/usr/local/apache2/bin/apxs --with-openssl --enable-zend-multibyte --with-gettext --with-mcrypt --enable-exif --with-png-dir=/usr/local/lib --enable-ftp --with-mhash --with-libxml-dir=/usr/local/lib --with-xsl --with-pspell
配置完成提示
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
Thank you for using PHP.
make
make install
修改/usr/local/apache2/conf/httpd.conf,在AddType部分加入如下内容
AddType application/x-httpd-php .php
解决webalizer 汉化后图表中乱码的问题
代码如下(webalizer_patch.pl):
- #!/usr/bin/perl
- ###############################################################################
- # webalizer_patch.pl
- # 原作:http://mail.tses.tcc.edu.tw/nuke/sections.php?op=viewarticle&artid=139
- # 修改:陶然 <taoran@taoran.net>
- # 功能: 解决HTML汉化后图表中乱码的问题
- # 方法:图表中乱码改用英文显示就正常了
- # 需要修改: graphs.c output.c webalizer_lang.h lang.h
- #
- # tar zxf webalizer-2.01-10-src.tgz
- # cd webalizer-2.01-10
- # ./configure --with-language=simplified_chinese
- # perl /path/of/webalizer_patch.pl ./
- # make
- # make install
- #
- ###############################################################################
- $file_graph = "$ARGV[0]/graphs.c";
- if(!(-e $file_graph)) {
- print "error open $file_graph
- ";
- print "Usage:
- perl $0 DIR
- ";
- #DIR是源文件目录,比如 ./
- exit;
- }
- #把图表中的月份和注释改成英文
- @old_var = ("msg_h_hits",
- "msg_h_pages",
- "msg_h_visits",
- "msg_h_files",
- "msg_h_sites",
- "msg_h_xfer",
- "s_month");
- @new_var = ("msg_gh_hits",
- "msg_gh_pages",
- "msg_gh_visits",
- "msg_gh_files",
- "msg_gh_sites",
- "msg_gh_xfer",
- "s_gmonth");
- open(IN,"<$file_graph");
- @lines = <IN>;
- $size = @lines;
- close(IN);
- `mv $file_graph $file_graph.bak`;
- open(OUT,">$file_graph");
- for($i=0;$i<$size;$i++) {
- for($k=0;$k<@old_var;$k++) {
- if($lines[$i] =~ s/$old_var[$k]/$new_var[$k]/g) {
- print "$i $old_var[$k] -> $new_var[$k]
- ";
- print OUT "/* --- Modified --- */
- ";
- }
- }
- print OUT $lines[$i];
- }
- close(OUT);
- #把图表中的标题改成英文显示
- $file_output = "$ARGV[0]/output.c";
- open(IN,"<$file_output");
- @lines = <IN>;
- $size = @lines;
- close(IN);
- `mv $file_output $file_output.bak`;
- open(OUT,">$file_output");
- for($i=0;$i<$size;$i++) {
- if($lines[$i] =~ s/msg_hmth_du,l_month/"Daily Usage for",s_gmonth/xg) {
- print "$i patched
- ";
- print OUT "/* --- Modified --- */
- ";
- } elsif($lines[$i] =~ s/msg_hmth_hu,l_month/"Hourly Usage for",s_gmonth/xg) {
- print "$i patched
- ";
- print OUT "/* --- Modified --- */
- ";
- } elsif($lines[$i] =~ s/msg_ctry_use,l_month/"Usage by Country for",s_gmonth/xg) {
- print "$i patched
- ";
- print OUT "/* --- Modified --- */
- ";
- } elsif($lines[$i] =~ s/msg_main_us/"Usage summary for"/xg) {
- print "$i patched
- ";
- print OUT "/* --- Modified --- */
- ";
- }
- print OUT $lines[$i];
- }
- close(OUT);
- #加入新的函数给前面显示的程序用
- print "
- patch $ARGV[0]/webalizer_lang.h
- ";
- $file_lang = "$ARGV[0]/webalizer_lang.h";
- open(OUT,">>$file_lang");
- print OUT qq~
- /* --- start Add ---*/
- char *msg_gh_hits = "Hits";
- char *msg_gh_pages = "Pages";
- char *msg_gh_visits= "Visits";
- char *msg_gh_files = "Files";
- char *msg_gh_sites = "Sites";
- char *msg_gh_xfer = "KBytes";
- char *s_gmonth[12]={ "Jan", "Feb", "Mar",
- "Apr", "May", "Jun",
- "Jul", "Aug", "Sep",
- "Oct", "Nov", "Dec"};
- /* -------------End------------- */
- ~;
- #补充声明这些新加的函数
- print "
- patch $ARGV[0]/lang.h
- ";
- $file_lang = "$ARGV[0]/lang.h";
- open(OUT,">>$file_lang");
- print OUT qq~
- /* --- start Add ---*/
- extern char *msg_gh_hits ;
- extern char *msg_gh_pages ;
- extern char *msg_gh_visits;
- extern char *msg_gh_files ;
- extern char *msg_gh_sites ;
- extern char *msg_gh_xfer ;
- extern char *s_gmonth[12];
- /* -------------End------------- */
- ~;
- print "Done ^_^
- ";
- #############################################################################
apache下htaccess的Invalid command 'AuthUserFile'错误
使用了apache的 .htaccess做身份认证后, 无法访问
查看日志报错如下:
"Invalid command 'AuthUserFile', perhaps misspelled or defined by a module not included in the server configuration"
google 一下, 在apache配置文件里加入如下配置
LoadModule authz_owner_module /usr/lib/apache2/modules/mod_authz_owner.so
LoadModule authn_file_module /usr/lib/apache2/modules/mod_authn_file.so
ok,问题解决
Tags: apache, htaccess, 身份验证如何关闭time_wait连接
结合netstat和awk命令来统计网络连接数
From: http://hi.baidu.com/thinkinginlamp/blog/item/afbcab64b1ad81f3f6365453.html
netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,"\t",state[key]}'
会得到类似下面的结果,具体数字会有所不同:
LAST_ACK 1
SYN_RECV 14
ESTABLISHED 79
FIN_WAIT1 28
FIN_WAIT2 3
CLOSING 5
TIME_WAIT 1669
状态:描述
CLOSED:无连接是活动的或正在进行
LISTEN:服务器在等待进入呼叫
SYN_RECV:一个连接请求已经到达,等待确认
SYN_SENT:应用已经开始,打开一个连接
ESTABLISHED:正常数据传输状态
FIN_WAIT1:应用说它已经完成
FIN_WAIT2:另一边已同意释放
ITMED_WAIT:等待所有分组死掉
CLOSING:两边同时尝试关闭
TIME_WAIT:另一边已初始化一个释放
LAST_ACK:等待所有分组死掉
也就是说,这条命令可以把当前系统的网络连接状态分类汇总。
下面解释一下为啥要这样写:
一个简单的管道符连接了netstat和awk命令。
------------------------------------------------------------------
先来看看netstat:
netstat -n
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 123.123.123.123:80 234.234.234.234:12345 TIME_WAIT
你实际执行这条命令的时候,可能会得到成千上万条类似上面的记录,不过我们就拿其中的一条就足够了。
------------------------------------------------------------------
再来看看awk:
/^tcp/
滤出tcp开头的记录,屏蔽udp, socket等无关记录。
state[]
相当于定义了一个名叫state的数组
NF
表示记录的字段数,如上所示的记录,NF等于6
$NF
表示某个字段的值,如上所示的记录,$NF也就是$6,表示第6个字段的值,也就是TIME_WAIT
state[$NF]
表示数组元素的值,如上所示的记录,就是state[TIME_WAIT]状态的连接数
++state[$NF]
表示把某个数加一,如上所示的记录,就是把state[TIME_WAIT]状态的连接数加一
END
表示在最后阶段要执行的命令
for(key in state)
遍历数组
print key,"\t",state[key]
打印数组的键和值,中间用\t制表符分割,美化一下。
如发现系统存在大量TIME_WAIT状态的连接,通过调整内核参数解决,
vim /etc/sysctl.conf
编辑文件,加入以下内容:
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
然后执行 /sbin/sysctl -p 让参数生效。
net.ipv4.tcp_syncookies = 1 表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
net.ipv4.tcp_fin_timeout 修改系統默认的 TIMEOUT 时间
下面附上TIME_WAIT状态的意义:
客户端与服务器端建立TCP/IP连接后关闭SOCKET后,服务器端连接的端口
状态为TIME_WAIT
是不是所有执行主动关闭的socket都会进入TIME_WAIT状态呢?
有没有什么情况使主动关闭的socket直接进入CLOSED状态呢?
主动关闭的一方在发送最后一个 ack 后
就会进入 TIME_WAIT 状态 停留2MSL(max segment lifetime)时间
这个是TCP/IP必不可少的,也就是“解决”不了的。
也就是TCP/IP设计者本来是这么设计的
主要有两个原因
1。防止上一次连接中的包,迷路后重新出现,影响新连接
(经过2MSL,上一次连接中所有的重复包都会消失)
2。可靠的关闭TCP连接
在主动关闭方发送的最后一个 ack(fin) ,有可能丢失,这时被动方会重新发
fin, 如果这时主动方处于 CLOSED 状态 ,就会响应 rst 而不是 ack。所以
主动方要处于 TIME_WAIT 状态,而不能是 CLOSED 。
TIME_WAIT 并不会占用很大资源的,除非受到攻击。
还有,如果一方 send 或 recv 超时,就会直接进入 CLOSED 状态
继续阅读:
查看Apache并发请求数及其TCP连接状态
Linux 反 DDOS的几个设置
