どうもきよです!
WEBプログラマとして仕事をしていく上で、
避けては通れない「環境構築」
最近はdockerで一発構築!みたいなことも多いですが、
Vagrantを使用したプロジェクトもまだまだたくさんありま
なので、今回はVagrant + Virtualboxを使用した、
CakePHP3のプロジェクトを立ち上げるまでの手順を書いて
■バージョン
・CentOS: 7.2
・PHP: 7.2
・Apache: 2.4
・MySQL 5.7
1.Vagrant + Virtualbox のインストール & BOXファイルを使用してCentOS7のインスタンス立ち上げ
2.Apacheのインストール(Webサーバ)
3.PHPのインストール
4.MySQLのインストール
5.CakePHPのプロジェクト作成
上記のようなフェーズで分けて書いて行きます。
今回は第1回ということで、
「Vagrant + Virtualbox のインストール & BOXファイルを使用してCentOS7のインスタンス立ち上げ」
についてです。
①.「Virtualbox」のインストール
まず、
仮想環境って何よ?って方もいらっしゃると思います。
仮想環境のイメージとしては、
今あなたが使っているPCの中に、
なんのこっちゃわからないかもしれませんが、
なので下記からインストールしてみてください!
https://www.virtualbox.org/
(
Windowsの方は → 「Windows hosts」 ってやつをクリック!
Macの方は → 「OS X hosts」 ってやつをクリック!
exeファイルがダウンロードされると思うので、
これで「Virtualbox」についてはおしまいです。
次はVagrantのインストールです。
②.「Vagrant」のインストール
Vagrantってなに?Dockerとなにが違うの?とか疑問になるところは多いですが、Vagrantは、仮想環境の管理ソフトみたいに思ってもらえればまずはいいかと思います。
Vagrantを使うことで、仮想環境の立ち上げ、共有ファイルの設定や仮想環境の削除などを簡単にできるようになります。
GUIでやってもいいんですけど、きっとプログラマの皆さんはコマンドの方が楽だと感じるでしょう!
また、Vagrantを使う最大のメリットは、チームで開発したりする際に、
開発環境の内容を統一させることができるところだと思います。
ミドルウェアのバージョンなどで左右されるプログラミング言語たちですが、
簡単に統一させられればそれに越したことはないですよね!
なので使います!
ちなみに、最近はDockerを使うところも多いので、Vagrantを理解できたらDockerにチャレンジしてみてください!
(DockerとVagrantは結構違うんですけどねw)
というわけで、下記からインストールしてみてください!
https://www.vagrantup.com
Virtualboxと同様に、exeファイルがダウンロードされると思うので、
インストールをしてみてください!
インストールが完了したら、
Windowsの方 → コマンドプロンプト (windowsのメニューから検索してみてね)
Macの方 → ターミナル
を開いて、任意のディレクトリまで移動してください。
移動するには「cd」コマンドを使います。
僕は下記みたいなディレクトリまで移動します。
(好きにディレクトリを作ってもらって大丈夫です)
cd Document/cake_1
ここまできたら、さっきインストールしたVagrantが使える状況か確認しましょう。
vagrant -h
これでコマンドの説明みたいなのが、たくさん出てきたらOKです!
Usage: vagrant [options] <command> [<args>] -v, --version Print the version and exit. -h, --help Print this help. Common commands: box manages boxes: installation, removal, etc. cloud manages everything related to Vagrant Cloud destroy stops and deletes all traces of the vagrant machine global-status outputs status Vagrant environments for this user halt stops the vagrant machine help shows the help for a subcommand init initializes a new Vagrant environment by creating a Vagrantfile login package packages a running vagrant environment into a box plugin manages plugins: install, uninstall, update, etc. port displays information about guest port mappings powershell connects to machine via powershell remoting provision provisions the vagrant machine push deploys code in this environment to a configured destination rdp connects to machine via RDP reload restarts vagrant machine, loads new Vagrantfile configuration resume resume a suspended vagrant machine snapshot manages snapshots: saving, restoring, etc. ssh connects to machine via SSH ssh-config outputs OpenSSH valid configuration to connect to the machine status outputs status of the vagrant machine suspend suspends the machine up starts and provisions the vagrant environment upload upload to machine via communicator validate validates the Vagrantfile version prints current and latest Vagrant version winrm executes commands on a machine via WinRM winrm-config outputs WinRM configuration to connect to the machine For help on any individual command run `vagrant COMMAND -h` Additional subcommands are available, but are either more advanced or not commonly used. To see all subcommands, run the command `vagrant list-commands`.
③.BOXファイルのローカル登録
VagrantとVirtualboxがインストールできたと思うので、
Vagrantを使ってCentOS7.2(Linux)のBOXをローカルに登録します。
BOXというのは何かと言うと、パッケージみたいなもので、仮想環境の元となるものです。
イメージしやすく説明すると、家電量販店で新しいパソコン買ってきた
みたいな感じです(ざっくり)
これがVagrantの最大の良さだと思います。
チームで開発する際に、このBOXファイルを共有することで、
全く同じ環境が瞬時に作れます。
というわけでターミナルから下記のコマンドを実行してみてください。
vagrant box add centos72 https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.2/vagrant-centos-7.2.box
ちょっと時間はかかるかもですが、上記コマンドでBOXがローカル登録できます。
下記を実行して、ローカル登録できているか確認します。
vagrant box list
下記がコンソールに表示されたら成功です。
centos72 (virtualbox, 0)
④.仮想環境立ち上げ
そして次はローカル登録したBOXを実際に立ち上げる作業です。
②の工程で作業ディレクトリを作ったと思います。
自分は「cake_1」というディレクトリを作って、
このディレクトリを今回のプロジェクトに使おうと思うので、
ここで下記を実行します。
(手順通りにやっていれば、作成したディレクトリにいると思います)
vagrant init centos72
これを実行すると、Virtualboxにcentos7.2の仮想環境が出来上がります。
それと、下記コマンドを実行してみてください。
ls -l
「Vagrantfile」っていうファイルができていればOKです。
この「Vagrantfile」というのがどういう内容で、
仮想環境を立ち上げるかなどを記載する設定ファイルです。
http://localhost:8080
でアクセスできるように下記のように修正してください。
まず下記コマンドで、viエディタでVagrantfileを開きます。
viエディタがわからない人は、メモ帳で開いても大丈夫です。
下記の内容に修正してください。まるっとコピーして貼り付けちゃって大丈夫です。
-*- 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 = "centos72" # 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 config.vm.synced_folder ".", "/vagrant", mount_options: ['dmode=777','fmode=777'] # 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" 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 config.vm.provision :shell, run: "always", :inline => <<-EOT sudo systemctl start httpd EOT end
そしてこれで仮想環境を立ち上げることができるようになりましたので、
下記を実行して仮想環境を立ち上げてみましょう。
vagrant up
なんかたくさん文字が出てきて大丈夫かよ!?ってなるかもですけど、
大丈夫です。こんな感じの出ますよ。
Bringing machine 'default' up with 'virtualbox' provider... ==> 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: 80 (guest) => 8080 (host) (adapter 1) 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: Machine booted and ready! ==> default: Checking for guest additions in VM... default: The guest additions on this VM do not match the installed version of default: VirtualBox! In most cases this is fine, but in rare cases it can default: prevent things such as shared folders from working properly. If you see default: shared folder errors, please make sure the guest additions within the default: virtual machine match the version of VirtualBox you have installed on default: your host and reload your VM. default: default: Guest Additions Version: 5.2.4 default: VirtualBox Version: 6.0 ==> default: Configuring and enabling network interfaces... ==> default: Mounting shared folders... default: /vagrant => /....... ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> default: flag to force provisioning. Provisioners marked to run always will still run. ==> default: Running provisioner: shell... default: Running: inline script
これで立ち上がりました!確認してみましょう!
vagrant status
上記を実行すると
default running (virtualbox)
これが出ると思います。走ってますねー!
これで第1回はおしまいです!
最後に仮想環境の止め方ですね!下記で仮想環境が止まります!
vagrant halt
実行後に、vagrant statusで確認すると、
runningだったのがpower offってなってると思います。
立ち上げたら止めること忘れないでくださいねー!
それでは次回は、
Apacheのインストール(Webサーバ)
をお送りします!
Apacheのインストール(Webサーバ)