Skip to content

Instantly share code, notes, and snippets.

@groovelab
Last active April 21, 2016 11:18
Show Gist options
  • Save groovelab/dc2a434e2db0b27320ac to your computer and use it in GitHub Desktop.
Save groovelab/dc2a434e2db0b27320ac to your computer and use it in GitHub Desktop.
UbuntuにSwiftとPerfectLibをインストールする

AWS

EC2インスタンスを作成

AMI

ubuntu-wily-daily-amd64-server-20151209 (Ubuntu 15.10)

セキュリティグループを設定

インバウンド

SSH, HTTP を許可

サブドメインを設定

elastic IP

xxx.xxx.xxx.xxx -> EC2インスタンスに割り当て

DNS

aws.your.domain A xxx.xxx.xxx.xxx 追加

ssh

キーペアを作成する

ログイン

$ ssh -i ~/.ssh/aws.pem [email protected]

ubuntuのバージョン確認

$ cat /etc/lsb-release
$ uname -a

日本時間にする

$ sudo dpkg-reconfigure tzdata

Swiftのインストール

最新のスナップショットは swift.org で確認

スナップショットからSwiftをインストール

$ wget https://swift.org/builds/development/ubuntu1510/swift-DEVELOPMENT-SNAPSHOT-2016-02-08-a/swift-DEVELOPMENT-SNAPSHOT-2016-02-08-a-ubuntu15.10.tar.gz
$ tar xzf swift-DEVELOPMENT-SNAPSHOT-2016-02-08-a-ubuntu15.10.tar.gz
$ cd swift-DEVELOPMENT-SNAPSHOT-2016-02-08-a-ubuntu15.10/
$ sudo chown -R root:root usr/
$ cd usr
$ sudo cp -fR ./* /usr/

RPMパッケージをインストール

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get -y install clang
$ sudo apt-get clean

Swiftの動作確認

$ swift --version
$ swift

PerfectLib インストール

RPMパッケージをインストール

$ sudo apt-get -y install libssl-dev libevent-dev libsqlite3-dev libicu-dev libcurl4-openssl-dev uuid-dev
 make make-guile

PerfectLibをインストール

$ cd ~
$ mkdir swift
$ cd swift
$ git clone https://github.com/PerfectlySoft/Perfect.git
$ cd Perfect/PerfectLib
$ sudo make clean
$ sudo make
$ sudo make install
$ ll /usr/local/lib/*Perfect*
/usr/local/lib/PerfectLib.so -> (your_home)/swift/Perfect/PerfectLib/PerfectLib.so
/usr/local/lib/PerfectLib.swiftdoc -> (your_home)/swift/Perfect/PerfectLib/PerfectLib.swiftdoc
/usr/local/lib/PerfectLib.swiftmodule -> (your_home)/swift/Perfect/PerfectLib/PerfectLib.swiftmodule
$ cd ../../

PerfectServerをインストール

$ cd Perfect/PerfectServer
$ sudo make clean
$ sudo make
$ sudo ln -sf "$(pwd)/perfectserverfcgi" /usr/local/bin/
$ sudo ln -sf "$(pwd)/perfectserverhttp" /usr/local/bin/
$ ll /usr/local/bin/perfect*
/usr/local/bin/perfectserverfcgi -> (your_home)/SwiftBBS/Perfect/PerfectServer/perfectserverfcgi
/usr/local/bin/perfectserverhttp -> (your_home)/SwiftBBS/Perfect/PerfectServer/perfectserverhttp
$ cd ../../

Perfect Server HTTP を起動

Perfect Server HTTP はport:8181 でHTTPリクエストを受け付けるSwift製Webサーバ

  • 起動スクリプトを作成
$ cd /var/www
$ sudo vi perfectServerHttp.sh
$ sudo chmod +x perfectServerHttp.sh
$ sudo ./perfectServerHttp.sh start
  • usage
$ sudo ./perfectServerHttp.sh start
$ sudo ./perfectServerHttp.sh stop
$ sudo ./perfectServerHttp.sh restart

Perfect Server FastCGI を起動

Perfect Server FastCGI はApacheからFastCGIとしてリクエストを処理するインターフェース

以後、Perfect Server Httpは起動せず、Perfect Server FastCGIのみを起動する

  • 起動スクリプトを作成
$ cd /var/www
$ sudo vi perfectServerFcgi.sh
$ sudo chmod +x perfectServerFcgi.sh
$ sudo ./perfectServerFcgi.sh start
  • usage
$ sudo ./perfectServerFcgi.sh start
$ sudo ./perfectServerFcgi.sh stop
$ sudo ./perfectServerFcgi.sh restart

mod_perfect インストール

$ sudo apt-get -y install apache2
$ cd Connectors/mod_perfect/
$ sudo make
$ sudo mv mod_perfect.so /usr/lib/apache2/modules/
$ sudo chown root:root /usr/lib/apache2/modules/mod_perfect.so
$ sudo chmod 644 /usr/lib/apache2/modules/mod_perfect.so
$ ll /usr/lib/apache2/modules/mod_perfect.so

apacheの設定

$ cd /etc/apache2
$ sudo vi mods-available/perfect.load
$ sudo a2enmod perfect
$ sudo a2enmod rewrite
$ sudo cp sites-available/000-default.conf sites-available/000-default.conf.org
$ sudo vi sites-available/000-default.conf
$ sudo service apache2 start

ブラウザから動作確認

http://aws.your.domain へアクセス

Exampleのデプロイ

.soファイル

$ cd ~/swift/Perfect/Examples/
$ sudo make
$ cd /var/www
$ sudo mkdir PerfectLibraries
$ sudo ln -s ~/swift/Perfect/Examples/URL\ Routing/URLRouting.so 
$ sudo ln -s ~/swift/Perfect/Examples/Authenticator/AuthenticatorServer.so
$ sudo ln -s ~/swift/Perfect/Examples/Upload\ Enumerator/UploadEnumerator.so
$ sudo ln -s ~/swift/Perfect/Examples/Tap\ Tracker/TapTrackerServer.so

.mustacheファイル

$ cd /var/www
$ cd html
$ sudo mkdir authenticator
$ sudo mkdir taptracker
$ sudo mkdir uploadfile
$ sudo cp ~/swift/Perfect/Examples/Authenticator/Authenticator/*.mustache authenticator/
$ sudo cp ~/swift/Perfect/Examples/Upload\ Enumerator/Upload\ Enumerator/*.mustache uploadfile/
$ sudo cp ~/swift/Perfect/Examples/Tap\ Tracker/*.mustache taptracker/

そのままだと Authenticatorが正しく動作しないので以下のように修正

- document.location = '/login'
+ document.location = location.pathname + '/login'

PerfectServerを再起動

$ ./perfectServerFcgi.sh restart

ブラウザから動作確認

#! /bin/sh
do_start() {
cd /var/www/
perfectserverhttp &
}
do_stop() {
pkill -f perfectserverhttp
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
restart)
do_stop
do_start
;;
esac
exit 0
#! /bin/sh
CWD=`dirname "${0}"`
cd "${CWD}"
PIDFILE="perfectserverfcgi.pid"
do_start() {
if [ -e $PIDFILE ]; then
echo "already started, because $PIDFILE exists"
exit 1
fi
perfectserverfcgi &
echo $! > $PIDFILE
echo "start PID=$!"
}
do_stop() {
if [ ! -e $PIDFILE ]; then
echo "not started"
exit 1
fi
PID=$(cat $PIDFILE)
kill -9 $PID
rm $PIDFILE
echo "stop PID=$PID"
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
restart)
do_stop
do_start
;;
esac
exit 0
LoadModule perfect_module /usr/lib/apache2/modules/mod_perfect.so
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/webroot
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# for perfect
<Directory "/var/www/webroot">
Require all granted
DirectoryIndex index.mustache index.html
</Directory>
RewriteEngine on
# unless a directory, remove trailing slash
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
# resolve .mustache file for extensionless mustache urls
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}\.mustache -f
RewriteRule ^(.*)$ $1.mustache [NC,PT,L]
# redirect external .mustache requests to extensionless url
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.mustache([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.mustache $1 [R=301,L]
<Location ~ "^.*\.[Mm][Uu][Ss][Tt][Aa][Cc][Hh][Ee]$">
SetHandler perfect-handler
</Location>
# for using Perfect's URL routing system
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule (.*) - [L,NS,H=perfect-handler]
</VirtualHost>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment