LoginSignup
96

More than 5 years have passed since last update.

MacにVagrant、Cent OS、apacheをインストール

Last updated at Posted at 2013-05-26

手軽に仮想環境を構築できるVagrantを試しました。
VagrantでCent OSを構築、続けてCent OSにapacheをインストールして、ホストのMacからapacheのテストページを表示します。

まずVagrantのインストール。
http://downloads.vagrantup.com/ のVagrant-1.2.2.dmgをインストールしました。

インストール後、VagrantにCent OSのBoxを追加します。
BoxはVagrantで使用できる仮想環境のパッケージです。
次のサイトでBoxが配布されています(本家)。
http://www.vagrantbox.es/
また、githubでも様々なBoxがあります。
例えば「Vagrant」と「PHP」で検索すれば、Laravelの開発環境が整ったBoxなどが見つかりました。

今回は本家BoxのCent OSを追加します。
Macのターミナルから次を入力。centosという名前でBoxを追加です。
(Virtual Box用なので別途Virtual Boxはインストールが必要)

$ vagrant box add centos http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130427.box

initすると、centosの設定ファイルVagrantfileが作成されます。

$ vagrant init centos

Vagrantfileを編集して、ホストのMacから接続できるIPアドレスを設定します。
次がコメントアウトされているので、コメントを外します。


   config.vm.network :private_network, ip: "192.168.33.10"

centosを起動します。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant

sshで接続します。

$ vagrant ssh

apacheのインストール、自動起動の設定、起動。

$ sudo yum install httpd
$ sudo  sudo chkconfig --level 345 httpd on
$ sudo /etc/init.d/httpd start

ポート80を外側からアクセスできるようにiptablesを設定。

$ sudo vi /etc/sysconfig/iptables

次の行を追加して保存します。


-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

そしてiptablesを再起動。

$ sudo /etc/init.d/iptables restart

これでMacのブラウザから192.168.33.10にアクセスするとApacheのテストページが表示されます。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
96