Special instructions

Recently, I found a good PHP bucket development environment on GitHub, which is built using Docker. But look at the update dynamic, the author rarely maintained. I also joined the group and found that many developers using the repository encountered many problems. In order to make this warehouse play a real role, change fork a warehouse, guarantee the long time to update and solve the problems that everyone encountered.

Gitee Warehouse address: gitee.com/bruce_qiq/p…

GitHub repository address: github.com/bruceqiq/ph…

Directions for use

If you find something wrong with it, you can issue it. If your problem is more urgent, need to be quickly solved, you can add the following public number, after attention, click on the relevant resources menu, even get the support of the warehouse maintainer.

documentation

DNMP (Docker + Nginx + MySQL + php7/5 + Redis) is a full-featured LNMP one-click installation program.

It is better to read the catalogue in advance before use, so as to get started quickly, and to eliminate problems in time.

Features of DNMP project:

  1. 100%Open source
  2. 100%Comply with Docker standards
  3. Support for multiple versions of PHP coexistence, can be arbitrary switch (PHP5.4, PHP5.6, PHP7.1, PHP7.2, PHP7.3)
  4. Multiple domain names can be bound
  5. Supports HTTPS and HTTP/2
  6. The PHP source code, MySQL data, configuration file, and log file can be directly modified and viewed in Host
  7. Built-in full PHP extension installation command
  8. The default supportpdo_mysql,mysqli,mbstring,gd,curl,opcacheAnd other popular extensions can be flexibly configured based on the environment
  9. Common services can be selected with one click:
    • Multiple PHP versions: PHP5.4, PHP5.6, PHP7.1-7.3
    • Web services: Nginx, Openresty
    • Databases: MySQL5, MySQL8, Redis, memcached, MongoDB, and ElasticSearch
    • Message queue: RabbitMQ
    • Auxiliary tools: Kibana, Logstash, phpMyAdmin, phpRedisAdmin, AdminMongo
  10. Practical project application, ensure100%available
  11. All images are from the official Docker warehouse, safe and reliable
  12. Windows, Linux, and MacOs are available in one configuration
  13. Supports quick installation of extended commandsinstall-php-extensions apcu

directory

  • 1. Directory structure
  • 2. Quick use
  • 3. The PHP and extension
    • 3.1 Switching the VERSION of PHP used by Nginx
    • 3.2 Installing the PHP Extension
    • 3.3 Quick installation of PHP extensions
    • 3.4 Using the PHP Command line for Host (php-CLI)
    • 3.5 using composer
  • 4. Manage commands
    • 4.1 Server start and build commands
    • 4.2 Adding Shortcut Commands
  • 5. The use of the Log
    • 5.1 Nginx log
    • 5.2 PHP – FPM log
    • MySQL 5.3 log
  • 6. Database management
    • 6.1 phpMyAdmin
    • 6.2 phpRedisAdmin
  • 7. Safe use in a formal environment
  • 8. Frequently Asked Questions
    • 8.1 How to Use curl in PHP code
    • 8.2 Docker Uses cron Scheduled Tasks
    • 8.3 Docker container time
    • 8.4 How Do I Connect the MySQL server to the Redis Server

1. Directory structure

/ ├── ElasticSearch │ ├── mongo MongoDB │ ├── MySQL8 │ ├─ mysql5 │ ├─ mysql5 ├── elasticsearch elasticsearch Configuration file ├── MySQL8 Configuration file ├─ mysql5 mysql5 │ ├── PHP 7.6-php7.3 │ ├── php54 PHP5.4 │ ├─ redis ├─ logs ├── docker-compose.sample.yml Docker service Configuration example file ├── env.smaple Environment configuration example file ├─ WWW PHP code directoryCopy the code

2. Quick use

  1. The local installation
    • git
    • DockerLinux, Windows 10 Build 15063+, or MacOS 10.12+ is a must64A)
    • Docker - compose 1.7.0 +
  2. cloneProject:
    $ git clone https://github.com/yeszao/dnmp.git
    Copy the code
  3. If it is notrootUser, you need to add the current userdockerGroups of users:
    $ sudo gpasswd -a ${USER} docker
    Copy the code
  4. Copy and name the configuration filecopyCommand) to start:
    $CD DNMP # Go to the project directory $cp env.sample. env # Copy the environment variable file $cp docker-compose.sample.yml docker-compose Configuration file. By default, three services are started: # Nginx, PHP7, and MySQL8. For example: Redis, # PHP5.6, # PHP5.4, MongoDB, ElasticSearch, etc. For example: $docker-compose upCopy the code
  5. In the browser access:http://localhostorhttps://localhost(Self-signed HTTPS demo) you can see the effect of the PHP code in the file./www/localhost/index.php.

3. The PHP and extension

3.1 Switching the VERSION of PHP used by Nginx

First, you need to start another version of PHP, such as PHP5.4, by deleting the PHP5.4 comments in front of the docker-compose. Yml file and then starting the PHP5.4 container.

Change the fastcgi_pass host address from PHP to php54, as follows:

    fastcgi_pass   php:9000;
Copy the code

To:

    fastcgi_pass   php54:9000;
Copy the code

Where PHP and php54 are the names of the servers in the docker-compose. Yml file.

Finally, restart Nginx to take effect.

$ docker exec -it nginx nginx -s reload
Copy the code

Here are two NginX’s. The first is the container name and the second is the nginx program in the container.

3.2 Installing the PHP Extension

A lot of PHP functionality is implemented through extensions, and installing extensions is a bit of a time-consuming process. So, in addition to PHP’s built-in extensions, we only install a few extensions by default in the env.sample file. To install more extensions, open your.env file and modify the following PHP configuration to add the required PHP extensions:

PHP_EXTENSIONS=pdo_mysql,opcache,redis       # PHP list of extensions to install, separated by commas
PHP54_EXTENSIONS=opcache,redis                 List of extensions to install in # PHP 5.4, separated by commas
Copy the code

Then rebuild the PHP image.

docker-compose build php
Copy the code

See the env.sample comment block in the same file for an explanation of the available extensions.

3.3 Quick installation of PHP extensions

Step 1 Enter the container:

docker exec -it php /bin/sh

install-php-extensions apcu 
Copy the code

2. Supports quick installation of the extended list

Extension PHP 5.5 PHP 5.6 PHP 7.0 PHP 7.1 PHP 7.2 PHP 7.3 PHP 7.4
amqp
apcu
apcu_bc
bcmath
bz2
calendar
cmark
dba
decimal
enchant
exif
ffi
gd
gettext
gmagick
gmp
grpc
http
igbinary
imagick
imap
interbase
intl
ldap
mailparse
mcrypt
memcache
memcached
mongo
mongodb
msgpack
mssql
mysql
mysqli
oauth
odbc
opcache
opencensus
parallel*
pcntl
pcov
pdo_dblib
pdo_firebird
pdo_mysql
pdo_odbc
pdo_pgsql
pdo_sqlsrv
pgsql
propro
protobuf
pspell
pthreads*
raphf
rdkafka
recode
redis
shmop
snmp
snuffleupagus
soap
sockets
solr
sqlsrv
ssh2
sybase_ct
sysvmsg
sysvsem
sysvshm
tdlib*
tidy
timezonedb
uopz
uuid
wddx
xdebug
xhprof
xmlrpc
xsl
yaml
zip
zookeeper

This extension comes from github.com/mlocati/doc… Refer to sample files

3.4 Using the PHP Command line for Host (php-CLI)

  1. referencebash.alias.sampleExample file to copy the corresponding PHP CLI function to the host~/.bashrcFile.
  2. To make the file work:
    source ~/.bashrc
    Copy the code
  3. Then you can execute PHP commands on the host:
    ~ PHP -v PHP 7.2.13 (CLI) (Built-in: Dec 21 2018 02:22:47) (NTS) Copyright (c) 1997-2018 The PHP Group Zend Engine V3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache V7.2.13, Copyright (c) 1999-2018 Zend Technologies with Zend OPcache V7.2.13, By Zend Technologies with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick RethansCopy the code

3.5 using composer

Method 1: Use the Composer command on the host

  1. Determine the path to the Composer cache. For example, my DNMP download in~/dnmpDirectory, where composer is cached~/dnmp/data/composer.
  2. referencebash.alias.sampleSample file to copy the corresponding PHP Composer function to the host~/.bashrcFile.

    Note here that the ~/ DNMP /data/ Composer directory in the sample file needs to be the directory identified in the first step.

  3. To make the file work:
    source ~/.bashrc
    Copy the code
  4. You can use Composer in any directory on the host:
    cd ~/dnmp/www/
    composer create-project yeszao/fastphp project --no-dev
    Copy the code
  5. (Optional) The first time you use Composer~/dnmp/data/composerDirectory to generate oneconfig.jsonFile in which you can specify a domestic warehouse, for example:
    {
        "config": {},
        "repositories": {
            "packagist": {
                "type": "composer"."url": "https://packagist.laravel-china.org"}}}Copy the code

Method 2: Use the Composer command inside the container

Another way to do this is to enter the container and execute the composer command, using the PHP7 container as an example:

docker exec -it php /bin/sh
cd /www/localhost
composer update
Copy the code

4. Manage commands

4.1 Server start and build commands

To manage services, add the server name to the end of the command, for example:

$ docker-compose up                         # Create and start all containers
$ docker-compose up -d                      # Create and start all containers in background mode
$ docker-compose up nginx php mysql         # Create and start multiple containers for nginx, PHP and mysql
$ docker-compose up -d nginx php  mysql     # Start nginx, PHP and mysql containers by creating and running in the background


$ docker-compose start php                  # Start service
$ docker-compose stop php                   # Stop service
$ docker-compose restart php                # Restart service
$ docker-compose build php                  # Build or rebuild the service

$ docker-compose rm php                     Remove and stop the PHP container
$ docker-compose down                       Stop and remove containers, networks, images and mount volumes
Copy the code

4.2 Adding Shortcut Commands

During development, we may often use Docker exec-it to enter containers, and it is a convenient way to alias commonly used commands.

First, look at the available containers in the host:

$ docker ps           # View all running containers
$ docker ps -a        # All containers
Copy the code

The NAMES column in the output is the container name, which if you use the default configuration would be nginx, PHP, php56, mysql, etc.

Next, open the ~/.bashrc or ~/.zshrc file and add:

alias dnginx='docker exec -it nginx /bin/sh'
alias dphp='docker exec -it php /bin/sh'
alias dphp56='docker exec -it php56 /bin/sh'
alias dphp54='docker exec -it php54 /bin/sh'
alias dmysql='docker exec -it mysql /bin/bash'
alias dredis='docker exec -it redis /bin/sh'
Copy the code

The next time you get into a container, it’s very quick, like the PHP container:

$ dphp
Copy the code

4.3 Viewing the Docker Network

ifconfig docker0
Copy the code

Specifies the hosts address for the extra_hosts container to access the host

5. The use of the Log

The location where the Log file is generated depends on the value of each Log configuration in conf.

5.1 Nginx log

The Nginx log is the most frequently used log, so we put it in the root log directory.

/var/log/nginx = /var/log/nginx = /var/log/nginx = /var/log/nginx = /var/log/nginx = /var/log/nginx = /var/log/nginx = /var/log/nginx = /var/log/nginx = /var/log/nginx = /var/log/nginx = /var/log/nginx

error_log  /var/log/nginx/nginx.localhost.error.log  warn;
Copy the code

5.2 PHP – FPM log

In most cases, phP-fpm logs will be output to Nginx logs, so no additional configuration is required.

In addition, it is recommended to enable the error log directly in PHP:

error_reporting(E_ALL);
ini_set('error_reporting'.'on');
ini_set('display_errors'.'on');
Copy the code

If you really need it, follow the steps to open it (in the container).

  1. Enter the container, create the log file and modify the permissions:
    $ docker exec -it php /bin/sh
    $ mkdir /var/log/php
    $ cd /var/log/php
    $ touch php-fpm.error.log
    $ chmod a+w php-fpm.error.log
    Copy the code
  2. Open and modify the phP-fpm configuration file on the hostconf/php-fpm.conf, find the following line, delete the comment, and change the value to:
    php_admin_value[error_log] = /var/log/php/php-fpm.error.log
    Copy the code
  3. Restart the PHP-fpm container.

MySQL 5.3 log

Because MySQL in the MySQL container is started by the MySQL user, it cannot add log files in /var/log by itself. So, we put the MySQL log in the same directory as data, that is, in the project MySQL directory, corresponding to the /var/lib/mysql./ directory in the container.

slow-query-log-file     = /var/lib/mysql/mysql.slow.log
log-error               = /var/lib/mysql/mysql.error.log
Copy the code

This is the configuration of the log file in mysql.conf.

6. Database management

In this project, phpMyAdmin for MySQL online management and phpRedisAdmin for Redis online management are enabled in docker-compose. Yml by default, which can be modified or deleted as required.

6.1 phpMyAdmin

The address of the port that the phpMyAdmin container maps to the host is: 8080, so the address for accessing phpMyAdmin on the host is:

http://localhost:8080
Copy the code

MySQL connection information:

  • Host :(MySQL container network for this project)
  • Port:3306
  • Username :(manually enter in the phpmyadmin interface)
  • Password :(manually enter the phpmyadmin interface)

6.2 phpRedisAdmin

The phpRedisAdmin container maps to the host port address: 8081, so the address to access phpMyAdmin on the host is:

http://localhost:8081
Copy the code

Redis connection information is as follows:

  • Host: (Redis container network for this project)
  • port: 6379

7. Safe use in a formal environment

To use in a formal environment, please:

  1. Turn off XDebug debugging in php.ini
  2. Enhance security policies for accessing the MySQL database
  3. Enhanced security policies for Redis access

8 FaQs

8.1 How to Use curl in PHP code

Refer to this issue: github.com/yeszao/dnmp…

8.2 Docker Uses cron Scheduled Tasks

Docker uses cron scheduled tasks

8.3 Docker container time

The container time is set to the TZ variable in the.env file. For all supported time zones, see timezone list, wikipedia or PHP supported timezone list, and PHP official website.

8.4 How Do I Connect the MySQL server to the Redis Server

There are two cases,

In the first case, in YOUR PHP code.

/ / MySQL connection
$dbh = new PDO('mysql:host=mysql; dbname=mysql'.'root'.'123456');

/ / connect to Redis
$redis = new Redis();
$redis->connect('redis'.6379);
Copy the code

Because the container connects to the expose port and is on the same network as the container, the host parameter of the connection is directly the container name, and the port parameter is the port inside the container. For more information, see Docker-Compose Ports vs. Expose.

In the second case, you connect from the host via the command line or a tool such as Navicat. If the host is connected to mysql and redis, the container must map the port to the host via ports. For example, the docker-compose. Yml file has the following ports configuration: port 3306: port 3306 for the host and port 3306 for the container.

$mysql -h127.0.0.1 -uroot -p123456 -P3306 $reis -cli -h127.0.0.1Copy the code

The host parameter cannot be localhost because it communicates with mysql through sock file by default. The container is isolated from the host file system and therefore needs to connect through TCP, so you need to specify IP.

8.5 How does PHP in a container connect to host MySQL

1. The host runs ifconfig docker0 to obtain the IP address that inet is connected to

$ ifconfig docker0
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ...
Copy the code

2. Run the Mysql command on the host

 mysql>GRANT ALL PRIVILEGES ON*. *TO 'root'@The '%' IDENTIFIED BY '123456' WITH GRANT OPTION;
 mysql>flush privileges; // "root" "123456" is the user name and password of the database. // "%" is the IP address allowed to access the database. % means any IP address and can also be specifiedflush privilegesRefreshing permission InformationCopy the code

3. Connect directly to the PHP container using 172.0.17.1:3306