Vagrant をインストールして設定するにはどうすればいいですか?

Vagrant をインストールして設定するにはどうすればいいですか?

Vagrant は仮想マシン用の強力なツールです。ここでは、Ubuntu 上で Virtualbox と Vagrant をセットアップして使用し、再現可能な仮想マシンをプロビジョニングする方法について説明します。

仮想マシンは複雑ではない

開発者は長年にわたり、ワークフローの一部として仮想マシンを使用してきました。これにより、ソフトウェアが実行される環境を交換および変更することができ、通常はプロジェクト A では PHP 5.3 が必要でプロジェクト B では PHP 5.4 が必要であるなど、プロジェクト間の競合を防ぐことができます。

また、仮想マシンを使用すると、運用環境をミラーリングするための専用ハードウェアではなく、使用しているコンピューターのみが必要になります。

また、複数の開発者がプロ​​ジェクトに取り組んでいて、全員がすべての要件を備えた環境を実行できる場合にも便利ですが、複数のマシンを維持し、すべての要件が同じバージョンであることを確認するのは難しい場合があります。ここで Vagrant が役立ちます。

仮想マシンを使用する利点

  • 仮想マシンはホスト環境とは別です
  • コード要件に合わせてカスタム VM をカスタマイズできます。
  • 他の仮想マシンには影響しません
  • ホスト上で実行できないプログラム、例えばWindowsでしか実行できないソフトウェアをUbuntuで実行することができます。

Vagrantとは

つまり、仮想マシンを操作し、仮想マシンの作成と削除を自動化できるツールです。

これは、インストールするオペレーティング システムや、IP やディレクトリ同期などのその他のオプションを Vagrant に伝えるVagrantFileと呼ばれる構成ファイルを中心に機能します。仮想マシンに構成スクリプトを追加することもできます。

このVagrantFile共有することで、プロジェクトのすべての開発者がまったく同じ仮想マシンを使用できるようになります。

インストール要件

VirtualBoxをインストールする

VirtualBox は仮想マシンを実行するプログラムであり、Ubuntu リポジトリからインストールできます。

  1. sudo apt-get install virtualbox

Vagrantをインストールする

Vagrant 自体については、https://www.vagrantup.com/downloads.html にアクセスして、ご使用のオペレーティング システムで使用できるインストール パッケージを確認してください。

拡張機能のインストール

仮想マシンとフォルダーを共有する予定の場合は、次のプラグインをインストールする必要があります。

  1. vagrant plugin install vagrant - vbguest

Vagrantの設定

まず、Vagrant 用のフォルダーを作成する必要があります。

  1. mkdir ~ /Vagrant/ test - vm
  2. cd ~ /Vagrant/ test - vm

VagrantFile を作成します:

  1. vagrant init

仮想マシンを起動します。

  1. vagrant up

マシンにログインします:

  1. vagrant - ssh

この時点で、基本的な Vagrant マシンとVagrantFileというファイルが作成されます。

カスタムメイド

上記の手順で作成されたVagrantFile次のようになります。

Vagrantファイル:

  1. # -*- mode : ruby -*-
  2. # vi : set ft = ruby :
  3. # All Vagrant configuration is done below . The "2" in Vagrant . configure
  4. # configures the configuration version ( we support older styles for
  5. # backwards compatibility ). Please don 't change it unless you know what
  6. # you' re doing .
  7. Vagrant . configure ( "2" ) do | config |
  8. # The most common configuration options are documented and commented below .
  9. # For a complete reference , please see the online documentation at
  10. # https : //docs.vagrantup.com.
  11. # Every Vagrant development environment requires a box . You can search for
  12. # boxes at https : //vagrantcloud.com/search.
  13. config . vm . box = "base"
  14. # Disable automatic box update checking . If you disable this , then
  15. # boxes will only be checked for updates when the user runs
  16. # `vagrant box outdated` . This is not recommended .
  17. # config . vm . box_check_update = false
  18. # Create a forwarded port mapping which allows access to a specific port
  19. # within the machine from a port on the host machine . In the example below ,
  20. # accessing "localhost:8080" will access port 80 on the guest machine .
  21. # NOTE : This will enable public access to the opened port
  22. # config . vm . network "forwarded_port" , guest : 80 , host : 8080
  23. # Create a forwarded port mapping which allows access to a specific port
  24. # within the machine from a port on the host machine and only allow access
  25. # via 127.0 . 0.1 to disable public access
  26. # config . vm . network "forwarded_port" , guest : 80 , host : 8080 , host_ip : "127.0.0.1"
  27. # Create a private network , which allows host - only access to the machine
  28. # using a specific IP .
  29. # config . vm . network "private_network" , ip : "192.168.33.10"
  30. # Create a public network , which generally matched to bridged network .
  31. # Bridged networks make the machine appear as another physical device on
  32. # your network .
  33. # config . vm . network "public_network"
  34. # Share an additional folder to the guest VM . The first argument is
  35. # the path on the host to the actual folder . The second argument is
  36. # the path on the guest to mount the folder . And the optional third
  37. # argument is a set of non - required options .
  38. # config . vm . synced_folder "../data" , "/vagrant_data"
  39. # Provider - specific configuration so you can fine - tune various
  40. # backing providers for Vagrant . These expose provider - specific options .
  41. # Example for VirtualBox :
  42. #
  43. # config . vm . provider "virtualbox" do | vb |
  44. # # Display the VirtualBox GUI when booting the machine
  45. # vb . gui = true
  46. #
  47. # # Customize the amount of memory on the VM :
  48. # vb . memory = "1024"
  49. # end
  50. #
  51. # View the documentation for the provider you are using for more
  52. # information on available options .
  53. # Enable provisioning with a shell script . Additional provisioners such as
  54. # Puppet , Chef , Ansible , Salt , and Docker are also available . Please see the
  55. # documentation for more information about their specific syntax and use .
  56. # config . vm . provision "shell" , inline : <<- SHELL
  57. # apt-get update
  58. # apt-get install - y apache2
  59. # SHELL
  60. end

これで、このVagrantFileによって基本的な仮想マシンが作成されます。しかし、Vagrant の背後にある考え方は、特定のタスク用に仮想マシンを構成することなので、コメントを削除して構成を調整します。

Vagrantファイル:

  1. # -*- mode : ruby -*-
  2. # vi : set ft = ruby :
  3. Vagrant . configure ( "2" ) do | config |
  4. # Set the Linux Version to Debian Jessie
  5. config . vm . box = "debian/jessie64"
  6. # Set the IP of the Box
  7. config . vm . network "private_network" , ip : "192.168.33.10"
  8. # Sync Our Projects Directory with the WWW directory
  9. config . vm . synced_folder "~/Projects" , "/var/www/"
  10. # Run the following to Provision
  11. config . vm . provision "shell" , path : "install.sh"
  12. end

これで、Linux バージョンを debian jessie に設定し、使用する IP を設定し、必要なフォルダーを同期し、最後にinstall.shを実行してシェル コマンドを実行できる、シンプルなVagrantFileができました。

インストール.sh:

  1. #! / usr / bin / env bash
  2. # Variables
  3. DBHOST = localhost
  4. DBNAME = dbname
  5. DBUSER = dbuser
  6. DBPASSWD = test123
  7. echo "[ Provisioning machine ]"
  8. echo "1) Update APT..."
  9. apt-get - qq update
  10. echo "1) Install Utilities..."
  11. apt-get install - y tidy pdftk curl xpdf imagemagick openssl vim git
  12. echo "2) Installing Apache..."
  13. apt-get install - y apache2
  14. echo "3) Installing PHP and packages..."
  15. 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
  16. # php5 - suhosin - extension , php5 - mysqlnd
  17. echo "4) Installing MySQL..."
  18. debconf - set - selections <<< "mysql-server mysql-server/root_password password secret"
  19. debconf - set - selections <<< "mysql-server mysql-server/root_password_again password secret"
  20. apt-get install - y mysql - server
  21. mysql - uroot - p$DBPASSWD - e "CREATE DATABASE $DBNAME"
  22. mysql - uroot - p$DBPASSWD - e "grant all privileges on $DBNAME.* to '$DBUSER'@'localhost' identified by '$DBPASSWD'"
  23. echo "5) Generating self signed certificate..."
  24. mkdir - p / etc / ssl / localcerts
  25. 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
  26. chmod 600 / etc / ssl / localcerts / apache *
  27. echo "6) Setup Apache..."
  28. a2enmod rewrite
  29. > /etc/ apache2 / sites - enabled / 000 - default . conf
  30. echo "
  31. <VirtualHost *:80>
  32. ServerAdmin webmaster@localhost
  33. DocumentRoot /var/www/
  34. ErrorLog ${APACHE_LOG_DIR}/error.log
  35. CustomLog ${APACHE_LOG_DIR}/access.log combined
  36. </VirtualHost>
  37. " >> /etc/ apache2 / sites - enabled / 000 - default . conf
  38. service apache2 restart
  39. echo "7) Composer Install..."
  40. curl -- silent https : //getcomposer.org/installer | php
  41. mv composer . phar / usr / local / bin / composer
  42. echo "8) Install NodeJS..."
  43. curl - sL https : //deb.nodesource.com/setup_6.x | sudo -E bash -
  44. apt-get - qq update
  45. apt-get - y install nodejs
  46. echo "9) Install NPM Packages..."
  47. npm install - g gulp gulp - cli
  48. echo "Provisioning Completed"

上記の手順を実行すると、ディレクトリにVagrantFileinstall.shが作成されます。 vagrant を実行すると、次のことが行われます。

  • Debian Jessie を使用した仮想マシンの作成
  • マシンのIPを192.168.33.10に設定する
  • ~/Projects/var/www/ディレクトリを同期する
  • Apache、Mysql、PHP、Git、Vim をインストールして設定する
  • Composerをインストールして実行する
  • Nodejsとgulpをインストールする
  • MySQLデータベースを作成する
  • 自己署名証明書の作成

VagrantFileinstall.sh他の人と共有することで、2 つの異なるマシンでまったく同じ環境を使用できます。

<<:  VR 仮想現実の簡単な分析: VR 仮想現実技術とは何ですか?

>>:  この記事はIaaS、PaaS、SaaSを理解するのに役立ちます

推薦する

疑似オリジナリティとは実際には何を意味するのでしょうか?

ウェブマスターの友人にとって、「疑似オリジナリティ」という言葉は馴染みのない言葉ではありませんが、す...

ハイブリッドクラウドの導入が依然として低い理由

数年前、ハイブリッド クラウド バーストの概念は非常に魅力的でした。プライベート クラウドとパブリッ...

データベースをAmazon Web Servicesに簡単に移行

今日では、企業はデータベースなしではクラウドに移行できません。ただし、クラウド上にはさまざまな種類の...

テンセントクラウドは「ダブル11」で主流の電子商取引のクラウド使用量を2倍にし、星星海はコンピューティングパワー需要の90%をサポートした。

11月12日、テンセントクラウドは、ダブル11期間中にプラットフォーム上の主流電子商取引企業のクラウ...

「形式的な最適化」は、BaiduのWeb2.0スパム対策メカニズムをトリガーする可能性もあります。

SEO は比較的人気のあるオンライン マーケティングの形態であり、比較的安価で安定したオンライン マ...

ベテランが段階的に外部リンクスペシャリストを「育成」する方法

SEO スーパーバイザーは、最適化プランの全体的な構築だけでなく、より重要な最適化アイデアのカバーに...

justhost: ノボシビルスク・アドマン・データセンターの無制限トラフィックVPSの簡単なレビュー

justhost は、ロシア極東のノボシビルスク データ センターで、デフォルトの最小帯域幅 200...

新年最初の仕事の日に、本番環境の分散ファイルシステムがクラッシュしました!

[[383073]]著者は、正確にスケジュールされたタスクと遅延キュー処理機能を備えた、高同時実行シ...

ファーウェイの石耀宏氏:産業のアップグレードの重要な10年、ファーウェイのクラウドとコンピューティングは企業の追い越し達成を支援する

6月24日、国家発展改革委員会、科学技術部、工業情報化部、国家ラジオテレビ総局、中国サイバースペース...

VMware が Gartner の統合エンドポイント管理マジック クアドラントで 5 年連続リーダーに選出

VMware (NYSE: VMW) は、ガートナー社の 2022 年統合エンドポイント管理 (UE...

もう一つの 10 億ドルの教訓: Blog.com の崩壊 (パート 2)

著者プロフィール: 林俊は、CITIC Press および Blue Lion の契約ライターであり...

サイト全体の最適化 VS キーワードの最適化、本当の「王」は誰でしょうか?

SEO ウェブサイトの最適化といえば、誰もがキーワード ランキングの最適化を思い浮かべると思います。...

中国オートレンタルはウォーバーグ・ピンカスから2億ドルの投資を受けるが、これはIPOの資金調達額を上回る額である。

7月9日の午後、CAR社とウォーバーグ・ピンカスは、ウォーバーグ・ピンカスがCAR社に2億ドルの株式...

#移瓦工VPS# bandwagonhost ロサンゼルス MC データセンター/ニューヨーク データセンターを追加 + 評価

Bandwagonhost VPS は、ロサンゼルスの Multacom とニューヨークの Mult...

Perfect Diary は完璧になれるでしょうか?

Perfect Diaryはソーシャル電子商取引と国民的ファッションのトレンドを活用し、資金の助けを...