ãããVPS éçºç°å¢ç·¨ (Rails+nginx+unicorn)
基本設定編ã§æä½éã®è¨å®ãçµäºããã®ã§ãä»åº¦ã¯ãµã¼ãã¹ãå ¬éããããã®ç°å¢ãæ´ãã¾ãããnginxå¨ããããåããã¾ããã§ããããã¨ããããåãã¨ããã¾ã§ã¯è¡ããã®ã§ããããã¿ã¤ããä½ã£ã¦ãã人ã«è¦ãããç°å¢ãåºæ¥ã¾ããã
ã¾ãã¯å¾ãããã«ãã§å¿ è¦ã¨ãªãããã±ã¼ã¸+αãå ¨é¨å ¥ãã¦ããã¾ãã
# yum install yum-fastestmirror # yum update # yum install gcc gcc-c++ openssl* pcre* zlib* gd* libpng* libjpeg* libxml* libxslt* readline*
MySQLã¤ã³ã¹ãã¼ã«
yumã§ã¤ã³ã¹ãã¼ã«ãã¦ãèªåã§èµ·åããããã«ãã¦ããã¾ãã
# yum install mysql mysql-devel mysql-server # /etc/rc.d/init.d/mysqld start # chkconfig mysqld on
rootãã¹ã¯ã¼ããè¨å®ã
$ mysql -u root mysql> grant all PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'password'; mysql> delete from mysql.user where password = '';
Railsã¤ã³ã¹ãã¼ã«
ç¶ãã¦Railsãyumã§ããã±ã¼ã¸ãç¡ãã£ããã®ã¯ã½ã¼ã¹ããå
¥ãã¦ãã¾ãã
git
# cd /usr/local/src # wget http://git-core.googlecode.com/files/git-1.7.7.3.tar.gz # tar zxf git-1.7.7.3.tar.gz # cd git-1.7.7.3 # ./configure # make && make install
RVM
$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm) $ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile $ . .bash_profile
ruby
$ rvm install ruby-1.9.2 --with-readline-dir=/usr/include/readline $ rvm ruby-1.9.2 --default
node.js
Rails3.1ãåä½ããããã¨ããã¨ã©ã³ã¿ã¤ã ã¨ã©ã¼(ExecJS::RuntimeUnavailable)ã«ãªã£ãã®ã§ã¤ã³ã¹ãã¼ã«ã
# cd /usr/local/src # wget http://nodejs.org/dist/v0.6.1/node-v0.6.1.tar.gz # tar zxf node-v0.6.1.tar.gz # cd node-v0.6.1 # ./configure # make && make install
rails
$ gem install rails $ gem install mysql2
ä¸å¿ãã®æç¹ã§é©å½ãªå ´æã«ããã¸ã§ã¯ããä½æãã¦åä½ç¢ºèªãã¦ããã¾ãã
80ãã¼ãã§èµ·åãã¦ããã©ã¦ã¶ããã¢ã¯ã»ã¹ã§ããã°å¤§ä¸å¤«ã§ãã
$ cd /var/www $ rails new sample -d mysql $ cd sample $ rvmsudo rails s -p 80
mysqlã«ã¤ãªãããªãå ´åã¯/tmp/mysql.sockã®å ´æã確èªãã¦ãã ããã
ä»åã¯ãã®ããã«å¤æ´ãã¦ãã¾ãã
- socket: /tmp/mysql.sock + socket: /var/lib/mysql/mysql.sock
unicornã¤ã³ã¹ãã¼ã«
ç¶ãã¦unicornã§ãããããã¯gemä¸çºã§å®äºã
$ gem install unicorn
ã¢ããªã±ã¼ã·ã§ã³æ¯ã«è¨å®ãã¡ã¤ã«ãè¨è¿°ãã¾ãã
ã¨ããããåãã°è¯ãã®ã§ãæä½éã®å
容ã®ã¿è¨è¿°ã
# vi /var/www/sample/config/unicorn.rb listen '/tmp/unicorn.sock' pid '/tmp/unicorn.pid'
ããã§ãåä½ç¢ºèªãã¦ããã¾ãã
$ rvmsudo unicorn_rails -c config/unicorn.rb -p 80
åä½ã確èªã§ãããããã¼ã¢ã³ã¨ãã¦èµ·åãã¦ããã¾ãã
8080ãã¼ãã§åä½ãã¾ãã
$ unicorn_rails -c config/unicorn.rb -D
nginxã¤ã³ã¹ãã¼ã«
æå¾ã«nginxã§ãã
å°ç¨ã®ã¦ã¼ã¶ã¼ã追å ãã¦ãã½ã¼ã¹ããã¤ã³ã¹ãã¼ã«ãã¾ããã³ã³ãã¤ã«ãªãã·ã§ã³ã¯Compile-time optionsã«è©³ç´°ãè¨è¼ããã¦ãã¾ãããã«ãããæã«ããã¢ã¸ã¥ã¼ã«ã追å ã§ããªããããªã®ã§ãå¿ è¦ãªãã®ã¯å ¥ãã¦ããã¾ãããã
# useradd -s /sbin/nologin nginx # cd /usr/local/src # wget http://nginx.org/download/nginx-1.0.9.tar.gz # tar zxf nginx-1.0.9.tar.gz # cd nginx-1.0.9 # ./configure \ --conf-path=/etc/nginx/nginx.conf \ --prefix=/usr/local/nginx \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_gzip_static_module \ --with-http_realip_module # make && make install
ãµã¼ãã¼ãèµ·åãã¦ãWelcomeãã¼ã¸ã表示ãããã確èªãã¾ãã
// ãµã¼ãã¼èµ·å # /usr/local/nginx/sbin/nginx // ãµã¼ãã¼åæ¢ # /usr/local/nginx/sbin/nginx -s quit
nginxçµç±ã§ãunicornã®ããã»ã¹ã«ã¢ã¯ã»ã¹ã§ããããã«è¨å®ãã¾ããæçµçã«ã¯ç®¡çããããããã«åå²ãã¾ãããã¨ããããåä½ç¢ºèªã§ããã°ããã®ã§nginx.confãã¡ã¤ã«ãç´æ¥æ¸ãæãã¾ããã
#vi /etc/nginx/conf/nginx.conf user nginx; worker_processes 1; pid /var/run/nginx.pid; error_log /var/log/nginx/error.log; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; sendfile on; keepalive_timeout 65; upstream unicorn_app { server unix:/tmp/unicorn.sock; } server { listen 80; server_name _; location / { proxy_pass http://unicorn_app; } } }
ããä¸åº¦/usr/local/nginx/sbin/nginxã§ãµã¼ãã¼ãèµ·åãã¦ã¢ã¯ã»ã¹ããã¨ãWelcomeãã¼ã¸ã§ã¯ãªããRailsã¢ããªã®ãã¼ã¸ã表示ããã¾ããæå¾ã«èµ·åç¨ã®ã¹ã¯ãªããã追å ãã¦ããã¾ãã
# vi /etc/init.d/nginx #! /bin/sh ### BEGIN INIT INFO # Provides: nginx # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: nginx web server # Description: nginx web server ### END INIT INFO DESC="nginx web server" NAME=nginx DAEMON=/usr/local/nginx/sbin/$NAME PIDFILE=/var/run/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME case "$1" in start) $DAEMON ;; stop) kill `cat $PIDFILE` ;; *) echo "Usage: $SCRIPTNAME {start|stop}" >&2 exit 1 ;; esac
èªåã§èµ·åããããã«è¨å®ã
# chmod 755 /etc/init.d/nginx # chkconfig --add nginx # chkconfig nginx on
以ä¸ã§çµäºã§ãã
è²ã
調ã¹ã¤ã¤åæ¥ãããã¾ãã..ã