いぬれおがんばれお

がんばる日記です

さくらVPSにUbuntu LTS 14.04をインストールした話(2)

前回の記事を参考に、Rails環境とサーバのセキュリティ周りを構築していきます

vimをインストールする

前回vimをインストールするのを忘れていて、ファイル修正が地獄でした…

sudo apt-get install vim

nginxをインストールする

sysv-rc-confはサービスの起動設定を簡単に登録できるようにするものです

sudo apt-get install nginx
sudo apt-get install sysv-rc-conf
sudo sysv-rc-conf nginx on
sudo /etc/init.d/nginx restart

ここまででIPアドレスの直アクセスしたりすると下記画像のようなページにアクセスできます

f:id:inureo:20150403113009p:plain

ちなみに、下記コマンドで構文確認ができます

nginx -t

参考URL

これから始める人のためのNginx(2):Nginxのインストールと基本設定 (1/4) - @IT Ubuntu 12.04 + ruby 1.9.3 + nginx + unicorn + Redmine - くじらにっき++

rbenv, ruby-build, rubyのインストール

build-essentialは開発用パッケージを一括インストールできるそうです

sudo apt-get install build-essential
sudo apt-get install libssl-dev

git をインストールしてない方はしてくださいね

sudo apt-get install git

早速 rbenvruby-build をインストールしていきます

git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo '' >> ~/.profile
echo '# rbenv' >> ~/.profile
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.profile
echo 'eval "$(rbenv init -)"' >> ~/.profile
source ~/.profile
type rbenv
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

rubyのバージョンを指定してインストール、そしてrehash

rbenv install -l
rbenv install 2.1.3
rbenv rehash

そしてrails環境を整えるために、bundlerとrailsをインストール

gem install bundler
gem install rails

Ubuntu 14.04 で rbenv, ruby-build を使用して Ruby のバージョンを管理する. | hirooka.pro Lector note - How to install build-essential and scim-anthy on Ubuntu 6.06 - Linux > Ubuntu 6.06 LTS - Documents apt - How to install build-essential? - Ask Ubuntu

サーバのgit設定

cd ~
mkdir .ssh
cd .ssh
ssh-keygen
# 下記コマンドで出力したものをGitHub / BitBucketなどのキー登録画面で登録
cat ~/.ssh/id_rsa.pub

僕は var/ 配下にアプリケーションを置くのでパーミッションを変更して、そこにgit cloneします

chown user:user var/
cd var/
git clone hogehoge

これで完了!ついでにbundle installもしてしまいました(╹◡╹)

mysqlのインストール

途中ピンク色の画面になり、パスワードを要求されます(びびった) また、libmysqlclient-devはmysql2 gemで必要だそうです

sudo apt-get install mysql-server
sudo apt-get install libmysqlclient-dev
sudo sysv-rc-conf mysql on

mysqlのユーザ追加

mysql -uroot -p
CREATE USER 'user'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost';

mysqlの設定を変更

mysqlで使う文字コードUTF-8にします

sudo vim /etc/my.cnf

それぞれのセクションに下記を追加

[mysqld]
character-set-server=utf8
skip-character-set-client-handshake
default-storage-engine=INNODB

[mysqldump]
default-character-set=utf8

[mysql]
default-character-set=utf8

そして再起動

sudo service mysql restart

mysqlのセキュリティ設定もします

下記を実行。詳しくは参考URLをどうぞ!

/usr/bin/mysql_secure_installation

[CentOS] MySQLサーバーのインストールを行うの巻 ~mysql_secure_installationを使ってみよう~ - TrippyBoyの愉快な日々

参考サイト

セキュリティ設定

iptablesを設定して、不要なポートを閉じたり、怪しい通信をはじきます。shellで下記を実行していく。

iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP

iptables -A INPUT -i lo -j ACCEPT 
iptables -A INPUT -p icmp -j ACCEPT

# 下記は許可するポート、80はhttp通信、443はhttps、10022はssh!
iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 10022 -j ACCEPT

iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

iptables -P INPUT DROP 
iptables -P OUTPUT ACCEPT

参考サイト:iptablesの設定方法|さくらインターネット公式サポートサイト

次に、iptablesを再起動時に自動で反映するために設定していきます。/etcにファイルを作る。rootになって作業します。

sudo su -
cd /etc
iptables-save > iptables_rule
cp iptables_rule iptables_rule.org

下記が設定ファイルからの復活方法

iptables-restore < iptables_rule

if-pre-up.d に、サーバ起動時に実行されるshファイルをおいておく

cd /etc/network/if-pre-up.d/
vim set_iptables.sh

shの内容は下記

#!/bin/sh
/sbin/iptables-restore < /etc/iptables_rule
exit 0

実行権限をつけてあげて出来上がり

chmod +x set_iptables.sh

Unicornを動かす

MacでもよくみるQtないよエラー!ubuntuではどうやるのか。

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /home/inureo/.rbenv/versions/2.1.3/bin/ruby extconf.rb
Command 'qmake -spec linux-g++ ' not available

Makefile not found

Gem files will remain installed in /home/inureo/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/capybara-webkit-1.3.0 for inspection.
Results logged to /home/inureo/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/extensions/x86_64-linux/2.1.0-static/capybara-webkit-1.3.0/gem_make.out
An error occurred while installing capybara-webkit (1.3.0), and Bundler cannot continue.
Make sure that `gem install capybara-webkit -v '1.3.0'` succeeds before bundling.

下記でOK

sudo apt-get install libqt4-dev

Installing Qt and compiling capybara webkit · thoughtbot/capybara-webkit Wiki

もう一度bundle install、無事に通る(╹◡╹)

アプリケーションのmysqlユーザを追加

GRANT ALL ON database.* TO 'user'@'127.0.0.1' IDENTIFIED BY 'password';

ユニコーンを起動

bundle exec unicorn -D -c ./config/unicorn/production.rb -E production

ユニコーンを殺す時は下記

kill -QUIT `cat tmp/pids/unicorn.pid`

nginx + unicorn + Railsの設定方法 - Qiita Ruby on Rails - Nginx & Unicorn で動かす! - mk-mode BLOG

シリーズ

さくらVPSにUbuntu LTS 14.04をインストールした話(1)