カテゴリー
未分類

Running laravel on laradock apache2

In the official laradock documentation, there is a lot of sample code to run nginx, but laradock also provides a mechanism to use apache.

Getting Started

First of all, if there is a service that is running, let’s bring it down, to prevent troublesome troubles.

docker-compose down

Create the following localhost.conf file in the laradock/apache2/sites folder on the host OS side.

laradock/apache2/sites/localhost.conf

<VirtualHost *:80>
  ServerName localhost
  DocumentRoot /var/www/public/
  Options Indexes FollowSymLinks

  <Directory "/var/www/public/">
    AllowOverride All
    <IfVersion < 2.4>
      Allow from all
    </IfVersion>
    <IfVersion >= 2.4>
      Require all granted
    </IfVersion>
  </Directory>

</VirtualHost>
This is what the image looks like.

Then record the localhost.conf file to your Docker image with the following command:

docker-compose build apache2

Now you are ready to go.

Start the container in the same way as for nginx, but specify the apache2 service instead of the nginx service

docker-compose up -d apache2 mysql

More details

There is a difference in the default settings of nginx and apache2 in laradock.

default nginx settings of laradock.

The default setting of nginx of laradock is to make the /var/www/public folder of the OS in the container as Document Root when accessed from http://localhost.

https://github.com/laradock/laradock/blob/master/nginx/sites/default.conf#L13

default apache2 settings of laradock .

On the other hand, in apache2 of laradock, when accessing with http://localhost, the /var/www folder of the OS in the container is set to be published.

If you modify this to expose the /var/www/public folder like nginx, you can develop laravel with the same usability as when using nginx.

/ laradock / apache2 / sites folder

A file called sample.conf.example is provided in the laradock/apache2/sites/ folder.

https://github.com/laradock/laradock/blob/master/apache2/sites/sample.conf.example

I copied this file, pasted as a new file called localhost.conf, and rewrote the following three directories.

  • ServerName
  • DocumentRoot
  • Directory

Note that it works even if the file name is not localhost.conf, but the end of the file name must end with .conf.

In laradock, if you save the * .conf file in the laradock/apache2/sites/ folder on the host OS side, it seems that it is set to be recognized by apache.

カテゴリー
未分類

laradock の apache2 で laravel を動作させる

laradock の 公式ドキュメントでは、nginxを起動するサンプルコードが多いですが、laradock には apache を使う仕組みも用意されています。

操作手順

面倒なトラブルを防ぐため、まずは起動しているサービスがあれば、 down させましょう

docker-compose down

ホストOS側の laradock/apache2/sites フォルダに、以下のlocalhost.confファイルを新しく作成します。

laradock/apache2/sites/localhost.conf

<VirtualHost *:80>
  ServerName localhost
  DocumentRoot /var/www/public/
  Options Indexes FollowSymLinks

  <Directory "/var/www/public/">
    AllowOverride All
    <IfVersion < 2.4>
      Allow from all
    </IfVersion>
    <IfVersion >= 2.4>
      Require all granted
    </IfVersion>
  </Directory>

</VirtualHost>
このようなイメージとなります。

次に、以下のコマンドで localhost.conf ファイルを Dockerイメージに記録します。

docker-compose build apache2

これで準備は完了です。

nginxの時と同じ要領でコンテナを起動しますが、nginx サービスではなく apache2 サービスを指定します

docker-compose up -d apache2 mysql

もっと詳しく

laradock では nginx と apache2 の初期設定に違いがあります。

laradock の nginx の初期設定

laradock の nginx の初期設定 には、http://localhost でアクセスした場合に、コンテナ内のOS の /var/www/public フォルダを公開させる記述があります。

https://github.com/laradock/laradock/blob/master/nginx/sites/default.conf#L13

laradock の apache2 の初期設定

一方、 laradock の apache2 では、http://localhost でアクセスした場合は、コンテナ内のOSの /var/www フォルダを公開する設定となっています。

これを、 nginx と同様に /var/www/public フォルダを公開するように修正すれば、 nginx を利用していた時と同じ使い勝手で laravel を開発できます。

/laradock/apache2/sites フォルダ

laradock/apache2/sites/ フォルダには sample.conf.example というファイルが用意されています。

https://github.com/laradock/laradock/blob/master/apache2/sites/sample.conf.example

このファイルをコピーして、新しく localhost.conf というファイルを追加し、以下の3つのディレクトリを書き換えました。

  • ServerName
  • DocumentRoot
  • Directory

なお、localhost.conf というファイル名でなくても動作はしますが、ファイル名の末尾は、.confで終わる必要があります。

laradock では、 ホスト OS側の laradock/apache2/sites/ フォルダの中に、*.conf ファイルを保存すると、apacheによって認識されるように設定されているようです。

カテゴリー
未分類

laradock で 、ERROR: Named volume “data/minio/data:/export:rw” is used in service “minio” but no declaration was found in the volumes section. というエラーが発生。

.env ファイルにDATA_PATH_HOST=dataと書いてしまっていました。

本当は、以下のように ./data と設定したかったのです。

# Choose storage path on your machine. For all storage systems
DATA_PATH_HOST=./data

それを、以下のように、dataの前の ./ を書き忘れてしまい、標題のようなエラーが発生しました。

# Choose storage path on your machine. For all storage systems
DATA_PATH_HOST=data

DATA_PATH_HOST=./data と書きかえたら、問題が解消されました。

事態が改善するかもしれません。

もっと詳しく

Laradockをインストールした直後は通常、 env-example ファイルを .env というファイル名でコピーします。

その .env ファイルには DATA_PATH_HOST という項目があり、以下のような記述になっています。

# Choose storage path on your machine. For all storage systems
DATA_PATH_HOST=~/.laradock/data

この設定のままだと、MySQLのデータなどが、laradockフォルダの外に保存されます。

それはそれで便利なケースもありますが、プロジェクトごとに laradock フォルダの中にdataフォルダという名前で保存されるように設定しようとしました。

その時に変更するのが、 DATA_PATH_HOST という項目です。

この設定は、.envとは別のファイル、 docker-compose.yaml ファイルの中で、コンテナが使用するボリュームとして使われています。

https://github.com/laradock/laradock/blob/master/docker-compose.yml#L427-L428

### Minio ################################################
    minio:
      build: ./minio
      volumes:
        - ${DATA_PATH_HOST}/minio/data:/export
        - ${DATA_PATH_HOST}/minio/config:/root/.minio

もし、DATA_PATH_HOST の値が ./data ではなく、ただの data だとすると、はこれらの設定は以下のように解釈されます。

### Minio ################################################
    minio:
      build: ./minio
      volumes:
        - data/minio/data:/export
        - data/minio/config:/root/.minio

先頭が/~... など、相対パスとして使う記号から始まるボリュームは、ホストOSのファイルパスだと解釈されます。(今回は、こちらが正解でした。)

しかし、ただ data と書いてしまうと、それはホストOSのファイルパスではなく、「ボリューム名」として解釈されます。

Dockerは、data/minio/data と名付けられたボリュームの設定が、docker-compose.yamlのどこかに記述されていると解釈して探しますが、実際には、そんなものは書かれておらず、今回のエラーとなったようです。

HTMLでの相対パスの書き方とは、考え方が違う

HTMLでの外部ファイルへのリンクでは、先頭の ./ は、あってもなくても同じように動作します。

<script src="script.js"></script>
<script src="./script.js"></script>

しかし、docker-compose.yml の コンテナに使用するボリュームの設定では ./ があればファイルパス、なければ「Named Volume」という、全く異なった解釈となるようでした。

Compose file version 3 reference

https://docs.docker.com/compose/compose-file/#short-syntax-3