phpfarm + fastcgi で vhost ごとに異なるバージョンの PHP を動かす on apache 2.2 + centos 5.7

さくらVPS の CentOS 5.7 に phpfarm を入れて Apache 2.2 + FastCGI を設定して名前ベースのバーチャルホスト上で phpinfo を表示するまで(環境作りブログではないはずなのに、なぜかこんなネタばかり)。基本的に参考サイトをなぞっただけですが。

手順概要

  1. phpfarm を git clone
  2. mod_fastcgi をソースからコンパイル
  3. apache の設定を書く
続きを読む

CentOS 5.7 + Passenger

CentOS 5.7 に Passenger を入れて Rack アプリを動かしたメモ。

開発元が用意するリポジトリから mod_passenger と rubygem-passenger を入れる。

参考:Phusion Passenger native packages for RedHat/Fedora/CentOS – Phusion Corporate Blog

# rpm -Uvh http://passenger.stealthymonkeys.com/rhel/5/passenger-release.noarch.rpm
# yum install mod_passenger rubygem-passenger
# rpm -qa \*passenger                  
mod_passenger-3.0.11-9.el5.centos
rubygem-passenger-3.0.11-9.el5.centos

Apache モジュールをインストール。

# passenger-install-apache2-module

手元の環境ではここで curl-devel と ruby-devel が足りないよと言われたので、手動で入れてやりなおし。

# yum install curl-devel.x86_64 ruby-devel.x86_64
# passenger-install-apache2-module

途中で表示されたバーチャルホストの設定例をメモ( /etc/httpd/conf.d/passenger.conf 内でも確認できる)。

<VirtualHost *:80>
  ServerName www.yourhost.com
  DocumentRoot /somewhere/public   # <-- be sure to point to 'public'!
  <Directory /somewhere/public>
    AllowOverride all              # <-- relax Apache security settings
    Options -MultiViews            # <-- MultiViews must be turned off
  </Directory>
</VirtualHost>

どうやら以下のようなディレクトリ構成にすると勝手に動いてくれるらしい。

参考:PassengerでRackアプリを動かす - As Sloth As Possible

/path/to/example.jp/rackhello/
    +- hello.rb
    +- config.ru
    +- public/     <- ここを DocumentRoot にする。空でもOK。

vhost を設定する前に Hello World を用意。

参考:Route 477 - Rack日本語リファレンス

$ mkdir -pv /path/to/example.jp/rackhello/public
$ vi /path/to/example.jp/rackhello/hello.rb
require 'rubygems'
require 'rack'
class HelloApp
  def call(env)
    [200, {"Content-Type" => "text/plain"}, ["Hello, Rack"]]
  end
end
$ vi /path/to/example.jp/rackhello/config.ru
require './hello.rb'

run HelloApp.new

Apache を設定。

$ su -
# vi /etc/httpd/conf.d/vhost.jp.example.conf
<VirtualHost *:80>

ServerName example.jp
DocumentRoot /path/to/example.jp/rackhello/public

ErrorLog logs/example.jp-error_log
CustomLog logs/example.jp-access_log common

ServerAdmin webmaster@example.jp

</VirtualHost>
# /etc/init.d/httpd configtest
Syntax OK
# /etc/init.d/httpd reload
Reloading httpd:                   [  OK  ]

楽だー。

Ubuntu Oneiric + nginx + uwsgi + sinatra (rack)

Ubuntu Oneiric (11.10) + nginx + uwsgi な環境で Sinatra アプリを uwsgi に載せるまで。

まず sinatra のインストールから。 apt で sinatra をインストールしたい場合、現在は ruby-rack がみつからず依存関係エラーが出る。

$ sudo -s
# apt-get install ruby-sinatra
[...]
以下のパッケージには満たせない依存関係があります:
 ruby-sinatra : 依存: ruby-rack しかし、インストールすることができません
E: 問題を解決することができません。壊れた変更禁止パッケージがあります。
# apt-get install ruby-rack
[...]
パッケージ ruby-rack はデータベースには存在しますが、利用できません。
おそらく、そのパッケージが見つからないか、もう古くなっているか、
あるいは別のソースからのみしか利用できないという状況が考えられます

E: パッケージ 'ruby-rack' にはインストール候補がありません

ubuntu-proposed リポジトリに対策済みパッケージがあるので、有効にしてから ruby-sinatra をインストール。

参考:Bug #843734 in ruby-sinatra (Ubuntu): “ruby-sinatra : Depends: ruby-rack but it is not installable”

# vi /etc/apt/sources.list
...
deb http://jp.archive.ubuntu.com/ubuntu/ oneiric-proposed main restricted universe multiverse
deb-src http://jp.archive.ubuntu.com/ubuntu/ oneiric-proposed main restricted universe multivers
...
# apt-get update
# apt-get install ruby-sinatra

Sinatra DSL の Hello World なファイルを作る。

続きを読む

Ubuntu Oneiric の /etc/uwsgi/{apps-available,apps-enabled}

前回upstart を使って uwsgi を起動したけど、パッケージの仕組みに沿った方法を使ってみる。

Debian 系の基本通り、 /etc/uwsgi/apps-available/ に設定をファイルを置いて /etc/uwsgi/apps-enabled/ からシンボリックリンクする。

/etc/uwsgi/apps-available の設定は --inherit /usr/lib/uwsgi/conf/default.ini オプション付きで実行されるので、デフォルトの内容も確認しておく(継承するデフォルト設定ファイルのパスは /etc/default/uwsgi 内で設定されている) *1

$ dpkg -l uwsgi
...
ii  uwsgi              0.9.8.1-1          fast, self-healing application container server
$ uwsgi --version
uWSGI 0.9.8.1-debian
$ sudo -s
# cat /usr/lib/uwsgi/conf/default.ini  # 出力結果からコメント削除済み
[uwsgi]
autoload = true
master = true
workers = 2
no-orphans = true
pidfile = /run/uwsgi/%n/pid
socket = /run/uwsgi/%n/socket
chmod-socket = 660
log-date = true
uid = www-data
gid = www-data

pidfile と socket の %n プレースホルダには、設定ファイル名から拡張子を除いた文字列が入る。以下のように hello.ini を作ると /run/uwsgi/hello/socket というソケットが作られることになる。

# vi /etc/uwsgi/apps-available/hello.ini

デフォルトを意識して最低限の設定だけ。

[uwsgi]
plugins = python
logto = /var/log/uwsgi/hello.log
mount = /hello=/home/cu39/code/python/hello.py

リンク作成、起動、確認。

# ln -sv /etc/uwsgi/apps-available/hello.ini /etc/uwsgi/apps-enabled/
`/etc/uwsgi/apps-enabled/hello.ini' -> `/etc/uwsgi/apps-available/hello.ini'
# /etc/init.d/uwsgi start hello
 * Starting app server(s) uwsgi        [ OK ]
# ls -F /run/uwsgi/hello
pid  socket=
# ps -Af | grep uwsgi
www-data  6779     1  0 Jan18 ?        00:00:03 /usr/bin/uwsgi --ini /etc/uwsgi/apps-enabled/hello.ini --daemonize /var/log/uwsgi/hello.log --inherit /usr/lib/uwsgi/conf/default.ini
www-data  6787  6779  0 Jan18 ?        00:00:00 /usr/bin/uwsgi --ini /etc/uwsgi/apps-enabled/hello.ini --daemonize /var/log/uwsgi/hello.log --inherit /usr/lib/uwsgi/conf/default.ini
www-data  6788  6779  0 Jan18 ?        00:00:00 /usr/bin/uwsgi --ini /etc/uwsgi/apps-enabled/hello.ini --daemonize /var/log/uwsgi/hello.log --inherit /usr/lib/uwsgi/conf/default.ini

*1:このあたりの詳しい挙動は /etc/init.d/uwsgi や /usr/lib/uwsgi/init/do_command あたりを参照。

Ubuntu Oneiric + nginx + uwsgi + moin

インストール

nginx は PPA から入れる。

$ sudo -s
# nginx=stable
# add-apt-repository ppa:nginx/$nginx
# apt-get update 
# apt-get install nginx

uwsgi 関連もインストール。

# apt-get install uwsgi uwsgi-plugin-python

バージョン確認。

# nginx -v
nginx version: nginx/1.0.11
# uwsgi --version
uWSGI 0.9.8.1-debian
# python -V
Python 2.7.2+

hello world!

helloworld してみる。 /etc/nginx/sites-available/default を以下のように設定、テスト。

# vi /etc/nginx/sites-available/default
server {
        server_name localhost;

        location / {
                include uwsgi_params;
                uwsgi_pass 127.0.0.1:9090;
        }
}
# /etc/init.d/nginx configtest
Testing nginx configuration: nginx.
続きを読む