頑張って、長続きしたい。

メモ書きをひたすら書く。継続は力なり。

MacにVirtualBoxとVagrant入れて、CentOS7.0にDjango環境を構築した。

タイトルの通り。
Mac OSのバージョンは10.9.4

VirtualBoxVagrantのインストール

Homebrewを使ってインストール

brew cask install virtualbox
brew cask install vagrant

バージョンは
VirtualBox:4.3.16
Vagrant:1.6.5

CentOS7.0のBoxを追加

以下のURLから使用したいBoxを選ぶ。
A list of base boxes for Vagrant - Vagrantbox.es

今回はCentOS7.0のBoxを選択。

$ vagrant box add centos70 https://f0fff3908f081cb6461b407be80daf97f07ac418.googledrive.com/host/0BwtuV7VyVTSkUG1PM3pCeDJ4dVE/centos7.box
==> box: Adding box 'centos70' (v0) for provider:
    box: Downloading: https://f0fff3908f081cb6461b407be80daf97f07ac418.googledrive.com/host/0BwtuV7VyVTSkUG1PM3pCeDJ4dVE/centos7.box
==> box: Successfully added box 'centos70' (v0) for 'virtual box’!

仮想マシンの作成・起動

ダウンロードしたBoxを使って仮想マシンを作成・起動してみる。
$ mkdir -p ~/Vagrant/CentOS70
$ cd ~/Vagrant/CentOS70
$ vagrant init centos70
$ vagrant up

すると以下の様なマウントできねぇよ!みたいなエラーが出てくる。

Failed to mount folders in Linux guest. This is usually because
the "vboxsf" file system is not available. Please verify that
the guest additions are properly installed in the guest and
can work properly. The command attempted was:

mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant

The error output from the last command was:

/sbin/mount.vboxsf: mounting failed with the error: No such device

ちょっと調べると解決方法が見つかるのでそれを実行。

vagrantでmountエラーの解決方法 - Qiita

$ vagrant ssh
[vagrant@localhost ~]$ sudo /etc/init.d/vboxadd setup
[vagrant@localhost ~]$ exit
$ vagrant reload

無事エラーが出ずに仮想マシンの起動に成功!

Djangoの開発環境構築

まず、Pythonのバージョンを確認。

[vagrant@localhost ~]$ python -V
Python 2.7.5

次にpipをインストール。

[vagrant@localhost ~]$ wget http://peak.telecommunity.com/dist/ez_setup.py
[vagrant@localhost ~]$ python ez_setup.py
Setuptools version 0.6c11 or greater has been installed.
(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)
# 既に入ってたorz
[vagrant@localhost ~]$ sudo easy_install pip

そしてDjangoをインストールと確認。

[vagrant@localhost ~]$ sudo pip install django
[vagrant@localhost ~]$ python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print django.get_version()
1.7
>>> exit()

Djangoのプロジェクトを作成・サーバー起動

[vagrant@localhost ~]$ mkdir django
[vagrant@localhost ~]$ cd django/
[vagrant@localhost django]$ django-admin.py startproject helloworld
[vagrant@localhost django]$ cd helloworld/
[vagrant@localhost helloworld]$ python manage.py migrate
[vagrant@localhost helloworld]$ python manage.py runserver

無事成功したが、仮想マシン上でのサーバー起動なため、単純にWebブラウザからアクセスできない。
Vagrantfileを編集することで、指定したポートへのアクセス(ホストOS)をゲストOSの指定ポートに転送してくれるみたい。

Vagrant で Python(Django) 開発環境を準備する - Qiita

# config.vm.network "forwarded_port", guest: 80, host: 8080
# 以下に変更
config.vm.network "forwarded_port", guest: 8000, host: 8080

仮想マシンを再起動、もう一度Djangoプロジェクトのサーバーを起動。

$ vagrant reload
$ vagrant ssh
[vagrant@localhost ~]$ cd django/helloworld/
[vagrant@localhost helloworld]$ python manage.py runserver 0.0.0.0:8000

Webブラウザからlocalhost:8080にアクセス・・・できない。

悩むこと1日。あ、ポート開放してないじゃんと気づく。
CentOS7.0からはfirewallコマンドというのを使って設定するらしい。

[vagrant@localhost helloworld]$ sudo firewall-cmd --permanent --add-port=8000/tcp
[vagrant@localhost helloworld]$ sudo systemctl restart firewalld.service

そしてもう一度Webブラウザからlocalhost:8080にアクセスするとIt Works!の文字が!