Laravelをさくらのレンタルサーバに導入


この記事ではLaravel 4.2を使用しているつもりです。

Hello Laravel!!

Laravel は、MVCのWebアプリケーション開発用の無料・オープンソースのなPHPで書かれたWebアプリケーションフレームワークである。LaravelはMITライセンスの下でリリースされており、そのソースコードはGitHubにホスティングされている。マイクロソフトの.NETの開発に関わっていたTaylor Otwellが開発した。 PHPフレームワークのに2013年12月の人気調査によると、Laravelは2014年における最も有望なPHPフレームワークの1つとして挙げられた。 LaravelはPHP 5.3以上を対象とする新しいプロジェクトである。

Laravel - Wikipedia


さくらのレンタルサーバでLaravelを使ってみたい!! ということでさくっとインストール!
composerが必要なのでSSHにログインし入れます。

% curl -sS https://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /home/ie-saba/composer.phar
Use it: php composer.phar

上記のコマンドを実行するとホームディレクトリに composer.phar というファイルができるのでとりあえず実行してみましょう。

% php composer.phar 
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version ac497feabaa0d247c441178b7b4aaa4c61b07399 2014-06-10 14:13:12
(略)

次にLaravelをダウンロード、インストールします。今回のプロジェクト名はTestとします

% php composer.phar create-project laravel/laravel test --prefer-dist
Installing laravel/laravel (v4.2.0)
  - Installing laravel/laravel (v4.2.0)
    Loading from cache

Created project in test
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing symfony/translation (v2.5.0)
    Loading from cache

  - Installing symfony/security-core (v2.5.0)
    Loading from cache

  - Installing symfony/routing (v2.5.0)
    Loading from cache

  - Installing symfony/process (v2.5.0)
    Loading from cache

  - Installing psr/log (1.0.0)
    Loading from cache

  - Installing symfony/debug (v2.5.0)
    Loading from cache

  - Installing symfony/http-foundation (v2.5.0)
    Loading from cache

  - Installing symfony/event-dispatcher (v2.5.0)
    Loading from cache

  - Installing symfony/http-kernel (v2.5.0)
    Loading from cache

  - Installing symfony/finder (v2.5.0)
    Loading from cache

  - Installing symfony/dom-crawler (v2.5.0)
    Loading from cache

  - Installing symfony/css-selector (v2.5.0)
    Loading from cache

  - Installing symfony/console (v2.5.0)
    Loading from cache

  - Installing symfony/browser-kit (v2.5.0)
    Loading from cache

  - Installing swiftmailer/swiftmailer (v5.2.1)
    Loading from cache

  - Installing stack/builder (v1.0.2)
    Loading from cache

  - Installing predis/predis (v0.8.5)
    Loading from cache

  - Installing phpseclib/phpseclib (0.3.6)
    Loading from cache

  - Installing patchwork/utf8 (v1.1.23)
    Loading from cache

  - Installing nesbot/carbon (1.9.0)
    Loading from cache

  - Installing monolog/monolog (1.10.0)
    Loading from cache

  - Installing nikic/php-parser (v0.9.4)
    Loading from cache

  - Installing jeremeamia/superclosure (1.0.1)
    Loading from cache

  - Installing filp/whoops (1.1.1)
    Loading from cache

  - Installing ircmaxell/password-compat (1.0.3)
    Loading from cache

  - Installing d11wtq/boris (v1.0.8)
    Loading from cache

  - Installing symfony/filesystem (v2.5.0)
    Loading from cache

  - Installing classpreloader/classpreloader (1.0.2)
    Loading from cache

  - Installing laravel/framework (v4.2.3)
    Loading from cache

symfony/translation suggests installing symfony/config ()
symfony/translation suggests installing symfony/yaml ()
symfony/security-core suggests installing symfony/validator (For using the user password constraint)
symfony/security-core suggests installing symfony/expression-language (For using the expression voter)
symfony/routing suggests installing symfony/config (For using the all-in-one router or any loader)
symfony/routing suggests installing symfony/yaml (For using the YAML loader)
symfony/routing suggests installing symfony/expression-language (For using expression matching)
symfony/routing suggests installing doctrine/annotations (For using the annotation loader)
symfony/event-dispatcher suggests installing symfony/dependency-injection ()
symfony/http-kernel suggests installing symfony/class-loader ()
symfony/http-kernel suggests installing symfony/config ()
symfony/http-kernel suggests installing symfony/dependency-injection ()
predis/predis suggests installing ext-phpiredis (Allows faster serialization and deserialization of the Redis protocol)
phpseclib/phpseclib suggests installing pear-pear/PHP_Compat (Install PHP_Compat to get phpseclib working on PHP < 4.3.3.)
patchwork/utf8 suggests installing ext-intl (Use Intl for best performance)
monolog/monolog suggests installing graylog2/gelf-php (Allow sending log messages to a GrayLog2 server)
monolog/monolog suggests installing raven/raven (Allow sending log messages to a Sentry server)
monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server)
monolog/monolog suggests installing ruflin/elastica (Allow sending log messages to an Elastic Search server)
monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)
monolog/monolog suggests installing aws/aws-sdk-php (Allow sending log messages to AWS services like DynamoDB)
monolog/monolog suggests installing rollbar/rollbar (Allow sending log messages to Rollbar)
d11wtq/boris suggests installing ext-readline (*)
d11wtq/boris suggests installing ext-pcntl (*)
laravel/framework suggests installing doctrine/dbal (Allow renaming columns and dropping SQLite columns.)
Writing lock file
Generating autoload files
Generating optimized class loader
Compiling common classes
Compiling views
Application key [********************************] set successfully.

ホームディレクトリに test というディレクトリが作成されていると思います。
ただこれだけでは公開できないので www 配下に移動させます。

% mv ~/test ~/www/test

大事なこと
Laravelのインストールディレクトリは公開すべきではありません。
デフォルトの *.sakura.ne.jp でのアクセスを拒否した上で行ってください。

Sample

RewriteEngine on
RewriteCond %{HTTP_HOST} 自分のユーザ名\.sakura\.ne\.jp [NC]
RewriteRule .* - [F]
DirectoryIndex index.php index.cgi index.htm index.html .ht

レンタルサーバコントロールパネルでバーチャルホストを設定します。
バーチャルホストの公開ディレクトリを www/test/public に設定します。

さくらのレンタルサーバでは .htaccess 内に Optionsを設定できないので public ディレクトリの .htaccess を編集し保存します。
サンプルでは3行目をコメントアウトしています。
あと6行目に RewriteBase / を設定してあげます。

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        #Options -MultiViews
    </IfModule>

    RewriteEngine On
    RewriteBase /

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

その状態でウェブブラウザからアクセスするとデフォルトのページが表示されるはずです

Default

参考にさせていただきました :: さくらのレンタルサーバにLaravelをインストール - ふたりはララベル (Laravel PHP Framework)