Vagrant は仮想マシン用の強力なツールです。ここでは、Ubuntu 上で Virtualbox と Vagrant をセットアップして使用し、再現可能な仮想マシンをプロビジョニングする方法について説明します。 仮想マシンは複雑ではない開発者は長年にわたり、ワークフローの一部として仮想マシンを使用してきました。これにより、ソフトウェアが実行される環境を交換および変更することができ、通常はプロジェクト A では PHP 5.3 が必要でプロジェクト B では PHP 5.4 が必要であるなど、プロジェクト間の競合を防ぐことができます。 また、仮想マシンを使用すると、運用環境をミラーリングするための専用ハードウェアではなく、使用しているコンピューターのみが必要になります。 また、複数の開発者がプロジェクトに取り組んでいて、全員がすべての要件を備えた環境を実行できる場合にも便利ですが、複数のマシンを維持し、すべての要件が同じバージョンであることを確認するのは難しい場合があります。ここで Vagrant が役立ちます。 仮想マシンを使用する利点- 仮想マシンはホスト環境とは別です
- コード要件に合わせてカスタム VM をカスタマイズできます。
- 他の仮想マシンには影響しません
- ホスト上で実行できないプログラム、例えばWindowsでしか実行できないソフトウェアをUbuntuで実行することができます。
Vagrantとはつまり、仮想マシンを操作し、仮想マシンの作成と削除を自動化できるツールです。 これは、インストールするオペレーティング システムや、IP やディレクトリ同期などのその他のオプションを Vagrant に伝えるVagrantFile と呼ばれる構成ファイルを中心に機能します。仮想マシンに構成スクリプトを追加することもできます。 このVagrantFile 共有することで、プロジェクトのすべての開発者がまったく同じ仮想マシンを使用できるようになります。 インストール要件VirtualBoxをインストールするVirtualBox は仮想マシンを実行するプログラムであり、Ubuntu リポジトリからインストールできます。 -
sudo apt-get install virtualbox
VagrantをインストールするVagrant 自体については、https://www.vagrantup.com/downloads.html にアクセスして、ご使用のオペレーティング システムで使用できるインストール パッケージを確認してください。 拡張機能のインストール仮想マシンとフォルダーを共有する予定の場合は、次のプラグインをインストールする必要があります。 -
vagrant plugin install vagrant - vbguest
Vagrantの設定まず、Vagrant 用のフォルダーを作成する必要があります。 -
mkdir ~ /Vagrant/ test - vm -
cd ~ /Vagrant/ test - vm
VagrantFile を作成します: -
vagrant init
仮想マシンを起動します。 -
vagrant up
マシンにログインします: -
vagrant - ssh
この時点で、基本的な Vagrant マシンとVagrantFile というファイルが作成されます。 カスタムメイド上記の手順で作成されたVagrantFile 次のようになります。 Vagrantファイル: -
# -*- mode : ruby -*- -
# vi : set ft = ruby : -
# All Vagrant configuration is done below . The "2" in Vagrant . configure -
# configures the configuration version ( we support older styles for -
# backwards compatibility ). Please don 't change it unless you know what -
# you' re doing . -
Vagrant . configure ( "2" ) do | config | # The most common configuration options are documented and commented below . # For a complete reference , please see the online documentation at # https : //docs.vagrantup.com. # Every Vagrant development environment requires a box . You can search for # boxes at https : //vagrantcloud.com/search. config . vm . box = "base" # Disable automatic box update checking . If you disable this , then # boxes will only be checked for updates when the user runs # `vagrant box outdated` . This is not recommended . # config . vm . box_check_update = false # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine . In the example below , # accessing "localhost:8080" will access port 80 on the guest machine . # NOTE : This will enable public access to the opened port # config . vm . network "forwarded_port" , guest : 80 , host : 8080 # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine and only allow access # via 127.0 . 0.1 to disable public access # config . vm . network "forwarded_port" , guest : 80 , host : 8080 , host_ip : "127.0.0.1" # Create a private network , which allows host - only access to the machine # using a specific IP . # config . vm . network "private_network" , ip : "192.168.33.10" # Create a public network , which generally matched to bridged network . # Bridged networks make the machine appear as another physical device on # your network . # config . vm . network "public_network" # Share an additional folder to the guest VM . The first argument is # the path on the host to the actual folder . The second argument is # the path on the guest to mount the folder . And the optional third # argument is a set of non - required options . # config . vm . synced_folder "../data" , "/vagrant_data" # Provider - specific configuration so you can fine - tune various # backing providers for Vagrant . These expose provider - specific options . # Example for VirtualBox : # # config . vm . provider "virtualbox" do | vb | # # Display the VirtualBox GUI when booting the machine # vb . gui = true # # # Customize the amount of memory on the VM : # vb . memory = "1024" # end # # View the documentation for the provider you are using for more # information on available options . # Enable provisioning with a shell script . Additional provisioners such as # Puppet , Chef , Ansible , Salt , and Docker are also available . Please see the # documentation for more information about their specific syntax and use . # config . vm . provision "shell" , inline : <<- SHELL # apt-get update # apt-get install - y apache2 # SHELL -
end
これで、このVagrantFile によって基本的な仮想マシンが作成されます。しかし、Vagrant の背後にある考え方は、特定のタスク用に仮想マシンを構成することなので、コメントを削除して構成を調整します。 Vagrantファイル: -
# -*- mode : ruby -*- -
# vi : set ft = ruby : -
Vagrant . configure ( "2" ) do | config | # Set the Linux Version to Debian Jessie config . vm . box = "debian/jessie64" # Set the IP of the Box config . vm . network "private_network" , ip : "192.168.33.10" # Sync Our Projects Directory with the WWW directory config . vm . synced_folder "~/Projects" , "/var/www/" # Run the following to Provision config . vm . provision "shell" , path : "install.sh" -
end
これで、Linux バージョンを debian jessie に設定し、使用する IP を設定し、必要なフォルダーを同期し、最後にinstall.sh を実行してシェル コマンドを実行できる、シンプルなVagrantFile ができました。 インストール.sh: -
#! / usr / bin / env bash -
# Variables -
DBHOST = localhost -
DBNAME = dbname -
DBUSER = dbuser -
DBPASSWD = test123 -
echo "[ Provisioning machine ]" -
echo "1) Update APT..." -
apt-get - qq update -
echo "1) Install Utilities..." -
apt-get install - y tidy pdftk curl xpdf imagemagick openssl vim git -
echo "2) Installing Apache..." -
apt-get install - y apache2 -
echo "3) Installing PHP and packages..." -
apt-get install - y php5 libapache2 - mod - php5 libssh2 - php php - pear php5 - cli php5 - common php5 - curl php5 - dev php5 - gd php5 - imagick php5 - imap php5 - intl php5 - mcrypt php5 - memcached php5 - mysql php5 - pspell php5 - xdebug php5 - xmlrpc -
# php5 - suhosin - extension , php5 - mysqlnd -
echo "4) Installing MySQL..." -
debconf - set - selections <<< "mysql-server mysql-server/root_password password secret" -
debconf - set - selections <<< "mysql-server mysql-server/root_password_again password secret" -
apt-get install - y mysql - server -
mysql - uroot - p$DBPASSWD - e "CREATE DATABASE $DBNAME" -
mysql - uroot - p$DBPASSWD - e "grant all privileges on $DBNAME.* to '$DBUSER'@'localhost' identified by '$DBPASSWD'" -
echo "5) Generating self signed certificate..." -
mkdir - p / etc / ssl / localcerts -
openssl req - new - x509 - days 365 - nodes - subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" - out / etc / ssl / localcerts / apache . pem - keyout / etc / ssl / localcerts / apache . key -
chmod 600 / etc / ssl / localcerts / apache * -
echo "6) Setup Apache..." -
a2enmod rewrite -
> /etc/ apache2 / sites - enabled / 000 - default . conf -
echo " -
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined -
</VirtualHost> -
" >> /etc/ apache2 / sites - enabled / 000 - default . conf -
service apache2 restart -
echo "7) Composer Install..." -
curl -- silent https : //getcomposer.org/installer | php -
mv composer . phar / usr / local / bin / composer -
echo "8) Install NodeJS..." -
curl - sL https : //deb.nodesource.com/setup_6.x | sudo -E bash - -
apt-get - qq update -
apt-get - y install nodejs -
echo "9) Install NPM Packages..." -
npm install - g gulp gulp - cli -
echo "Provisioning Completed"
上記の手順を実行すると、ディレクトリにVagrantFile とinstall.sh が作成されます。 vagrant を実行すると、次のことが行われます。 - Debian Jessie を使用した仮想マシンの作成
- マシンのIPを192.168.33.10に設定する
~/Projects と/var/www/ ディレクトリを同期する- Apache、Mysql、PHP、Git、Vim をインストールして設定する
- Composerをインストールして実行する
- Nodejsとgulpをインストールする
- MySQLデータベースを作成する
- 自己署名証明書の作成
VagrantFile とinstall.sh 他の人と共有することで、2 つの異なるマシンでまったく同じ環境を使用できます。 |