conohaVPSでWordPressとPHP(Laravel)を共存させる

Laravel
この記事が対象とする環境
Git 2.39.3
Composer 2.5.8
Node.js 9.5.0
PHP 8.2.7
Laravel 9.19

ゴール

サーバー上のフォルダ構成は以下の通りです。

var/
    `-- www/
        `-- html/
            |-- blog/ ← WordPress install'd
            `-- laravel/ ← Laravel set up now

「blog」フォルダで「https://ドメイン/blog」を表示させます。
「laravel」フォルダで「https://ドメイン」を表示させます。

※LaravelのURLをサブディレクトリにする方法は別サイトを確認してください。本記事ではルートURLを前提としています。

PHP(Laravel)環境を構築する

前提

WordPressはセットアップ済みとします。WordPressの構築については以下の記事を参照してください。

管理者ユーザーに切り替える

パスワードの入力を求められるので、rootユーザーのパスワードを入力します。

su

 

Gitインストール

dnfコマンドでGitをインストールします。

dnf install -y git-all
Last metadata expiration check: 0:40:49 ago on Sun 11 Jun 2023 05:21:36 PM JST.
Dependencies resolved.
===================================================================================================
 Package                                         Architecture   Version         Repository    Size
===================================================================================================
Installing:
 git-all                                         noarch         2.39.3-1.el9    appstream    4.4 k
Installing dependencies:
 git                                             x86_64         2.39.3-1.el9    appstream     62 k
 git-core                                        x86_64         2.39.3-1.el9    appstream    4.3 M
 git-core-doc                                    noarch         2.39.3-1.el9    appstream    2.9 M
 git-credential-libsecret                        x86_64         2.39.3-1.el9    appstream     15 k
~ 省略 ~
Installing weak dependencies:
 perl-Clone                                      x86_64         0.45-6.el9      appstream     23 k

Transaction Summary
===================================================================================================
Install  52 Packages

Total download size: 16 M
Installed size: 63 M
Downloading Packages:
(1/52): git-2.39.3-1.el9.x86_64.rpm                            233 kB/s |  62 kB     00:00
(2/52): git-all-2.39.3-1.el9.noarch.rpm                         16 kB/s | 4.4 kB     00:00
(3/52): git-credential-libsecret-2.39.3-1.el9.x86_64.rpm       740 kB/s |  15 kB     00:00
~ 省略 ~
(52/52): tk-8.6.10-9.el9.x86_64.rpm                             19 MB/s | 1.6 MB     00:00
------------------------------------------------------------------------------------------
Total                                                           14 MB/s |  16 MB     00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                             1/1 
  Installing       : git-core-2.39.3-1.el9.x86_64               1/52 
  Installing       : perl-lib-0.65-480.el9.x86_64               2/52 
  Installing       : perl-TermReadKey-2.38-11.el9.x86_64        3/52 
~ 省略 ~
  Running scriptlet: git-all-2.39.3-1.el9.noarch               52/52 
  Verifying        : git-2.39.3-1.el9.x86_64                    1/52 
  Verifying        : git-all-2.39.3-1.el9.noarch                2/52 
  Verifying        : git-core-2.39.3-1.el9.x86_64               3/52 
~ 省略 ~
  Verifying        : utf8proc-2.6.1-4.el9.x86_64               52/52 
Installed products updated.

Installed:
  git-2.39.3-1.el9.x86_64                      git-all-2.39.3-1.el9.noarch                   git-core-2.39.3-1.el9.x86_64
  git-core-doc-2.39.3-1.el9.noarch             git-credential-libsecret-2.39.3-1.el9.x86_64  git-daemon-2.39.3-1.el9.x86_64
  git-email-2.39.3-1.el9.noarch                git-gui-2.39.3-1.el9.noarch                   git-instaweb-2.39.3-1.el9.noarch
  git-subtree-2.39.3-1.el9.x86_64              git-svn-2.39.3-1.el9.noarch                   gitk-2.39.3-1.el9.noarch
  gitweb-2.39.3-1.el9.noarch                   libsecret-0.20.4-4.el9.x86_64                 libserf-1.3.9-26.el9.x86_64
  perl-Authen-SASL-2.16-25.el9.noarch          perl-CGI-4.51-5.el9.noarch                    perl-Clone-0.45-6.el9.x86_64
  perl-Compress-Raw-Bzip2-2.101-5.el9.x86_64   perl-Compress-Raw-Zlib-2.101-5.el9.x86_64     perl-Digest-HMAC-1.03-29.el9.noarch
  perl-Digest-SHA-1:6.02-461.el9.x86_64        perl-Encode-Locale-1.05-21.el9.noarch         perl-Error-1:0.17029-7.el9.noarch
  perl-File-Find-1.37-480.el9.noarch           perl-GSSAPI-0.28-37.el9.x86_64                perl-Git-2.39.3-1.el9.noarch
  perl-Git-SVN-2.39.3-1.el9.noarch             perl-HTML-Parser-3.76-3.el9.x86_64            perl-HTML-Tagset-3.20-47.el9.noarch
  perl-HTTP-Date-6.05-7.el9.noarch             perl-HTTP-Message-6.29-3.el9.noarch           perl-I18N-Langinfo-0.19-480.el9.x86_64
  perl-IO-Compress-2.102-4.el9.noarch          perl-IO-HTML-1.004-4.el9.noarch               perl-LWP-MediaTypes-6.04-9.el9.noarch
  perl-MailTools-2.21-9.el9.noarch             perl-Memoize-1.03-480.el9.noarch              perl-Net-SMTP-SSL-1.04-16.el9.noarch
  perl-Term-ReadLine-1.17-480.el9.noarch       perl-TermReadKey-2.38-11.el9.x86_64           perl-Tie-4.6-480.el9.noarch
  perl-Time-HiRes-4:1.9764-462.el9.x86_64      perl-TimeDate-1:2.33-6.el9.noarch             perl-YAML-1.30-8.el9.noarch
  perl-filetest-1.03-480.el9.noarch            perl-lib-0.65-480.el9.x86_64                  subversion-1.14.1-5.el9.x86_64
  subversion-libs-1.14.1-5.el9.x86_64          subversion-perl-1.14.1-5.el9.x86_64           tk-1:8.6.10-9.el9.x86_64
  utf8proc-2.6.1-4.el9.x86_64                         

Complete!

Gitからソースをダウンロードする

Gitコマンドでソースを取得します。

cd /var/www/html/laravel
git clone -b ブランチ名 https://リポジトリパス

Composerダウンロード

wgetコマンドを使用します。wgetをインストールしていない場合、インストールしてください。

wget https://getcomposer.org/installer -O composer-installer.php
--2023-06-28 00:00:00--  https://getcomposer.org/installer
Resolving getcomposer.org (getcomposer.org)... 2607:5300:201:2100::4:d105, 142.44.245.229
Connecting to getcomposer.org (getcomposer.org)|2607:5300:201:2100::4:d105|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 58140 (57K) [application/octet-stream]
Saving to: ‘composer-installer.php’

composer-installer.php 100%[=====================>]  56.78K   368KB/s    in 0.2s    

2023-06-28 00:00:00 (368 KB/s) - ‘composer-installer.php’ saved [58140/58140]

Composerインストール

php composer-installer.php --filename=composer --install-dir=/usr/local/bin
All settings correct for using Composer
Downloading...

Composer (version 2.5.8) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

ライブラリをComposerでインストール

composer install
Installing dependencies from lock file (including require-dev)
Verifying lock file contents can be installed on current platform.
Your lock file does not contain a compatible set of packages. Please run composer update.

  Problem 1
    - inertiajs/inertia-laravel is locked to version v0.6.3 and an update of this package was not requested.
    - inertiajs/inertia-laravel v0.6.3 requires php ^7.2|~8.0.0|~8.1.0 -> your php version (8.2.7) does not satisfy that requirement.
  Problem 2
    - nette/schema is locked to version v1.2.2 and an update of this package was not requested.
    - nette/schema v1.2.2 requires php >=7.1 <8.2 -> your php version (8.2.7) does not satisfy that requirement.
  Problem 3
    - nette/schema v1.2.2 requires php >=7.1 <8.2 -> your php version (8.2.7) does not satisfy that requirement.
    - league/config v1.1.1 requires nette/schema ^1.2 -> satisfiable by nette/schema[v1.2.2].
    - league/config is locked to version v1.1.1 and an update of this package was not requested.

エラーが出てしまいました。

Your lock file does not contain a compatible set of packages. Please run composer update.
(ロック ファイルには、互換性のあるパッケージのセットが含まれていません。コンポーザーのアップデートを実行してください)

Composer Update

composer update
Loading composer repositories with package information
Info from https://repo.packagist.org: #StandWithUkraine
Updating dependencies
Lock file operations: 7 installs, 81 updates, 2 removals
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 126 installs, 0 updates, 0 removals
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

   INFO  Discovering packages.  

  inertiajs/inertia-laravel ...... DONE
  jenssegers/agent ............... DONE
  laravel/fortify ................ DONE
  laravel/jetstream .............. DONE
  laravel/sail ................... DONE
  laravel/sanctum ................ DONE
  laravel/tinker ................. DONE
  nesbot/carbon .................. DONE
  nunomaduro/collision ........... DONE
  nunomaduro/termwind ............ DONE
  spatie/laravel-ignition ........ DONE
  tightenco/ziggy ................ DONE

88 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force

   INFO  No publishable resources for tag [laravel-assets].  

No security vulnerability advisories found

Node.js(npm)インストール

dnfコマンドでNode.jsをインストールします。

dnf module -y install nodejs
Last metadata expiration check: 1:19:40 ago on Sun 11 Jun 2023 05:21:36 PM JST.
Dependencies resolved.
===============================================================================================================================================================
 Package                            Architecture             Version                                                         Repository                   Size
===============================================================================================================================================================
Installing group/module packages:
 nodejs                             x86_64                   1:18.14.2-2.module_el9+307+7e5e4074                             appstream                    12 M
 npm                                x86_64                   1:9.5.0-1.18.14.2.2.module_el9+307+7e5e4074                     appstream                   2.5 M
Installing weak dependencies:
 nodejs-docs                        noarch                   1:18.14.2-2.module_el9+307+7e5e4074                             appstream                   7.6 M
 nodejs-full-i18n                   x86_64                   1:18.14.2-2.module_el9+307+7e5e4074                             appstream                   8.4 M
Installing module profiles:
 nodejs/common                                                                                                                                                
Enabling module streams:
 nodejs                                                      18                                                                                               

Transaction Summary
===============================================================================================================================================================
Install  4 Packages

Total download size: 31 M
Installed size: 164 M
Downloading Packages:
(1/4): nodejs-docs-18.14.2-2.module_el9+307+7e5e4074.noarch.rpm                                                                 18 MB/s | 7.6 MB     00:00    
(2/4): npm-9.5.0-1.18.14.2.2.module_el9+307+7e5e4074.x86_64.rpm                                                                 18 MB/s | 2.5 MB     00:00    
(3/4): nodejs-full-i18n-18.14.2-2.module_el9+307+7e5e4074.x86_64.rpm                                                            13 MB/s | 8.4 MB     00:00    
(4/4): nodejs-18.14.2-2.module_el9+307+7e5e4074.x86_64.rpm                                                                      14 MB/s |  12 MB     00:00    
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                           22 MB/s |  31 MB     00:01     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                       1/1 
  Installing       : nodejs-docs-1:18.14.2-2.module_el9+307+7e5e4074.noarch                                                                                1/4 
  Installing       : nodejs-full-i18n-1:18.14.2-2.module_el9+307+7e5e4074.x86_64                                                                           2/4 
  Installing       : npm-1:9.5.0-1.18.14.2.2.module_el9+307+7e5e4074.x86_64                                                                                3/4 
  Installing       : nodejs-1:18.14.2-2.module_el9+307+7e5e4074.x86_64                                                                                     4/4 
  Running scriptlet: nodejs-1:18.14.2-2.module_el9+307+7e5e4074.x86_64                                                                                     4/4 
  Verifying        : nodejs-1:18.14.2-2.module_el9+307+7e5e4074.x86_64                                                                                     1/4 
  Verifying        : nodejs-docs-1:18.14.2-2.module_el9+307+7e5e4074.noarch                                                                                2/4 
  Verifying        : nodejs-full-i18n-1:18.14.2-2.module_el9+307+7e5e4074.x86_64                                                                           3/4 
  Verifying        : npm-1:9.5.0-1.18.14.2.2.module_el9+307+7e5e4074.x86_64                                                                                4/4 
Installed products updated.

Installed:
  nodejs-1:18.14.2-2.module_el9+307+7e5e4074.x86_64                                nodejs-docs-1:18.14.2-2.module_el9+307+7e5e4074.noarch                     
  nodejs-full-i18n-1:18.14.2-2.module_el9+307+7e5e4074.x86_64                      npm-1:9.5.0-1.18.14.2.2.module_el9+307+7e5e4074.x86_64                     

Complete!

ビルド

今回、LaravelでViteを選択しているのでビルドします。

npm run build
> build
> vite build

vite v3.2.3 building for production...
transforming (94) node_modules/@inertiajs/inertia/node_modules/axios/lib/helpers/bind.jsBrowserslist: caniuse-lite is outdated. Please run:
  npx update-browserslist-db@latest
  Why you should do it regularly: https://github.com/browserslist/update-db#readme
✓ 214 modules transformed.

フォルダのアクセス権限変更

laravel.logを出力する場合、storageフォルダへの書き込み権限が必要です。

chmod -R 707 /var/www/html/laravel/storage

nginx.confの編集

server {
    root /var/www/html/laravel/public/;

    location ^~ / {
        index index.html index.php;
        try_files $uri $uri/ /index.php$is_args$args;

        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_pass unix:/run/php-fpm/www.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }
    }

nginx、php-fpm再起動

systemctl restart nginx.service
systemctl restart php-fpm.service

完了です。

札幌在住エンジニア。JavaやPHPやWordPressを中心とした記事が中心です。

【SE歴】四半世紀以上
【Backend】php / java(spring) / c# / AdobeFlex / c++ / VB / cobol
【Frontend】 vue.js / jquery他 / javascript / html / css
【DB】oracle / mysql / mariadb / sqlite
【infrastructure】aws / oracle / gcp
【license】aws(saa-c03) / oracle master / XML Master / Sun Certified Programmer for the Java 2 Platform 1.4

Nobelをフォローする
Laravel
Nobelをフォローする

コメント

タイトルとURLをコピーしました