Docker 基于 LNMP 扩展安装

以下扩展安装基于 Docker 搭建 LNMP + Redis


全局安装 Laravel dd() 函数

创建 composer 文件夹
1
2
3
cd /data/php
mkdir composer
cd composer
新建 /data/php/composer/composer.json
1
2
3
4
5
6
7
8
9
10
{
"require": {
"squizlabs/php_codesniffer": "*",
"fxp/composer-asset-plugin": "^1.4",
"symfony/var-dumper": "3.3.16"
},
"autoload": {
"files": []
}
}
新建 /data/php/debugHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php

# install symfony/var-dump to your project
# composer require symfony/var-dumper

// use namespace
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper as SymfonyHtmlDumper;

/**
* Class HtmlDumper
*/
class HtmlDumper extends SymfonyHtmlDumper
{
/**
* Colour definitions for output.
*
* @var array
*/
protected $styles = [
'default' => 'background-color:#fff; color:#222; line-height:1.2em; font-weight:normal; font:12px Monaco, Consolas, monospace; word-wrap: break-word; white-space: pre-wrap; position:relative; z-index:100000',
'num' => 'color:#a71d5d',
'const' => 'color:#795da3',
'str' => 'color:#df5000',
'cchr' => 'color:#222',
'note' => 'color:#a71d5d',
'ref' => 'color:#a0a0a0',
'public' => 'color:#795da3',
'protected' => 'color:#795da3',
'private' => 'color:#795da3',
'meta' => 'color:#b729d9',
'key' => 'color:#df5000',
'index' => 'color:#a71d5d',
];
}

/**
* Class Dumper
*/
class Dumper
{
/**
* Dump a value with elegance.
*
* @param mixed $value
* @return void
*/
public function dump($value)
{
if (class_exists(CliDumper::class)) {
$dumper = 'cli' === PHP_SAPI ? new CliDumper : new HtmlDumper;
$dumper->dump((new VarCloner)->cloneVar($value));
} else {
var_dump($value);
}
}
}

if (! function_exists('dd')) {
/**
* Dump the passed variables and end the script.
*
* @param mixed
* @return void
*/
function dd(...$args)
{
foreach ($args as $x) {
(new Dumper)->dump($x);
}
die(1);
}
}

if (! function_exists('dda')) {
/**
* Dump the passed array variables and end the script.
*
* @param mixed
* @return void
*/
function dda(...$args)
{
foreach ($args as $x) {
(new Dumper)->dump($x->toArray());
}
die(1);
}
}
修改 /data/docker-compose.yml 后重启容器
1
2
3
4
5
php:
container_name: php
volumes:
- /data/php/debugHelper.php:/usr/local/etc/php/debugHelper.php
- /data/php/composer/composer.json:/var/www/.composer/composer.json
1
docker-compose restart
进入 php 容器安装 dd 函数依赖包并复制至本地
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
docker exec -it php /bin/bash
# 1.修改 /var/www 所属用户和组
chown -R www-data:www-data /var/www
# 2.设置 www-data 用户可登录(查看用户ID可使用命令 id www-data)
usermod -s /bin/bash -u 33 www-data
# 3.切换至 www-data
su www-data
# 4.设置 composer 镜像源
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
# 5.更新 composer 包
composer global update
# 6.退出 www-data 用户
exit
# 7.退出容器
exit
# 8.复制容器内的 vendor 目录至本地
docker cp php:/var/www/.composer/vendor /data/php/composer/
停止运行容器
1
docker-compose stop
映射 composerdebugHelper.php
  1. 修改 php.ini

    1
    auto_prepend_file = "/var/www/.composer/vendor/autoload.php"
  2. 修改 /data/php/composer.json

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    {
    "require": {
    "squizlabs/php_codesniffer": "*",
    "fxp/composer-asset-plugin": "^1.4",
    "symfony/var-dumper": "3.3.16"
    },
    "autoload": {
    "files": [
    "/usr/local/etc/php/debugHelper.php"
    ]
    }
    }
  3. 修改 docker-compose.yml 并重启(需重新构建)

    1
    2
    3
    4
    5
    php:
    container_name: php
    volumes:
    - /data/php/debugHelper.php:/usr/local/etc/php/debugHelper.php
    - /data/php/composer:/var/www/.composer
    1
    docker-compose up -d --build
  4. 进入容器, 更新 composer 自动加载

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    docker exec -it php /bin/bash
    # 1.修改 /var/www 所属用户和组
    chown -R www-data:www-data /var/www
    # 2.设置 www-data 用户可登录(查看用户ID可使用命令 id www-data)
    usermod -s /bin/bash -u 33 www-data
    # 3.切换至 www-data
    su www-data
    # 4.更新 composer 自动加载
    composer global dump-autoload
    # 5.退出 www-data 用户
    exit
    # 6.重新设置 www-data 用户不可登录(查看用户ID可使用命令 id www-data)
    usermod -s /usr/sbin/nologin -u 33 www-data
    # 7.退出容器
    exit

Docker PhpStorm + Xdebug

编辑 docker-php-ext-xdebug.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so
xdebug.profiler_enable=on
xdebug.trace_output_dir="/usr/local/php/xdebug_trace"
xdebug.profiler_output_dir="/usr/local/php/xdebug_profiler"
xdebug.default_enable=0
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
#宿主机IP
xdebug.remote_host=192.168.0.7
xdebug.remote_port=9001
xdebug.remote_connect_back=0
xdebug.remote_autostart=1
xdebug.idekey=PHPSTORM
xdebug.remote_log=/usr/local/php/xdebug/remote.log
配置 PhpStorm
1
2
3
4
# 1.配置端口
Settings -> Languages & Frameworks -> PHP -> Debug -> Debug port 配置端口与 xdebug 一致
# 2.映射目录(例:Windows - D:/docker/app/default -> /data/app/default)
Settings -> Languages & Frameworks -> PHP -> Servers -> Use Path mappings -> Absolute path on the server (docker 容器内的项目路径)
PhpStorm 启用监听即可

Docker wkhtmltopdf, 基于原先打包好的 sevming/php72:0.1 镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 1.运行容器
docker run -itd --rm --name testPhp sevming/php72:0.1

# 2.进入 php 容器
docker exec -it testPhp /bin/bash

# 3.安装软件
set -eux \
&& apt-get update \
&& apt-get install -y --no-install-recommends wget unzip fontconfig \
libfontenc1 libjpeg62-turbo libx11-6 libx11-data libxau6 libxcb1 \
libxdmcp6 libxext6 libxrender1 x11-common xfonts-75dpi \
xfonts-base xfonts-encodings xfonts-utils

# 4.安装 wkhtmltopdf
wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.buster_amd64.deb -O wkhtmltox_0.12.5-1.buster_amd64.deb \
&& dpkg -i wkhtmltox_0.12.5-1.buster_amd64.deb

# 5.清理无用的依赖包
set -eux \
&& apt-get autoremove \
&& apt-get autoclean \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# 6.默认安装[微软雅黑], 字体安装参考 https://sevming.github.io/PHP/html-to-pdf-image.html

# 7.生成镜像
docker build -t sevming/php72:0.2 .

# 8.上传镜像
docker push sevming/php72:0.2
0%