Mac(Yosemite)のVagrant(ubuntu14.04 server)にPHP開発環境を構築する
流れ的に、もちろんPHPはphpenvで・・・、と言いたい所だけどもPHPだけはphpenvを使わない。
RubyやPythonと違ってPHPは基本的にスクリプト単体では動かさないからだ。
つまり他のアプリケーションと連動して動作することになる。apacheだったりnginxだったり。
一般的に「LAMP」なんて言うぐらいでひと纏まりで1セットだったりするので、phpenvで言語だけ切り替えても仕方ないと思う。
と言うより、Vagrantあるんだから環境まとめて作ってそれ使えばいいじゃない。そっちの方が単純で分かり易い。
テスト環境を作る
まずはテスト環境を作る。
VagrantのBoxは前回作った「base_ubuntu1404」を使用する。
$ mkdir ~/dev/vagrant/dev_test $ cd ~/dev/vagrant/dev_test $ vagrant init base_ubuntu1404 A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant. $ ls -l total 8 -rw-r--r-- 1 zow staff 3027 1 17 22:46 Vagrantfile $
ホスト名変更
次にVagrantfileを編集する
以下を追記する
config.vm.hostname = "dev-test"
これはホスト名を設定している。
Vagrantでは複数環境を扱うので、どの環境にログインしているのかを明確にする為、最初にホスト名だけは個別に設定しておいた方が良い。
こんな感じになった
$ cat 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 = "base_ubuntu1404" # 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 # sudo apt-get update # sudo apt-get install -y apache2 # SHELL config.vm.hostname = "dev-test" end $
仮想環境起動
次にこの環境を起動する。
$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'base_ubuntu1404'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: dev_test_default_1421502579336_18302 ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 => 2222 (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: Connection timeout. Retrying... default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if its present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: No guest additions were detected on the base box for this VM! Guest default: additions are required for forwarded ports, shared folders, host only default: networking, and more. If SSH fails on this machine, please install default: the guest additions and repackage the box to continue. default: default: This is not an error message; everything may continue to work properly, default: in which case you may ignore this message. ==> default: Setting hostname... ==> default: Mounting shared folders... default: /vagrant => /Users/zow/dev/vagrant/dev_test Failed to mount folders in Linux guest. This is usually because the "vboxsf" file system is not available. Please verify that the guest additions are properly installed in the guest and can work properly. The command attempted was: mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant The error output from the last command was: stdin: is not a tty /sbin/mount.vboxsf: mounting failed with the error: No such device $
前回作成した物なのでエラーもそのまま出ている。
共有ディレクトリ設定
sshの鍵は勝手に書き換えてくれるので共有ディレクトリ設定だけやる。
実行するコマンド…その1
sudo /etc/init.d/vboxadd setup
実際の作業はこちら
$ vagrant ssh Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-44-generic x86_64) * Documentation: https://help.ubuntu.com/ System information as of Sat Jan 17 22:49:57 JST 2015 System load: 0.52 Processes: 76 Usage of /: 17.1% of 7.26GB Users logged in: 0 Memory usage: 9% IP address for eth0: 10.0.2.15 Swap usage: 0% Graph this data and manage this system at: https://landscape.canonical.com/ Last login: Sat Jan 17 21:19:21 2015 from 10.0.2.2 vagrant@dev-test:~$ sudo /etc/init.d/vboxadd setup Removing existing VirtualBox DKMS kernel modules ...done. Removing existing VirtualBox non-DKMS kernel modules ...done. Building the VirtualBox Guest Additions kernel modules ...done. Doing non-kernel setup of the Guest Additions ...done. Starting the VirtualBox Guest Additions ...done. vagrant@dev-test:~$ echo $? 0 vagrant@dev-test:~$
最後に行っているのは実行したコマンドのステータスの表示。
とりあえず正常終了した時のステータスを取得しておく。
MySQLインストール
MySQLをインストールする。
実行するコマンド…その2
sudo apt-get install mysql-server -y
実行するとこんな感じ
vagrant@dev-test:~$ sudo apt-get install mysql-server -y パッケージリストを読み込んでいます... 完了 依存関係ツリーを作成しています 状態情報を読み取っています... 完了 以下の特別パッケージがインストールされます: libaio1 libdbd-mysql-perl libdbi-perl libmysqlclient18 libterm-readkey-perl mysql-client-5.5 mysql-client-core-5.5 mysql-common mysql-server-5.5 mysql-server-core-5.5 提案パッケージ: libclone-perl libmldbm-perl libnet-daemon-perl libplrpc-perl libsql-statement-perl tinyca mailx 以下のパッケージが新たにインストールされます: libaio1 libdbd-mysql-perl libdbi-perl libmysqlclient18 libterm-readkey-perl mysql-client-5.5 mysql-client-core-5.5 mysql-common mysql-server mysql-server-5.5 mysql-server-core-5.5 アップグレード: 0 個、新規インストール: 11 個、削除: 0 個、保留: 0 個。 8,998 kB 中 0 B のアーカイブを取得する必要があります。 この操作後に追加で 96.3 MB のディスク容量が消費されます。 パッケージを事前設定しています ... 以前に未選択のパッケージ libaio1:amd64 を選択しています。 (データベースを読み込んでいます ... 現在 85705 個のファイルとディレクトリがインストールされています。) Preparing to unpack .../libaio1_0.3.109-4_amd64.deb ... Unpacking libaio1:amd64 (0.3.109-4) ... 以前に未選択のパッケージ mysql-common を選択しています。 Preparing to unpack .../mysql-common_5.5.40-0ubuntu0.14.04.1_all.deb ... Unpacking mysql-common (5.5.40-0ubuntu0.14.04.1) ... 以前に未選択のパッケージ libmysqlclient18:amd64 を選択しています。 Preparing to unpack .../libmysqlclient18_5.5.40-0ubuntu0.14.04.1_amd64.deb ... Unpacking libmysqlclient18:amd64 (5.5.40-0ubuntu0.14.04.1) ... 以前に未選択のパッケージ libdbi-perl を選択しています。 Preparing to unpack .../libdbi-perl_1.630-1_amd64.deb ... Unpacking libdbi-perl (1.630-1) ... 以前に未選択のパッケージ libdbd-mysql-perl を選択しています。 Preparing to unpack .../libdbd-mysql-perl_4.025-1_amd64.deb ... Unpacking libdbd-mysql-perl (4.025-1) ... 以前に未選択のパッケージ libterm-readkey-perl を選択しています。 Preparing to unpack .../libterm-readkey-perl_2.31-1_amd64.deb ... Unpacking libterm-readkey-perl (2.31-1) ... 以前に未選択のパッケージ mysql-client-core-5.5 を選択しています。 Preparing to unpack .../mysql-client-core-5.5_5.5.40-0ubuntu0.14.04.1_amd64.deb ... Unpacking mysql-client-core-5.5 (5.5.40-0ubuntu0.14.04.1) ... 以前に未選択のパッケージ mysql-client-5.5 を選択しています。 Preparing to unpack .../mysql-client-5.5_5.5.40-0ubuntu0.14.04.1_amd64.deb ... Unpacking mysql-client-5.5 (5.5.40-0ubuntu0.14.04.1) ... 以前に未選択のパッケージ mysql-server-core-5.5 を選択しています。 Preparing to unpack .../mysql-server-core-5.5_5.5.40-0ubuntu0.14.04.1_amd64.deb ... Unpacking mysql-server-core-5.5 (5.5.40-0ubuntu0.14.04.1) ... Processing triggers for man-db (2.6.7.1-1ubuntu1) ... mysql-common (5.5.40-0ubuntu0.14.04.1) を設定しています ... 以前に未選択のパッケージ mysql-server-5.5 を選択しています。 (データベースを読み込んでいます ... 現在 86064 個のファイルとディレクトリがインストールされています。) Preparing to unpack .../mysql-server-5.5_5.5.40-0ubuntu0.14.04.1_amd64.deb ... Unpacking mysql-server-5.5 (5.5.40-0ubuntu0.14.04.1) ... 以前に未選択のパッケージ mysql-server を選択しています。 Preparing to unpack .../mysql-server_5.5.40-0ubuntu0.14.04.1_all.deb ... Unpacking mysql-server (5.5.40-0ubuntu0.14.04.1) ... Processing triggers for ureadahead (0.100.0-16) ... Processing triggers for man-db (2.6.7.1-1ubuntu1) ... libaio1:amd64 (0.3.109-4) を設定しています ... libmysqlclient18:amd64 (5.5.40-0ubuntu0.14.04.1) を設定しています ... libdbi-perl (1.630-1) を設定しています ... libdbd-mysql-perl (4.025-1) を設定しています ... libterm-readkey-perl (2.31-1) を設定しています ... mysql-client-core-5.5 (5.5.40-0ubuntu0.14.04.1) を設定しています ... mysql-client-5.5 (5.5.40-0ubuntu0.14.04.1) を設定しています ... mysql-server-core-5.5 (5.5.40-0ubuntu0.14.04.1) を設定しています ... mysql-server-5.5 (5.5.40-0ubuntu0.14.04.1) を設定しています ... 150117 23:31:36 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead. mysql start/running, process 27241 mysql-server (5.5.40-0ubuntu0.14.04.1) を設定しています ... Processing triggers for libc-bin (2.19-0ubuntu6.5) ... vagrant@dev-test:~$ echo $? 0 vagrant@dev-test:~$
コンソール的には上記みたいな感じだけど、実際は途中でパスワードを聞かれている。
Apacheインストール
Apacheをインストールする
実行するコマンド…その3
sudo apt-get install apache2 -y
実際の実行結果
vagrant@dev-test:~$ sudo apt-get install apache2 -y パッケージリストを読み込んでいます... 完了 依存関係ツリーを作成しています 状態情報を読み取っています... 完了 以下の特別パッケージがインストールされます: apache2-bin apache2-data libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap ssl-cert 提案パッケージ: apache2-doc apache2-suexec-pristine apache2-suexec-custom apache2-utils openssl-blacklist 以下のパッケージが新たにインストールされます: apache2 apache2-bin apache2-data libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap ssl-cert アップグレード: 0 個、新規インストール: 8 個、削除: 0 個、保留: 0 個。 1,284 kB のアーカイブを取得する必要があります。 この操作後に追加で 5,342 kB のディスク容量が消費されます。 取得:1 http://jp.archive.ubuntu.com/ubuntu/ trusty/main libapr1 amd64 1.5.0-1 [85.1 kB] 取得:2 http://jp.archive.ubuntu.com/ubuntu/ trusty/main libaprutil1 amd64 1.5.3-1 [76.4 kB] 取得:3 http://jp.archive.ubuntu.com/ubuntu/ trusty/main libaprutil1-dbd-sqlite3 amd64 1.5.3-1 [10.5 kB] 取得:4 http://jp.archive.ubuntu.com/ubuntu/ trusty/main libaprutil1-ldap amd64 1.5.3-1 [8,634 B] 取得:5 http://jp.archive.ubuntu.com/ubuntu/ trusty-updates/main apache2-bin amd64 2.4.7-1ubuntu4.1 [839 kB] 取得:6 http://jp.archive.ubuntu.com/ubuntu/ trusty-updates/main apache2-data all 2.4.7-1ubuntu4.1 [160 kB] 取得:7 http://jp.archive.ubuntu.com/ubuntu/ trusty-updates/main apache2 amd64 2.4.7-1ubuntu4.1 [87.5 kB] 取得:8 http://jp.archive.ubuntu.com/ubuntu/ trusty/main ssl-cert all 1.0.33 [16.6 kB] 1,284 kB を 7秒 で取得しました (175 kB/s) パッケージを事前設定しています ... 以前に未選択のパッケージ libapr1:amd64 を選択しています。 (データベースを読み込んでいます ... 現在 86142 個のファイルとディレクトリがインストールされています。) Preparing to unpack .../libapr1_1.5.0-1_amd64.deb ... Unpacking libapr1:amd64 (1.5.0-1) ... 以前に未選択のパッケージ libaprutil1:amd64 を選択しています。 Preparing to unpack .../libaprutil1_1.5.3-1_amd64.deb ... Unpacking libaprutil1:amd64 (1.5.3-1) ... 以前に未選択のパッケージ libaprutil1-dbd-sqlite3:amd64 を選択しています。 Preparing to unpack .../libaprutil1-dbd-sqlite3_1.5.3-1_amd64.deb ... Unpacking libaprutil1-dbd-sqlite3:amd64 (1.5.3-1) ... 以前に未選択のパッケージ libaprutil1-ldap:amd64 を選択しています。 Preparing to unpack .../libaprutil1-ldap_1.5.3-1_amd64.deb ... Unpacking libaprutil1-ldap:amd64 (1.5.3-1) ... 以前に未選択のパッケージ apache2-bin を選択しています。 Preparing to unpack .../apache2-bin_2.4.7-1ubuntu4.1_amd64.deb ... Unpacking apache2-bin (2.4.7-1ubuntu4.1) ... 以前に未選択のパッケージ apache2-data を選択しています。 Preparing to unpack .../apache2-data_2.4.7-1ubuntu4.1_all.deb ... Unpacking apache2-data (2.4.7-1ubuntu4.1) ... 以前に未選択のパッケージ apache2 を選択しています。 Preparing to unpack .../apache2_2.4.7-1ubuntu4.1_amd64.deb ... Unpacking apache2 (2.4.7-1ubuntu4.1) ... 以前に未選択のパッケージ ssl-cert を選択しています。 Preparing to unpack .../ssl-cert_1.0.33_all.deb ... Unpacking ssl-cert (1.0.33) ... Processing triggers for man-db (2.6.7.1-1ubuntu1) ... Processing triggers for ureadahead (0.100.0-16) ... Processing triggers for ufw (0.34~rc-0ubuntu2) ... libapr1:amd64 (1.5.0-1) を設定しています ... libaprutil1:amd64 (1.5.3-1) を設定しています ... libaprutil1-dbd-sqlite3:amd64 (1.5.3-1) を設定しています ... libaprutil1-ldap:amd64 (1.5.3-1) を設定しています ... apache2-bin (2.4.7-1ubuntu4.1) を設定しています ... apache2-data (2.4.7-1ubuntu4.1) を設定しています ... apache2 (2.4.7-1ubuntu4.1) を設定しています ... Enabling module mpm_event. Enabling module authz_core. Enabling module authz_host. Enabling module authn_core. Enabling module auth_basic. Enabling module access_compat. Enabling module authn_file. Enabling module authz_user. Enabling module alias. Enabling module dir. Enabling module autoindex. Enabling module env. Enabling module mime. Enabling module negotiation. Enabling module setenvif. Enabling module filter. Enabling module deflate. Enabling module status. Enabling conf charset. Enabling conf localized-error-pages. Enabling conf other-vhosts-access-log. Enabling conf security. Enabling conf serve-cgi-bin. Enabling site 000-default. * Starting web server apache2 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message * ssl-cert (1.0.33) を設定しています ... Processing triggers for libc-bin (2.19-0ubuntu6.5) ... Processing triggers for ureadahead (0.100.0-16) ... Processing triggers for ufw (0.34~rc-0ubuntu2) ... vagrant@dev-test:~$ echo $? 0 vagrant@dev-test:~$
関連パッケージをインストール
実行するコマンド…その4
sudo apt-get install apache2-utils -y
実行結果
パッケージリストを読み込んでいます... 完了 依存関係ツリーを作成しています 状態情報を読み取っています... 完了 以下のパッケージが新たにインストールされます: apache2-utils アップグレード: 0 個、新規インストール: 1 個、削除: 0 個、保留: 0 個。 83.3 kB のアーカイブを取得する必要があります。 この操作後に追加で 345 kB のディスク容量が消費されます。 取得:1 http://jp.archive.ubuntu.com/ubuntu/ trusty-updates/main apache2-utils amd64 2.4.7-1ubuntu4.1 [83.3 kB] 83.3 kB を 5秒 で取得しました (15.5 kB/s) 以前に未選択のパッケージ apache2-utils を選択しています。 (データベースを読み込んでいます ... 現在 86884 個のファイルとディレクトリがインストールされています。) Preparing to unpack .../apache2-utils_2.4.7-1ubuntu4.1_amd64.deb ... Unpacking apache2-utils (2.4.7-1ubuntu4.1) ... Processing triggers for man-db (2.6.7.1-1ubuntu1) ... apache2-utils (2.4.7-1ubuntu4.1) を設定しています ... vagrant@dev-test:~$ echo $? 0 vagrant@dev-test:~$
PHPのインストール
とりあえずubuntu純正のphp5をインストール
実行するコマンド…その5
sudo apt-get install php5 -y
実際の実行結果
vagrant@dev-test:~$ sudo apt-get install php5 パッケージリストを読み込んでいます... 完了 依存関係ツリーを作成しています 状態情報を読み取っています... 完了 以下の特別パッケージがインストールされます: libapache2-mod-php5 php5-cli php5-common php5-json php5-readline 提案パッケージ: php-pear php5-user-cache 以下のパッケージが新たにインストールされます: libapache2-mod-php5 php5 php5-cli php5-common php5-json php5-readline アップグレード: 0 個、新規インストール: 6 個、削除: 0 個、保留: 0 個。 4,860 kB のアーカイブを取得する必要があります。 この操作後に追加で 20.5 MB のディスク容量が消費されます。 続行しますか? [Y/n] n 中断しました。 vagrant@dev-test:~$ sudo apt-get install php5 -y パッケージリストを読み込んでいます... 完了 依存関係ツリーを作成しています 状態情報を読み取っています... 完了 以下の特別パッケージがインストールされます: libapache2-mod-php5 php5-cli php5-common php5-json php5-readline 提案パッケージ: php-pear php5-user-cache 以下のパッケージが新たにインストールされます: libapache2-mod-php5 php5 php5-cli php5-common php5-json php5-readline アップグレード: 0 個、新規インストール: 6 個、削除: 0 個、保留: 0 個。 4,860 kB のアーカイブを取得する必要があります。 この操作後に追加で 20.5 MB のディスク容量が消費されます。 取得:1 http://jp.archive.ubuntu.com/ubuntu/ trusty/main php5-json amd64 1.3.2-2build1 [34.4 kB] 取得:2 http://jp.archive.ubuntu.com/ubuntu/ trusty-updates/main php5-common amd64 5.5.9+dfsg-1ubuntu4.5 [442 kB] 取得:3 http://jp.archive.ubuntu.com/ubuntu/ trusty-updates/main php5-cli amd64 5.5.9+dfsg-1ubuntu4.5 [2,164 kB] 取得:4 http://jp.archive.ubuntu.com/ubuntu/ trusty-updates/main php5-readline amd64 5.5.9+dfsg-1ubuntu4.5 [12.1 kB] 取得:5 http://jp.archive.ubuntu.com/ubuntu/ trusty-updates/main libapache2-mod-php5 amd64 5.5.9+dfsg-1ubuntu4.5 [2,206 kB] 取得:6 http://jp.archive.ubuntu.com/ubuntu/ trusty-updates/main php5 all 5.5.9+dfsg-1ubuntu4.5 [1,302 B] 4,860 kB を 10秒 で取得しました (467 kB/s) 以前に未選択のパッケージ php5-json を選択しています。 (データベースを読み込んでいます ... 現在 86785 個のファイルとディレクトリがインストールされています。) Preparing to unpack .../php5-json_1.3.2-2build1_amd64.deb ... Unpacking php5-json (1.3.2-2build1) ... 以前に未選択のパッケージ php5-common を選択しています。 Preparing to unpack .../php5-common_5.5.9+dfsg-1ubuntu4.5_amd64.deb ... Unpacking php5-common (5.5.9+dfsg-1ubuntu4.5) ... 以前に未選択のパッケージ php5-cli を選択しています。 Preparing to unpack .../php5-cli_5.5.9+dfsg-1ubuntu4.5_amd64.deb ... Unpacking php5-cli (5.5.9+dfsg-1ubuntu4.5) ... 以前に未選択のパッケージ php5-readline を選択しています。 Preparing to unpack .../php5-readline_5.5.9+dfsg-1ubuntu4.5_amd64.deb ... Unpacking php5-readline (5.5.9+dfsg-1ubuntu4.5) ... 以前に未選択のパッケージ libapache2-mod-php5 を選択しています。 Preparing to unpack .../libapache2-mod-php5_5.5.9+dfsg-1ubuntu4.5_amd64.deb ... Unpacking libapache2-mod-php5 (5.5.9+dfsg-1ubuntu4.5) ... 以前に未選択のパッケージ php5 を選択しています。 Preparing to unpack .../php5_5.5.9+dfsg-1ubuntu4.5_all.deb ... Unpacking php5 (5.5.9+dfsg-1ubuntu4.5) ... Processing triggers for man-db (2.6.7.1-1ubuntu1) ... php5-json (1.3.2-2build1) を設定しています ... php5_invoke: Enable module json for cli SAPI php5_invoke: Enable module json for apache2 SAPI php5-common (5.5.9+dfsg-1ubuntu4.5) を設定しています ... Creating config file /etc/php5/mods-available/pdo.ini with new version php5_invoke: Enable module pdo for cli SAPI php5_invoke: Enable module pdo for apache2 SAPI Creating config file /etc/php5/mods-available/opcache.ini with new version php5_invoke: Enable module opcache for cli SAPI php5_invoke: Enable module opcache for apache2 SAPI php5-cli (5.5.9+dfsg-1ubuntu4.5) を設定しています ... update-alternatives: /usr/bin/php (php) を提供するために 自動モード で /usr/bin/php5 を使います Creating config file /etc/php5/cli/php.ini with new version php5_invoke json: already enabled for cli SAPI php5_invoke opcache: already enabled for cli SAPI php5_invoke pdo: already enabled for cli SAPI php5-readline (5.5.9+dfsg-1ubuntu4.5) を設定しています ... Creating config file /etc/php5/mods-available/readline.ini with new version php5_invoke: Enable module readline for cli SAPI php5_invoke: Enable module readline for apache2 SAPI libapache2-mod-php5 (5.5.9+dfsg-1ubuntu4.5) を設定しています ... Creating config file /etc/php5/apache2/php.ini with new version php5_invoke readline: already enabled for apache2 SAPI php5_invoke json: already enabled for apache2 SAPI php5_invoke opcache: already enabled for apache2 SAPI php5_invoke pdo: already enabled for apache2 SAPI Module mpm_event disabled. Enabling module mpm_prefork. apache2_switch_mpm Switch to prefork * Restarting web server apache2 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message [ OK ] apache2_invoke: Enable module php5 * Restarting web server apache2 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message [ OK ] php5 (5.5.9+dfsg-1ubuntu4.5) を設定しています ... vagrant@dev-test:~$ echo $? 0 vagrant@dev-test:~$
次に関連パッケージをインストールしていく
実行するコマンド…その6
sudo apt-get install php5-mysql
実行結果
vagrant@dev-test:~$ sudo apt-get install php5-mysql パッケージリストを読み込んでいます... 完了 依存関係ツリーを作成しています 状態情報を読み取っています... 完了 以下のパッケージが新たにインストールされます: php5-mysql アップグレード: 0 個、新規インストール: 1 個、削除: 0 個、保留: 0 個。 62.9 kB のアーカイブを取得する必要があります。 この操作後に追加で 297 kB のディスク容量が消費されます。 取得:1 http://jp.archive.ubuntu.com/ubuntu/ trusty-updates/main php5-mysql amd64 5.5.9+dfsg-1ubuntu4.5 [62.9 kB] 62.9 kB を 5秒 で取得しました (11.7 kB/s) 以前に未選択のパッケージ php5-mysql を選択しています。 (データベースを読み込んでいます ... 現在 86873 個のファイルとディレクトリがインストールされています。) Preparing to unpack .../php5-mysql_5.5.9+dfsg-1ubuntu4.5_amd64.deb ... Unpacking php5-mysql (5.5.9+dfsg-1ubuntu4.5) ... Processing triggers for libapache2-mod-php5 (5.5.9+dfsg-1ubuntu4.5) ... php5-mysql (5.5.9+dfsg-1ubuntu4.5) を設定しています ... Creating config file /etc/php5/mods-available/mysql.ini with new version php5_invoke: Enable module mysql for cli SAPI php5_invoke: Enable module mysql for apache2 SAPI Creating config file /etc/php5/mods-available/mysqli.ini with new version php5_invoke: Enable module mysqli for cli SAPI php5_invoke: Enable module mysqli for apache2 SAPI Creating config file /etc/php5/mods-available/pdo_mysql.ini with new version php5_invoke: Enable module pdo_mysql for cli SAPI php5_invoke: Enable module pdo_mysql for apache2 SAPI Processing triggers for libapache2-mod-php5 (5.5.9+dfsg-1ubuntu4.5) ... vagrant@dev-test:~$ echo $? 0 vagrant@dev-test:~$
設定
mod_rewriteを有効にする
実行するコマンド…その7
sudo a2enmod rewrite
実行結果
vagrant@dev-test:~$ sudo a2enmod rewrite Enabling module rewrite. To activate the new configuration, you need to run: service apache2 restart vagrant@dev-test:~$ echo $? 0 vagrant@dev-test:~$
ドキュメントルートを共有ディレクトリに作る
実行するコマンド…その8
sudo rm -rf /var/www/html mkdir /vagrant/docroot sudo ln -s /vagrant/docroot /var/www/html echo '<?php phpinfo();?>' > /vagrant/docroot/index.php
実行結果
vagrant@dev-test:~$ sudo rm -rf /var/www/html vagrant@dev-test:~$ mkdir /vagrant/docroot vagrant@dev-test:~$ sudo ln -s /vagrant/docroot /var/www/html vagrant@dev-test:~$ echo '<?php phpinfo();?>' > /vagrant/docroot/index.php vagrant@dev-test:~$ cd /vagrant/docroot
apache2再起動
実行するコマンド…その9
sudo service apache2 restart
実行結果
vagrant@dev-test:/var/www/html$ sudo service apache2 restart * Restarting web server apache2 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message [ OK ] vagrant@dev-test:/var/www/html$
テストで80に接続してみる
vagrant@dev-test:~$ telnet localhost 80 Trying ::1... Connected to localhost. Escape character is '^]'.
接続したら「get」してみる。phpinfoがダラダラ出力されれば良い。
ここまでやったらテスト環境の構築は完了。
テスト環境の結果を踏まえてスクリプトを作る。
「実行するコマンド」って書いてたののがそれ。
ちょっと冗長だけど単純に書くとこんな感じ
#!/bin/bash #MySQLパスワードを設定する MYSQL_PASS="password" CheckStatus() { if [ $? -eq 0 ] then echo "$1 : 成功" else echo "$1 : 失敗" fi } #共有ディレクトリ設定 sudo /etc/init.d/vboxadd setup > /dev/null 2>&1 CheckStatus "共有ディレクトリ設定" #MySQLインストール dpkg -l |grep mysql-server > /dev/null 2>&1 if [ $? -ne 0 ] then echo "mysql-server-5.5 mysql-server/root_password password ${MYSQL_PASS}" |sudo /usr/bin/debconf-set-selections CheckStatus "MySQLパスワード設定" echo "mysql-server-5.5 mysql-server/root_password_again password ${MYSQL_PASS}" | sudo /usr/bin/debconf-set-selections CheckStatus "MySQLパスワード設定2回目" sudo apt-get install mysql-server -y > /dev/null 2>&1 CheckStatus "MySQLインストール" else echo "MySQLは既にインストールされています" fi #Apacheインストール dpkg -l | grep apache2 > /dev/null 2>&1 if [ $? -ne 0 ] then sudo apt-get install apache2 -y > /dev/null 2>&1 CheckStatus "Apacheインストール" else echo "Apache2は既にインストールされています" fi #Apache関連パッケージインストール dpkg -l | grep apache2-utils > /dev/null 2>&1 if [ $? -ne 0 ] then sudo apt-get install apache2-utils -y > /dev/null 2>&1 CheckStatus "Apache関連パッケージインストール" else echo "Apache2関連パッケージは既にインストールされています" fi #PHPインストール dpkg -l | grep php5 > /dev/null 2>&1 if [ $? -ne 0 ] then sudo apt-get install php5 -y > /dev/null 2>&1 CheckStatus "PHPインストール" else echo "PHPは既にインストールされています" fi #PHP関連パッケージインストール dpkg -l | grep php5-mysql > /dev/null 2>&1 if [ $? -ne 0 ] then sudo apt-get install php5-mysql -y > /dev/null 2>&1 CheckStatus "PHP関連パッケージインストール" else echo "PHP関連パッケージは既にインストールされています" fi #mod_rewrite有効化 sudo a2enmod rewrite > /dev/null 2>&1 CheckStatus "mod_rewrite有効化" #ドキュメントルート設定 ls /var/www/html | grep index.php > /dev/null 2>&1 if [ $? -ne 0 ] then sudo rm -rf /var/www/html sudo mkdir /vagrant/docroot sudo chown vagrant.vagrant /vagrant/docroot sudo ln -s /vagrant/docroot /var/www/html echo '<?php phpinfo();?>' > /vagrant/docroot/index.php ls /var/www/html | grep index.php > /dev/null 2>&1 CheckStatus "ドキュメントルート設定" else echo "ドキュメントルート設定は既にされています" fi #Apache2再起動 sudo service apache2 restart > /dev/null 2>&1 CheckStatus "Apache2再起動"
スクリプトを使ってPHP開発環境を作成する
さっそくスクリプトを使って開発環境を作成する
$ mkdir ~/dev/vagrant/dev_php01 $ cd ~/dev/vagrant/dev_php01 $ vagrant init base_ubuntu1404 A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant. $
Vagrantfileには以下を追記する
config.vm.hostname = "dev-php01" config.vm.provision "shell", path: "/Users/zow/dev/vagrant/build_script/dev_php.sh"
ホスト名の設定と、先ほど作ったビルド用スクリプトへのパスになる。
実際の設定はこちら
dev_php01 04:52:56 zow$ cat 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 = "base_ubuntu1404" # 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 # sudo apt-get update # sudo apt-get install -y apache2 # SHELL config.vm.hostname = "dev-php01" config.vm.provision "shell", path: "/Users/zow/dev/vagrant/build_script/dev_php.sh" end dev_php01 04:55:04 zow$
この状態で仮想環境を立ち上げる
$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'base_ubuntu1404'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: dev_php01_default_1421524670487_29740 ==> default: Fixed port collision for 22 => 2222. Now on port 2200. ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 => 2200 (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2200 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Connection timeout. Retrying... default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if its present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: No guest additions were detected on the base box for this VM! Guest default: additions are required for forwarded ports, shared folders, host only default: networking, and more. If SSH fails on this machine, please install default: the guest additions and repackage the box to continue. default: default: This is not an error message; everything may continue to work properly, default: in which case you may ignore this message. ==> default: Setting hostname... ==> default: Mounting shared folders... default: /vagrant => /Users/zow/dev/vagrant/dev_php01 Failed to mount folders in Linux guest. This is usually because the "vboxsf" file system is not available. Please verify that the guest additions are properly installed in the guest and can work properly. The command attempted was: mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant The error output from the last command was: stdin: is not a tty /sbin/mount.vboxsf: mounting failed with the error: No such device $
当然の事ながらいつも通りのエラーが発生している。
次に作成したスクリプトを動かすために以下を実行する。
vagrant provision
実行結果がこちら
$ vagrant provision ==> default: Running provisioner: shell... default: Running: /var/folders/yf/psgfw4xs3z5_dvfnyhtnx6c40000gn/T/vagrant-shell20150118-73280-1uwckna.sh ==> default: stdin: is not a tty ==> default: 共有ディレクトリ設定 : 成功 ==> default: MySQLパスワード設定 : 成功 ==> default: MySQLパスワード設定2回目 : 成功 ==> default: MySQLインストール : 成功 ==> default: Apacheインストール : 成功 ==> default: Apache関連パッケージインストール : 成功 ==> default: PHPインストール : 成功 ==> default: PHP関連パッケージインストール : 成功 ==> default: mod_rewrite有効化 : 成功 ==> default: ドキュメントルート設定 : 成功 ==> default: Apache2再起動 : 成功 $
こんな感じにインストール等々をやってくれる。
ちなみに、一度実行してしまった環境でもう一回実行するとこうなる。
$ vagrant provision ==> default: Running provisioner: shell... default: Running: /var/folders/yf/psgfw4xs3z5_dvfnyhtnx6c40000gn/T/vagrant-shell20150118-73358-1wjs9hm.sh ==> default: stdin: is not a tty ==> default: 共有ディレクトリ設定 : 成功 ==> default: MySQLは既にインストールされています ==> default: Apache2は既にインストールされています ==> default: Apache2関連パッケージは既にインストールされています ==> default: PHPは既にインストールされています ==> default: PHP関連パッケージは既にインストールされています ==> default: mod_rewrite有効化 : 成功 ==> default: ドキュメントルート設定は既にされています ==> default: Apache2再起動 : 成功
一応、インストールパッケージの有無を確認して実行するようにしたので、インストールは2回行うことは無いが、複数回やっても大丈夫な作業については実行されるようになっている。
ちなみに、このビルドスクリプトの内容と同じものを実際のubuntu環境で作りたいのであれば、実際のubuntu環境にスクリプトをコピーして、rootで実行してやれば問題なく動くはずだけど、共有ディレクトリとドキュメントルートの設定はvagrant特有なので、この部分はコメントアウトしてから実行すれば良い。
まだビルドスクリプトは改善の余地があると思うけど、ひとまずこんな所で我慢しておく。
bashスクリプト書きたくてvagrant弄ってる訳じゃないしw
プロビジョニングが終わったら一度開発環境を落とす
$ vagrant halt ==> default: Attempting graceful shutdown of VM... $
Vagrantfileを編集する
# config.vm.network "private_network", ip: "192.168.33.10" ↓ config.vm.network "private_network", ip: "192.168.33.100"
これでこの仮想マシンの80番ポートにアクセスできるようになる。
ポートフォワーディングで、ホストOSのポートにマッピングしてもいいんだけど、複数立ち上げた時に混乱しそうなので、IP振ってホスト単位で管理したほうがいいと思う。
設定変更したら起動する
$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Clearing any previously set forwarded ports... ==> default: Fixed port collision for 22 => 2222. Now on port 2200. ==> 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 => 2200 (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2200 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Connection timeout. Retrying... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... ==> default: Setting hostname... ==> default: Configuring and enabling network interfaces... ==> default: Mounting shared folders... default: /vagrant => /Users/zow/dev/vagrant/dev_php01 ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> default: to force provisioning. Provisioners marked to run always will still run. $
共有ディレクトリのエラーも消えて正常に立ち上がった。
ブラウザで仮想環境にアクセスしてみる。
ここでまさかのエラー。
原因は判ってて、共有ディレクトリをマウントしてない状態でドキュメントルートを作ってしまったので、マウントされたことで/vagrant/docrootが無くなってしまったからだ。
解決策は明快で、マウント出来るようになった状態で、もう一回プロビジョニングすれば良い。
$ vagrant provision ==> default: Running provisioner: shell... default: Running: /var/folders/yf/psgfw4xs3z5_dvfnyhtnx6c40000gn/T/vagrant-shell20150118-73823-yv3zqs.sh ==> default: stdin: is not a tty ==> default: 共有ディレクトリ設定 : 成功 ==> default: MySQLは既にインストールされています ==> default: Apache2は既にインストールされています ==> default: Apache2関連パッケージは既にインストールされています ==> default: PHPは既にインストールされています ==> default: PHP関連パッケージは既にインストールされています ==> default: mod_rewrite有効化 : 成功 ==> default: ドキュメントルート設定 : 成功 ==> default: Apache2再起動 : 成功 $
そうすると、インストール等はスキップして、ドキュメントルートだけ作成しなおしてくれる。
作成すればphpinfoが見れるようになる
これでPHP開発環境作成完了。