星期三, 9月 25, 2013

Arch linux Linode Web Server(Apache ,MariaDB ,PHP ,ROR)

Arch linux Linode Web Server(Apache ,MariaDB ,PHP ,ROR)

 最近租了個VPS,把EC2停掉了,也順便體驗一下什麼是開發速度極快的ROR,

在設定上還蠻麻煩的,也參考了些網路一些大大的資料。

最後可以跑 PHP 和 ROR 在同一個伺服器。

Set local

  1. vim /etc/locale.gen
  2. locale-gen

Add user(CAKE)

  1. useradd CAKE -m -s /bin/bash
  2. passwd CAKE

Pacman Update

  1. pacman -Syu
  2. pacman-db-upgrade
    • [發生無法更新時]
    • pacman-key --init
    • pacman-key --populate archlinux

Install

  1. pacman -S base base-devel
  2. pacman -S sudo
  3. pacman -S bash-completion
  4. pacman -S git
  5. pacman -S nodejs
  6. pacman -S ruby

Set Sudo User

  1. visudo
  2. [ADD username(CAKE) ]
    • CAKE ALL=ALL (ALL)
  3. [SAVE & EXIT ]
  4. 鎖住 root : passwd root -l
  5. logout

防火牆

  1. pacman -S ufw
  2. sudo ufw enable
  3. port
    • sudo ufw limit 22/tcp
    • ufw allow 80/tcp
  4. [開機啟動]
    • systemctl enable ufw.service

Mysql

  1. install MariaDB
  2. sudo systemctl start mysqld
  3. sudo systemctl enable mysqld
  4. sudo mysql_secure_installation

ROR

  1. gem install rails
    • gem install rails --no-ri --no-rdoc
    • gem update
    • [Apache]
      • [$] gem install passenger
      • [$] passenger-install-apache2-module
      • set config [apache config]
  2. gem install mysql2
  3. in your porject Gemfile file 設定套件檔案
    • 增加 gem 'mysql2' #增加mysql套件
  4. bundl e update raiils

apache config

##### IN LoadModule(load passenger_module(ror) )
LoadModule passenger_module /home/cake/.gem/ruby/2.0.0/gems/passenger-4.0.18/buildout/apache2/mod_passenger.so
PassengerRoot /home/cake/.gem/ruby/2.0.0/gems/passenger-4.0.18
PassengerDefaultRuby /usr/bin/ruby

…

##### VirtualHost and Alias(set php alias and ror alias)
<VirtualHost *:80>

DocumentRoot /srv/http
<Directory /srv/http>
    Allow from all
</Directory>

Alias /_test2013/ /home/cake/web/
<Directory "/home/cake/web/">
    Options Indexes
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

RailsEnv development

Alias /home /home/cake/web_ror/home/public
<Directory  /home/cake/web_ror/home/public>
    #PassengerAppRoot  /home/cake/web_ror/home/public
    PassengerAppRoot  /home/cake/web_ror/home
    Allow from all
    Options -MultiViews
    #RailsBaseURI /rails
</Directory>

</VirtualHost>

ROR create DB

  1. rails g model YOUR_MODEL_NAME name:string phone:integer birthday:date
  2. db/migrate/20130923065726_create_pbooks.rb
    • 為模組設定檔
  3. app/models/pbook.rb
    • 模組端
  4. rake db:migrate # (建立)模組設定檔

---- 2014 01/10 UPDATE ----

Mac ROR 設定 以及 別名設定

Apache config

<VirtualHost *:80>

DocumentRoot "/Library/WebServer/Documents"
<Directory /Library/WebServer/Documents>
    Allow from all
</Directory>

Alias /_test2014/ /Users/cake/www_web/php_web/
<Directory "/Users/cake/www_web/php_web/">
    Options Indexes
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

RailsEnv development

Alias /ruby_blog /Users/cake/www_web/ror_web/blog/public
<Directory  /Users/cake/www_web/ror_web/blog/public>
    PassengerAppRoot  /Users/cake/www_web/ror_web/blog
    SetEnv RAILS_RELATIVE_URL_ROOT /ruby_blog
    Allow from all
    Options -MultiViews
    #RailsBaseURI /rails
</Directory>

</VirtualHost>


ROR routes your Application add

  scope ENV['RAILS_RELATIVE_URL_ROOT'] || '/' do    # get "home/index" => "home#index"    resources :home     # resources :another_controller  end



Myapp::Application.routes.draw do
  scope ENV['RAILS_RELATIVE_URL_ROOT'] || '/' do
    resources :controller
   resources :another_controller
   match 'welcome/', :to => "welcome#index"
   root :to => "welcome#index"
end end


### 2014 01 22 update
如果使用 ubuntu 設定時
1. 將 passenger 設定資料 建立檔案 放入 /etc/apache2/modes-available/ 並且建立連結到 mods-enabled
     # ln -s /etc/apache2/modes-avaliable/ror.load /etc/apache2/modes-enabled/ror.load
2. 虛擬目錄設定 放入 sites-available 同樣建立捷徑至 sites-enabled
3. 也可以使用 sudo a2ensite XXXXX 建立捷徑
4. sudo apache2ctl configrtest  測試是否有誤
5. sudo apache2ctl restart

PS  如果發生 403 錯誤,則/etc/apache2/apache.conf 註解掉
<Directory />
...
</Directory>