17
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

XServerにLaravel12をデプロイした手順

17
Last updated at Posted at 2025-11-12

TL;DR

SSH接続し下記コマンドを実行します。

cd ~/xs123456.xsrv.jp/
# composerをインストール
curl -sS https://getcomposer.org/installer | php8.4
# アプリケーションの生成
php8.4 composer.phar create-project laravel/laravel example-app
# 既存のドキュメントルートを退避
mv public_html public_html_old
# 公開ディレクトリのシンボリックを作成
ln -s $PWD/example-app/public public_html
# (サブディレクトリに公開する場合)
ln -s $PWD/example-app/public public_html/hoge

https://xs123456.xsrv.jp にアクセスするとLaravelのアプリケーションが確認できます。サブディレクトリに公開した場合は、https://xs123456.xsrv.jp/hoge です。

VScodeでソースコードを編集する

1. SSH FS拡張機能をインストールします

VScode公式のRemote SSHを使おうとしたのですが、XServerのglibcが古すぎるため無理でした。
VScodeのSFTP拡張機能を使うとしたのですうが、不具合が多いのとローカルでソースコードを管理する必要があるため断念しました。

2. SSHの接続設定を追加します

Host: xs123456.xsrv.jp
Port: 10022
Root: /home/xs123456/xs123456.xsrv.jp/example-app/
Username: xs123456
Private key: xs123456.key

image.png

3. ワークスペースフォルダとして追加します

image.png

4. 編集するファイルをクリックして編集し保存します

image.png

Laravelのcronを設定する

下記のコマンドを設定しましょう

* * * * * cd /home/xs123456/xs123456.xsrv.jp/example-app && php8.4 artisan schedule:run >> /dev/null 2>&1

image.png

下記を追記し、cron.logを確認するとcronの動作確認ができます。

routes/console.php
<?php
use Illuminate\Support\Facades\Schedule;

Schedule::call(function () {
    $now = date('Y-m-d H:i:s');
    file_put_contents('cron.log', "Cron ran at $now\n", FILE_APPEND);
})->everyMinute();

php8.4 artisan schedule:listでcronの設定状況を確認できます。

image.png

SSH設定

1. 公開鍵を登録する

image.png

2. 実際にSSHで接続する

ssh -p 10022 -i xs123456.key xs123456@xs123456.xsrv.jp

PHPのバージョンアップ

Laravel 12.xはPHPのバージョンが8.2以上が必要です。

1. バージョン8.2以上を選択し設定します

image.png

デフォルトのPHPのバージョンを8.4にする

mkdir -p $HOME/bin
ln -s $(command -v php8.4) $HOME/bin/php
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc
# 再ログインする
php -v

SSH FS拡張機能のSSH接続で.bash_profileがロードされないため、.bashrcに環境変数を追加してます。

.bash_profileを紛失した場合はこちらからダウンロード
https://gist.github.com/duksis/4276441

デフォルトのcomposerのバージョンを2.xにする

# composerをダウンロード
curl -sS https://getcomposer.org/installer | php8.4
mkdir -p $HOME/bin
mv composer.phar $HOME/bin/composer
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc
# 再ログインする
composer -V

データベースの設定

1. データベースの追加

必要であればMySQLユーザーも追加してください

image.png

2. .envの編集

下記の環境変数を設定します。

cat << 'EOF' >> .env
DB_CONNECTION=mysql
DB_DATABASE=xs123456_test
DB_USERNAME=xs123456_user
DB_PASSWORD=password
EOF

3. マイグレーション

php8.4 artisan migrate

4. 動作確認

$ php8.4 artisan tinker --execute="var_dump(DB::select('SHOW DATABASES'))"
array(2) {
  [0]=>
  object(stdClass)#5357 (1) {
    ["Database"]=>
    string(18) "information_schema"
  }
  [1]=>
  object(stdClass)#5360 (1) {
    ["Database"]=>
    string(12) "xs123456_test"
  }
}

Node.js 22をインストール

ローカルでNode.jsインストールしてビルドファイルをXServerにアップロードでも大丈夫です。

17
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?