Introducing the development environment for running Laravel using Laradock. Since I am learning by myself through trial and error, there may be some mistakes, but I would appreciate it if you could point out in that case.
-Install Docker-for-mac ・ Start Docker
We will build a development environment to run Laravel using Laradock.
The official website has a wonderful environment for running Laravel (PHP) projects on Docker. https://laradock.io/
By using Laradock, you can easily build a web server or database on Docker.
-You can easily switch PHP versions (7.3 / 7.2 / 7.1 / 5.6) -You can choose your favorite database engine (MySQL / Postgres / MariaDB ...) ・ Combination of original software such as Memcached, HHVM, Beanstalkd is possible -All software such as PHP-FPM, NGINX, PHP-CLI work in separate containers · All Docker images are built from officially based images You can use Laradock for each project or a single Laradock for all projects.
First, open a terminal and match the path to the location where you want to create the directory. You can find the location of your current file path with pwd.
//It will display the location of the current directory with an absolute path.
$ pwd
//Create a new directory.
$mkdir directory name
//Change to the directory created by the cd command.
$cd Created directory name
//Check the absolute path of the created directory
$ pwd
After creating the directory, install laradock there.
Execute the following command.
//I am doing that I copy the laradock file from GitHub using a mechanism called git
$ git clone https://github.com/Laradock/laradock.git -b v9.6
You should see a few lines of message saying Cloning into'laradock' ...
When you are done, check if the directory called ls command is created.
$ ls
laradock
Next, create an .env file, which is one of the files related to the laradock settings. laradock prepares an env-example file as a template for the .env file, so I will create it based on this.
Please enter the following:
$ cd laradock
$ cp env-example .env
cp is a command to copy files.
cp Copy source file name Enter the copy destination file name to use.
You have now created an .env file based on your env-example file.
Next, use an editor etc. to edit the .env file.
.
└── Created directory
└── laradock
└── .env
After opening the .env file, find each of the following parts and change them as described.
//Change before
APP_CODE_PATH_HOST=../
DATA_PATH_HOST=~/.laradock/data
COMPOSE_PROJECT_NAME=laradock
//After change
//A directory to sync on the Laradock web server. Laradock itself does not have to be on docker separately, so only the directory of the laravel project is synchronized. The directory name where laravel will be mounted.
APP_CODE_PATH_HOST=../laravel
//A directory on your local PC that stores docker storage, etc. By default, databases etc. are saved in your home directory. Change it under the project directory
DATA_PATH_HOST=../data
//If you want to create a new laradock environment for each project, change the directory name to the one you created.
COMPOSE_PROJECT_NAME=Created directory name
Next, use Docker to start the development environment.
The unit of each service running on Docker is called a container.
Here, start the following four containers.
・ Workspace ・ Php-fpm ・ Nginx ・ Postgres
php-What is fpm
PHP FastCGI(The process started at the time of the first request is retained in the memory, and the process retained in the memory is executed for the next request.
It has the following functions.
Loose(graceful)Stop/Advanced process management including launch functionality
Different uid/gid/chroot/Starting a worker in environment, listening on different ports, different php.Use of ini(safe_Alternative to mode)
Log output to standard output and standard error output
Emergency reboot if opcode cache is corrupted
Fast upload support
"slowlog" -Recording scripts with very long execution times(It records the PHP backtrace as well as the script name. To get a backtrace, execute the remote process using ptrace or an equivalent mechanism_Read data)
fastcgi_finish_request() -Something time-consuming process(Video conversion, statistical information processing, etc.)A special function to terminate the request and output all the data while continuing
dynamic/Invoking a static child process
Basic SAPI operating status(Apache mod_Equivalent to status)
php.ini-based config file
What is CGI
PHP on the web server(Languages that generate dynamic content)It is a mechanism to operate.
CGI spawns and destroys processes whenever a user requests them. If there is a large number of requests, processes will be created and destroyed accordingly, which will lead to poor performance.
What is FastCGI
The process started at the time of the first request is retained in the memory, and the process retained in the memory is executed for the next request. It solves CGI problems, speeds up programs and reduces server load.
There is postgres in the container to start, which is a database system called PostgreSQL.
This time, PostgreSQL will use version 11.6.
Therefore, modify the first line of the Dockerfile in the laradock / postgres directory as follows.
laradock/postgres/Dockerfile.
1 FROM postgres:11.6
After editing, go to "Created Directory / laradock Directory" in the terminal.
$ cd ~/Created directory/laradock
Then enter the following command.
$ docker-compose up -d workspace php-fpm nginx postgres
The meaning of the command is as follows.
docker-compose is a command to use a function called Docker Compose that handles multiple containers at the same time. up is the command used to start the container with Docker Compose -d is an option to return to terminal operation after starting the container
When I run the command, I get a lot of messages.
At the first time, the software itself (Docker image) of each container is downloaded, so it takes about several minutes to start the container.
Finally, if you see the following message, the container has started successfully.
Creating Created directory_docker-in-docker_1 ... done
Creating Created directory_workspace_1 ... done
Creating Created directory_php-fpm_1 ... done
Creating Created directory_nginx_1 ... done
Creating Created directory_porstgres_1 ... done
If you don't see these 5 messages, try docker-compose up -d workspace php-fpm nginx postgres again.
Once the container has started, launch your browser and type localhost in the address bar.
If you get 404 Not Found, it's OK.
If you want to stop the container, enter the following command.
$ docker-compose stop
If you want to restart the container, enter the following command.
docker-compose up -d workspace php-fpm nginx postgres
Enter the following command in the created directory / laradock directory.
$ docker-compose exec workspace composer create-project --prefer-dist laravel/laravel . "6.8.*"
To execute the command starting with docker-compose, you need to move to the created directory / laradock directory and then execute it.
The installation will take a few minutes. When the message is displayed, many files should be created under the created directory / laravel directory.
If you wait for a while, the installation will not start,
[InvalidArgumentException]
Could not find package laravel/laravel with version 6.8.*.
If the message is displayed, change the "6.8. *" Part of the command to "6. *. *" And install.
After installing Laravel, access localhost with your browser. It is OK if the following screen is displayed.
By default, Laravel's time setting is UTC (Coordinated Universal Time), which is 9 hours later than Japan time.
Change this to Japan time.
Please change the timezone of / created directory / laravel / config / app.php to'Asia / Tokyo', as follows.
.
└── Created directory
└── laravel
└── config
└── app.php
laravel/config/app.php.
//Abbreviation
/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/
'timezone' => 'Asia/Tokyo', //--Change this line
//Abbreviation
This completes the installation of Laravel.
Recommended Posts