趣味のプロジェクトで、APIサーバーとしてLaravel製の軽量PHPフレームワーク、Lumenを採用することにしました。
ここではVagrantを使ったローカルの開発環境を作るための手順を残します。
Contents
前提
- ホストOS: Mac OS X El Capitan
- ゲストOS: CentOS 7.1 (bento/centos-7.1)
- ホストOSにVagrant 1.7.4 をインストール済み
今回は開発用なので、Lumenは /vagrant の中に配置し、
パーミッションの問題を避けるために、nginxもvagrantユーザーで起動することにします。
ベースのVagrant box
Chef社がメンテしているCentOS 7の bento/centos-7.1 を採用しました。
7系のCentOSであれば他のものでも問題ないと思います。
6系やEC2上で実行する場合、特に使用するリポジトリをOSに適当なものに変更してください。
先にBoxをローカルにダウンロードしておきます。
|
1 |
$ vagrant box add bento/centos-7.1 |
プレーンな環境構築
適当なフォルダにVagrantfileを用意して起動します。
|
1 2 3 4 |
Vagrant.configure(2) do |config| config.vm.box = "bento/centos-7.1" config.vm.network :private_network, ip: "192.168.33.10" end |
|
1 |
$ vagrant up |
起動できたら、sshで接続して全パッケージをアップデートしておきましょう。
|
1 2 |
$ vagrant ssh $ sudo yum update -y |
共有フォルダのマウントエラー
vagrant upで Failed to mount folders in Linux guest. ってエラーが出たときは、次のようにゲストOS内でGuest Additionsのリビルドを行うと解決する。
|
1 2 3 4 5 |
$ vagrant ssh $ sudo /etc/init.d/vboxadd setup $ exit $ vagrant halt $ vagrant up |
nginxのインストール
ここから先はゲストOSにログインして、 sudo su - でrootに昇格して作業します。
|
1 2 3 4 5 |
リポジトリを追加 # yum install http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm # yum install --enablerepo=nginx nginx # nginx -v nginx version: nginx/1.8.1 |
php7のインストール
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
EpelとRemiリポジトリをインストール # yum -y install epel-release # wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm # rpm -ivh ./remi-release-7.rpm リポジトリを指定してインストール # yum --enablerepo=remi-php70 install php Lumenの実行に必要なモジュールをインストール # yum --enablerepo=remi-php70 install php-pdo php-mbstring php-pecl-zip Nginx用の php-fpm # yum --enablerepo=remi-php70 install php-fpm あとはお好みで # yum --enablerepo=remi-php70 install php-mcrypt php-gd php-xml php-bcmath php-enchant php-intl # php -v PHP 7.0.4 (cli) (built: Mar 2 2016 18:03:26) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies |
php70-phpをインストールした場合
上記手順でインストールした場合はスキップして問題ありませんが、php70-のプリフィクスを付けてremiのリポジトリからインストールした場合は次の手順で、phpコマンドにパスを通す必要があります。
|
1 2 3 4 5 6 |
# source /opt/remi/php70/enable # echo "source /opt/remi/php70/enable" >> /etc/profile # php -v PHP 7.0.4 (cli) (built: Mar 2 2016 17:13:39) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies |
また、phpの設定ファイル等も /etc/opt/remi/php70 下に配置されることになるので、読み進める際は適宜変換してください。
Lumenのインストール
いくつか前提があるので先に準備します。
Composerのインストール
Lumenはcompsoer経由でインストールします。
|
1 2 3 4 |
# curl -sS https://getcomposer.org/installer | php # mv composer.phar /usr/local/bin/composer # composer --version Composer version 1.0-dev (a8e9df55dc62d8806360be2f79ad112b8678d397) 2016-03-17 11:42:19 |
composerでインストールしたプログラム用のパスを通しておきます。
|
1 2 3 4 |
# echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bash_profile # source ~/.bash_profile # composer --version Composer version 1.0-dev (e8b1a5f35772e39ca21ab855a278bd84a0a534b2) 2016-03-29 07:53:57 |
gitとzipのインストール
Lumenをインストールする際に使います。
|
1 |
# yum install git zip unzip |
Lumenのインストール
Composerでインストーラーをインストールします。
|
1 2 3 |
# composer global require "laravel/lumen-installer" # lumen --version Lumen Installer version 1.0.1 |
Lumenプロジェクトを適当なフォルダで初期化します。
|
1 2 3 4 5 6 |
# cd /vagrant # lumen new api Crafting application... Application ready! Build something amazing. # ls api Vagrantfile |
各種設定
準備は整いました。
デフォルトのプロジェクトを公開してみます。
phpの設定
お好みで php.ini ファイルを設定してください。
nginxの設定
今回は /vagrant/api にLumenをインストールした前提で進めていきます。
/vagrant以下はユーザー vagrant の管理下にあるので、今回はサーバー等もこのユーザーで実行します。
|
1 2 3 4 5 6 7 |
-user nginx; +user vagrant; http { - sendfile on; + sendfile off; } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
location / { - root /usr/share/nginx/html; - index index.html index.htm; + root /vagrant/api/public; + index index.php index.html index.htm; } - #location ~ \.php$ { - # root html; - # fastcgi_pass 127.0.0.1:9000; - # fastcgi_index index.php; - # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; - # include fastcgi_params; - #} + location ~ \.php$ { + root /vagrant/api/public; + fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; + fastcgi_index index.php; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + include fastcgi_params; + } |
起動して、設定もここで行います。
|
1 2 |
# systemctl start nginx # systemctl enable nginx |
php-fpmの設定
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
-user = apache +user = vagrant -group = apache +group = vagrant -listen = 127.0.0.1:9000 +listen = /var/run/php-fpm/php-fpm.sock -;listen.owner = nobody -;listen.group = nobody +listen.owner = vagrant +listen.group = vagrant |
|
1 2 |
# systemctl start php-fpm # systemctl enable php-fpm |
動作確認
Vagrantfileで設定しておいたゲストOSに、ホストOSのブラウザからローカルIPで接続してみます。
所感
軽くやってみたはずが結構長くなりました。nginxに拘らなければ、phpのビルトインサーバーが楽かもしれませんね。
もし環境をチームで配布するようになれば、本当はChefで自動化したいです。



