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を理解するのに役立ちます

推薦する

Ping An クラウドネイティブデータベースの開発と実践

今号のゲストスピーカー:平安科技のシニアR&Dエンジニア、宋歌氏紹介: Ping An Te...

ウェブサイト運営=SEO?

はじめに: 新しい Web サイトが立ち上げられ、すべてのプログラムと機能が準備されると、Web マ...

医療業界SEOチームのSEO編集作業の焦点

著者は、多くの SEO チームが医療業界のプロモーション業務における SEO 編集に十分な注意を払っ...

外国人住宅交換観光ウェブサイトのモデルの分析:魅力と恐怖

ホームエクスチェンジ観光が盛んになって以来、安全問題は常に懸念事項であり、特に「家を買うためだけに一...

SEOブログの収益モデルは心配、自己娯楽が主流になる可能性

実は、SEOブログは市場の再編に直面し始めています。多くのSEOブログはすでに閉鎖のジレンマに直面し...

エッジコンピューティングは 2021 年の新しいクラウドになるのでしょうか?

業界の専門家は、レイテンシを削減し、パーソナライズされたコンテンツ配信とカスタマイズされたセキュリテ...

クライアントの非現実的な SEO の夢についての簡単な議論

個別に注文を受ける方は、さまざまなお客様に出会うことになります。こうしたお客様とどのように接していま...

左に垂直検索、右に百度検索

当時、検索大手のグーグルはウェブサイトのトラフィック転換でボトルネック期を迎え、成長が鈍化していたが...

ASO最適化とプロモーションの総合ガイド、ダウンロード数を2倍にしたAPPプロモーションの実践的なヒントを共有!

3か月で、私はASOの完全な初心者から基本的な知識を身につけました。ASOに関する記事を100以上読...

キーワードに焦点を当ててウェブサイトのコンテンツを作成することは無駄ではない

検索エンジンの大幅な拡大に伴い、キーワードの役割はますます重要になってきました。人間の社会生活のペー...

IaaS とは何ですか? PaaSとは何ですか? SaaS とは何ですか?

最近、友人から「IaaS とは何ですか?」と尋ねられました。 PaaSとは何ですか? SaaS とは...

racknerd: 複数の赤い封筒付き VPS、年間 35 ドル、KVM/4G メモリ/3 コア/45g ハード ドライブ/6T トラフィック、ロサンゼルス データ センター、Alipay 対応

ウェブマスターは、ラックナードの公式レッドエンベローププレゼント活動を開始しました。ここでは、公式の...

dedipath: 月額 139 ドル、1Gbps 帯域幅、無制限トラフィック、ラスベガス、デュアルソケット e5-2630L/32g メモリ/12T ハードドライブ

Dedipath のラスベガス データ センターには、コスト効率が非常に優れ、デュアル コア e5 ...

Rhino Cloud Enterprise Cloud ウェブサイト: ウェブサイトの構築について話すとき、私たちは何について話しているのでしょうか?

2018年最もホットなプロジェクト:テレマーケティングロボットがあなたの参加を待っています企業がオン...