さくらの VPS を使う - 2. Apache, MySQL, Redmine

Apache

出典

$ sudo yum install httpd
$ sudoedit /etc/httpd/conf/httpd.conf
ServerAdmin に連絡先を書く。メールよりはトップページのURLのほうがいいかもしれない。
ServerName にURLを書く(ホスト名でもいいらしい)。
DocumentRoot を変えたいなら変える(私は変えなかったので /var/www/html のまま)。
Options を Options -Indexes FollowSymLinks に変更(Indexes を無効化)。
ServerTokens OS を ServerTokens Prod に変更(OS情報を返さない)。
ServerSignature Off (サーバー情報を返さない)。
$ sudo apachectl configtest
$ sudo service https start
$ sudo chkconfig httpd on
$ chkconfig --list httpd
httpd 0:off1:off2:on3:on4:on5:on6:off

ブラウザでアクセスしてみてデフォルトページが表示されることを確認する。

MySQL

$ sudo yum install mysql-server mysql-devel
$ sudo chkconfig mysqld on
$ sudo service mysqld start
$ sudo mysqladmin -u root password 'ぱすわーど'
$ mysql -u root -p mysql
mysql> SELECT user, password, host FROM user;

mysql> DELETE FROM user WHERE user = '' OR ( user = 'root' AND host != 'localhost' );
Query OK, 4 rows affected (0.00 sec)
mysql> SHOW DATABASES;
mysql> DROP DATABASE test;

Ruby on Rails

最初は MySQL から逃れようとしたのだが、WPやMTは公式には MySQL しか使えないと知り、結局は逃れられないと悟った。 Redmine を入れるために必要な Ruby 系のソフトのバージョンは以下。出典

出典

$ sudo yum install rubygems ruby-devel sqlite-devel
$ sudo gem update
(此処から先はたぶんやる必要がない。Redmine の項目でやる $ sudo gem install bundler --no-rdoc --no-ri $ sudo bundle install --without development test postgresql mysql でおk)
$ sudo gem install pkg-config
$ sudo gem install rack -v '1.4.4'  (rails をインストールしようとしたら rack の 1.4.0 が必要と言われたので。最新版 1.5.0 だとエラーが出たので一つ前を入れるべき)
$ sudo gem install rails  (かなり時間がかかるので注意)
(Depending on your version of ruby, you may need to install ruby rdoc/ri data:
 = 1.8.7 : gem install rdoc-data; rdoc-data --install
とか表示されたけどたぶんドキュメントに関してだから無視して良い)
$ ruby -v
ruby 1.8.7
$ gem -v
1.3.7
$ rails --version
Rails 3.2.11
$ sqlite3 -version
3.6.20

ってことで必要な条件は満たしている(ちょっと戸惑ったが 3 < 11 らしい。そりゃそうか)

Redmine

このへんから 出典 http://rubyforge.org/frs/?group_id=1850 から最新版の Redmine のソースの URL を取得。

$ wget http://rubyforge.org/frs/download.php/76722/redmine-2.2.2.tar.gz
$ tar zxvf redmine-2.2.2.tar.gz
$ sudo mv redmine-2.2.2 /var/lib/redmine
$ cd /var/lib/redmine/
$ sudo gem install bundler --no-rdoc --no-ri
$ sudo bundle install --without development test postgresql rmagick
$ cp config/database.yml.example config/database.yml
$ vi config/database.yml
production:
  adapter: mysql
  database: redmine 
  host: localhost
  username: redmine 
  password: "XXXXX"
  encoding: utf8
$ vi config/configuration.yml
production:
    email_delivery:
        delivery_method: :smtp
        smtp_settings:
            address: "localhost"
            port: 25
            domain: 'example.com'
imagemagick_convert_command: /usr/bin/convert
$ mysql -u root -p
mysql> create database redmine default character set utf8;
mysql> grant all privileges on redmine.* to redmine@localhost identified by 'XXXXX';
$ sudo bundle exec rake generate_secret_token
$ sudo gem install passenger --no-rdoc --no-ri
$ sudo passenger-install-apache2-module
たくさんエラーメッセージが出るのでそれに従って、もう一度実行。
$ sudo yum install curl-devel openssl-devel zlib-devel httpd-devel apr-devel apr-util-devel
$ sudo yum install ImageMagick ImageMagick-devel
$ sudo passenger-install-apache2-module
$ sudo RAILS_ENV=production bundle exec rake db:migrate

以下のように設定項目が出るのでメモ

Please edit your Apache configuration file, and add these lines:

   LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-3.0.19/ext/apache2/mod_passenger.so
   PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-3.0.19
   PassengerRuby /usr/bin/ruby

After you restart Apache, you are ready to deploy any number of Ruby on Rails
applications on Apache, without any further Ruby on Rails-specific
configuration!

Suppose you have a Rails application in /somewhere. Add a virtual host to your
Apache configuration file and set its DocumentRoot to /somewhere/public:

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

And that's it! You may also want to check the Users Guide for security and
optimization tips, troubleshooting and other useful information:

  /usr/lib/ruby/gems/1.8/gems/passenger-3.0.19/doc/Users guide Apache.html

ここから作業再開

$ sudoedit /etc/httpd/conf.d/passenger.conf
# Passengerの基本設定。
# passenger-install-apache2-module --snippet を実行して表示される設定を使用。
# 環境によって設定値が異なりますので以下の3行はそのまま転記しないでください。
#
LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.17/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.17
PassengerRuby /usr/local/bin/ruby

# Passengerが追加するHTTPヘッダを削除するための設定(任意)。
#
Header always unset "X-Powered-By"
Header always unset "X-Rack-Cache"
Header always unset "X-Content-Digest"
Header always unset "X-Runtime"

# 必要に応じてPassengerのチューニングのための設定を追加(任意)。
# 詳しくはPhusion Passenger users guide(http://www.modrails.com/documentation/Users%20guide%20Apache.html)をご覧ください。
PassengerMaxPoolSize 20
PassengerMaxInstancesPerApp 4
PassengerPoolIdleTime 3600
PassengerUseGlobalQueue on
PassengerHighPerformance on
PassengerStatThrottleRate 10
PassengerSpawnMethod smart
RailsAppSpawnerIdleTime 86400
RailsFrameworkSpawnerIdleTime 0
$ sudo service httpd restart
$ sudo chown -R apache:apache /var/lib/redmine
$ sudoedit /etc/httpd/conf.d/passenger.conf

次の設定は動作の確認のために使う。終わったら別に conf ファイルを作ってちゃんとルートを設定する。

<VirtualHost *:80>
    ServerName www.example.jp
    DocumentRoot /var/lib/redmine/public
</VirtualHost>

ファイルを日付の昇順に並べ替えるために、files_controller.rb をいじる。

$ sudoedit /var/lib/redmine/app/controllers/files_controller.rb
- sort_init 'filename', 'asc'
+ sort_init 'created_on', 'desc'

2014-07-23 追記: $ sudo yum install ImageMagick ImageMagick-devel imagemagick_convert_command: /usr/bin/convert を追加。