星期五, 九月 21, 2012

WordPress/Nginx打开permalinks并安装Super Cache插件



WordPress的Super Cache插件通过缓存页面的方式极大提高服务器响应,降低服务器负载。Super Cache依赖permalinks功能的开启,而permalinks又依赖Apache服务器的mod_rewrite模块。因为原生不支持,所以在Nginx下安装Super Cache优化WordPress性能需要全程手动,是件很繁琐的事情。
首先和在Apache平台下一样,在后台设置,打开permalinks,安装并开启Super Cache插件,开启过程中需要修改wp-config.php文件,加入一句:
define('WP_CACHE', true);
在Apache下Wordpress开启permalinks时会生成一个.htaccess文件,自动或者手动写入rewrite规则,内容如下:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Nginx没有.htaccess文件,所以要通过在配置文件中写入重定向规则实现。在/etc/nginx下创建文件wp-super-cache文件,写入以下内容
        # enable search for precompressed files ending in .gz
 # nginx needs to be complied using –-with-http_gzip_static_module
 # for this to work, comment out if using nginx from aptitude
        gzip_static on;

 # if the requested file exists, return it immediately
        if (-f $request_filename) {
                break;
        }

        set $supercache_file '';
        set $supercache_uri $request_uri;

        if ($request_method = POST) {
                set $supercache_uri '';
        }

        # Using pretty permalinks, so bypass the cache for any query string
        if ($query_string) {
                set $supercache_uri '';
        }

        if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
                set $supercache_uri '';
        }

 # if we haven't bypassed the cache, specify our supercache file
        if ($supercache_uri ~ ^(.+)$) {
                set $supercache_file /wp-content/cache/supercache/$http_host/$1index.html;
        }

 # only rewrite to the supercache file if it actually exists
        if (-f $document_root$supercache_file) {
                rewrite ^(.*)$ $supercache_file break;
        }

 # all other requests go to Wordpress
        if (!-e $request_filename) {
                rewrite . /index.php last;
        }
这里假设wordpress的安装目录是/。然后打开文件/etc/nginx/site-enabled/default,在location /区块代码中加入include wp-super-cache;一句,加好的代码如下:
location / {
    #如果出现404,请检查此级或者更高一级server代码块中root标记是否正确
    #...other stuffs...
    include wp-super-cache;
}
以上工作完成了permalinks需要的路径重写,但是由于WordPress代码会检查mod_rewrite模块是否存在,如果不存在,permalinks访问路径中会包含index.php,比如原来访问/post-123,现在会变成/index.php/post-123,也就是不会完全开启permalinks。这会给Super Cache开启造成问题,所以需要修改代码,找一个开启的plugin,在其中源文件中加入代码:
add_filter( 'got_rewrite', '__return_true', 999 );
我是加在了wp-content/plugins/wp-super-cache/wp-cache.php的最后一行。做完这些后去WP后台删除已有的缓存文件,访问任何页面,打开页面源代码,检查是否存在Super Cache提示信息,信息看起来像下面这个样子:
<!-- Dynamic page generated in 0.156 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-09-21 06:09:29 -->
如果存在,恭喜你,Super Cahce已经在nginx平台上的WordPress中成功运行。Tip:在调试过程中,可以借助Super Cache的debug功能,很快发现问题。

星期四, 九月 20, 2012

不安装插件给WordPress编辑器添加按钮


WordPress自带的编辑器实在DT,想变一下文字背景颜色竟然都没有。装插件怕麻烦。于是Google之,发现可以用添加代码的方式给自带编辑器增加一排按钮。
要编辑的代码源文件在WordPress安装目录/wp-contet/themes/当前激活主题/functions.php。打开文件,在最后一行添加
function my_mce_buttons_3($buttons) { 
 /**
  * Add in a core button that's disabled by default
  */
 $buttons[] = 'sup';
 $buttons[] = 'sub';
 $buttons[] = 'backcolorpicker';

 return $buttons;
}
add_filter('mce_buttons_3', 'my_mce_buttons_3');
这样就给编辑器增加了第三行按钮,分别是上标、下标和背景颜色选择。

当然也可以添加其它按钮或者再添加第四行。全部支持的按钮代码在官网可以找到:http://www.tinymce.com/wiki.php/Buttons/controls

从Google博客搬家到Wordpress


一直都在用Google的blogger,觉得速度快,也很稳定,不过因为你懂的原因,访问是个大问题,有时候很郁闷。所以决定在其它地方找个WP替换之。本来挺简单的事,WP有个blogger importer plugin就可以完成,但是用的时候发现第一步google账户验证就过不去,估计是插件太久没更新用的接口过时所致。所以干脆手动。基本过程分三步:导出、转换、导入。

1. Blogger本身提供了数据导出功能,在管理界面下的Settings->Other->Blog tools下,导出文件为XML格式。

2. 为了能方便WP导入此XML数据,需要对数据格式进行转换,可以在网站:http://blogger2wordpress.appspot.com/ 做,上传blogger格式的文件,转换完成为Wordpress格式XML文件。需要注意的是因为Google App Engine自身的限制,此网站不支持2MB以上的文件转换。

3. 导入功能需要WordPress Importer插件支持,安装并激活Wordpress Importer插件。之后到Tools->Import下面导入,导入格式选择WordPress。

数据导入非常方便,但是有一点要说明,就是原贴内的图片和其它附件还会链接到原来的blogger网站,而且原贴内的链接目标也不会改变。如果希望完美,还需要手动一一修改,对大博来说,绝对不会是轻松的事情。

安装和使用pgadmin管理vps上的postgresql数据库

Postgresql管理和mysql稍有不同,尤其是开始的安装和设置。这里在debian 6上操作,默认的postgresql版本为8.4版。参考:http://wiki.debian.org/PostgreSql

安装:这个很简单,不过注意一下有些版本postgresql只有客户端,得额外安装server,有的名字是postgresql-server,我这个是postgresql-8.4
apt-get install postgresql-8.4 postgresql

设置:有关数据库运行参数配置在/etc/postgresql/8.4/main/postgresql.conf中,用户登陆相关在/etc/postgresql/8.4/main/pg_hba.conf中,一般默认就可以。
postgresql默认会添加本地用户postgres,数据库内置默认管理管理员用户也是postgres,使用本地用户postgres启动数据库客户端psql时,默认得到的就是管理员控制台,可以直接用sql语言操作数据库。不过默认情况下本地用户和数据库用户postgres都没有密码,不能直接登陆。要登陆数据库,需要在shell以root权限执行su postgres命令
su postgres     #root shell执行,不用密码切换到postgres用户
psql                 #登陆postgresql
\password       #更改当前用户(postgres)密码,之后可以通过网络连接
CREATE USER mypguser WITH PASSWORD 'mypguserpass';  #创建非管理员用户
CREATE DATABASE mypgdatabase;     #创建数据库
GRANT ALL PRIVILEGES ON DATABASE mypgdatabase to mypguser;   #给mypguser用户mypgdatabase数据库的管理权限
^D   #退出postgresql客户端

其它的使用跟mysql差不多,也可以用图形界面管理工具pgadmin管理,首先在官网下载:http://www.pgadmin.org/,安装并运行,打开菜单->File->Add Server,添加连接参数(postgres用户修改密码后也可以用它登陆管理)。如下图:



星期三, 九月 19, 2012

保存debian 6下的iptables设置

在debian 6下,可以用if-up脚本实现重启网络或者系统后iptables规则自动恢复。(以下操作需要root权限,有底纹为shell命令
首先确认iptables规则正确
iptables -L

保存规则
iptables-save > /etc/firewall.conf

创建if-up脚本以便重启网络恢复规则

echo "#!/bin/sh" > /etc/network/if-up.d/iptables 
echo "iptables-restore < /etc/firewall.conf" >> /etc/network/if-up.d/iptables 
chmod +x /etc/network/if-up.d/iptables 

星期二, 九月 18, 2012

用nginx/php-fpm/postgresql在低配VPS上建站

前贴减少小内存低配VPS上CentOS 6.2系统内存使用尝试优化LAMP服务器的内存使用,事实上,由于设计原因,apache和mysql都是耗内存大户,再优化在256MB以下的VPS上运行也不会很宽松。所以本篇尝试用nginx/php-fpm/postgresql的组合在低配VPS上建站。这里使用Debian 6系统。

以下操作需要root权限,有底纹的文字为代码或者配置文件内容
1. 添加必要的源
vi /etc/apt/sources.list
加入以下内容
deb http://packages.dotdeb.org stable all
导入dotdeb源密钥
wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | sudo apt-key add -
rm dotdeb.gpg
调整源优先级,减小第三方源对系统稳定性的影响(参考:调整第三方软件源优先级减小对系统影响
cat > /etc/apt/preferences.d/dotdeb-org << EOF
Package: *
Pin: origin packages.dotdeb.org
Pin-Priority: 200
EOF
为了安装dotdeb上的php-fpm及其依赖包,需要以下命令为php相关软件包调整优先级,参考:
http://howto.biapy.com/en/debian-gnu-linux/system/software/setup-the-dotdeb-apt-repository-on-debian
#获得php相关包列表
PACKAGES=$(command wget "http://packages.dotdeb.org/dists/squeeze/php5/binary-$(command dpkg --print-architecture)" \
    --quiet --output-document=- \
    | command grep "href=" | command grep -v "h1" | command grep -v "\.\./" \
    | command sed -e 's/^[^>]*>\([^_]*\)_.*$/\1/' | command tr "\n" " ")
PECL_PACKAGES=$(command wget "http://packages.dotdeb.org/dists/squeeze/php5-pecl/binary-$(command dpkg --print-architecture)" \
    --quiet --output-document=- \
    | command grep "href=" | command grep -v "h1" | command grep -v "\.\./" \
    | command sed -e 's/^[^>]*>\([^_]*\)_.*$/\1/' | command tr "\n" " ")
#设置php相关包优先级
command echo "Package: ${PACKAGES} \\
    ${PECL_PACKAGES}
Pin: origin packages.dotdeb.org
Pin-Priority: 500" \
    > '/etc/apt/preferences.d/dotdeb-org-php5'
更新源
apt-get update
apt-get upgrade

2. 安装需要的软件包
apt-get install nginx php5 php5-apc php5-fpm php5-pgsql postgresql

3. 配置nginx
编辑/etc/nginx/nginx.conf,调整性能和安全性
#设置工作进程数,根据cpu数或者内核数调整(可以用cat /proc/cpuinfo看到)
worker_processes 1;
#关闭nginx版本提示,提高安全性
server_tokens off;
#为除ie6以外的浏览器打开数据压缩
gzip on;
gzip_disable "msie6";
编辑/etc/nginx/sites-enabled/default为默认站点添加php支持
location ~ \.php$ {
        root /var/www;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_index index.php;
        include fastcgi_params;
}

4. 配置php
编辑/etc/php5/fpm/php.ini
cgi.fix_pathinfo=0
编辑/etc/php5/fpm/pool.d/www.conf
#确定监听地址和端口
listen = 127.0.0.1:9000
#根据需要调整进程数

pm = dynamic                                     #dynamic:自动调节子进程数(建议,性能稍好),ondemand:只在需要时启动子进程(更省内存)
pm.max_children = 5
pm.start_servers = 1                           #ondemand未用
pm.min_spare_servers = 1                  #ondemand未用
pm.max_spare_servers = 1                 #ondemand未用
pm.max_requests = 500

pm.process_idle_timeout = 120s;        #dynamic未用

5. 配置postgresql,适当减小内存使用,编辑/etc/postgresql/8.4/main/postgresql.conf
shared_buffers = 8MB
根据需要同时可以修改temp_buffers, work_mem, maintenance_work_mem, max_stack_depth

6. 替换/bin/sh为dash,而不使用bash,这样可以减少shell脚本执行时的资源占用
apt-get install dash
dpkg-reconfigure dash
cd /bin
#用ll /bin/sh确认/bin/sh现在指向,如果不是dash用下面两个命令手动修改
ln -sf dash sh
ln -sf dash sh.distrib

7. 关闭mail daemon,dns server等非必要的服务,用syslog-ng替换rsyslog,其它轻量级的软件包替换原来的软件包等。

做完这些步骤后一个openvz上的debian 6的服务器运行时内存占用约为30M,如下图

用Dropbox备份VPS上的网站数据

用Dropbox做网站备份是一个老话题。基本思路就是把网站数据集中到用户目录下~/Dropbox目录内,然后通过Dropbox同步备份到服务器,为了减小资源占用,一般用cron脚本在用户量小时自动打开Dropbox,备份完成后再关闭。

除非特别指明,下面的操作不需要root权限,有底纹的文字为代码
1. 安装Dropbox
访问dropbox网站
https://www.dropbox.com/install?os=lnx
下载并安装dropbox,以普通用户安装时,程序会被安装到用户目录内~/.dropbox

2. 调整目录结构,把网站数据放到用户目录内
#创建日志文件
mkdir ~/log
#创建并进入Dropbox目录
mkdir ~/Dropbox
cd ~/Dropbox
# 创建db目录,用来放数据库备份
mkdir db
# 创建html目录,放网页文件
mkdir html
# 创建conf目录,放网站设置相关文件(建议把网站设置放到/etc/httpd/conf.d中的单独文件)
mkdir conf

3. 创建备份脚本文件,假设用户名为bob,网站数据库为bob_db
下载dropbox控制脚本程序
cd ~
wget http://www.dropbox.com/download?dl=packages/dropbox.py
chmod +x dropbox.py

备份开始文件backup_start.sh
#!/bin/sh
exec 2>&1
exec &>> ~/log/backup.log
date
echo "backup db daily..."
mysqldump --user bob -pdbpassword bob|gzip > /home/box/db/bak/dbbak_`date +'%m%d%Y'`.sql.gz
echo "removing backups older than 3 days..., DB has versioning system, no more backups needed"
find /home/box/db/bak/ -name "dbbak_*.sql.gz" -mtime +3 -exec rm {} \;
echo "starting dropbox..."
/home/bob/dropbox.py start
以上脚本自动备份mysql数据库内容到db目录下,删除超过3天的备份(这是安全的,因为dropbox默认有版本系统,可以找回删除文件,这样可以省空间),并启动dropbox进行备份

为了节省资源,备份完成后用脚本关闭dropbox,编写脚本backup_stop.sh
#!/bin/sh
exec 2>&1
exec &>> ~/log/backup.log
/home/bob/dropbox.py stop
date
echo "dropbox stoped."

4. 添加crontab项,需要root权限
0 3 * * * bob /bin/sh /home/bob/backup_start.sh
0 5 * * * bob /bin/sh /home/bob/backup_stop.sh
凌晨3:00-5:00访问量应该是比较小的

Dropbox备份网站比较方便,但是资源占用较大,不适合低端vps用。而且dropbox网站本身数据并不加密,所以对数据安全性敏感的谨慎使用。

调整第三方软件源优先级减小对系统影响

使用redhat和debian的时候,经常需要安装一些第三方软件源的包。但是考虑到系统稳定性,有时候又不想安装这些源上其它软件的更新。这时,就需要通过源优先级调整来实现。

以下操作需要root权限,底纹文字为代码
1. CentOS
参考官方文档:http://wiki.centos.org/PackageManagement/Yum/Priorities
需要安装yum priority plugin
yum install yum-plugin-priorities
确认/etc/yum/pluginconf.d/priorities.conf文件中enable=1

Redhat的yum中数值越低,优先级越高,调整官方源到高优先级,编辑/etc/yum.repos.d/CentOS-Base.repo,根据官方文档,分别把base, addons, updates, extras, centosplus调整为1,即增加字段priority=1

给第三方源添加优先级99,即增加字段priority=99,这样在升级时,已经安装的包只从原有官方源升级,即使第三方源有更新的包。

2. Debian
官方文档:http://manpages.debian.net/cgi-bin/man.cgi?sektion=5&query=apt_preferences&apropos=0&manpath=sid&locale=en
Apt系统不需要安装新的软件包,其系统本来就内置优先级管理,直接设置即可,不过apt中数值越高的优先级越高。默认的官方源优先级好像是500,可以在官方文档确认一下。为了减小第三方包对系统的影响,直接设置新添加的包优先级即可。比如dotdeb是比较常用的第三方包,在source.list中添加源
deb http://packages.dotdeb.org stable all
添加文件/etc/apt/preferences.d/dotdeb-org指定给此源一个比较低的优先级200
cat > /etc/apt/preferences.d/dotdeb-org << EOF
Package: *
Pin: origin packages.dotdeb.org
Pin-Priority: 200
EOF

更详细的资料可以参照官方文档

星期三, 九月 12, 2012

避免弱口令,配置putty使用密钥认证登陆CentOS 6.2服务器,提高服务器安全性

几个月前的中文版putty后门事件和国内几大论坛密码泄漏事件可能让很多人至今记忆犹新。事实上,即使密码存放是安全可靠的,密码安全本身也不一定可靠,电脑处理速度越来越快,弱口令的破解也越来越简单,参照前贴:怎样构建安全的密码,如何安全地存储及使用密码。所以,选密码本身就是一个头疼的问题,密码太简单,容易被破解,密码很复杂,自己容易忘。尤其对站长来说,服务器密码丢失,损失将是巨大的。幸运的是,对于Linux服务器,我们可以选择用非对称密钥认证,整个过程私钥永远只在本地存贮,只要本地数据安全,服务器就是安全的。而密码本身只在需要root权限时使用,双重认证,很大程度上增加了服务器的安全。

有底纹的文字为配置文件内容或者shell命令
1. 准备工作
下载必需的软件,putty和puttygen。如果你有服务器,相信你已经有putty了,不过这里还是提醒一下,putty应该从官方网站下载,而且尽量使用英文原版,不要使用翻译的,中文版putty血淋淋的教训就在眼前,下载地址:
http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe
puttygen是用来生成密钥对的工具,同时它还可以在openssh和putty所有的密钥格式间进行转换,下载地址:
http://the.earth.li/~sgtatham/putty/latest/x86/puttygen.exe

2. 生成登陆密钥对
打开puttygen,点击下边第一个按钮Generate生成密钥,点击后,生成密钥时,可以在程序上边部空白面板处快速移动鼠标,这样能加快密钥生成。程序完成后如下图:
程序上半部分不可编辑文本框“Public key for pasting into OpenSSH authorized_keys file”里面就是需要用到的公钥,后面设置的时候会上传到服务器上。保存生成的公钥和私钥,点"Save public key",选择保存路径,命名为"pub.txt",注意是文本文件,为了下面设置时方便使用:点"Save private key"按钮,选择存放路径,命名为"pri.ppk"。

3. 上传公钥到服务器
用putty连接并登陆服务器,注意用你自己的普通用户登陆。运行如下命令
mkdir -p ~/.ssh
cat > ~/.ssh/authorized_keys
ssh-rsa <这里粘贴pub.txt的内容,跳过前两行和最后一行,只粘贴密钥本身,也就是第2步public key编辑框内的内容,确保密钥数据只有一行,删除puttygen添加的换行符>
EOF
接下来是必须步骤,设置权限,不能忽略
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

测试登陆,打开putty,按常规填入服务器地址。先不要连接,在左侧选择Connection->Data,在右侧对话框"Auto login username"中填入用户名(注意,是你自己刚才上传过密钥的普通用户名,不是root)。继续在左侧选择Connection->SSH->Auth,在右侧找到"Private key file for authentication",点Browse按钮,选择你刚才生成的密钥文件"pri.ppk". 如下图:

再check一下设置,服务器、用户名、私钥文件都无误,点击Open连接。如果登陆成功,祝贺你,密钥登陆已经工作了。到此你可以把上面的设置保存成一个session,下次登陆时直接选择session名就能连接,非常方便。到此安全设置还没结束,耐心继续。

以下工作需要root权限
4. 增加安全性,禁用密码登陆
接第3步,现在用su命令直接切换到root用户获得root shell。如果你已经logout,请重新用root用户登陆,原来怎么做现在就怎么做,系统登陆方式到现在还没有改变,旧的东西都还在。用vi或者其它编辑器打开文件/etc/ssh/sshd_config,找到PasswordAuthentication,改为
PasswordAuthentication no
注意,做完此项后,将只可以使用密钥方式登陆。如果你想直接登陆root用户,请按照3步骤给root生成并上传密钥

[建议]同时禁止root用户直接登陆,即使是密钥方式。方法是,找到PermitRootLogin字段,去掉注释,改为
PermitRootLogin no
重启服务器,或者sshd。
/etc/init.d/sshd restart
注意,做完此项后,root用户将无法登陆,即使给root用户上传密钥也无法登陆。唯一获得root shell的方法是使用su命令进行用户切换。

5. [可选]进一步的安全限制
做完第4步后,你就给自己的服务器上了双保险,唯一获得root权限的方法是同时取得普通用户的登陆私钥和root用户的密码。在以上的设置中,整个过程没有传输私钥,所以只要你本地密钥和root密码两个之一是安全的,你的服务器一般来讲就是安全的。
不过,如果你的服务器有多位用户,让多位用户同时持有root用户密码可能不是很好的安全选择,所以可以采用开启sudo选项给相关用户root权限。(需要root权限)
编辑/etc/sudoers文件,centos下不要直接用vi编辑此文件,用visudo命令,程序会自动处理相关权限和其它问题
visudo
找到"root    ALL=(ALL)       ALL"一行
在其下添加(假设bob是你的用户名)
bob    ALL=(ALL)       ALL
保存退出(ESC + :wq)

测试sudo功能,使用普通用户登陆(bob)
sudo -i
输入此用户(bob)的密码,如果成功获得root shell,那么sudo功能设置成功。以后有sudo权限的用户不需要root的密码,直接用自己的密码sudo -i命令即可获得root权限。更多sudo的用法请在网上查询。

[可选]禁用root用户。如果你的服务器上有用户只有普通权限,为了防止他们破解root口令获得root权限,你可以选择禁用root用户。方法是在root shell下运行命令
usermod -L root
注意:此项做完后将不能使用su命令切换用户获得root权限。所以,一定确保系统中存在可用的有sudo权限的普通用户,否则你将把自己锁在系统外!!!

结贴:网络安全永远不会过度。根据需要,建议读者至少将第4步设置做完。

减少小内存低配VPS上CentOS 6.2系统内存使用

内存有多大,程序就有多长。程序日渐增长的内存需求让我等用低端VPS的吊丝情何以堪。此贴介绍限制mysql、apache等内存消耗大户的方法,虽然这么做会降低性能,不过相较于可怜的128M甚至64M的系统内存来说,可能性能瓶颈本来就不是这里吧。

需要root权限,有底纹的文字为shell命令或配置文件内容
1. 使用mysql小内存配置
进入/etc,备份原my.cnf
mv my.cnf my.cnf.old
使用小内存版的设置文件,x.xx.x为机器实际安装mysql版本
cp /usr/share/doc/mysql-server-x.xx.x/my-small.cnf /etc/my.cnf
编辑my.cnf,关闭innodb
在[mysqld]字段下增加
skip-innodb

2. 修改apache参数,编辑/etc/httpd/conf/httpd.conf
减少进程数,找到下面几个参数并修改
StartServers       1
MinSpareServers    1
MaxSpareServers    5
ServerLimit       64
MaxClients        64
MaxRequestsPerChild  4000
减小超时时间
Timeout 30
打开keepalive选项,增加连接复用
KeepAlive On
减小保持连接的超时时间,减小浪费
KeepAliveTimeout 3

3. php相关优化
安装php缓存模块,减少php重新编译的需要,运行命令安装
yum install php-pecl-apc
[可选]减少php可用最大内存,编辑/etc/php.ini
memory_limit = 64M
注意:此选项设置过小可能导致某些程序运行不正常

4. 减小本地控制台console个数为1个
对VPS来说,本地控制台应该没有用,编辑/etc/sysconfig/init
ACTIVE_CONSOLES=/dev/tty[1-1]

5. 将sshd、ftp等可以使用xinet的服务使用xinet共享开启,而不要用独立进程(请参考其它资料)

6. 关闭不必要的服务
使用chkconfig --list查看所有服务
chkconfig service_name off关闭相当服务

做过所有这些后,我的vps,256M内存,开机大概占用70M内存,长时间运行也使用swap也不会太多。

星期二, 九月 11, 2012

CentOS 6.2 架设l2tp/ipsec服务器

VPN服务器经常用到,有时为了访问办公室网络,有时为了你懂的原因。这里介绍一下l2tp/ipsec VPN服务器在CentOS 6.2下的架设。L2TP服务器使用UDP协议,对防火墙兼容好,IPsec实现加密,实现安全通信。而且IOS和Android原生支持此协议,因此比OpenVPN来得方便些。

这里参考了:http://wiki.nikoforge.org/L2TP/IPSec_VPN_Setup_on_Centos_6_(64-bit)_for_use_with_Android_ICS_and_iOS_5_Clients
并对过程做了少量修改,增加了必需了小步骤,增加了NAT的设置。

开始之前请确认现在登陆用户为root,否则会提示权限不足。有底纹的文字为在shell中直接输入的代码。
1. 必要软件包的安装
整个过程需要4个软件包:libpcap、ppp、ipsec-tools和xl2tpd(debian 6下安装需要增加racoon),其中前两个centos官方源就有,后两个需要从其它网站下载,先安装前两个
yum libpcap
yum ppp
下载并安装后两个包,此时版本分别为0.8.0-3和1.3.1-4
wget http://repo.nikoforge.org/redhat/el6/i386/ipsec-tools-0.8.0-3defpsk.el6.i386.rpm
rpm -i ipsec-tools-0.8.0-3defpsk.el6.i386.rpm
wget http://vesta.informatik.rwth-aachen.de/ftp/pub/Linux/fedora-epel/6/i386/xl2tpd-1.3.1-4.el6.i686.rpm
rpm -i xl2tpd-1.3.1-4.el6.i686.rpm

安装完成编写必要的脚本和配置文件
2. 环境初始化脚本,在shell下输入
cat > /etc/racoon/init.sh << EOF
#!/bin/sh
# set security policies
echo -e "flush;\n\
        spdflush;\n\
        spdadd 0.0.0.0/0[0] 0.0.0.0/0[1701] udp -P in  ipsec esp/transport//require;\n\
        spdadd 0.0.0.0/0[1701] 0.0.0.0/0[0] udp -P out ipsec esp/transport//require;\n"\
        | setkey -c
# enable IP forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -s  192.168.123.0/24 -o eth0 -j MASQUERADE
iptables -I FORWARD -s  192.168.123.0/24 -j ACCEPT
iptables -I FORWARD -d  192.168.123.0/24 -j ACCEPT
EOF
更改权限:chmod 750 /etc/racoon/init.sh

3. IPsec设置
cat > /etc/racoon/racoon.conf << EOF
path include "/etc/racoon";
path pre_shared_key "/etc/racoon/psk.txt";
path certificate "/etc/racoon/certs";
path script "/etc/racoon/scripts";
remote anonymous
{
        exchange_mode    aggressive,main;
        passive          on;
        proposal_check   obey;
        support_proxy    on;
        nat_traversal    on;
        ike_frag         on;
        dpd_delay        20;
        proposal
        {
                encryption_algorithm  aes;
                hash_algorithm        sha1;
                authentication_method pre_shared_key;
                dh_group              modp1024;
        }
        proposal
        {
                encryption_algorithm  3des;
                hash_algorithm        sha1;
                authentication_method pre_shared_key;
                dh_group              modp1024;
        }
}
sainfo anonymous
{
        encryption_algorithm     aes,3des;
        authentication_algorithm hmac_sha1;
        compression_algorithm    deflate;
        pfs_group                modp1024;
}
EOF
设置权限:chmod 600 /etc/racoon/racoon.conf

设置共享密钥
cat >/etc/racoon/psk.txt  << EOF
* mysharedkey
EOF
设置权限:chmod 600 /etc/racoon/psk.txt

4. 设置L2TP服务
cat > /etc/xl2tpd/xl2tpd.conf << EOF
[global]
ipsec saref = yes
force userspace = yes
[lns default]
local ip = 192.168.123.200
ip range =  192.168.123.201- 192.168.123.210
refuse pap = yes
require authentication = yes
ppp debug = yes
length bit = yes
pppoptfile = /etc/ppp/options.xl2tpd
EOF

5. 配置PPP
cat > /etc/ppp/options.xl2tpd << EOF
ms-dns 8.8.8.8
require-mschap-v2
asyncmap 0
auth
crtscts
lock
hide-password
modem
debug
name l2tpd
proxyarp
lcp-echo-interval 10
lcp-echo-failure 100
EOF

设置拨号用户密码
cat > /etc/ppp/chap-secrets << EOF
# client       server    secret       IP addresses
  vpnuser      *         vpnpassword     *
EOF

6. 启动服务
/etc/racoon/init.sh
/etc/init.d/racoon start
/etc/init.d/xl2tpd start
如果需要开机自动启动,把/etc/init.d/xl2tpd加入/etc/rc.local,并运行下面两个命令
chkconfig racoon on
chkconfig xl2tpd on

已知问题:
1. 如果系统无法连接,可以查看日志找出错误原因,如果是找不到ppp设备,尝试手动创建
mknod /dev/ppp c 108 0

2. Windows XP由于IPsec实现于标准不一致,拨号会失败。查看日志能看到:“ERROR: Expecting IP address type in main mode, but FQDN.”,这样的错误信息。解决办法只能是自己重新编译ipsec-tools,去掉标准检查部分的代码,patch代码如下:
--- src/racoon/ipsec_doi.c.orig Thu Feb  2 23:37:17 2006
+++ src/racoon/ipsec_doi.c Sun Sep 24 23:28:42 2006
@@ -3277,10 +3277,9 @@
     iph1->approval->authmethod == OAKLEY_ATTR_AUTH_METHOD_PSKEY) {
  if (id_b->type != IPSECDOI_ID_IPV4_ADDR
   && id_b->type != IPSECDOI_ID_IPV6_ADDR) {
- plog(LLV_ERROR, LOCATION, NULL,
+ plog(LLV_WARNING, LOCATION, NULL,
  "Expecting IP address type in main mode, "
  "but %s.\n", s_ipsecdoi_ident(id_b->type));
- return ISAKMP_NTYPE_INVALID_ID_INFORMATION;
  }
  }

Debian 6下架设的不同处:
1. 需要安装软件包为5个,增加racoon
2. ipsec在debian下有自己的配置文件,可以不需要/etc/racoon/init.sh文件
ipforwarding在/etc/sysctl.conf中设置
net.ipv4.ip_forward=1
setkey在/etc/ipsec-tools.conf中设置
flush;
spdflush;
spdadd 0.0.0.0/0[0] 0.0.0.0/0[1701] udp -P in  ipsec esp/transport//require;
spdadd 0.0.0.0/0[1701] 0.0.0.0/0[0] udp -P out ipsec esp/transport//require;
iptables规则用if-up脚本设置,参照:保存debian 6下的iptables设置

附:Iphone设置L2TP拨号方法
进入设置->通用->网络->VPN
点击添加VPN设置类型选择L2TP,输入参数如下:

星期四, 九月 06, 2012

免费顶级域名注册

.tk(talk的缩写) 域名现在被越来越多的人关注。按官方网站的说法,tk域名的注册数比整个.cn和.ru域名加起来都多。。。

原来注册.tk域名有比较多的限制,比如说3个月必须有至少25个访问数,还有必须显示它的广告,这都限制了tk域名的发展。现在好像这些限制都取消了,注册的顶级域名可以做URL转发也可以直接解析到自定义的IP上,甚至可以自己完全定制域名NS域。注册时选择期限可以是1到12个月,到期前15天自己手动在控制面板刷新即可,非常方便超值。

新手站长在学习或者起步阶段使用免费空间可以省一笔可观的费用。不过由于免费空间普遍被滥用,所以一些空间要求注册者自己拥有顶级域名才能注册,这里面就有老牌的Freehostia和pipni这些口碑非常好的空间。这样,注册一个免费的顶级域名的优势就很明显了。

.tk域名注册过程很简单,填入email注册,或者直接用支持openid的账号登陆就行,完全不需要身份认证,非常方便。一般免费注册只要求名字要4个或者以上的字符数即可。注册地址在这: 免费注册TK顶级域名

测试网站请点这里:http://nevil.tk/ , 这是完全免费域名的效果。有兴趣可以注册一个试试,反正不要钱^_^

Free top level domain name

Domain names help people to remember web sites easily, without domain name, people would have to remember web sites in number form address such as 12.34.56.78. For example, we usually access Google at http://www.google.com, you can also access it at http://74.125.31.99/. Obviously, the English form is mush easier for visitors to remember.

So if one wants to build his/her own web site on the Internet, a beautiful domain name may be a good start. And of course, the shorter, the better. I bet the Google guys won't like the name googleblahlblahblahhahahah.com :-) The shortest domain name available now should be top level domain names, as in 2012. Top level domain name is just a little longer than root names (which is not available for register now), like google.com. It is the coolest domain name one can get nowadays, if you can think up a good word. Usually, one top level domain name costs you about $10 one year, however, with one exception, the .TK (abbrev. for talk) top level domain.

The .TK domain is the only free top level domain name in the world. It is free to register, free to use, and free to renew before expires. You can use .tk names for URL redirect. You can also point it directly to some IP address. And you can set up your own name server to serve your domain. It is every useful to one who's learning how the Internet works or web masters who are not so rich.

Well, here is my .tk name http://nevil.tk/ if you are interested. You can also get your own free top level domain here: Register free top level .TK Domain.

Thanks for reading.

Browser is leaking your privacy.

You know Internet is not safe.
You know web sites using cookies to track your activities and get familiar with your habits.
Do you know even when you disable cookies, your browser is still submitting information that can be used to identify you?

Try this link (it won't harm your computer, I promise): http://nevil.tk/

This is an small example showing you what your browser is telling the server. In this page, you can see your IP address, User Agent of your browser and language setting of your OS.

So what can be seen from this negligible information? Well, all that is needed to identify you. 

First, your IP address can be used to see where you are from. If  http://nevil.tk/  is not obvious, try enter what you see on this page to http://www.geoiptool.com/. Is this map familiar to you?

Second, User Agent usually contains information of what browser you are using, and what is your Operating System. Browser type is basically right there in the string. For Windows OS, you can see "Windows NT version_number". Windows XP has version number 5.1, Vista and Windows 7 are 6.0 and 6.1 respectively.

Last but not least, languages. This is languages your browser likes, also language display settings on your OS. From this setting, one can tell whether you are Russian or probably American travelling in Russia.

Scared? Not so much! We can always protect our privacy. If you care about your privacy, TOR(The Onion Routing) is always a good choice. Download it at http://torproject.org/, it's free, it's open source, it's safe. With Tor, all your surfs seems to be from the same browser type, the same OS and random IP. The IP address may be from your neighbor, also can be from the other half of the earth. However either your neighbor or the guy on the other half of the earth can actually see who you are, because Tor not only encrypt your information, but also passes the information over several middle tor nodes before him/her. Tor is designed to protect your privacy, it is designed to be safer when more people using it. So give it a try.

That's it, thanks for reading. Happy surfing!