Laravel3、コードをドキュメントルートから外す(SSH未使用)
タグ: Laravel3
共有サーバーなどでコマンドシェル(SSH)が使用できない場合に、Laravelのコアを複数サイトで共有し、ドキュメントルートから実行コードを移動するシェルスクリプトの紹介です。
詳細は、前記事をご覧ください。
(追記:2013/03/18 スクリプトを更新しました。前バージョンでは、ローカルとリモートのドキュメントルートが同一名として取り扱っていましたが、今回のバージョンでは別々に指定できるようにしました。)
前回と同じ2サイトの状況で考えましょう。全く同じですが、前回はホストの構造で、今回はローカルの構造を想定しています。
/home
/yourhome
/public_html <= ドキュメントルート
/site_a <= 仮想ホストA、以下はLaravelの構造
/application
/bundle
/laravel
/public
/bundle
/css
/img
/js
index.php
/storage
artisan
paths.php
/site_b <= 仮想ホストB、以下はLaravelの構造
/application
/bundle
/laravel
/public
/bundle
/css
/img
/js
index.php
/storage
artisan
paths.php
今回のバージョンは、html_public下はいじりません。代わりに作業用のディレクトリー(下の例ではlaravel-unite)に構成します。
/home
/yourhome
/laravel-unite
/laravel_dir
/laravel <= Laravelのコア、全サイト共通で使用
/sites
/site_a <=仮想ホストAのコード部分とstorage
/application
/bundle
/storage
artisan
paths.php
/site_b <=仮想ホストBのコード部分とstorage
/application
/bundle
/storage
artisan
paths.php
/public_html
/site_a <= publicの内容
/bundle
/css
/img
/js
index.php
/site_b <= publicの内容
/bundle
/css
/img
/js
index.php
/public_html <= 本来のドキュメントルートの内容は変更しない
この構造にしておき、laravel_uniteディレクトリーを圧縮し、ホスト先にアップ、解凍後にhtml_publicとlaravel-dirを適当な場所にコピーします。もしくはFTPでダイレクトに一つずつアップしましょう。
# インストール方法
前記事をご覧ください。注意点は変わりません。
設定するディレクトリの内容は変わります。
publichtml: アップ先のホストのドキュメントルートをフルパスで指定します。間違ってローカルの環境のパスを設定しないようにしてください。localhtml: ローカルのドキュメントルートをフルパスで指定します。codedir: アップ先でコードをまとめるディレクトリーを指定します。間違ってローカル環境のパスを指定しないでください。workdir: ローカルの作業用ディレクトリーパスです。フルパスで指定してください。このディレクトリーの中に構築されます。
シェルのコードは以下の通りです。
# !/usr/bin/sh
set -e
# Please set your html document root (On production server)
publichtml=/On-Production-Server/home/Your-Remote-Home-Directory/public_html
# Please set your html document root (On local server)
localhtml=/On-Local-Server/home/Your-Local-Home-Directory/local_html
# Please set your directory to collect core/core files/directories (On production server)
codedir=/On-Production-Server/home/Your-Remote-Home-Directory/laravel-dirs
# Please set directory that file structure put into(On local server)
workdir=/On-Local-Server/home/Your-Local-Home-Directory/laravel-unite
if [ $# != 1 ]
then
echo
echo Unite Laravel file structure into out of web document root.
echo
echo Usage: $0 Target-directory-name
echo
echo Note: Before first run this command, please set your directories to fit your host envrionments.
echo
exit 1
fi
laravelcore="${codedir}/laravel"
sitesdir="${codedir}/sites"
distdir="${sitesdir}/$1"
targetdir4production="${publichtml}/$1"
targetdir4local="${localhtml}/$1"
basename=`basename ${publichtml}`
publicbase="${workdir}/${basename}"
basename=`basename ${codedir}`
codebase="${workdir}/${basename}"
basename=`basename ${sitesdir}`
sitesbase="${codebase}/${basename}"
basename=`basename ${laravelcore}`
corebase="${codebase}/${basename}"
targetbase="${sitesbase}/$1"
if [ $publichtml = '/On-Production-Server/home/Your-Remote-Home-Directory/public_html' ]
then
echo Error! Please open this script with a editor, and set your production server's document root to 'publichtml'.
exit 10
fi
if [ $localhtml = '/On-Local-Server/home/Your-Local-Home-Directory/local_html' ]
then
echo Error! Please open this script with a editor, and set your local server's document root to 'localhtml'.
exit 11
fi
if [ $codedir = '/On-Production-Server/home/Your-Remote-Home-Directory/laravel-dirs' ]
then
echo Error! Please open this script with a editor, and set your directory path that collect your codes to 'codedir'.
fi
if [ $workdir = '/On-Local-Server/home/Your-Local-Home-Directory/laravel-unite' ]
then
echo Error! Please open this script with a editor, and set you directory path to make upload folder to 'workdir'.
fi
if [ $publicbase = $codebase ]
then
echo Error! This script will make two directories into $workdir named from base directory name of 'publichtml' and 'codedir'.
echo Both last name of directory paths \(base name\) are same. So please specify diffrent names with them.
fi
if [ ! -d $targetdir4local ]
then
echo Error! Target directory is not found. \($targetdir4local\)
exit 2
fi
if [ ! -f "${targetdir4local}/paths.php" ]
then
echo Error! Maybe already united. No paths.php in $targetdir4local
exit 3
fi
if [ ! -d $workdir ]
then
mkdir $workdir
fi
if [ ! -d $publicbase ]
then
mkdir $publicbase
fi
if [ ! -d $codebase ]
then
mkdir $codebase
fi
if [ ! -d $sitesbase ]
then
mkdir $sitesbase
fi
# copy laravel root folder to united directory
cp -fa $targetdir4local $sitesbase
# mv public folder to temporary document root.
mv "${targetbase}/public" "${publicbase}/$1"
# copy core folder if not exist in united directory.
if [ ! -d $corebase ]
then
cp -fa "${targetbase}/laravel/" $corebase
fi
# remove core folder
rm -R "${targetbase}/laravel"
# replace paths.php path in index.php
sed -i -e "
s+../paths.php+${distdir}/paths.php+
" "${publicbase}/$1/index.php"
# replace public/sys path in paths.php
sed -i -e "
s+paths[ \n\r\f\t]*[[ \n\r\f\t]*'sys'[ \n\r\f\t]*][ \n\r\f\t]*=[ \n\r\f\t]*'laravel'+paths['sys'] = '${laravelcore}'+
s+paths[ \n\r\f\t]*[[ \n\r\f\t]*'public'[ \n\r\f\t]*][ \n\r\f\t]*=[ \n\r\f\t]*'public'+paths['public'] = '${targetdir4production}'+
" "${targetbase}/paths.php"
echo "Successfully complited."
exit 0
使用方法
前回のコマンドと同じです。
作業手順
前記の例でコマンド実行後の処理を追ってみましょう。
- 出来上がったlaravel-uniteフォルダーを圧縮し、ホストにアップします。
- 解凍し、フォルダーを開きます。
- ドキュメントルートであるpublic_htmlへ移動し、site_a、site_bフォルダーを削除します。
- 解凍したフォルダーの中のpublic_htmlフォルダーに含まれるsite_aとsite_bフォルダーをドキュメントルートへ移動します。
- 解凍したフォルダーの中にあるlaravel_dirをシェルのcodedirで指定した場所へ移動します。
- 仮想ホストの設定でサイトA、Bのドキュメントルートを設定しなおしてください。