Created
November 15, 2013 06:22
-
-
Save satomixx/7480014 to your computer and use it in GitHub Desktop.
Ruby2.0 + Rails4.0 + Passenger + Apache + MySQL + CentOS6.4 + Logwatch + 高速化設定 ( Webサーバー構築四苦八苦 4th Day, Ruby on Rails編 ) ref: http://qiita.com/tsumekoara/items/88cb83a0a14670540719
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo yum install -y zlib-devel perl-ExtUtils-MakeMaker httpd httpd-devel openssl-devel libyaml-devel libxml2-devel libxslt-devel libffi-devel readline-devel pcre-devel iconv-devel sqlite-devel mysql mysql-server mysql-devel curl-devel nkf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cd | |
$ wget http://www.sqlite.org/sqlite-autoconf-3071300.tar.gz | |
$ tar zxvf sqlite-autoconf-3071300.tar.gz | |
$ cd sqlite-autoconf-3071300 | |
$ ./configure | |
$ make | |
$ sudo make install | |
$ make clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-------------------------------------------- | |
Deploying a Ruby on Rails application: an example | |
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: | |
/home/{hostname}/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/passenger-4.0.24/doc/Users guide Apache.html | |
http://www.modrails.com/documentation/Users%20guide%20Apache.html | |
Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-) | |
https://www.phusionpassenger.com | |
Phusion Passenger is a trademark of Hongli Lai & Ninh Bui. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo vim /etc/httpd/conf.d/passenger.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo service httpd start #またはrestart | |
sudo /sbin/chkconfig httpd on |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo service mysqld start #初回の初期化処理 | |
sudo mysqladmin -u root password pass(=passは任意のpassword※あとで使う) | |
sudo /sbin/chkconfig mysqld on |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cd | |
$ rails new app | |
$ cd app | |
$ rails g scaffold sample name:string | |
$ rake db:migrate | |
$ rails s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo chmod755/home/{username} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cd | |
$ rm -rf app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# db作成 | |
$ mysql -u root -p | |
#password入力(先のmysqlで作った任意のpassword) | |
# mysqlに入りました | |
mysql> CREATE DATABASE app_01(任意の名前); | |
mysql> show databases; # 指定した名前のDBが出来たかどうか確認 | |
mysql> exit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cd | |
$ wget http://nodejs.org/dist/v0.8.5/node-v0.8.5.tar.gz | |
$ tar zxvf node-v0.8.5.tar.gz | |
$ cd node-v0.8.5 | |
$ ./configure | |
$ make | |
$ sudo make install | |
$ make clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cd | |
$ rails new app -d mysql | |
$ cd app | |
$ vim config/database.yml | |
------- | |
config/database.yml | |
development | |
adapter: mysql2 | |
encoding: utf8 | |
reconnect: false | |
database: app_development (←ここにさっき作ったDBの名前を入れる) | |
pool: 5 | |
username: root | |
password: #ここに先ほど設定したpassword記入 | |
socket: /var/lib/mysql/mysql.sock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rails g scaffold sample name:string | |
$ rake db:migrate | |
$ rails s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo chmod755/home/{username} | |
#その他、読み取り権限があるかをチェック#ll -a を実行して、rがついているかどうか |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo vim /etc/httpd/conf/httpd.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/etc/httpd/conf/httpd.conf | |
#最終行付近(コメント#を外す) | |
#NameVirtualHost *:80 | |
->NameVirtualHost *:80 | |
#以下を末尾に追記 | |
先ほどメモっておいたVirutalHostの記載をココで使います。 | |
<VirtualHost *:80> | |
ServerName {IPアドレスなど} | |
# !!! Be sure to point DocumentRoot to 'public'! | |
DocumentRoot /home/{hostname}i/app/public | |
RailsEnv development | |
<Directory /home/{hostname}/app/public> | |
# This relaxes Apache security settings. | |
AllowOverride all | |
# MultiViews must be turned off. | |
Options -MultiViews | |
Order allow,deny | |
Allow from all | |
</Directory> | |
</VirtualHost> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ service httpd configtest | |
Syntax OK | |
$ sudo service httpd restart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ vim Gemfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ bundle install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cd | |
$ git clone git://github.com/sstephenson/rbenv.git .rbenv | |
$ echo 'export PATH="$HOME/.rbenv/bin/:$HOME/.rbenv:$PATH"'>>~/.bash_profile | |
$ echo 'eval "$(rbenv init -)"'>>~/.bash_profile | |
$ echo 'export PATH="$HOME/.rbenv/bin/:$HOME/.rbenv:$PATH"'>>~/.zshrc | |
$ echo 'eval "$(rbenv init -)"' >> ~/.zshrc | |
$ exec $SHELL | |
$ mkdir -p ~/.rbenv/plugins | |
$ cd ~/.rbenv/plugins | |
$ git clone git://github.com/sstephenson/ruby-build.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'coffee-script' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo yum install -y logwatch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ vim /usr/share/logwatch/default.conf/logwatch.conf | |
---- | |
#ログが保存されているディレクトリ | |
LogDir = /var/log | |
#メールの送信先 | |
MailTo = username@hoge.com | |
#アーカイブされたログも含めるかどうか | |
Archives = Yes | |
#レポートの日付範囲を3つのオプションから。All, Today, Yesterday | |
Range = Yesterday | |
ログの詳細度、Low (=0), Med (=5), High (=10) または 0から10までの数字で | |
Detail = High | |
# チェックの対象となるサービスを /usr/share/logwatch/scripts/services/ 以下、あるいは All で指定。 | |
Service = All | |
# メールコマンドパス | |
mailer = "sendmail -t" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo vim /etc/aliases | |
---- | |
#最終行に以下を追加 | |
root: sample@sample.com #メールアドレスを追加 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo newaliases | |
$ echo test | mail root |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# rbenvでインストール可能なものが表示されるので、確認 | |
$ rbenv install -l | |
# 今回はRuby2.0 の最新版 Ruby 2.0.0-p247 をインストール | |
$ rbenv install 2.0.0-p247 | |
$ rbenv rehash | |
# 2.0.0-p247 をデフォルトの Ruby に設定。 | |
$ rbenv global 2.0.0-p247 | |
## 諸々確認 | |
# rbenvのバージョン | |
$ rbenv versions | |
system | |
* 2.0.0-p247 (set by ~/local/rbenv/version) | |
# Ruby コマンドの場所 | |
$ which ruby | |
~/local/rbenv/shims/ruby | |
# 現在使用中のRuby のバージョン確認 | |
$ ruby --version | |
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux] | |
# gem のバージョンやインストール済みの gem を確認。 | |
$ gem --version | |
$ gem list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rbenv exec gem install bundler | |
$ rbenv rehash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gem install rails --no-document | |
$ gem install rake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gem install passenger | |
$ source ~/.zshrc もしくは bash_profile | |
$ passenger-install-apache2-module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Welcome to the Phusion Passenger Apache 2 module installer, v4.0.19. | |
This installer will guide you through the entire installation process. It | |
shouldn't take more than 3 minutes in total. | |
Here's what you can expect from the installation process: | |
1. The Apache 2 module will be installed for you. | |
2. You'll learn how to configure Apache. | |
3. You'll learn how to deploy a Ruby on Rails application. | |
Don't worry if anything goes wrong. This installer will advise you on how to | |
solve any problems. | |
Press Enter to continue, or Ctrl-C to abort. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-------------------------------------------- | |
The Apache 2 module was successfully installed. | |
Please edit your Apache configuration file, and add these lines: | |
LoadModule passenger_module /home/{hostname}/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/passenger-4.0.24/buildout/apache2/mod_passenger.so | |
PassengerRoot /home/{hostname}/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/passenger-4.0.24 | |
PassengerDefaultRuby /home/{hostname}/.rbenv/versions/2.0.0-p247/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! | |
Press ENTER to continue. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See https://github.com/sstephenson/execjs#readme for more supported runtimes | |
#gem 'therubyracer', platforms: :ruby | |
-> gem 'therubyracer', platforms: :ruby #コメントを外します |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<VirtualHost *:80> | |
ServerName {IPアドレスなど} | |
# !!! Be sure to point DocumentRoot to 'public'! | |
DocumentRoot /home/{hostname}/app/public/ | |
RailsEnv development | |
<Directory /home/{hostname}/app/public/> | |
# This relaxes Apache security settings. | |
AllowOverride all | |
# MultiViews must be turned off. | |
Options -MultiViews | |
Order allow,deny | |
Allow from all | |
</Directory> | |
<Directory "/home/{hostname}/app/public/assets/"> | |
ExpiresActive On | |
ExpiresDefault "access plus 1 year" | |
Header set Cache-Control "public" | |
Header unset ETag | |
FileETag None | |
# Gzip圧縮 | |
AddEncoding x-gzip .gz | |
Header set Vary Accept-Encoding | |
RewriteEngine on | |
RewriteCond %{HTTP:Accept-Encoding} \b(x-)?gzip\b | |
RewriteCond %{REQUEST_FILENAME}.gz -f | |
RewriteRule ^(.*)$ $1.gz [L] | |
# レスポンスヘッダ〜の追加 | |
Header set X-Frame-Options SAMEORIGIN | |
Header set X-Content-Type-Options nosniff | |
Header set X-XSS-Protection "1; mode-block" | |
<FilesMatch \.css\.gz$> | |
ForceType text/css | |
</FilesMatch> | |
<FilesMatch \.js\.gz$> | |
ForceType text/javascript | |
</FilesMatch> | |
</Directory> | |
</VirtualHost> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
PassengerHighPerformance on | |
PassengerStatThrottleRate 10 | |
PassengerSpawnMethod smart | |
RailsAppSpawnerIdleTime 86400 | |
PassengerMaxPreloaderIdleTime 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment