ChefでソースからApacheをインストールとおまけでIptablesを設定
いつの間にやら4杯目!それではぼちぼち動的な事をやってみようと思います。
前回作ったVagrantのファイルをGitHubにあげました。アカウントはTwetterと同じtakekentwにしました。よろろ。
takekentw/vagrant_001 · GitHub:
設定ファイルを弄る前に、下記の参考サイトでちょこっと勉強しました。
Ruby on Railsをはじめよう | Mitakalab:
さ~っとやってみてふむふむ(*´▽`*)と、ぼんやり輪郭ができたので実際に検証してみます。
前回か前々回にも言っていたと思いますが、Chefで設定ファイルを設定するもう1つのやり方です。
参考サイト
apache2.4のrecipeを書いてみた – Qiita:k-motoyan
ひとまずは上記の参考サイトを参考にしながら、ソースからApacheのインストールという事だけをおこなうようにしました。
これがRubyか・・。
Iptablesは基本的な設定をするような感じにしてみました。
あと、Chefをする場合はTreeコマンドは入れておいた方がいいと思いますね。
前回と同じIPですが、これはWin機の方のVMwareで立てたサーバーのIPです。
$ cat nodes/192.168.24.64.json
{
"user": {
"name": "root",
"password" : "$1$wJVOjlmk$NdQ/FUU6EMVT394jhuS/I."
},
"run_list": [
"yum",
"recipe[selinux::disabled]",
"recipe[apache]"
],
"automatic": {
"ipaddress": "192.168.24.64"
}
}$ cat site-cookbooks/apache/recipes/default.rb
#
# Cookbook Name:: apache
# Recipe:: default
#
# Copyright 2014, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
# 基本的なパッケージをインストール
%w{ traceroute gcc make pcre pcre-devel openssl-devel wget tar }.each do |p|
package p do
action :install
end
end
# 外部cookbookを利用してselinuxを無効
selinux_state "SELinux Disabled" do
action :disabled
end
# sourceのインストールディレクトリ
install_dir = '/usr/local/src'
# インストールするソースの情報
source_info = {
:apr => {
:file_name => 'apr-1.5.1.tar.gz',
:file_dir => 'apr-1.5.1',
:configure => './configure --prefix=/opt/apr/apr-1.5.1',
:remote_uri => 'http://ftp.jaist.ac.jp/pub/apache/apr/apr-1.5.1.tar.gz'
},
:apr_util => {
:file_name => 'apr-util-1.5.4.tar.gz',
:file_dir => 'apr-util-1.5.4',
:configure => './configure --prefix=/opt/apr-util/apr-util-1.5.4 --with-apr=/opt/apr/apr-1.5.1',
:remote_uri => 'http://ftp.jaist.ac.jp/pub/apache/apr/apr-util-1.5.4.tar.gz'
},
:httpd => {
:file_name => 'httpd-2.4.10.tar.gz',
:file_dir => 'httpd-2.4.10',
:configure => './configure --prefix=/opt/httpd/httpd-2.4.10 --enable-ssl --with-apr=/opt/apr/apr-1.5.1 --with-apr-util=/opt/apr-util/apr-util-1.5.4',
:remote_uri => 'http://ftp.jaist.ac.jp/pub/apache/httpd/httpd-2.4.10.tar.gz'
}
}
# 情報を利用してインストール
source_info.each do |key, info|
remote_file "/tmp/#{info[:file_name]}" do
source "#{info[:remote_uri]}"
end
script 'install_httpd' do
not_if 'ls /etc/init.d/httpd'
interpreter 'bash'
user 'root'
code <<-EOL
install -d #{install_dir}
tar zxvf /tmp/#{info[:file_name]} -C #{install_dir}
cd #{install_dir}/#{info[:file_dir]} && #{info[:configure]} && make && make install
EOL
end
end
# IPtablesをコードで入力
script 'iptables_create' do
interpreter 'bash'
user 'root'
code <<-EOL
#!/bin/sh
iptables -F
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
#BroadCast Guard
iptables -A INPUT -d 255.255.255.255 -j DROP
iptables -A INPUT -d 224.0.0.1 -j DROP
iptables -A INPUT -d 192.168.0.255 -j DROP
iptables -N RH-Firewall-1-INPUT
iptables -A INPUT -j RH-Firewall-1-INPUT
iptables -A FORWARD -j RH-Firewall-1-INPUT
iptables -A RH-Firewall-1-INPUT -i lo -j ACCEPT
iptables -A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
iptables -A RH-Firewall-1-INPUT -p 50 -j ACCEPT
iptables -A RH-Firewall-1-INPUT -p 51 -j ACCEPT
iptables -A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#SMTP HTTP SNMP POP SSH Guard
iptables -A RH-Firewall-1-INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
iptables -A RH-Firewall-1-INPUT -p tcp -m state --state NEW --dport 80 -j ACCEPT
iptables -A RH-Firewall-1-INPUT -p tcp -m state --state NEW --dport 3000 -j ACCEPT
iptables -A RH-Firewall-1-INPUT -m limit --limit 1/s -j LOG --log-prefix "[FW DROP] : "
iptables -A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
/etc/rc.d/init.d/iptables save
/etc/rc.d/init.d/iptables start
echo settings complete
EOL
endRubyのスクリプトもほぼマネだし、まだまだ独り立ちできてないです。
上のスクリプトは、こないだDockerでサーバーを立てて手動でやった事と全く同じことをしているのですが、やっぱりこりゃあ楽ですなぁ。
MPMをいろいろな感じで入れたりできるくらいはやりたいなぁと。
4杯目ですが、Chefはほぼ1回目なのでこのあたりで今回は終了。
あ、knifeで繋いでrsyncした時にパスワードを聞かれまくる現象は公開鍵を登録すれば回避できるので、そこはさくっと済ませましょう。