Since I received a PHP project, I thought I would do it with MAMP, but suddenly it became "Do I need MAMP ...?: Thinking:".
Because I didn't use DB this time, and I thought that Mac has PHP and Apache.
Terminal
$ php -v
PHP 7.3.11 (cli) (built: Jun 5 2020 23:50:40) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.11, Copyright (c) 1998-2018 Zend Technologies
$ httpd -v
Server version: Apache/2.4.41 (Unix)
Server built: Jun 5 2020 23:42:06
Yeah yeah it's in.
However, since the PHP version of the production environment where the source works was 5 series, it was necessary to switch the PHP version.
I came up with three ways to prepare the development environment.
There aren't that many PHP projects, but I don't like to mess around with the local environment, and I'm used to being able to start Apache with a command poppy like Linux, so I decided to adopt the third Docker this time. did.
environment | Version etc. |
---|---|
MacBook Pro | 2019 model |
OS | macOS Catalina |
Docker Engine | v19.03.13 |
PHP | 5.4 |
Select ** Download for Mac ** on the Official Site (https://www.docker.com/get-started) to download Docker Desktop.
Run Docker.dmg and install it.
Run it when the installation is complete. After execution, you may be asked for a password, but enter it as appropriate.
When executed, a whale will appear on the right side of the menu bar above. (Sorry for being tiny) ↓
Try typing a command in the terminal to check.
$ docker version
Client: Docker Engine - Community
Cloud integration: 1.0.2
Version: 19.03.13
API version: 1.40
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 16:58:31 2020
OS/Arch: darwin/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.13
API version: 1.40 (minimum version 1.12)
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:07:04 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.3.7
GitCommit: 8fba4e9a7d01810a393d5d25a3621dc101981175
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
Write a Dockerfile to create an image.
Create an appropriate directory and create a Dockerfile.
$ mkdir php5_apache
$ cd php5_apache
$ vim Dockerfile
Let's borrow the Official Docker Image.
You can build a container that also includes Apache by writing php: <version> -apache
.
The version used this time is * 5.4 *, so describe it as it is.
Dockerfile
FROM php:5.4-apache
docker build <Dockerfile path> -t <image name>: <tag name>
Build with.
Here, specify * php5_apache * as the image name and * 1.0 * as the version of the tag.
-t
: Option to specify name and tag
$ docker build ./ -t php5_apache:1.0
Let's check if it was built.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php5_apache 1.0 7246b9f23253 5 years ago 470MB
By the way, you can also check it from the dashboard of Docker Desktop.
Let's start it immediately.
I already have the source I want to move, so I will mount it.
The command is as follows.
docker run -d -p <host side port>: <container side port> -v <host side path>: <container side path> php5_apache: 1.0
The explanation of the option used this time is as follows.
---d
: Start in detached mode (background execution)
---p
: Expose the container port to the host side
---v
: Mount the host directory on the container
$ docker run -d -p 80:80 -v <Working directory>:/var/www/html php5_apache:1.0
Let's check if it started successfully.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
469f65bd6a4f php5_apache:1.0 "apache2-foreground" About a minute ago Up About a minute 0.0.0.0:80->80/tcp recursing_hodgkin
Since it opened on port 80, enter localhost
in the address bar of the browser without specifying the port to open it.
It is OK if PHP etc. are output safely.
Once you have created a container, you can start it with docker start <container ID or container name>
and stop it with docker stop <container ID or container name>
.
This time I will try using the container name. It was named * recursing_hodgkin *, so let's stop and start it.
$ docker stop recursing_hodgkin
$ docker start recursing_hodgkin
You can log in as root below. By the way, the OS is Debian.
$ docker exec -it recursing_hodgkin /bin/bash
When it comes out, it's the familiar exit
.
# exit
When I looked at the log, I found an error-like log.
$ docker logs recursing_hodgkin
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Thu Dec 10 09:58:54.402679 2020] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/5.4.45 configured -- resuming normal operations
[Thu Dec 10 09:58:54.402792 2020] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
The content is "The domain name has not been set. I'm in trouble: disappointed_relieved:". You kindly wrote the solution, "Set the ServerName directive."
Maybe it's okay if you don't set it, but it's unpleasant that the error remains, so I'll set it.
(I was just playing around with CentOS, so I ended up with Debian's Apache config file or something like that.)
First, log in to docker.
$ docker exec -it recursing_hodgkin /bin/bash
I'm used to vim, so I installed it. I couldn't install vim without apt update, so I have to do it. I thought I should have written it in the Dockerfile from the beginning ...
In the container
# apt update
# apt -y upgrade
# apt install -y vim
# vim /etc/apache2/conf-enabled/httpd.conf
It seems that /etc/apache2/conf-enabled/*.conf is read as a config file, so I will describe it in this.
httpd.conf
ServerName localhost:80
Restart Apache service.
# /etc/init.d/apache2 reload
[ ok ] Reloading web server: apache2.
When I play with CentOS, it restarts with systemctl restart httpd
, so I want to restart with service apache2 restart
, but when I execute this, the container also died, so hit the above command ..
** I didn't put the module. ** **
I heard such a voice from somewhere.
Put the module,
# docker-php-ext-install mbstring
Add to ini and enable it.
php.ini
extension=mbstring.so
The customary apache restart.
# /etc/init.d/apache2 reload
I'm not very familiar with Docker, so I left it as a memorandum.
There was a stumbling block when actually starting development, such as issuing an error log of php and writing the part written as a bonus in the Dockerfile, so I would like to write an article about it later.
Recommended Posts