Laravel開発環境構築

--------------------------------------------------------------------------------

・前処理

  ・用意するもの

    CentOS-8.2.2004-x86_64-dvd1.iso

--------------------------------------------------------------------------------

・Oracle VM VirtualBox マネージャ  で  新規作成

  ・名前        lara02
  ・タイプ      Linux
  ・バージョン  Other Linux (64bit)

  ・メモリサイズ  1024MB
  ・仮想ハードディスクを作成する
  ・ハードディスクのファイルタイプ: VDI(VirtualBox Disk Image)
  ・物理ハードディスクにあるストレージ: 可変サイズ
  ・ファイルの場所とサイズ: lara02, 80GB
  ・[作成]

--------------------------------------------------------------------------------

・ストレージ

  ・CentOS-8.2.2004-x86_64-dvd1.iso

・ネットワーク
  ・固定IPの場合
    ・[設定(S)] - ネットワーク(固定IP)
      ・ブリッジ・アダプタ
      ・[OK]

  ・DHCPの場合
    ・[設定(S)] - ネットワーク(Local Only)
      ・NAT
      ・Host Only
      ・[OK]

--------------------------------------------------------------------------------

・インストール

  ・起動(T)
  ・Install CentOS 8
    ・Language: English (United States)
    ・DATE & TIME: Asia Tokyo
    ・KEYBOARD: Japanese
    ・SOFTWARE SELECTION: Minimal Install;
    ・KDUMP: disabled
    ・NETWORK & HOST ; Ethernet = ON; lara02.mydomain
    ・[Begin Installation]

    ・ROOT PASSWORD
    ・USER CREATION       // <-- devusr

    ・(処理終了を待つ)

    ・Reboot

    ・ネットワーク設定
      ・DHCPの場合(Local Only の場合)
        /etc/sysconfig/network-scripts/ifcfg-enp0s8
          ONBOOT=yes

      ・固定IPの場合
        $ su -
        # cd /etc/sysconfig/network-scripts
        # cp ifcfg-enp0s3 _ifcfg-enp0s3_org
        # vi ifcfg-enp0s3
        # diff _ifcfg-enp0s3_org ifcfg-enp0s3
        < BOOTPROTO="dhcp"
        ---
        > BOOTPROTO="static"
        > IPADDR="192.168.3.223"
        > NETMASK="255.255.255.0"
        > GATEWAY="192.168.3.1"
        #

        # vi /etc/resolv.conf
        # cat /etc/resolv.conf
        nameserver 192.168.3.1
        #

    ・Reboot
      # shutdown -r now

--------------------------------------------------------------------------------

・以降、client端末からsshでログインし、処理します。

--------------------------------------------------------------------------------

# yum -y update

###
### reboot
###
# shutdown -r now

# yum -y install epel-release https://rpms.remirepo.net/enterprise/remi-release-8.rpm; \
yum config-manager --set-enabled PowerTools

# yum -y install tar wget net-tools gcc make zip unzip emacs nkf

# yum -y install mariadb mariadb-server mariadb-devel

# systemctl enable mariadb.service; \
systemctl start mariadb.service

# mysql -u root -p
パスワード入力はCR(Enter)だけ。
MariaDB [(none)]> set password for 'root'@'localhost' = password('...password...');
MariaDB [(none)]> create database devdb character set utf8;
MariaDB [(none)]> create user 'devusr'@'localhost' identified by '...password...';
MariaDB [(none)]> grant all privileges on devdb.* to 'devusr'@'localhost';
MariaDB [(none)]> exit

# yum -y module install php:remi-7.4

# yum -y install zlib-devel curl-devel openssl-devel \
httpd-devel apr-devel apr-util-devel libffi-devel

# yum -y install php-bcmath php-dba php-dbg \
php-embedded php-enchant php-gd php-gmp php-intl php-ldap \
php-mysqlnd php-odbc php-opcache php-pdo php-pgsql \
php-process php-recode php-snmp php-soap php-xmlrpc php-zip

# systemctl enable httpd; \
systemctl start httpd

# firewall-cmd --permanent --add-service=http; \
firewall-cmd --permanent --add-service=https; \
firewall-cmd --reload

# cd /etc
# cp php.ini ~/php.ini
# vi php.ini
# diff ~/php.ini php.ini
588a589
> error_log = /var/log/php_error.log
923a925
> date.timezone = Asia/Tokyo
1017c1019
< pdo_mysql.default_socket=
---
> pdo_mysql.default_socket=/var/lib/mysql/mysql.sock

# touch /var/log/php_error.log; \
chown apache:apache /var/log/php_error.log; \
chmod 666 /var/log/php_error.log

# setsebool -P httpd_can_network_connect 1; \
setsebool -P httpd_can_network_connect_db 1

# systemctl restart httpd

###
### npm
###
# yum -y install npm

###
### Image Magick
###
# yum -y install ImageMagick ImageMagick-devel

###
### composer
###
# cd
# wget https://getcomposer.org/installer -O composer-installer.php
# php composer-installer.php --filename=composer --install-dir=/usr/local/bin
# composer self-update --preview
# rm -f composer-installer.php


###
### .bashrc
###
# cd
# vi .bashrc
# cat .bashrc
<<<
# .bashrc

# User specific aliases and functions

# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

alias ls='ls'
alias ll='ls -la'
alias e='emacs'
alias delb='rm -f *~ .??*~'
>>>

###
### .emacs
###
# cd
# vi .emacs
# cat .emacs
<<<
(setq-default tab-width 4 indent-tabs-mode nil)

(add-hook 'php-mode-hook
          (lambda ()
            (setq tab-width 4)
            (setq c-basic-offset 4)))
>>>

###
### 参照: https://readouble.com/laravel/
###
### PATH="$HOME/.local/bin:$HOME/bin:$PATH;$HOME/.composer/vendor/bin:..."
###

# yum -y install git
# su - devusr
$ composer global about
$ vi .bashrc
$ cat .bashrc
<<<
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific environment
PATH="$HOME/.local/bin:$HOME/bin:$PATH;$HOME/.composer/vendor/bin:$HOME/.composer/vendor/laravel/installer/bin"
export PATH

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

alias ls='ls'
alias ll='ls -la'
alias e='emacs'
alias delb='rm -f *~ .??*~'
>>>

$ composer global require laravel/installer
$ composer create-project --prefer-dist laravel/laravel lar7
$ exit

# mv /home/devusr/lar7 /var/www/lar7; \
chown -R devusr:devusr /var/www/lar7; \
chcon -R -t httpd_sys_rw_content_t /var/www/lar7

# chown -R apache:apache /var/www/lar7/storage

# cd /etc/httpd/conf.d
# vi lar7.conf
# cat lar7.conf
<VirtualHost *:80>
    ServerName lara02.mydomain
    DocumentRoot /var/www/lar7/public
    SetEnv APPLICATION_ENV "development"       // <-- 環境変数
    <Directory /var/www/lar7/public>
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
        <IfModule mod_authz_core.c>
            Require all granted
        </IfModule>
    </Directory>
</VirtualHost>
#

# systemctl restart httpd

### 例:
### Client側(Windows10)のC:\Windows\System32\drivers\etc\hosts
### "192.168.56.124  lara02.mydomain"を追加。

### Check http://lara02.mydomain

# vi /etc/hosts
<<<
次の1行を追加
192.168.56.124  lara02.mydomain
>>>
# su - devusr
$ cd /var/www/lar7
$ php artisan --version
$ exit
#


/*
 * https://qiita.com/sano1202/items/6021856b70e4f8d3dc3d
 *
 * ──/var/www/lar7
 *     ├── app       ・・・アプリケーションのロジック
 *     ├── bootstrap ・・・laravelフレームワークの起動コード
 *     ├── config    ・・・設定ファイル
 *     ├── database  ・・・MigrationファイルなどDB関連
 *     ├── public    ・・・Webサーバのドキュメントルート
 *     ├── resources ・・・ビューや言語変換用ファイルなど
 *     ├── routes    ・・・ルーティング用ファイル
 *     ├── storage   ・・・フレームワークが使用するファイル
 *     ├── tests     ・・・テストコード
 *     └── vendor    ・・・Composerでインストールしたライブラリ
 */

# touch /var/www/lar7/storage/logs/laravel.log
# chmod 666 /var/www/lar7/storage/logs/laravel.log

# su - devusr
$ vi album.sql
$ cat album.sql
--
-- mysql -u devusr -p devdb
--

use devdb;

DROP TABLE IF EXISTS `album`;
CREATE TABLE `album` (
    `id`      INT(11)      NOT NULL AUTO_INCREMENT,
    `artist`  VARCHAR(100) NOT NULL,
    `title`   VARCHAR(100) NOT NULL,
    `del_flg` BOOLEAN NOT NULL DEFAULT '0',
    `c_ts`    TIMESTAMP,
    `c_id`    BIGINT,
    `c_ip`    VARCHAR(80),
    `u_ts`    TIMESTAMP,
    `u_id`    BIGINT,
    `u_ip`    VARCHAR(80),
    PRIMARY KEY (id)
);

INSERT INTO `album` (`artist`, `title`) VALUES
('The Military Wives', 'In My Dreams'),
('Adele', '21'),
('Bruce Springsteen', 'Wrecking Ball (Deluxe)'),
('Bruno Mars', 'Unorthodox Jukebox'),
('Various Artists', 'Call the Midwife (Music From the TV Series)');

UPDATE `album` SET c_ts = NOW(), c_id = 1, c_ip = '127.0.0.1';
$

$ mysql -u devusr -p devdb
> \. album.sql
> exit
$

$ cd /var/www/lar7
$ cp .env .env.org
$ vi .env
$ diff .env.org .env
1c1
< APP_NAME=Laravel
---
> APP_NAME=Lara02
5c5
< APP_URL=http://localhost
---
> APP_URL=http://lara02.mydomain
12,14c12,14
< DB_DATABASE=laravel
< DB_USERNAME=root
< DB_PASSWORD=
---
> DB_DATABASE=devdb
> DB_USERNAME=devusr
> DB_PASSWORD=...password...
$

$ php artisan tinker
Psy Shell v0.10.4 (PHP 7.4.9 — cli) by Justin Hileman
>>> DB::select('select * from album where id = 1');
=> [
     {#3081
       +"id": 1,
       +"artist": "The Military Wives",
       +"title": "In My Dreams",
       +"del_flg": 0,
       +"c_ts": "2020-06-10 12:48:45",
       +"c_id": 1,
       +"c_ip": "127.0.0.1",
       +"u_ts": "0000-00-00 00:00:00",
       +"u_id": null,
       +"u_ip": null,
     },
   ]
>>> q
Exit:  Goodbye
$

$ cd /var/www/lar7
$ composer update
$ composer fund

$ cd /var/www/lar7/config
$ vi database.php
<<<
...
        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'devdb'),
            'username' => env('DB_USERNAME', 'devusr'),
            'password' => env('DB_PASSWORD', '...password...'),
...
>>>

$ cd /var/www/lar7
$ php artisan make:controller AlbumController
$ php artisan make:model Album
$ cd /var/www/lar7/app
$ vi Album.php
$ cat Album.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Facades\DB;

class Album extends Model
{
    public function listAll() {
        $records = DB::select('SELECT * FROM `album`');
        return $records;
    }
}
$

$ cd /var/www/lar7/app/Http/Controllers
$ vi AlbumController.php
$ cat AlbumController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Album;

class AlbumController extends Controller
{
    public function index() {
        return 'Hello world.';
    }

    public function list() {
        $album = new Album;
        $dat = ['records' => $album->listAll()];
        return view('album.list', $dat);
    }
}
$

$ cd /var/www/lar7/resources/views
$ mkdir album
$ cd /var/www/lar7/resources/views/album
$ vi list.blade.php
$ cat list.blade.php
<!DOCTYPE html>
<html>
  <head>
    <meta charset ="UTF-8" />
    <title>Album list</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
  </head>
  <body>
    <table class="table">
      <tr>
        <th>Artist</th>
        <th>Title</th>
      </tr>
      @foreach($records as $record)
      <tr>
        <td>{{ $record->artist }}</td>
        <td>{{ $record->title }}</td>
      </tr>
      @endforeach
    </table>
  </body>
</html>
$

$ cd /var/www/lar7/routes
$ vi web.php
<<<
この2行を追加。
Route::get('/album',      'AlbumController@index');
Route::get('/album/list', 'AlbumController@list');
>>>

以下の2つのページをチェック。
1. http://lara02.mydomain/album
2. http://lara02.mydomain/album/list

.end of line