WEB開発

LaravelをXServer(エックスサーバー)で使う インストール編

[affi id=1]

XServerにlaravelをインストールするまでの手順です。

laravelの他のバージョンでも同様の方法でインストールできます。

前提条件として、XServerへSSH接続ができる状態にしておいてください。

今回やること

独自ドメイン(sample.com)にアクセスしてLaravelが動いていることを確認する。

サーバ要件

公式サイトでは以下のような要件が挙げられています。

PHP >= 7.2.0
BCMath PHP拡張
Ctype PHP拡張
JSON PHP拡張
Mbstring PHP拡張
OpenSSL PHP拡張
PDO PHP拡張
Tokenizer PHP拡張
XML PHP拡張

https://readouble.com/laravel/6.x/ja/installation.html

PHPが7.2.0以上であればいいみたいですね。

以下のコマンドで確認します。

php -v

私は以下のように出ました。

PHP 7.2.20 (cli) (built: Jul 10 2019 06:39:30) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

7.2以上出なければ7.2以上に設定してください。

Laravelのインストール

LaravelはComposerを依存パッケージの管理に使用しています。ですから、Laravelを始める前に、自分の開発機にComposerを確実にインストールしておいてください。

https://readouble.com/laravel/6.x/ja/installation.html

とのことなのでComposerをインストールしましょう。

Composerのインストール

SSH接続でログインしてホームディレクトリで以下を実行します。

https://getcomposer.org/download/にアクセスして以下のコマンドを実行します。

2020/10/19現在最新。

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'c31c1e292ad7be5f49291169c0ac8f683499edddcfd4e42232982d0fd193004208a58ff6f353fde0012d35fdd72bc394') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

以下のようなメッセージが出てきたら完了です。

All settings correct for using Composer
Downloading...
Composer (version 1.9.1) successfully installed to: /home/uedive/composer.phar
Use it: php composer.phar

それではlaravelのインストールに戻りましょう。

laravelインストーラをダウンロードします。

composer global require laravel/installer

すると以下のように出力されます。

Changed current directory to /home/[サーバID]/.config/composer
Using version ^3.0 for laravel/installer
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 14 installs, 0 updates, 0 removals
  - Installing symfony/process (v5.0.1): Downloading (100%)         
  - Installing symfony/polyfill-ctype (v1.13.1): Downloading (100%)         
  - Installing symfony/filesystem (v5.0.1): Downloading (100%)         
  - Installing psr/container (1.0.0): Downloading (100%)         
  - Installing symfony/service-contracts (v2.0.1): Downloading (100%)         
  - Installing symfony/polyfill-php73 (v1.13.1): Downloading (100%)         
  - Installing symfony/polyfill-mbstring (v1.13.1): Downloading (100%)         
  - Installing symfony/console (v5.0.1): Downloading (100%)         
  - Installing ralouphie/getallheaders (3.0.3): Downloading (100%)         
  - Installing psr/http-message (1.0.1): Downloading (100%)         
  - Installing guzzlehttp/psr7 (1.6.1): Downloading (100%)         
  - Installing guzzlehttp/promises (v1.3.1): Downloading (100%)         
  - Installing guzzlehttp/guzzle (6.5.0): Downloading (100%)         
  - Installing laravel/installer (v3.0.1): Downloading (100%)         
symfony/service-contracts suggests installing symfony/service-implementation
symfony/console suggests installing symfony/event-dispatcher
symfony/console suggests installing symfony/lock
symfony/console suggests installing psr/log (For using the console logger)
guzzlehttp/psr7 suggests installing zendframework/zend-httphandlerrunner (Emit PSR-7 responses)
guzzlehttp/guzzle suggests installing psr/log (Required for using the Log middleware)
Writing lock file
Generating autoload files

いい感じですね。

[ad]

プロジェクトを作成する

composerコマンドで新しいプロジェクトを作成しましょう。

今回はsub.sample.comというプロジェクト名で作成します。

cd
composer create-project --prefer-dist laravel/laravel sub.sample.com

エラーが出なければ成功です。

上記のコマンドで実行した場所の直下にsub.sample.comというフォルダができています。この中のpublicというフォルダがドキュメントルートになります。

サブドメインを作成するとあなたの独自ドメインのフォルダ内に作成したサブドメインのフォルダが作成されています。

例)sub.sample.comを作成した場合

sample.com/public/sub フォルダができています。

このフォルダを一度消して作成したプロジェクトのpublicのシンボリックリンクを貼ります。

rm -rf /home/[ユーザー名]/sample.com/public/sub

XServerではドキュメントルートはpublic_htmlになっているので

ln -s -f /home/[ユーザー名]/sub.sample.com/public /home/[ユーザー名]/sample.com/public/sub

それではsample.comにアクセスしてみましょう。

Laravelという文字のあるホームページが出てくれば作成は完了です。お疲れ様でした。

次は設定編です。

-WEB開発
-, , ,