
軟骨(ヘリックス)のピアス探しに!ボディピアスブランドおすすめいろいろ
- Life ファッション
新着記事
人気記事
Web制作でのローカル開発環境ツールといえばXamppとVagrantが人気ですが、案件ごとでWebサーバーやPHPなどのバージョンを別で構築できたりなど自由度の高いVagrantでローカル開発環境を構築しました。
vagrant upしたときに、Authentication failure.というエラーでつまづきもしましたが環境構築することができたので順を追ってみていきましょう。
Vagrantは仮想マシンの操作を簡単にしてくれるコマンドツールです。本来であれば仮想マシンで開発環境を構築するには複雑なコマンドを使い労力がとてもかかりますが、Vagrant利用することで簡単に開発環境の構築や共有をすることができます。
VirtualBoxは使用しているPC上で仮想のPCを作成して別のOSを使うことのできる仮想ソフトです。先述したとおり、VagrantはVirtualBoxのような仮想ソフトで機能します。
まずは仮想環境の本体になるVirtualBoxを下記のページからダウンロードをします。
ダウンロードページはこちら
ダウンロードページへアクセスするとこのようなページになるので、使用しているPCのOSを選択してダウンロードとインストールをします。
VirtualBoxのインストールができたらVagarntを下記のページからダウンロードをします。
ダウンロードページはこちら
ダウンロードページへアクセスするとこのようなページになるので、VirtualBoxと同じように使用しているPCのOSを選択してダウンロードとインストールをします。
インストールが終わったらコマンドプロンプトでインストールの確認をしてみましょう。
コマンドプロンプトはWindosメニュー > すべてのアプリ > Windosシステムツール > コマンドプロンプト で起動できます。
vagrant -v
上記のコマンドを入力してVagarntのバージョンが表示されれば成功しています!
Boxファイルというのは、仮想マシンを起動したときのテンプレートになるイメージファイルで、自分で作成することもできますが、すでに作成されているファイルを利用する方が早いです。
公開されているBoxファイルを検索できるサイトがあるので、そこから起動させたいOSなどを探しましょう。
Boxファイルの検索サイト
Atlas
サイトへアクセスするとこのようなページが表示されるのでフォームに使用したいOSなどを入力して検索します。例えばcentOSで検索をします。すると、centOSのBoxファイル一覧が表示されます。
今回はcentOS7を使用したかったのでbento/centos-7.1を利用します。
bentoというのはChef社が公開しているBoxファイルで安心して利用することができるとのことでした。
bento/centos-7.1をクリックすると上記画像のページが表示されます。
providerというのは仮想マシンのことで、今回はVirtualBoxを利用するのでvirtualboxを探します。
ここで記載されている
vagrant init bento/centos-7.1 vagrant up --provider virtualbox
というコマンドを後で入力します。
任意の場所にVagarnt用フォルダを作成して、さらにBoxファイルごとでファイルが生成されるのでBoxファイル用のフォルダも作成しておきます。こうしておくことで後々の管理が分かりやすくなります。
コマンドプロンプトで今回はローカルディスクにVagarnt用フォルダを作成しました。
mkdir C:\Vagrant //Vagrantフォルダ作成 cd C:\Vagrant //Vagrantフォルダへ移動 mkdir centos71 //Boxファイル用にcentos71というフォルダを作成 cd centos71 //centos71フォルダへ移動
Vagrantfileは仮想マシンの構成が記述されているファイルです。ここに起動する仮想マシンの指定や共有フォルダの指定などが記述されています。
先程、検索したBoxファイルのコマンドをcentos71フォルダへ移動した状態でコマンドプロンプトから実行します。
vagrant init bento/centos-7.1
これでcentos71フォルダの中にVagrantfileが作成されました。
作成されたVagrantfileをテキストエディタで開いてコメントアウトを一部はずします。
# -*- 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://atlas.hashicorp.com/search. config.vm.box = "bento/centos-7.1" # 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. # config.vm.network "forwarded_port", guest: 80, host: 8080 # 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. # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies # such as FTP and Heroku are also available. See the documentation at # https://docs.vagrantup.com/v2/push/atlas.html for more information. # config.push.define "atlas" do |push| # push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME" # end # 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
Boxファイルを検索したときには起動のコマンドが
vagrant up --provider virtualbox
となっていましたが、Vagrantfileの編集でVirtualBoxを起動させる記述のコメントアウトを外してあげたので
vagrant up
上記だけのコマンドで仮想マシンを起動することができます。
特にエラーも出ずにVirtualBoxが立ち上がれば成功です!
C:\vagrant\centos71>vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Checking if box 'bento/centos-7.1' is up to date... ==> default: Clearing any previously set forwarded ports... ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat default: Adapter 2: hostonly ==> default: Forwarding ports... default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Remote connection disconnect. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying...
今回、起動すると上記のような画面でループしてタイムアウトされてしまうという事態が起こりました。。。
どうやら仮想マシンの起動はできているがローカル側と仮想側の認証鍵が合っていないようでした。
認証鍵が合っていないということなので、新たに認証鍵を作成してローカル側と仮想側へ設置してあげることで解決ができます。
今回は、SSHのログインなどで利用しているTera Termで認証鍵を作成しました。
Tera Termを起動すると接続設定のポップアップが現れるのでキャンセルをクリックして、設定 > SSH鍵生成 から生成ボタンをクリックします。鍵の種類はデフォルトのRSAのままで大丈夫です。
今回はパスフレーズには何も入力せずに作成しました。
まず、Virtualfaileを作成したときに生成されたローカル側の鍵を削除します。鍵のパスは下記になります。
C:\Vagrant\centos71\.vagrant\machines\default\virtualbox\private_key
上記のprivate_keyを削除して、新たに作成したid_rsaを入れます
まず、仮想マシンにSSHログインします。コマンドプロンプトではSSHログインできないので鍵の作成時に利用したTera Termに下記の情報でログインします。
ホスト: 127.0.0.1:2222
ポート: 22
ユーザー: vagrant
パスフレーズ: vagrant
※鍵認証ではなく、プレインテキストを使うにチェックをしてログインする
su -
ログインができたら上記のコマンドでrootユーザーに切り替えます。パスワードはログイン時と同じvagrantです。
ここで一度、新たに作成したid_rsa.pubをテキストエディタで開いて内容を全てコピーしておきます。
vi /home/vagrant/.ssh/authorized_keys
上記のコマンドで仮想マシン側の鍵を開き内容を全て削除して、先程コピーしたid_rsa.pubの内容をペーストして保存します。
chmod 0755 /home/vagrant chmod 0700 /home/vagrant/.ssh chmod 0600 /home/vagrant/.ssh/authorized_keys systemctl reboot
パーミッションを適切にしてOSの再起動をするために上記のコマンドを入力します。
config.ssh.private_key_path = "C:\\vagrant\\centos71\\.vagrant\\machines\\default\\virtualbox\\id_rsa" config.ssh.forward_agent = true
Vagrantfileの最後に書いてあるendの一行前に上記を追記して、ローカル側の鍵の場所を指定してあげます。
vagrant up
改めて上記コマンドで仮想マシンの起動をします。
先程のAuthentication failure.というエラーが出ずに起動が完了すれば成功です!
コマンドプロンプトではSSHログインできないのでSSHクライアントのTera Termで下記の情報でログインします。
ホスト: 127.0.0.1:2222
ポート: 22
ユーザー: vagrant
※鍵認証なので「鍵を使う」にチェックをして、秘密鍵ボタンをクリックして作成したid_rsaを指定する
ユーザー名: vagrant
パスワード: vagrant
ユーザー名: root
パスワード: vagrant
これでログインができれば仮想サーバーは準備できたことになります。この後は、サーバーの環境構築をして好みの設定にしていきローカル開発環境が出来上がります。
コチラも読んでみませんか?