LoginSignup
36
53

More than 5 years have passed since last update.

Vagrantでローカル開発環境構築

Last updated at Posted at 2016-08-10

目標

・XAMPP/MAMPからの移行

・プロジェクト案件ごとに仮想ドメインを指定できるようにし、同一構成の仮想環境を簡単に、そして複数作成できるようにする◎

VirtualBoxのインストール

  1. VirtualBoxにアクセス
  2. 「DOWNLOADS」押下
  3. 「VirtualBox platform packages」からOS選択

Vagrantのインストール

  1. Vagrantにアクセス
  2. 「DOWNLOAD」ボタンを押下
  3. OS選択
  4. terminal.appで「$ vagrant -v」でバージョンが返ってこればインストール完了

VagrantでCentOSを立ち上げる

terminal.app
$ mkdir [ディレクトリ名]  # 仮想マシンを入れるディレクトリ作成
$ cd [ディレクトリ名]  # 作成したディレクトリに移動
$ vagrant box add [Box名] [URL] # URLは「※VagrantBox」からURLをコピーしてくる
$ vagrant init [BOX名]  # Vagrant初期化
$ vi Vagrantfile # 192.168.33.10の行の#を削除し、コメントアウトを解除
$ vagrant up  # 仮想マシン起動

VagrantBox

.sshディレクトリのパーミッション変更

terminal.app
$ vagrant ssh # 仮想マシンへ接続
[vagrant@localhost ~]$ chmod 0700 /home/vagrant/.ssh
[vagrant@localhost ~]$ chmod 0600 /home/vagrant/.ssh/authorized_keys
[vagrant@localhost ~]$ chown -R vagrant /home/vagrant/.ssh

LAMP環境構築

terminal.app
$ vagrant ssh # 仮想マシンへ接続
[vagrant@localhost ~]$ sudo yum -y update

Apacheのインストール

terminal.app
[vagrant@localhost ~]$ sudo yum -y install httpd
[vagrant@localhost ~]$ sudo service httpd start # Apache起動
[vagrant@localhost ~]$ sudo chkconfig httpd on # 自動起動設定(任意)

MySQLのインストール ※mysql5.6の場合

terminal.app
[vagrant@localhost ~]$ wget http://downloads.mysql.com/archives/get/file/MySQL-client-5.6.20-1.linux_glibc2.5.x86_64.rpm
[vagrant@localhost ~]$ wget http://downloads.mysql.com/archives/get/file/MySQL-server-5.6.20-1.linux_glibc2.5.x86_64.rpm
[vagrant@localhost ~]$ wget http://downloads.mysql.com/archives/get/file/MySQL-shared-compat-5.6.20-1.linux_glibc2.5.x86_64.rpm
[vagrant@localhost ~]$ wget http://downloads.mysql.com/archives/get/file/MySQL-devel-5.6.20-1.linux_glibc2.5.x86_64.rpm

[vagrant@localhost ~]$ sudo yum install MySQL-client-5.6.20-1.linux_glibc2.5.x86_64.rpm
[vagrant@localhost ~]$ sudo yum install MySQL-server-5.6.20-1.linux_glibc2.5.x86_64.rpm
[vagrant@localhost ~]$ sudo yum install MySQL-shared-compat-5.6.20-1.linux_glibc2.5.x86_64.rpm
[vagrant@localhost ~]$ sudo yum install MySQL-devel-5.6.20-1.linux_glibc2.5.x86_64.rpm

[vagrant@localhost ~]$ sudo service mysqld start # MySQL起動
[vagrant@localhost ~]$ sudo chkconfig mysqld on # 自動起動設定(任意)

PHPのインストール

terminal.app
[vagrant@localhost ~]$ yum info|grep php # phpのバージョンなどを確認してインストールする

centos6xの場合
[vagrant@localhost ~]$ sudo rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
[vagrant@localhost ~]$ sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm    

[vagrant@localhost ~]$ sudo yum -y install --enablerepo=remi --enablerepo=remi-php56 php php-mbstring php-mcrypt php-pear php-mysqli # PHP5.6インストールする場合の例

シンボリックリンク設定

terminal.app
[vagrant@localhost ~]$ sudo rm -r /var/www/html
[vagrant@localhost ~]$ sudo ln -s /vagrant /var/www/html

仮想ドメインの設定

仮想サーバーの設定

terminal.app
[vagrant@localhost ~]$ sudo vi /etc/httpd/conf.d/vhosts.conf
vhosts.conf
    NameVirtualHost *:80
    <VirtualHost *:80>
        ServerName vagrant.dev
        DocumentRoot /var/www/html/

        <Directory "/usr/local/vhosts/vhost1">
            Options All
            AllowOverride All
            Order Allow,Deny
            Allow from all
        </Directory>
    </VirtualHost>

    # vhost1
    <VirtualHost *:80>
        ServerName vhost1.vagrant.dev
        DocumentRoot /var/www/html/vhost1

        <Directory "/var/www/html/vhost1">
            Options All
            AllowOverride All
            Order Allow,Deny
            Allow from all
        </Directory>
    </VirtualHost>

ローカル環境の設定

terminal.app
$ sudo vi /etc/hosts
hosts
    ##
    # Host Database
    #
    # localhost is used to configure the loopback interface
    # when the system is booting.  Do not change this entry.
    ##
    127.0.0.1       localhost
    255.255.255.255 broadcasthost
    ::1             localhost
    fe80::1%lo0     localhost
    192.168.33.10   vhost1.vagrant.dev  ←vagrantのIPの設定を追加

36
53
0

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
36
53