In this article, a new graduate inexperienced engineer will explain how to build an environment for Laravel. There are some points that cannot be reached, but thank you. Do it without using git. It also explains configuration files such as Dockerfile and docker-compose.yml. It is for people who want to understand the settings and build an environment. Also, for Laravel beginners! ..
Operating environment Mac(Catallina 10.15.7) Docker 19.03.13 docker-compose 1.27.4
Please install docker and docker-compose in advance. Reference article Installing docker and docker-compose (mac version)
├── Docker/
│ ├── App/
│ │ ├ 000-default.conf
│ │ ├ Dockerfile
│ │ └ php.ini
│ └── DB/
│ ├ my.cnf
│ └ volume/
│
├── src/
└── docker-compose.yml
I have an article here about the tree command to do this kind of display. Easy display of directory structure! Tree command (Mac) Introducing frequently used options
000-default.conf This is the Apache setting. Please describe the following contents.
000-default.conf
<VirtualHost *:80>
ServerAdmin laravel@localhost
DocumentRoot /var/www/html/laravelapp/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/html/laravelapp/public>
AllowOverride All
</Directory>
</VirtualHost>
1st line: It stipulates that the virtual host is set for port 80.
2nd line: The address of the server. If the request is sent at the address laravel @ localhost, it means to use the virtual host just mentioned. This time it's local, so it's appropriate.
3rd line: The directory path of the error log is specified. *
4th line: The directory path of the access log is specified. *
Lines 5-7: All directives with the context .htaccess
in the container to be created are available.
/ etc / apache2 / envvars
in the container after it is created.Reference article Name-based virtual host How to publish sites with multiple domains on one Apache server Note: Check the Apache configuration file (http.conf) (1)
Dockerfile It will be the setting of the php (laravel) container. Please describe the following contents.
Dockerfile
FROM php:7.4-apache
ADD php.ini /usr/local/etc/php/
ADD 000-default.conf /etc/apache2/sites-enabled/
RUN cd /usr/bin && curl -s http://getcomposer.org/installer | php && ln -s /usr/bin/composer.phar /usr/bin/composer
RUN apt-get update \
&& apt-get install -y \
git \
zip \
unzip \
vim \
libpng-dev \
libpq-dev \
&& docker-php-ext-install pdo_mysql
RUN mv /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled
RUN /bin/sh -c a2enmod rewrite
7.4-apache
I'm pulling an image called php: 7.4-apache from Docker Hub.
`ADD php.ini /usr/local/etc/php/
ADD 000-default.conf /etc/apache2/sites-enabled/`
I'm copying php.ini, which I'll explain later, and 000-default.conf, which I'll explain above, to the specified directory in the container that creates it.
`RUN cd /usr/bin && curl -s http://getcomposer.org/installer | php && ln -s /usr/bin/composer.phar /usr/bin/composer`
Inside the container, I have installed the composer required to install laravel.
`RUN apt-get update \
&& apt-get install -y \
git \
zip \
unzip \
vim \
libpng-dev \
libpq-dev \
&& docker-php-ext-install pdo_mysql`
I'm installing middleware inside a container. You can use git, zip, unzip, vim, libpng-dev, libpq-dev in the container.
`RUN mv /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled
RUN /bin/sh -c a2enmod rewrite`
Inside the container, I'm running the mv command to move rewrite.load to the specified directory.
After that, I'm running a command that enables rewrite.load. Please see the second reference material for details.
Reference material
[Understand the Apache directory](https://kana-linux.info/linux/apache%E3%81%AE%E3%83%87%E3%82%A3%E3%83%AC%E3%82 % AF% E3% 83% 88% E3% 83% AA% E3% 82% 92% E7% 90% 86% E8% A7% A3% E3% 81% 97% E3% 82% 88% E3% 81% 86 )
[Enable Apache modules on Ubuntu](https://www.t3a.jp/blog/infrastructure/apache2-mod-add/)
[Building a Laravel + Apache + MySQL development environment with Docker](https://note.com/pocke_techblog/n/n8af813848fa0)
php.ini
It will be the setting of php.
Please copy and paste the following.
#### **`php.ini`**
```ruby
[Date]
date.timezone = "Asia/Tokyo"
[mbstring]
default_charset = "UTF-8"
mbstring.language = "Japanese"
date.timezone = "Asia/Tokyo"
You are literally setting the time zone.
default_charset
Specify the default character code to be output in the HTTP header.
mbstring.language = "Japanese"
Set the default language.
Reference material List of php.ini directives Items that can be set in php.ini settings Edit php.ini
my.cnf This is the MySQL server configuration file. Detailed settings are described in docker-compose.yml described later, so they are minimized.
my.cnf
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
[client]
default-character-set=utf8mb4
[mysqld] character-set-server=utf8mb4 collation-server=utf8mb4_unicode_ci
The character code is set to utf8mb4.
[client] default-character-set=utf8mb4
Client character code settings
Reference article Current notes on setting charset to utf8mb4 in MySQL (InnoDB) Set character code Basic MySQL ~ Part 2 ~ my.cnf (configuration file)
docker-compose.yml docker-compose configuration file (settings for each container)
docker-compose.yml
# docker-Defines the version used by compose.
version: '3'
#Each element that runs the application. In services, app, db and phpmyadmin are set as child elements.
services:
#The container that runs laravel is listed below from here.
app:
#Decide the name of the container here.
container_name: laravel_app
#It is a setting of which port to connect. It connects port 8080 of the host and port 80 of the container. Virtual host on port 80 is 000-default.It has already been set in conf.
ports:
- "8000:80"
#Specifies the location of the Dockerfile settings. Build based on the contents of the Dockerfile.
build: ./Docker/App
#The location where the container and host directories are synchronized is specified here. Here is the source of laravel.
volumes:
- ./src:/var/www/html
#The container that runs MySQL is set up from here.
db:
#MySQL 5 from Docker Hub.It is a setting that pulls the official image of 7.
image: mysql:5.7
#The container name is specified.
container_name: laravel_db
#You are specifying an environment variable inside a MySQL container. Please set any one.
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: laraveldb
MYSQL_USER: dbuser
MYSQL_PASSWORD: dbpass
TZ: 'Asia/Tokyo'
#This is the command at startup.
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
#The directory is synchronized. Please note that if you do not write this, you will lose data from the database when you close the container. For more information, you may want to check docker persistence.
volumes:
- ./Docker/DB/volume:/var/lib/mysql
- ./Docker/DB/my.cnf:/etc/mysql/conf.d/my.cnf
#It is a setting to connect to port 13306 of the host and port 3306 in the container.
ports:
- 13306:3306
#The settings of phpmyadmin are described in the writing.
phpmyadmin:
#The container name is decided.
container_name: phpmyadmin
#It is a setting that pulls the image of phpmyadmin from Docker Hub.
image: phpmyadmin/phpmyadmin
#Environment variable settings for phpmyadmin. Please refer to and describe the MySQL settings. PMA_The db of services is specified for HOSTS.
environment:
- PMA_ARBITRARY=1
- PMA_HOSTS=db
- PMA_USER=root
- PMA_PASSWORD=root
ports:
- 3000:80
Reference article I explained how to write docker-compose.yml Build a MySQL trial environment with Docker (also phpMyAdmin) How to build LAMP + phpMyAdmin environment with PHP 7.4 and MariaDB 10.5 with Docker
Build. (Go in the directory where the docker-compose.yml file is located)
[Mac]$ docker-compose build
:
:
Successfully built c1c2970ecb4b
Successfully tagged docker_laravel_app:latest
If it finishes without any problem, start the container with the following command.
[Mac]$ docker-compose up -d
-d is attached when launching a container in the background. If you don't need it, remove it.
Check if the container is up.
[Mac]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2164a5c2371 docker_laravel_app "docker-php-entrypoi…" 3 minutes ago Up 2 minutes 0.0.0.0:8000->80/tcp laravel_app
5d80cab0e266 mysql:5.7 "docker-entrypoint.s…" 3 minutes ago Up 2 minutes 33060/tcp, 0.0.0.0:13306->3306/tcp laravel_db
ad4786c13249 phpmyadmin/phpmyadmin "/docker-entrypoint.…" 3 minutes ago Up 2 minutes 0.0.0.0:3000->80/tcp phpmyadmin
There is no problem if the container information is output.
Go inside the container and install the laravel project.
Enter the container with docker exec.
[Mac]$ docker exec -it laravel_app bash
root@e2164a5c2371:/var/www/html# //If it is displayed as shown on the left, it is in the container.
Install laravel using composer. Since composer is already installed as set in the Dockerfile, you can install it with the following command. This time I will install 6 series laravel. [Technical book] I have (https://www.amazon.co.jp/PHP-E3-83-95-E3-83-AC-E3-83-BC-E3-83-A0-E3- 83-AF-E3-83-BC-E3-82-AFLaravel-E5-85-A5-E9-96-80-E7-AC / dp / 4798060992 / ref = dp_ob_image_bk) is using 6 series of laravel Install with 6 series. (It will take some time.)
[In the container]# composer create-project "laravel/laravel=~6.0" --prefer-dist laravelapp
To install the latest version, execute the following command
[In the container]# composer create-project laravel/laravel --prefer-dist laravelapp
Make sure the laravelapp directory is installed in the / var / www / html
inside the container and the src directory on the host side.
When developing, there is no problem if you edit the source code in the src / laravelapp directory on the host OS side.
Since port 8000 of the host OS is connected to port 80 in the container as set in docker-compose.yml http://localhost:8000 If you access and the TOP page of laravel is output, there is no problem.
Containers created with Docker-compose are resolved by container name. (You can communicate by container name.)
To connect to a MySQL container from a laravel container, you can access MySQL by accessing port 3306 with the host name (container name) laravel_db.
While referring to the one set in docker-compose.yml, there is a MySQL setting part in the .env file in the src / laravel
directory, so describe it as follows. If you have changed the settings in docker-compose.yml, please adjust accordingly.
DB_CONNECTION=mysql
DB_HOST=laravel_db
DB_PORT=3306
DB_DATABASE=laraveldb
DB_USERNAME=dbuser
DB_PASSWORD=dbpass
I will actually connect.
[Mac]$ docker exec -it laravel_app bash
[In the container]# cd laravel_app
[In the container]# php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table (0.2 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table (0.18 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated: 2019_08_19_000000_create_failed_jobs_table (0.08 seconds)
If it is output as above, there is no problem.
phpmyadmin is a tool that allows you to easily view and manage the contents of the DB on your browser. Please see the reference materials for details. Useful for SQL management! How to use phpMyAdmin [for beginners] How to use phpMyAdmin
Access the URL below and check that phpmyadmin is displayed. (It has already been set to port 3000 in docker-compose.yml) http://localhost:3000/
Enter the MySQL container and try entering the MySQL server. Try executing the commands in the following order.
[Mac]$ docker exec -it laravel_db bash
[In the container]root@5d80cab0e266:/# mysql -uroot -p
Enter password:(docker-compose.Root user password determined in yml)
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.7.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| laraveldb |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.02 sec)
mysql> use laraveldb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+---------------------+
| Tables_in_laraveldb |
+---------------------+
| failed_jobs |
| migrations |
| password_resets |
| users |
+---------------------+
4 rows in set (0.00 sec)
mysql>
mysql -uroot -p
Specify the user with -u. This time, enter as the root user.
-p is an option to enter the password.
show databases;
Outputs all databases.
use laraveldb;
The command is to use the laraveldb database.
show tables;
It is a command to output all the tables of the database (laraveldb) in use.
This completes the environment construction of laravel, and all we have to do is proceed with the development work! !! Thank you for reading to the end.
Reference article summary Name-based virtual host How to publish sites with multiple domains on one Apache server Note: Check the Apache configuration file (http.conf) (1) [Understand the Apache directory](https://kana-linux.info/linux/apache%E3%81%AE%E3%83%87%E3%82%A3%E3%83%AC%E3%82 % AF% E3% 83% 88% E3% 83% AA% E3% 82% 92% E7% 90% 86% E8% A7% A3% E3% 81% 97% E3% 82% 88% E3% 81% 86 ) Enable Apache modules on Ubuntu Building a Laravel + Apache + MySQL development environment with Docker ↑ I especially referred to it. List of php.ini directives Items that can be set in php.ini settings Edit php.ini Current notes on setting charset to utf8mb4 in MySQL (InnoDB) Set character code Basic MySQL ~ Part 2 ~ my.cnf (configuration file) I explained how to write docker-compose.yml Build a MySQL trial environment with Docker (also phpMyAdmin) How to build LAMP + phpMyAdmin environment with PHP 7.4 and MariaDB 10.5 with Docker Useful for SQL management! How to use phpMyAdmin [for beginners] How to use phpMyAdmin
Recommended Posts