nirasan's tech blog

趣味や仕事の覚え書きです。Linux, Perl, PHP, Ruby, Javascript, Android, Cocos2d-x, Unity などに興味があります。

Mac で Vagrant を使って Slim の Hello World

はじめに

  • MacVagrant を使って CentOS 環境を立ち上げて、PHP の micro web application framework の Slim で Hello World をするまでのメモです

vagrantCentOS イメージの取得 @Host

vagrant box add centos65-x86_64-20131205 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.1/centos65-x86_64-20131205.box

起動 @Host

mkdir ~/Document/Vagrant/centos
cd ~/Document/Vagrant/centos
vagrant init centos65-x86_64-20131205
vagrant up

ログイン @Host

vagrant ssh
su - 
password: vagrant

各種サービスのインストール @Guest

yum install php apache mysql-server vim
/etc/init.d/httpd start
/etc/init.d/mysqld start
chkconfig httpd on
chkconfig mysqld on

タイムゾーンの設定 @Guest

cp -p /usr/share/zoneinfo/Japan /etc/localtime

ポートフォワーディングの設定 @Host

vi Vagrantfile
----
  config.vm.network :forwarded_port, guest:   80, host: 20080
  config.vm.network :forwarded_port, guest: 3306, host: 23306
----
vagrant reload

httpdの動作確認

Host のウェブブラウザで http://127.0.0.1:20080 にアクセス

Slim のインストール

composer のインストール

cd /var/www/html
curl -s https://getcomposer.org/installer | php
vi composer.json
----
    {
        "require": {
            "slim/slim": "2.*"
        }
    }
----
php composer.phar install

ウェブサーバー設定

vi .htaccess
----
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L]
----
vi /etc/httpd/conf/httpd.conf
----
    <Directory "/var/www/html">
        AllowOverride All
----
/etc/init.d/httpd restart

動作確認用のファイル作成

vi index.php
----
    <?php
    require 'vendor/autoload.php';
    
    $app = new \Slim\Slim();
    $app->get('/', function () {
        echo "Hello world";
    });
    $app->get('/hello/:name', function ($name) {
        echo "Hello, $name";
    });
    $app->run();
----

動作確認

http://127.0.0.1:20080/hello/yourname にアクセスして動作確認