2011/03/10  |  Written by  |  under Blog

いつも忘れるのでコマンドだけ貼っとく。

参考:Apache/SSL自己証明書の作成とmod sslの設定 – maruko2 Note.

秘密鍵の作成

1
% openssl genrsa -aes128 1024 > domain.key

秘密鍵のパスフレーズの解除

1
2
% mv domain.key domain.key.org
% openssl rsa -in domain.key.org > domain.key

公開鍵の作成

1
% openssl req -new -key domain.key > domain.csr

サーバ証明書の作成

1
% openssl x509 -in domain.csr -days 365 -req -signkey domain.key > domain.crt
 
このエントリーを含むはてなブックマークはてなブックマーク - SSLの自己証明書の発行覚書 この記事をクリップ!Livedoorクリップ - SSLの自己証明書の発行覚書 Googleブックマークに追加 Digg This
Tags: , ,
2010/05/13  |  Written by  |  under Blog

以前の記事はDebianだったのですが、それをCentOSに設定したので、そのメモ。

ここを参考に、FastCGI用daemonのスクリプトを作成。

1
% sudo vim /etc/init.d/php-fastcgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/sh
#
# php-cgi - php-fastcgi swaping via  spawn-fcgi
#
# chkconfig:   - 85 15
# description:  Run php-cgi as app server
# processname: php-cgi
# config:      /etc/sysconfig/phpfastcgi (defaults RH style)
# pidfile:     /var/run/php_cgi.pid
# Note: See how to use this script :
# http://www.cyberciti.biz/faq/rhel-fedora-install-configure-nginx-php5/
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
spawnfcgi="/usr/bin/spawn-fcgi"
php_cgi="/usr/bin/php-cgi"
prog=$(basename $php_cgi)
server_ip=127.0.0.1
server_port=9000
server_user=nginx
server_group=nginx
server_childs=5
pidfile="/var/run/php_cgi.pid"
 
# do not edit, put changes in /etc/sysconfig/phpfastcgi
[ -f /etc/sysconfig/phpfastcgi ] && . /etc/sysconfig/phpfastcgi
 
start() {
    [ -x $php_cgi ] || exit 1
    [ -x $spawnfcgi ] || exit 2
    echo -n $"Starting $prog: "
    daemon $spawnfcgi -a ${server_ip} -p ${server_port} -u ${server_user} -g ${server_group} -P ${pidfile} -C ${server_childs} -f ${php_cgi}
    retval=$?
    echo
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} $prog -QUIT
    retval=$?
    echo
    [ -f ${pidfile} ] && /bin/rm -f ${pidfile}
    return $retval
}
 
restart(){
    stop
    sleep 2
    start
}
 
rh_status(){
    status -p ${pidfile} $prog
}
 
case "$1" in
    start)
        start;;
    stop)
        stop;;
    restart)
        restart;;
    status)
        rh_status;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 3
esac

今回はspawn-fcgiを使うので、そのパッケージもインストールします。そして実行権限をつけて、自動起動するように設定。

1
2
3
4
% sudo yum install spawn-fcgi
% sudo chmod a+x /etc/init.d/php-fastcgi
% sudo /sbin/chkconfig --add php-fastcgi
% sudo /sbin/chkconfig php-fastcgi on

次にnginxの設定。CentOSではDebianのように個別の設定に分かれていないので、/etc/nginx/nginx.confを直接編集します。今回は変更した部分のみ掲載します。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    server {
        listen       80;
        server_name  stnard.jp;

        access_log  /var/log/nginx/stnard.jp.access.log  main;

        location / {
            root   /var/www/html;
            index  index.php index.html index.htm;

            # static files
            if (-f $request_filename) {
                expires 30d;
                break;
            }

            # request to index.php
            if (!-e $request_filename) {
                rewrite ^(.+)$  /index.php?q=$1 last;
            }
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
    }

作成したら構文チェックして再起動します。ついでにFastCGIの方も起動しておきます。

1
2
3
% sudo /sbin/service nginx configtest
% sudo /sbin/service nginx restart
% sudo /sbin/service php-fastcgi start

とこんな感じで、Debianと同じような環境ができあがりました。

 
このエントリーを含むはてなブックマークはてなブックマーク - WordPress on nginx with FastCGIのCentOSの場合 この記事をクリップ!Livedoorクリップ - WordPress on nginx with FastCGIのCentOSの場合 Googleブックマークに追加 Digg This
Tags: , , ,

Standardプランの契約ができたので、早速設定。

  • /etc/ssh/sshd_config
    1
    2
    3
    Port xxxxx # ポート番号変更
    PasswordAuthentication no # パスワードログインの禁止
    PermitRootLogin no # rootログインの禁止
  • 必要なパッケージの導入
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $ sudo su -
    # yum update
    # yum install zsh lv
    # chsh -s /bin/zsh
    # useradd hoge
    # passwd hoge
    # su - hoge
    $ chsh -s /bin/zsh
    $ exit
    # visudo
  • 公開鍵の登録とsshサーバの再起動、確認
  • パッケージのインストールと設定
    1
    2
    % sudo yum install mysql-server php-mysql
    % sudo vim /etc/httpd/conf/httpd.conf
  • iptablesの設定
  • PHPのインストール
    1
    2
    3
    4
    $ sudo yum install php-pear-Net-Socket php-pear php-common php-gd php-devel php php-mbstring php-pear-Mail php-cli php-imap php-snmp php-pdo php-xml php-pear-Auth-SASL php-ldap php-pear-Net-SMTP php-mysql
    $ sudo vim /var/www/html/index.php
    $ sudo /sbin/service httpd restart
    $ sudo rm /var/www/html/index.php
  • nginxのインストール
    1
    2
    3
    4
    5
    $ sudo /sbin/service httpd stop
    $ sudo rpm -Uvh http://download.fedora.redhat.com/pub/epel/5Server/x86_64/epel-release-5-3.noarch.rpm
    $ sudo yum install nginx
    $ sudo chkconfig httpd off
    $ sudo chkconfig nginx on

と、ここまで設定して面倒になったので続きは後日。

 
このエントリーを含むはてなブックマークはてなブックマーク - DTIのServersMan@VPS Standardプランで最初にやったこと この記事をクリップ!Livedoorクリップ - DTIのServersMan@VPS Standardプランで最初にやったこと Googleブックマークに追加 Digg This
Tags: , ,
2010/04/20  |  Written by  |  under Blog

やっぱりApacheのメモリ消費量が多い気がしてきたのでnginxに移行しました。要点としては、mod_phpのようなものはないため、FastCGIでPHPを動作させる必要があると言うこと。

インストールは大して難しくないのですが、いかんせん日本語での情報が少ないのが難点です。基本的には公式wikiPHP/FastCGI Exampleを参考にすれば問題ありません。以下、Debian (lenny) での作業ログです。
まずは必要なパッケージをインストールします。その前に、必要に応じてApacheを停止しておきます。

1
2
% sudo invoke-rc.d apache2 stop
% sudo aptitude install php5-cgi nginx

次にFastCGI用daemonのスクリプトを作成。

1
% sudo vim /etc/init.d/php5-fastcgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
BIND=127.0.0.1:9000
USER=www-data
PHP_FCGI_CHILDREN=2
PHP_FCGI_MAX_REQUESTS=1000

PHP_CGI=/usr/bin/php5-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0

start() {
      echo -n "Starting PHP FastCGI: "
      start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}
stop() {
      echo -n "Stopping PHP FastCGI: "
      killall -q -w -u $USER $PHP_CGI
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}

case "$1" in
    start)
      start
  ;;
    stop)
      stop
  ;;
    restart)
      stop
      start
  ;;
    *)
      echo "Usage: php5-fastcgi {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL

作成したら実行権限をつけて自動起動の設定をし、起動しておきます。

1
2
3
% sudo chmod a+x /etc/init.d/php5-fastcgi
% sudo update-rc.d php5-fastcgi defaults
% sudo invoke-rc.d php5-fastcgi start

次にnginxの設定です。DebianではApacheと同じく、/etc/nginx/site-availableに設定ファイルを入れて、有効にしたいものだけ/etc/nginx/site-enabledにシンボリックリンクを張る感じになります。今回は/etc/nginx/site-available/wordpressと言うファイル作ります。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# for wordpress
upstream wordpress {
  server 127.0.0.1:9000;
}

server {
  listen   80 default;
  server_name  stnard.jp;

  location / {
    root /var/www;
    index index.php index.html;

    # static files
    if (-f $request_filename) {
      expires 30d;
      break;
    }

    # request to index.php
    if (!-e $request_filename) {
      rewrite ^(.+)$  /index.php?q=$1 last;
    }
  }

  location ~ \.php$ {
                fastcgi_pass   wordpress;
                fastcgi_index  index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;
  }

  location ~ /\.ht {
    deny all;
  }

  access_log  /var/log/nginx/wordpress.access.log combined;
  error_log  /var/log/nginx/wordpress.error.log;
}

作成したらシンボリックリンクを張って、設定ファイルの構文チェックをします。ついでにデフォルト設定のシンボリックリンクは削除しておきます。

1
2
3
4
5
6
% cd /etc/nginx/site-enabled/
% sudo ln -s /etc/nginx/site-available/wordpress ./
% sudo rm default
% sudo nginx -t
the configuration file /etc/nginx/nginx.conf syntax is ok
configuration file /etc/nginx/nginx.conf test is successful

上記で問題なければ、nginxを起動して動作確認。

1
% sudo invoke-rc.d nginx start

移行してみた感想としては、Apacheよりも何倍も高速になった気がします。恐らくはFastCGIをdaemon化したことが大きいのでしょうが、体感できる高速さはやはり魅力です。用途に応じてnginxやlighttpdなどの高速Webサーバを試してみるのもいいのではないでしょうか。

 
このエントリーを含むはてなブックマークはてなブックマーク - WordPress on nginx with FastCGIに移行してみた この記事をクリップ!Livedoorクリップ - WordPress on nginx with FastCGIに移行してみた Googleブックマークに追加 Digg This
Tags: , ,