過去ログ/Project「Momiji」汎用仮想アプライアンス

2019年5月4日 (土) 12:40時点におけるIppei Suzuki (トーク | 投稿記録)による版 (ページの作成:「 '''※この記事は 過去ログ に含まれている記事です''' == 概要 == 最低限の環境がセットアップされた汎用の仮想アプライア…」)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
※この記事は 過去ログ に含まれている記事です

概要

最低限の環境がセットアップされた汎用の仮想アプライアンスを作成し、~ 新規に開発をはじめる場合にそれをインポートするだけで開発環境が揃うことを目的にしている。~ 想定される開発環境の準備手順は下記の通り。

  1. VirtualBoxをインストール(アプライアンスの作成はversion 4.2.4)
  2. 汎用仮想アプライアンス「Momiji」をインポート
  3. パスワードでログイン
  4. 固定IPを変更
# vi /etc/sysconfig/network-scripts/ifcfg-eth1
  1. sshルールを変更(公開鍵の追加、パスワードログインのオフなど)
  2. 必要なソフトウェアをインストール

このページでは汎用仮想アプライアンス完成までの作業手順を記録する。


CentOSのインストール

CentOSのインストール自体は特に問題は無いはず。今回はCentOS 6.3を選択。

インストールの詳細はこちらを参考に。

Sim@Memo: 【メモ】CentOS6.0をインストールする(VirtualBox for Mac)ホストOSからゲストOSへのアクセス設定も


ネットワークの設定

ネットワークの設定についてはこちらを参考に。

VirtualBoxにCentOSを入れてWebサーバを叩くまでメモ - erio_nk

ネットワーク設定が正常に完了したかどうかは次で確認出来る

  • インターネットへの接続(NAT) → 外部サイトにpingを送る
ping www.yahoo.co.jp
  • ホストOSからゲストOSへの接続(ホストオンリーアダプタ)→ ブラウザまたはターミナルで接続

その他のカーネル・モジュールをインストール

# yum -y install gcc gcc-c++ make kernel-devel wget telnet

気がついただけでもこれだけ入ってなかった。

ユーザーの作成とsshの設定

作業ユーザーの作成

# adduser ippei
# passwd ippei

ユーザーの権限を追加

wheelグループに追加する

# usermod -G wheel ippei

sudoの使用を許可

# visudo

としてsudoの設定ファイルを開いた上で、以下の通りに変更する。

# %wheel  ALL=(ALL)       ALL
↓
%wheel  ALL=(ALL)       ALL

公開鍵/秘密鍵の設定

公開鍵、秘密鍵をローカルで用意しておき、公開鍵を以下のファイルに追記するというのが基本的な流れ。

# vi /home/ippei/.ssh/authorized_keys

その後、sshd_configファイルを編集して設定を行う

# vi /etc/ssh/sshd_config

sshdをリスタートして設定を反映

# /etc/init.d/sshd restart

詳細はこちらを参考に。

さくらVPSのsshによる鍵認証の設定 - STUDIO KINGDOM

  • 注意
    • セキュリティポリシーからすると「パスワードを使ったログイン」は禁止しておくべきだが、~

仮想アプライアンスインポート後に固定IPがコンフリクトするなどした場合にターミナルからログインできなくなるため、~ 仮想アプライアンスでは「パスワードを使ったログイン」は有効にしておき、~ インポート後運用に入る段階で設定を見直すことにしておく。


iptablesの設定

デフォルトでは80番ポートが開いていないのでその対応を含めiptablesを設定する。

設定反映用のファイルを作成する

# cd ~
# vi iptables-init.sh

iptablesの設定

#!/bin/sh

iptables -F
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

#---------------------------------------------------------------------------
# コネクション済のパケットを通過させる
#---------------------------------------------------------------------------
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#---------------------------------------------------------------------------
# pingに応答させる。pingに応答させないならコメントアウト
#---------------------------------------------------------------------------
iptables -A INPUT -p icmp -j ACCEPT

#---------------------------------------------------------------------------
# ローカルネットワークからの接続を許可する
#---------------------------------------------------------------------------
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp -s 192.168.0.0/16 -j ACCEPT
iptables -A INPUT -p udp -s 192.168.0.0/16 -j ACCEPT

#---------------------------------------------------------------------------
# 他の所有サーバや決まった出先など、
# 特別に許可するネットワークをIPで指定する
#---------------------------------------------------------------------------
#iptables -A INPUT -p tcp -s 111.222.xxx.xxx -j ACCEPT

#---------------------------------------------------------------------------
# 過去にアタックしてきた等、既知の危険なホストを拒否する
#---------------------------------------------------------------------------
# 2009/02/07 DNS attack
iptables -A INPUT -s 89.149.221.182 -j DROP
# 2009/02/21 DNS attack
iptables -A INPUT -s 62.109.4.89 -j DROP

#---------------------------------------------------------------------------
# 外向きに公開するサービスのポートを空ける
# ここでは pop や ssh などのパスワード付サービスは開けない
#---------------------------------------------------------------------------
iptables -A INPUT -p udp --dport domain -j ACCEPT
iptables -A INPUT -p udp --dport ntp -j ACCEPT
iptables -A INPUT -p tcp --dport http -j ACCEPT
iptables -A INPUT -p tcp --dport https -j ACCEPT
iptables -A INPUT -p tcp --dport smtp -j ACCEPT
iptables -A INPUT -p tcp --dport ftp -j ACCEPT
iptables -A INPUT -p tcp --dport submission -j ACCEPT

#---------------------------------------------------------------------------
# パスワード付サービスをアクセス回数制限付で空ける
# 以下は各IPごとに1時間に30回のアクセス制限
# (例)5/m:1分5回まで、10/h:1時間10回まで、30/d:1日30回まで
#---------------------------------------------------------------------------
iptables -A INPUT -p tcp --dport 22 -m hashlimit --hashlimit-name ssh --hashlimit 30/h --hashlimit-mode srcport --hashlimit-burst 10 -j ACCEPT

設定反映、保存

# sh iptables-init.sh
# service iptables save


Guest Additions のインストール

Guest Additions のマウント

# mkdir /mnt/cdrom
# mount /dev/sr0 /mnt/cdrom
# cd /mnt
# ./VBoxLinuxAdditions.run

ログはこんな感じ。

Verifying archive integrity... All good.
Uncompressing VirtualBox 4.1.20 Guest Additions for Linux.........
VirtualBox Guest Additions installer
Removing installed version 4.1.20 of VirtualBox Guest Additions...
Removing existing VirtualBox DKMS kernel modules           [  OK  ]
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module                   [  OK  ]
Building the shared folder support module                  [  OK  ]
Building the OpenGL support module                         [  OK  ]
Doing non-kernel setup of the Guest Additions              [  OK  ]
Starting the VirtualBox Guest Additions                    [  OK  ]
Installing the Window System drivers                       [FAILED]
(Could not find the X.Org or XFree86 Window System.)

最後に「FAILED」があるけどこれはGUIを使ってない場合に出るので今は問題なし。


必要なソフトウェアのインストール・設定

remiレポジトリの導入

最新バージョンをインストールするためにremiレポジトリを入れておく。

# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpm
# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
# vi /etc/yum.repos.d/remi.repo

Apache

# yum -y install httpd

2.2.15がインストールされる。

サーバ起動時に起動するようにchkconfigを変更。

# sbin/chkconfig httpd on

設定

.htaccessを使えるようにする

# vi /etc/httpd/conf/httpd.conf
AllowOverride None
 ↓
AllowOverride All


MySQL

# yum -y install mysql-server mysql-devel mysql-lib

5.5.28が入った。

設定

文字コードをUTF-8にするため/etc/my.cnfに下記の設定を追加。

# vi /etc/my.cnf
[client]
default-character-set = utf8

[mysqld]
character-set-server = utf8

サーバ起動時に起動するようにchkconfigを変更。

# sbin/chkconfig mysqld on

起動して初期設定を行う。

# service mysqld start
# mysql_secure_installation 

「rootパスワード無し」「rootのremoteからのアクセス禁止」で設定。


PHP

yum -y install php php-devel php-pear php-mbstring php-gd php-mysql php-xml

5.3.18が入った。

設定

date.timezoneを設定しないと次の警告が表示される。

PHP Warning:  date(): It is not safe to rely on the system's timezone settings.

You are == required* to use the date.timezone setting or the date_default_timezone_set() function. ==

In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier.
We selected 'Asia/Tokyo' for 'JST/9.0/no DST' instead in /usr/share/pear/symfony/config/sfRootConfigHandler.class.php on line 91

放って置いても良いのだけど鬱陶しいので設定。

# vi /etc/php.ini
date.timezone = 'Asia/Tokyo'


Xdebugのエラー表示が崩れてしまうのを防ぐため、html_errorsをOnに。

html_errors = On

PEAR/PECLライブラリのインストール

インストールするのは下記の通り。

  • PEAR
    • Benchmark
    • Cache_Lite
    • DB
    • HTTP_Client
    • XML_RPC
  • PECL
    • APC
    • Memcached
    • Xdebug

すべてyumでインストール可能。

# yum -y install php-pear-Benchmark php-pear-Cache-Lite php-pear-DB php-pear-HTTP-Client php-pear-XML-RPC
# yum -y install php-pecl-apc php-pecl-memcached php-pecl-xdebug

PECLモジュールの読み込みは/etc/php.d以下に配置される設定ファイルで行われるので、/etc/php.iniに追記する必要はない。


汎用性を損なうためインストールしないソフトウェア

FTP

現在はセキュリティの観点からFTPは使用しないのが常識なので入れない。~ 便利なんですけど平文ですからね。

Symfony

フレームワークはプロジェクト毎に変わりうるので入れない。

キャッシュ・ミドルウェア(memcacheなど)

運用の仕方によって変わるので、ここでは入れない。

仮想アプライアンスのエクスポート

必要なソフトウェアのインストールと最低限の設定が終わったらエクスポートする。

手元のファイルでは734MB。インポートは30秒くらいで終わる。