「インストール - Laravel」の版間の差分

提供:MochiuWiki - SUSE, Electronic Circuit, PCB
ナビゲーションに移動 検索に移動
71行目: 71行目:


== 仮想ホストを構築する場合 (推奨) ==
== 仮想ホストを構築する場合 (推奨) ==
外部のクライアントPCからLaravelのプロジェクトに接続する場合、仮想ホストを構築する必要がある。<br>
外部のクライアントPCからLaravelのプロジェクトに接続する場合、仮想ホストを構築すべきである。<br>
<br>
<br>
SUSEの場合、/etc/apache2/vhosts.dディレクトリに、以下のような仮想ホスト向けの設定ファイルを作成する。<br>
SUSEの場合、以下に示すような仮想ホスト向けの設定ファイルを作成する。<br>
ここでは、仮想ホスト名を<u>laravel01</u>、ドキュメントルートを<u>~/htdocs/laravel01/public</u>ディレクトリとしている。<br>
* Apache2
*: /etc/apache2/vhosts.d/
* NginX
*: /etc/nginx/vhosts.d/
<br>
ここでは、以下に示すような仮想ホストを設定する。<br>
* 仮想ホスト名
*: <u>laravel01</u>
* ドキュメントルート
*: <u>~/htdocs/laravel01/public/</u>
<br>
<br>
<u>※注意</u><br>
<u>※注意</u><br>
<u>Laravelのプロジェクトディレクトリにあるpublicディレクトリがドキュメントルートであるため、このディレクトリをアクセスするように設定する。</u><br>
<u>Laravelのプロジェクトディレクトリにあるpublicディレクトリがドキュメントルートであるため、このディレクトリをアクセスするように設定する。</u><br>
# NginXの場合
sudo vi /etc/nginx/vhosts.d/vhost-laravel01.conf
# Apache2の場合
  sudo vi /etc/apache2/vhosts.d/vhost-laravel01.conf
  sudo vi /etc/apache2/vhosts.d/vhost-laravel01.conf
<br>
<br>
<syntaxhighlight lang="nginx">
# /etc/nginx/vhosts.d/vhost-laravel01.confファイル
# 仮想ホスト laravel01の設定
server {
    listen      80;
    server_name  laravel01;
    access_log  log/laravel01.access.log main;
    error_page  404  /404.html;
    location = /404.html {
        root  /home/<ユーザ名>/htdocs/laravel01/public;
    }
    # redirect server error pages to the static page /50x.html
    #
    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
        root  /home/<ユーザ名>/htdocs/laravel01/public;
    }
    location / {
        root  /home/<ユーザ名>/htdocs/laravel01/public;
        index  index.php index.html index.htm;
    }
    # このセクションは、NginxからPHP-FPMの通信はUnixドメインソケット通信でlistenしているFastCGIサーバにPHPスクリプトを渡すために使用される
    location ~ \.php$
    {
        root          /home/<ユーザ名>/htdocs/laravel01/public;
        fastcgi_pass  unix:/var/run/php-fpm/php-fpm.sock;
        #fastcgi_pass  127.0.0.1:9000;
        fastcgi_index  index.php;
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        # proxy_pass  http://127.0.0.1;
    }
    location /mp3 {
        # sendfile関数の使用の有効化 / 無効化
        sendfile  on;
        # 1回のsendfile関数で転送できるデータ量を制限する (大きなサイズのファイルを転送する場合、接続が長時間ブロックされる可能性があるため)
        # この制限が無い場合、1つの高速な接続がワーカープロセスを停止させる可能性がある
        sendfile_max_chunk  1m;  # Limits chunks to 1[MB]
    }
}
</syntaxhighlight>
<br>
<syntaxhighlight lang="apache">
  # /etc/apache2/vhosts.d/vhost-laravel01.confファイル
  # /etc/apache2/vhosts.d/vhost-laravel01.confファイル
   
   
207行目: 276行目:
     </Directory>
     </Directory>
  </VirtualHost>
  </VirtualHost>
</syntaxhighlight>
<br>
<br>
次に、サーバ側のPCの/etc/hostsファイルにおいて、以下の設定を追記する。<br>
次に、サーバ側のPCの/etc/hostsファイルにおいて、以下の設定を追記する。<br>
215行目: 285行目:
  127.0.0.1    laravel01
  127.0.0.1    laravel01
<br>
<br>
次いで、外部のクライアントPCの/etc/hostsファイルにおいて、以下の設定を追記する。<br>
次に、外部のクライアントPCの/etc/hostsファイルにおいて、以下の設定を追記する。<br>
  sudo vi /etc/hosts
  sudo vi /etc/hosts
<br>
<br>
225行目: 295行目:
<br>
<br>
<u>※注意</u><br>
<u>※注意</u><br>
<u>もし、Laravelのプロジェクトディレクトリに対して、ユーザ名やグループ名をApache2のものに変更する時、</u><br>
<u>特に、パッケージ管理システムからPHPおよびWebサーバをインストールした場合、Laravelのプロジェクトディレクトリ下のファイルおよびディレクトリにおいて、</u><br>
<u>SUSEの場合、Apache2のユーザ名およびグループ名は<code>www</code>である。</u><br>
<u>ユーザ名およびグループ名をNginX / Apache2のものに変更が必要な場合がある。</u><br>
<u>SUSEの場合、NginX / Apache2のユーザ名およびグループ名は、それぞれ<code>wwwrun</code>、<code>www</code>である。</u><br>
<br>
<br>
変更方法は、以下に示す2種類ある。<br>
変更方法は、以下に示す2種類ある。<br>
* 方法 1 (推奨)
* 方法 1 (推奨)
*: まず、ユーザを<code>www</code>グループに追加する。
*: まず、ユーザを<code>www</code>グループに追加する。
*: <code>sudo usermod -aG www "${USER}"</code>
*: <code>sudo usermod -aG www "$USER"</code>
*: <br>
*: <br>
*: 次に、Laravelのプロジェクトディレクトリにあるstorageディレクトリのグループ名を変更する。
*: 次に、Laravelのプロジェクトディレクトリにあるstorageディレクトリのグループ名を変更する。
*: <code>cd <Laravelのプロジェクト></code>
*: <code>cd <Laravelのプロジェクト></code>
*: <code>sudo chown -R "${USER}":www storage bootstrap/cache</code>
*: <code>sudo chown -R "$USER":www storage bootstrap/cache</code>
*: <br>
*: <br>
*: ユーザとWebサーバの両方に対して、パーミッションを変更する。
*: ユーザとWebサーバの両方に対して、パーミッションを変更する。
246行目: 317行目:
*: <br>
*: <br>
* 方法 2
* 方法 2
*: まず、ユーザを<code>wwwrun</code>グループに追加する。
*: まず、ユーザを<code>www</code>グループに追加する。
*: <code>sudo usermod -aG www "${USER}"</code>
*: <code>sudo usermod -aG www "$USER"</code>
*: <br>
*: <br>
*: 次に、Laravelのプロジェクトディレクトリにあるstorageディレクトリのグループ名を変更する。
*: 次に、Laravelのプロジェクトディレクトリにあるstorageディレクトリのグループ名を変更する。
*: <code>sudo chown -R "${USER}":www <Laravelのプロジェクトディレクトリ></code>
*: <code>sudo chown -R "$USER":www <Laravelのプロジェクトディレクトリ></code>
*: <br>
*: <br>
*: LaravelのプロジェクトディレクトリにあるLaravelのプロジェクトディレクトリのパーミッションを変更する。
*: LaravelのプロジェクトディレクトリにあるLaravelのプロジェクトディレクトリのパーミッションを変更する。

2023年12月9日 (土) 16:20時点における版

概要

Composerは、依存性管理ツールである。
依存性管理とは、任意のライブラリは、特定のライブラリが存在しないと動作しないということを管理する。

したがって、Composerを使用してLaravelをインストールすることで、Laravelが正常に動作する環境になる。

ここでは、ComposerとLaravelのインストール手順について記載する。


前提条件

インストール - PHPの記事にアクセスして、PHPおよびPHPモジュールをインストールする。

php.iniファイル等に設定したXdebugの設定をコメントアウトする。


Composerのインストール

以下に、Composerの公式Webサイトを示す。
https://getcomposer.org/download

Composerのインストーラをダウンロードする。

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"


インストーラのSHA-384を確認する。

php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') \
{ echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"


Composerをインストールする。
composer.pharファイルが生成される。

php composer-setup.php --install-dir=<Composerのインストールディレクトリ>


composer.pharファイルのシンボリックリンクを作成する。

ln -s composer.phar composer


.profileファイル等に、Composerのインストールディレクトリを環境変数PATHに追加する。

vi ~/.profile


~/.profileファイル

export PATH="/<Composerのインストールディレクトリ>:$PATH"


また、Composerをアンインストールする場合は、以下のコマンドを実行する。

php -r "unlink('composer-setup.php');"



Laravelのインストール

Composerを使用して、Laravelをインストールする。

まず、Laravelを使用したプロジェクトを作成するため、以下のコマンドを実行する。

mkdir ~/htdocs
cd ~/htdocs

composer create-project laravel/laravel --prefer-dist <Laravelを使用するプロジェクト名>

# Laravelのバージョンを指定する場合
composer create-project laravel/laravel --prefer-dist <Laravelを使用するプロジェクト名> "X.Y"

# 例1: メジャーバージョンのみを指定する場合 (マイナーバージョンは最新のものがダウンロードされる)
composer create-project laravel/laravel --prefer-dist <Laravelを使用するプロジェクト名> "8.*"

# 例2: メジャーバージョンとマイナーバージョンを指定する場合
composer create-project laravel/laravel --prefer-dist <Laravelを使用するプロジェクト名> "8.6"



仮想ホストを構築しない場合(非推奨)

作成したLaravelのプロジェクトディレクトリに移動する。

cd <Laravelを使用するプロジェクトディレクトリ>


プロジェクトのトップディレクトリで、以下のコマンドを実行する。

php -S localhost:8000 -t public



仮想ホストを構築する場合 (推奨)

外部のクライアントPCからLaravelのプロジェクトに接続する場合、仮想ホストを構築すべきである。

SUSEの場合、以下に示すような仮想ホスト向けの設定ファイルを作成する。

  • Apache2
    /etc/apache2/vhosts.d/
  • NginX
    /etc/nginx/vhosts.d/


ここでは、以下に示すような仮想ホストを設定する。

  • 仮想ホスト名
    laravel01
  • ドキュメントルート
    ~/htdocs/laravel01/public/


※注意
Laravelのプロジェクトディレクトリにあるpublicディレクトリがドキュメントルートであるため、このディレクトリをアクセスするように設定する。

# NginXの場合
sudo vi /etc/nginx/vhosts.d/vhost-laravel01.conf

# Apache2の場合
sudo vi /etc/apache2/vhosts.d/vhost-laravel01.conf


 # /etc/nginx/vhosts.d/vhost-laravel01.confファイル
 
 # 仮想ホスト laravel01の設定
 server {
    listen       80;
    server_name  laravel01;
 
    access_log  log/laravel01.access.log main;
 
    error_page  404  /404.html;
    location = /404.html {
        root   /home/<ユーザ名>/htdocs/laravel01/public;
    }
 
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /home/<ユーザ名>/htdocs/laravel01/public;
    }
 
    location / {
        root   /home/<ユーザ名>/htdocs/laravel01/public;
        index  index.php index.html index.htm;
    }
 
    # このセクションは、NginxからPHP-FPMの通信はUnixドメインソケット通信でlistenしているFastCGIサーバにPHPスクリプトを渡すために使用される
    location ~ \.php$
    {
        root           /home/<ユーザ名>/htdocs/laravel01/public;
 
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        #fastcgi_pass   127.0.0.1:9000;
 
        fastcgi_index  index.php;
        include        fastcgi_params;
 
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 
        # proxy_pass   http://127.0.0.1;
    }
 
    location /mp3 {
        # sendfile関数の使用の有効化 / 無効化
        sendfile  on;
 
        # 1回のsendfile関数で転送できるデータ量を制限する (大きなサイズのファイルを転送する場合、接続が長時間ブロックされる可能性があるため)
        # この制限が無い場合、1つの高速な接続がワーカープロセスを停止させる可能性がある
        sendfile_max_chunk  1m;  # Limits chunks to 1[MB]
    }
 }


 # /etc/apache2/vhosts.d/vhost-laravel01.confファイル
 
 # VirtualHost template
 # Note: to use the template, rename it to /etc/apache2/vhost.d/yourvhost.conf. 
 # Files must have the .conf suffix to be loaded.
 #
 # See /usr/share/doc/packages/apache2/README.QUICKSTART for further hints 
 # about virtual hosts.
 #
 # Almost any Apache directive may go into a VirtualHost container.
 # The first VirtualHost section is used for requests without a known
 # server name.
 #
 <VirtualHost *:80>
    ServerAdmin webmaster@laravel01
    ServerName laravel01
 
    # DocumentRoot: The directory out of which you will serve your
    # documents. By default, all requests are taken from this directory, but
    # symbolic links and aliases may be used to point to other locations.
    DocumentRoot /home/<ユーザ名>/htdocs/laravel01/public
 
    # if not specified, the global error log is used
    ErrorLog /home/<ユーザ名>/htdocs/log/laravel01-error_log
    CustomLog /home/<ユーザ名>/htdocs/log/laravel01-access_log combined
 
    # don't loose time with IP address lookups
    HostnameLookups Off
 
    # needed for named virtual hosts
    UseCanonicalName Off
 
    # configures the footer on server-generated documents
    ServerSignature On
 
    # Optionally, include *.conf files from /etc/apache2/conf.d/
    #
    # For example, to allow execution of PHP scripts:
    #
    # Include /etc/apache2/conf.d/php5.conf
    #
    # or, to include all configuration snippets added by packages:
    # Include /etc/apache2/conf.d/*.conf
 
    # ScriptAlias: This controls which directories contain server scripts.
    # ScriptAliases are essentially the same as Aliases, except that
    # documents in the realname directory are treated as applications and
    # run by the server when requested rather than as documents sent to the client.
    # The same rules about trailing "/" apply to ScriptAlias directives as to
    # Alias.
    #
    ScriptAlias /cgi-bin/ "/home/<ユーザ名>/htdocs/cgi-bin/"
 
    # "/srv/www/cgi-bin" should be changed to whatever your ScriptAliased
    # CGI directory exists, if you have one, and where ScriptAlias points to.
    #
    <Directory "/home/<ユーザ名>/htdocs/cgi-bin">
        AllowOverride None
        Options +ExecCGI -Includes
        AddHandler cgi-script .cgi .pl .py .sh
        <IfModule !mod_access_compat.c>
            Require all granted
        </IfModule>
        <IfModule mod_access_compat.c>
            Order allow,deny
            Allow from all
        </IfModule>
    </Directory>
 
    # UserDir: The name of the directory that is appended onto a user's home
    # directory if a ~user request is received.
    #
    # To disable it, simply remove userdir from the list of modules in APACHE_MODULES
    # in /etc/sysconfig/apache2.
    #
    <IfModule mod_userdir.c>
        # Note that the name of the user directory ("public_html") cannot simply be
        # changed here, since it is a compile time setting. The apache package
        # would have to be rebuilt. You could work around by deleting
        # /usr/sbin/suexec, but then all scripts from the directories would be
        # executed with the UID of the webserver.
        UserDir public_html
        # The actual configuration of the directory is in
        # /etc/apache2/mod_userdir.conf.
        Include /etc/apache2/mod_userdir.conf
        # You can, however, change the ~ if you find it awkward, by mapping e.g.
        # http://www.example.com/users/karl-heinz/ --> /home/karl-heinz/public_html/
        #AliasMatch ^/users/([a-zA-Z0-9-_.]*)/?(.*) /home/$1/public_html/$2
    </IfModule>
 
    #
    # This should be changed to whatever you set DocumentRoot to.
    #
    <Directory "/home/<ユーザ名>/htdocs/laravel01/public">
        #
        # Possible values for the Options directive are "None", "All",
        # or any combination of:
        #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
        #
        # Note that "MultiViews" must be named *explicitly* --- "Options All"
        # doesn't give it to you.
        #
        # The Options directive is both complicated and important.  Please see
        # http://httpd.apache.org/docs/2.4/mod/core.html#options
        # for more information.
        #
        Options Indexes FollowSymLinks
 
        #
        # AllowOverride controls what directives may be placed in .htaccess files.
        # It can be "All", "None", or any combination of the keywords:
        #   Options FileInfo AuthConfig Limit
        #
        AllowOverride None
 
        #
        # Controls who can get stuff from this server.
        #
        <IfModule !mod_access_compat.c>
            Require all granted
        </IfModule>
        <IfModule mod_access_compat.c>
            Order allow,deny
            Allow from all
        </IfModule>
    </Directory>
 </VirtualHost>


次に、サーバ側のPCの/etc/hostsファイルにおいて、以下の設定を追記する。

sudo vi /etc/hosts


# サーバ側の/etc/hostsファイル

127.0.0.1     laravel01


次に、外部のクライアントPCの/etc/hostsファイルにおいて、以下の設定を追記する。

sudo vi /etc/hosts


# 外部のクライアントPC側の/etc/hostsファイル

<サーバのIPアドレス>     laravel01


最後に、Laravelのプロジェクトディレクトリにあるstorageディレクトリにおいて、グループ名またはパーミッションを変更する。

※注意
特に、パッケージ管理システムからPHPおよびWebサーバをインストールした場合、Laravelのプロジェクトディレクトリ下のファイルおよびディレクトリにおいて、
ユーザ名およびグループ名をNginX / Apache2のものに変更が必要な場合がある。
SUSEの場合、NginX / Apache2のユーザ名およびグループ名は、それぞれwwwrunwwwである。

変更方法は、以下に示す2種類ある。

  • 方法 1 (推奨)
    まず、ユーザをwwwグループに追加する。
    sudo usermod -aG www "$USER"

    次に、Laravelのプロジェクトディレクトリにあるstorageディレクトリのグループ名を変更する。
    cd <Laravelのプロジェクト>
    sudo chown -R "$USER":www storage bootstrap/cache

    ユーザとWebサーバの両方に対して、パーミッションを変更する。
    この設定は、不要の可能性がある。(要調査)
    find <Laravelのプロジェクトディレクトリ> -type f -exec chmod 664 {} \;
    find <Laravelのプロジェクトディレクトリ> -type d -exec chmod 775 {} \;

    Laravelのプロジェクトディレクトリにあるstorageディレクトリのパーミッションを変更する。
    sudo chmod -R ug+rwx storage bootstrap/cache

  • 方法 2
    まず、ユーザをwwwグループに追加する。
    sudo usermod -aG www "$USER"

    次に、Laravelのプロジェクトディレクトリにあるstorageディレクトリのグループ名を変更する。
    sudo chown -R "$USER":www <Laravelのプロジェクトディレクトリ>

    LaravelのプロジェクトディレクトリにあるLaravelのプロジェクトディレクトリのパーミッションを変更する。
    sudo chmod -R 775 <Laravelのプロジェクトディレクトリ>



動作確認

Webブラウザから、設定したルートドキュメントにアクセスする。
Laravelが正常に動作している場合、ページの中央にLaravelのロゴが表示されるデモページが表示される。

仮想ホストを構築していない場合

Webブラウザから、以下のURLを入力する。
http://localhost:8000

仮想ホストを構築している場合

Webブラウザから、以下のURLを入力する。
http://仮想ホスト名

デモページが表示されない場合、以下の点を確認する。

  • publicディレクトリにある.htaccessファイルのOptions -MultiViewsを、Options -MultiViews +FollowSymLinksに変更してみる。

  • 仮想ホストの設定で、ドキュメントルートがLaravelのプロジェクトのpublicディレクトリかどうか確認する。

  • 仮想ホストの設定で、publicディレクトリへのDirectoryディレクティブで、-MultiViewsを許可しているかどうか確認する。
    (学習目的であれば、AllowOverride allを指定すると、動作させやすい)

  • <Laravelのプロジェクトディレクトリ>/storageディレクトリのパーミッションが正しく設定されているかどうか確認する。

  • MCrypt PHP拡張モジュールは動作可能かどうか確認する。