ふたりはララベル (Laravel PHP Framework)

PHPフレームワークのLaravelの体験記を書いていきます。こんなタイトルのブログですが萌え系アニメは一秒たりとも観たことがありません。

さくらのレンタルサーバにLaravelをインストール

さくらのレンタルサーバにLaravelをインストールする手順を解説します。

事前にComposerが必要になります。
レンタルサーバにComposerをインストールする - ふたりはララベル (Laravel PHP Framework)


解説上、さくらのアカウント名を「sakura」とします。

まずはログイン

ホームディレクトリにいることを確認します。LaravelをWebアクセスが可能なディレクトリにインストールするのは推奨されていないので、まあホームディレクトリが無難

%pwd
/home/sakura

Laravelをダウンロードする

いよいよ本番です。「php composer.phar create-project laravel/laravel プロジェクト名 --prefer-dist」と打てばインストールが始まります。プロジェクト名は「futari」としておこう。

%php composer.phar create-project laravel/laravel futari --prefer-dist
Installing laravel/laravel (v4.0.9)
  - Installing laravel/laravel (v4.0.9)
    Downloading: connection...    Downloading: 100%

Created project in futari
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing filp/whoops (1.0.7)
    Downloading: connection...    Downloading: 100%

<中略>

Generating autoload files
Generating optimized class loader
Application key [XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX] set successfully.

32文字のアプリケーションキーも自動でセットしてくれました。これは便利。
futariディレクトリが作成された。

%ls
MailBox		futari		log		sblo_files	www
composer.phar	db		ports		tmp

publicフォルダをwww配下に置く

publicフォルダをWebアクセスが可能なフォルダに移動する必要があります。さくらのレンタルサーバだと「www」です。ついでにpublicのフォルダ名をfutariに変えておこう。

%cd futari
%ls
CONTRIBUTING.md	bootstrap	phpunit.xml	server.php
app		composer.json	public		vendor
artisan		composer.lock	readme.md
%mv public/ ../www/futari/

index.php内のパスを設定する

publicの位置と名前を変えたので、それに合わせてindex.php内のパスも変更します。「../futari/」を足せばいいだけです。

%cat index.php
<?php
/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylorotwell@gmail.com>
 */

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/

require __DIR__.'/../../futari/bootstrap/autoload.php';
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let's turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight these users.
|
*/

$app = require_once __DIR__.'/../../futari/bootstrap/start.php';

<以下略>

.htaccessを編集する

私の環境だとLaravelデフォルトの.htaccessの「Options +FollowSymLinks」「Options -MultiViews」があると失敗するのでコメントアウトしました。

%cat futari/.htaccess
<IfModule mod_rewrite.c>
    #Options -MultiViews
    #Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

ブラウザから確認する

http://ホスト名/futari/index.phpを実行すればLaravelの初期画面が見れます。