Archive

Posts Tagged ‘web’

在Nginx下运行perl-cgi脚本

September 16th, 2009 Wei No comments

把Web Server更换为Nginx后,发现BugZilla不能用了。打开网站显示403 Forbidden,想来是因为BugZilla使用Perl CGI运行的原因。查了Nginx为数不多的文档,发现Nginx不能像其他Web Server(例如Apache)那样直接运行perl-cgi的脚本。

试用几个不同的方法后,发现确实有方法可以比较简单的实现perl-cgi的功能:

首先下载一个Perl Wrapper,我把它另存为~/perl-fastcgi.pl。

修改perl-fastcgi.pl的属性,添加可执行权限。运行该文件,该脚本会创建一个/var/run/nginx/perl_cgi-dispatch.sock文件。

修改nginx的配置文件,在server section中添加:

location ~ \.cgi$ {
        root           /srv/www/domain.tld/public_html;
        fastcgi_pass    unix:/var/run/nginx/perl_cgi-dispatch.sock;
        fastcgi_index   index.cgi;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include         fastcgi_params;
}

配置工作就此完成,重新启动nginx即可。

对于BugZilla,可能还需要运行http://domain.tld/testagent.cgi。

Categories: Computer Science Tags: , , , , ,

Web服务器更换到Nginx

September 16th, 2009 Wei 3 comments

禁不住诱惑,还是把Web服务器更换为Nginx(读音[engine x]),这个俄罗斯人写的高性能的web服务器。[因为在Linode360方案中,使用Apache如果想达到比较好的性能,确实会比较频繁的出现OOM;要避免OOM,就只能减少并发处理能力和内存配置]

Debian Lenny自带的nginx是0.6x的,而目前最新版本是0.7x。因此决定,从官方网站安装。

本文简单介绍了完整的安装过程。

首先安装必要的package,这些package将会在后面的过程中用到:

$ sudo apt-get install libssl-dev libpcre3-dev build-essential
$ sudo apt-get install php5 php5-cgi mysql-server mysql-client

接下来下载并安装nginx(注意,下载的地址的可能会发生变化,可以参考http://wiki.nginx.org/NginxInstall#Stable):

$ wget http://sysoev.ru/nginx/nginx-0.7.62.tar.gz
$ tar -zxvf nginx-0.7.62.tar.gz
$ cd nginx-0.7.62
$ ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx/nginx.pid  --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --with-md5-asm --with-md5=/usr/include --with-sha1-asm --with-sha1=/usr/include --with-http_stub_status_module
$ make
$ sudo make install

安装spawn-fcgi(曾经lighttpd的一部分,用来管理php-cgi进程)

$ wget http://www.lighttpd.net/download/spawn-fcgi-1.6.2.tar.gz
$ tar -zxvf spawn-fcgi-1.6.2.tar.gz
$ cd spawn-fcgi-1.6.2
$ ./configure
$ make
$ sudo cp ./src/spawn-cgi /usr/bin/

创建nginx和php-fastcgi的init脚本

$ sudo vi /etc/init.d/php-fastcgi
$ sudo vi /etc/init.d/nginx
$ sudo chmod +x /etc/init.d/php-fastcgi
$ sudo chmod +x /etc/init.d/nginx

/etc/init.d/nginx文件内容:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: nginx init.d script for Ubuntu 8.10 and lesser versions.
# Description:       nginx init.d script for Ubuntu 8.10 and lesser versions.
### END INIT INFO
#------------------------------------------------------------------------------
# nginx - this script, which starts and stops the nginx daemon for ubuntu.
#
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server.  This \
#               script will manage the initiation of the \
#               server and its process state.
#
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /acronymlabs/server/nginx.pid
# Provides:    nginx
#
# Author:  Jason Giedymin
#          <jason.giedymin AT acronymlabs.com>.
#
# Version: 1.0 01-Apr-2009 jason.giedymin AT gmail.com
# Notes: nginx init.d script for Ubuntu 8.10 and lesser versions.
#
#------------------------------------------------------------------------------
#                               MIT X11 License
#------------------------------------------------------------------------------
#
# Copyright (c) 2009 Jason Giedymin, http://AcronymLabs.com
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
#                               Functions
#------------------------------------------------------------------------------
. /lib/lsb/init-functions

#------------------------------------------------------------------------------
#                               Consts
#------------------------------------------------------------------------------
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx

NAME=nginx
DESCRIPTION="Nginx Server..."

PIDSPATH=/var/run/nginx
PS=$NAME                                #the process, which happens to be the NAME
PIDFILE=$NAME.pid                       #pid file
RUNAS=root                              #user to run as

SCRIPT_OK=0                             #ala error codes
SCRIPT_ERROR=1                          #ala error codes
TRUE=1                                  #boolean
FALSE=0                                 #boolean

lockfile=/var/lock/subsys/nginx
NGINX_CONF_FILE="/etc/nginx/nginx.conf"

#------------------------------------------------------------------------------
#                               Simple Tests
#------------------------------------------------------------------------------

#test if nginx is a file and executable
test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
        . /etc/default/nginx
fi

#set exit condition
#set -e

#------------------------------------------------------------------------------
#                               Functions
#------------------------------------------------------------------------------

configtest() {
        $DAEMON -t -c $NGINX_CONF_FILE
}

getPSCount() {
        return `pgrep -f $PS | wc -l`
}

isRunning(){
        pidof_daemon
        PID=$?

        if [ $PID -gt 0 ]; then
                return 1
        else
                return 0
        fi
}

status(){
        isRunning
        isAlive=$?

        if [ "${isAlive}" -eq $TRUE ]; then
                echo "$NAME found running with processes:  `pidof $PS`"
        else
                echo "$NAME is NOT running."
        fi

}

removePIDFile(){
        if [ -f $PIDSPATH/$NAME.pid ]; then
                rm $PIDSPATH/$NAME.pid
        fi
}

start() {
        log_daemon_msg "Starting $DESCRIPTION"

        isRunning
        isAlive=$?

        if [ "${isAlive}" -eq $TRUE ]; then
                log_end_msg $SCRIPT_ERROR
        else
                start-stop-daemon --start --quiet --chuid $RUNAS --pidfile $PIDSPATH/$PIDFILE --exec $DAEMON
                chmod 400 $PIDSPATH/$PIDFILE
                log_end_msg $SCRIPT_OK
        fi
}

stop() {
        log_daemon_msg "Stopping $DESCRIPTION"

        isRunning
        isAlive=$?
        if [ "${isAlive}" -eq $TRUE ]; then
                start-stop-daemon --stop --quiet --pidfile $PIDSPATH/$PIDFILE

                removePIDFile

                log_end_msg $SCRIPT_OK
        else
                log_end_msg $SCRIPT_ERROR
        fi
}

reload() {
        configtest || return $?

        log_daemon_msg "Reloading (via HUP) $DESCRIPTION"

        isRunning
        if [ $? -eq $TRUE ]; then
                `killall -HUP $PS` #to be safe

                log_end_msg $SCRIPT_OK
        else
                log_end_msg $SCRIPT_ERROR
        fi
}

terminate() {
        log_daemon_msg "Force terminating (via KILL) $DESCRIPTION"

        PIDS=`pidof $PS` || true

        [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`

        for i in $PIDS; do
                if [ "$i" = "$PIDS2" ]; then
                        kill $i
                        removePIDFile
                fi
        done

        log_end_msg $SCRIPT_OK

}

pidof_daemon() {
    PIDS=`pidof $PS` || true

    [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`

    for i in $PIDS; do
        if [ "$i" = "$PIDS2" ]; then
            return 1
        fi
    done
    return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|force-reload)
        stop
        start
        ;;
  reload)
        $1
        ;;
  status)
        status
        ;;
  configtest)
        $1
        ;;
  terminate)
        $1
        ;;
  *)
        FULLPATH=/etc/init.d/$NAME
        echo "Usage: $FULLPATH {start|stop|restart|force-reload|status|configtest|terminate}"
        exit 1
        ;;
esac

exit 0

/etc/init.d/php-fastcgi文件内容:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          php-fastcgi
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop php-cgi in external FASTCGI mode
# Description:       Start and stop php-cgi in external FASTCGI mode
### END INIT INFO

# Author: Kurt Zankl <[EMAIL PROTECTED]>

# Do NOT "set -e"

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="php-cgi in external FASTCGI mode"
NAME=php-fastcgi
DAEMON=/usr/bin/php-cgi
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
PHP_CONFIG_FILE=/etc/php5/cgi/php.ini

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

# If the daemon is not enabled, give the user a warning and then exit,
# unless we are stopping the daemon
if [ "$START" != "yes" -a "$1" != "stop" ]; then
        log_warning_msg "To enable $NAME, edit /etc/default/$NAME and set START=yes"
        exit 0
fi

# Process configuration
export PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS
DAEMON_ARGS="-q -b $FCGI_HOST:$FCGI_PORT -c $PHP_CONFIG_FILE"

do_start()
{
        # Return
        #   0 if daemon has been started
        #   1 if daemon was already running
        #   2 if daemon could not be started
        start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
                || return 1
        start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON \
                --background --make-pidfile --chuid $EXEC_AS_USER --startas $DAEMON -- \
                $DAEMON_ARGS \
                || return 2
}

do_stop()
{
        # Return
        #   0 if daemon has been stopped
        #   1 if daemon was already stopped
        #   2 if daemon could not be stopped
        #   other if a failure occurred
        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE > /dev/null # --name $DAEMON
        RETVAL="$?"
        [ "$RETVAL" = 2 ] && return 2
        # Wait for children to finish too if this is a daemon that forks
        # and if the daemon is only ever run from this initscript.
        # If the above conditions are not satisfied then add some other code
        # that waits for the process to drop all resources that could be
        # needed by services started subsequently.  A last resort is to
        # sleep for some time.
        start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
        [ "$?" = 2 ] && return 2
        # Many daemons don't delete their pidfiles when they exit.
        rm -f $PIDFILE
        return "$RETVAL"
}
case "$1" in
  start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  restart|force-reload)
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop
        case "$?" in
          0|1)
                do_start
                case "$?" in
                        0) log_end_msg 0 ;;
                        1) log_end_msg 1 ;; # Old process is still running
                        *) log_end_msg 1 ;; # Failed to start
                esac
                ;;
          *)
                # Failed to stop
                log_end_msg 1
                ;;
        esac
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
        exit 3
        ;;
esac

创建nginx配置文件/etc/nginx/nginx.conf(并实现VirtualHost)

user www-data www-data;
worker_processes  4;
events {
    worker_connections  1024;
    use epoll;
}
http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        tcp_nopush     on;
        tcp_nodelay    on;

        keepalive_timeout  65;
        gzip  on;

        include /etc/nginx/sites-enabled/*; #这一句就是实现nginx VirtualHost的
}

创建VirtualHost文件/etc/nginx/sites-available/domain.tld

server {
        listen       80;
        server_name  domain.tld www.domain.tld;
        access_log      /srv/www/domain.tld/logs/access.log;
        location / {
                root   /srv/www/domain.tld/public_html;
                index  index.html index.htm index.php;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                root   /usr/html;
        }

        location ~ \.php$ {
                root           /srv/www/domain.tld/public_html;
                fastcgi_pass    127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }
}

Enable VirualHost

$ sudo ln -s /etc/nginx/sites-available/domain.tld /etc/nginx/sites-enabled/domain.tld

启动php-fastcgi和nginx

$ sudo /etc/init.d/php-fastcgi start
$ sudo /etc/init.d/nginx start

配置php-fastcgi和nginx自动启动

$ sudo apt-get install rcconf
$ sudo rcconf # 选中php-fastcgi和nginx

此时,php的配置文件使用/etc/php5/cgi/php.ini。

创建WordPress的rewrite rule:修改/etc/nginx/sites-available/domain.tld,在location /中添加如下的内容:

location / {
        root   /srv/www/weigblog.com/public_html;
        index  index.html index.htm index.php;
        if (!-e $request_filename) {
                rewrite (.*) /index.php;
        }
}

其中:
-e为存在目录或文件
-d为存在目录
-f为存在文件

Categories: Computer Science Tags: , , ,

美国人的服务咋这么晕呢

February 5th, 2009 Wei 1 comment

这次是域名服务,我的blog host在国外的主机上,因为之前服务上的一些变化,导致我在服务商帐户上留有$37的余款。他们不给refund,只给credit。这次正好赶上域名快过期了,想着这30多美元放着也是放着,不如把域名续了。可上次注册第二个域名的时候,他们直接从我的信用卡扣款了。为了保险起见,我给他们的billing部门发了邮件,询问这件事儿:

I have a domain name registered within current account. Could I pay for the extension of the domain name (2 yrs) using the credit in my account pool instead of charging from my credit card? Last time when I was extending the domain name, there seams not any step asking me to select from account balance or credit card and charged from my credit card by default. I don’t want to use my credit card this time.

他们客服人员给我的回复是:

Thank you for writing. Credits on account can be used for any of our provided service such as hosting renewal, upgrade or installation charges. They can not be used for non-Site5 provided services such as domain registrations, renewals or transfers (domain registration services are provided by Enom.com).

我一想,反正还有好几个月到期呢,既然不能用credit付域名的钱,那就算了。结果今天突然又想,反正早晚也得付。就直接点了“Extend Service”按钮,billing系统提示will charge from credit card,我已经知道了,就点了OK。结果知道发生了什么事儿吗?

支付过程瞬间就结束,而不是通过信用卡付款那种漫长的授权过程。在我还感叹“花钱可真容易”的时候,突然间发现我的帐户余额变了,从$30多变成了$10多。再查看billing history,只有invoice记录,而没有信用卡funding记录。貌似看起来,他们直接charge了我的帐户余额。

挺好,反正正愁花不出去呢。剩下的钱,正好还够2年域名费的。

Categories: diary Tags: , , , ,

主机空间成功迁移和升级

August 3rd, 2008 Wei No comments

自动独立域名以来,我一直使用Site5提供的主机空间。当时是$5 Deal的销售方案,2年过去了,Site5提供的空间方案也有所升级,主机配置也有所增加。还有大约半年时间,第一期合同就到期了,正好趁此机会将空间升级一下。

目前新的空间方案是这样的:

Feature Detail
Disk Space Unlimited
Bandwidth Unlimited
Ruby on Rails Supported
Technology Support 24×7
Domain Pointer Unlimited
Domain Parking Supported
Subdomains Supported
Email Supported
cPanel Supported
SSH Access Supported
FTP Access Supported w/ unlimited accounts
Fantastico Supported
PHP4 & 5 Supported
Perl 5.8+ Supported w/Free Module Installation
Python 2.2 Supported
GD Supported
ImageMagick 5+ Supported
MySQL 4 Unlimited w/ phpMyAdmin
CPU Intel(R) Xeon(R) CPU E5405 @ 2.00GHz (4cores *2)
Memory 4GB
Uptime Guarantee 99.9%

总体来说在这个价格上,这样的服务已经非常不错了。不过contract需要5年(60个月)才能享受到这个价格。

目前,2个blog都已经迁移过来的,其中一个需要从Wordpress 2.1升级到2.6,但DNS更新仍需要时间。Wiki和另一个主域名环境,暂时仍然需要Technology Support迁移到新系统中,所以仍然需要时间。

Categories: diary Tags: , , , , , ,

Gmail支持强制https方式的web访问

July 25th, 2008 Wei No comments

Gmail现在终于可以实现强制使用https方式的Web访问了,以前要实现这个功能,则需要Firefox的插件实现。

选择Settings,在General的最下面,即可看到Browser connection的选项。选中Always use https,则可以在每次都使用https方式访问(不管是从http还是https方式登录的)。

Categories: Computer Science Tags: , , ,