Share a DNMP environment built by myself. I think it’s relatively simple. In addition, please give advice and continue to improve!
https://github.com/839891627/…
1、 Background
What docker is and why it is used will not be repeated here. Here to share with you the PHP development environment I built.
This warehouse tries to follow“Principle of simplification”。 Minimize customization and hand over customization to users.Give a man a fish and you feed him for a day. Teach him how to fish and you feed him for a lifetime., I hope to give you room to play by yourself through simple examples. After all, everyone’s needs are different. Do big and complete things, butBloated and unable to understand
2、 Highlights
Well, self righteousHighlights
- simple
- Auxiliary script
- Optimization: script acceleration
- Organization: build the grouping of scripts, etc
- you say…
3、 Function description
1. Description of directory structure
.
├── . Env # environment variable definition. Here you can define the required PHP / nginx / MSYQL versions and port numbers
Image - build script of dockerfile # PHP image. It mainly installs some extensions and nodejs and python required by the front end
├── config
│♪ nginx # nginx configuration folder. For the configuration file of the new project, just add nginx configuration here
│♪ PHP # here's PHP ini。 I adjusted the session save_ Handler = redis and session save_ path = " tcp://redis:6379 ". means redis (container) is used to store the session
│ └── redis. Conf # redis configuration file
Table of contents -- directory of data # persistent database
│ ├── composer
│ ├── mysql
│ └── redis
├── docker-compose.yml
Logs - logs # store nginx logs
│ └── nginx
└ - resources # pre prepared resources
├── Python-3.8.0.tgz
├── mcrypt-1.0.3.tgz
├── mongodb-1.6.0.tgz
├── node-v12.13.0-linux-x64.tar.xz
├── redis-5.1.1.tgz
├── sources.list
├── swoole-src-4.4.12.zip
└── xdebug-2.8.0.tgz
2. Integrated services
- mysql
- php7.2/7.3
- nginx
- redis
3、 Use
It is strongly recommended that docker-compose.com be used before use Familiar dockeryml content
- Clone this project into the peer directory of your other development projects
– laravel # for example, there is a laravel project here
– DNMP # so you need clone here for this project - Start / create container environment
Then switch to the DNMP directory and execute
docker-compose up -d
#If you use the alias of zshrc given below, it is DUP - D
- Add the corresponding nginx configuration file. reference resources
config/nginx/conf.d/laravel.conf
- If the nginx configuration is adjusted, click
dnmp
Execute under directory (restart nginx container)docker-compose restart nginx
(if alias is used, executedr nginx
) -
You can access the project
- Configure hosthosts, specify the domain name
127.0.0.1 laravel.test
- When MySQL / redis is used in the project, it needs to be configured asContainer name。 For example, in
laravel/env.php
in
REDIS_ Host = redis # fill in redis, and the container will resolve automatically at runtime. Because docker PS checks, it is known that the redis container is called "redis" # REDIS_ Host = 127.0.0.1 # instead of IP REDIS_PASSWORD=null REDIS_PORT=6379 DB_CONNECTION=mysql DB_ Host = MySQL # similarly, fill in MySQL #DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret
- [optional] in this way, you have to configure host for each project. It’s more troublesome. You can usednsmasqCan reach a directory once and for all. Refer to the appendix below
- Configure hosthosts, specify the domain name
4、 Other functions
- Switch PHP version
opendocker-compose.yml
withinphp73Part, thendocker-compose up -d php73
, nginx.conf
withinfastcgi_pass php73:9000;
that will do
- Install extensions
docker-php-ext-install -h
View extensions that can be installed-
docker-php-ext-enable -h
Enable extensionIf the required extension is not available, install it through the source package. There is no explanation here
-
implement
php、composer
Such script
Need to enter the containerdocker exec -it php72 bash
, and then switch to the corresponding project for executionphp72Is the name of the container, in docker compose In YML
container_name: php72
Defined in
If you use alias, you can directly run commands under the host project without entering the container
5、 Appendix
Auxiliary command
Please refer to.zshrc
、.fish
to configure. Copy to your corresponding configuration
Unified setting.testDomain name to local
>Under Mac. Please try it yourself
brew install dnsmasq
/usr/local/etc/dnsmasq.conf
Inside configurationaddress=/.test/127.0.0.1
-
Create a dns resolver:
sudo mkdir -v /etc/resolver sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/test'
Xdeug use
>Installed is 3 X version. The default port number is * * 9003**
>For phpstorm configuration, please refer to the video here: https://www.bilibili.com/video/BV1J64y1u7dw#reply2806426126 (the port number in the video is 9001, and now the project should be 9003)
> `config/php/php72.ini`
>In the configuration, I have already configured it and enabled debug and profile (if not needed, just delete it directly)
Get / post / cookie add parameter ‘Xdebug_ Trigger = any value ‘
Different solutions to a project curl accessing B project in container
1. Programme I
Currently passed inPHP container, modify / etc / hosts to solve the problem
Docker inspect - f '{{range. Networksettings. Networks} {. IPAddress} {{end}}' nginx # first obtain the nginx container IP in the host Docker exec - it PHP bash # enters PHP container Echo 'IP domain returned in the first step test' >> /etc/hosts
1. Programme II
See docker compose yml
extra_hosts:
- laravel. Test: 172.20.128.2 # this IP is the nginx container IP and has been set to fixed. In this way, the IP address will not change every time you restart