Table of Contents
准备
创建目录
配置PHP
拉取php-fpm镜像
启动php-fpm
配置Nginx
拉取Nginx镜像
配置nginx.conf
启动Nginx
配置MySQL
拉取MySQL镜像
启动MySQL
常见问题
1、thinkphp报错 Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'
2、thinkphp 报错 STORAGE_WRITE_ERROR:./Application/Runtime/Cache/Home/4e64ea6a2012f26b832b14cbc2152b28.php
3、thinkphp验证码图片显示不出来
Home Operation and Maintenance Nginx How Docker quickly builds a PHP+Nginx+Mysql environment

How Docker quickly builds a PHP+Nginx+Mysql environment

May 14, 2023 pm 10:25 PM
php docker nginx

准备

创建目录

mkdir -p /Users/mwqnice/Documents/Program/phpProgram &&
mkdir -p /Users/mwqnice/Documents/Program/docker/php/php7.1.33/conf &&
mkdir -p /Users/mwqnice/Documents/Program/docker/php/php7.1.33/logs &&
mkdir -p /Users/mwqnice/Documents/Program/docker/nginx/conf.d &&
mkdir -p /Users/mwqnice/Documents/Program/docker/mysql/conf &&
mkdir -p /Users/mwqnice/Documents/Program/docker/mysql/logs &&
mkdir -p /Users/mwqnice/Documents/Program/docker/mysql/data &&
cd /Users/mwqnice/Documents/Program/docker/nginx/conf.d && sudo touch default.conf
Copy after login

配置PHP

拉取php-fpm镜像

docker pull php:7.1.33-fpm #版本7.1.33
Copy after login

启动php-fpm

docker run --name  mwq-php \
-v /Users/mwqnice/Documents/Program/phpProgram:/var/www/html \
-v /Users/mwqnice/Documents/Program/docker/php/php7.1.33/conf:/usr/local/etc/php \
-v /Users/mwqnice/Documents/Program/docker/php/php7.1.33/logs:/phplogs \
-d --link mwq-php php:7.1.33-fpm
Copy after login

--name mwq-php是容器的名字

/Users/mwqnice/Documents/Program/phpProgram是本地项目目录,/var/www/html是容器内项目存储目录

配置Nginx

拉取Nginx镜像

docker pull nginx:latest //拉取最新版本
Copy after login

配置nginx.conf

server {
    listen  80;
    server_name localhost;
    set $root /var/www/html/localhost;
    
    #access_log  /tmp/nginx/logs/localhost.net.access.log main;
    #error_log  /tmp/nginx/logs/localhost.net.error.log notice;

    location ~ .*.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
        root $root;
    }

    location / {
        root $root;
        index  index.php index.html index.htm;
        if ( -f $request_filename) {
            break;
        }
        if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php/$1 last;
            break;
        }
    }

    location ~ .php(.*)$ {
        root $root;
        set $script $uri;
        set $path_info "";
        if ($uri ~ "^(.+.php)(/.+)") {
            set $script $1;
            set $path_info $2;
        }
        fastcgi_pass mwq-php:9000;
        #fastcgi_index index.php;
        fastcgi_index    index.php?IF_REWRITE=1;
        fastcgi_param    PATH_INFO    $path_info;
        fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param    SCRIPT_NAME    $script;
        include          fastcgi_params;

    }

    location ~ /.ht {
        deny  all;
    }
    location ~ /.svn {
        deny  all;
    }
    location ~ /.git/ {
        deny  all;
    }
    location ~ /Logs/ {
        deny  all;
    }
    location ~ /Logs/.* {
    }
    location ~ /Logs/.* {
        deny  all;
    }
    location ~ .*.(sql|tar.gz|zip|gz|tar|rariso|rpm|apk|bak)$ {
        deny  all;
    }
  
}
Copy after login

启动Nginx

docker run --name mwq-nginx -p 80:80 -d \
-v /Users/mwqnice/Documents/Program/phpProgram:/var/www/html:ro \
-v /Users/mwqnice/Documents/Program/docker/nginx/conf.d:/etc/nginx/conf.d:ro \
--link mwq-php:php \
nginx
Copy after login

配置MySQL

拉取MySQL镜像

docker pull mysql:5.7.36 #版本5.7.36
Copy after login

启动MySQL

docker run -p 3306:3306 --name mwq-mysql \
-v /Users/mwqnice/Documents/Program/docker/mysql/conf:/etc/mysql/conf.d \
-v /Users/mwqnice/Documents/Program/docker/mysql/logs:/logs \
-v /Users/mwqnice/Documents/Program/docker/mysql/data:/mysql_data \
-e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.36
Copy after login
  • -p 3306:3306: 将容器的3306端口映射到主机的3306端口

  • -v /Users/mwqnice/Documents/Program/docker/mysql/conf:/etc/mysql 将主机/Users/mwqnice/Documents/Program/docker/mysql/conf目录挂载到容器的/etc/mysql

  • -e MYSQL_ROOT_PASSWORD=123456: 初始化root用户的密码

  • -d: 后台运行容器,并返回容器ID

常见问题

1、thinkphp报错 Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'

缺少pdo_mysql扩展,连接数据库失败

找到php.ini,docker中在/usr/local/etc/php中,复制一份php.ini,增加extension=pdo_mysql.so,重启php-fpm,如果还是不行,访问phpinfo页面,查看是否有pdo_mysql

How Docker quickly builds a PHP+Nginx+Mysql environment

如果没有,说明没有pdo_mysql扩展,需要编译

编译方法如下:

到docker的php容器中,在php文件夹下:

docker-php-ext-install pdo pdo_mysql
Copy after login

如果报 /usr/local/bin/docker-php-ext-enable: cannot create /usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini: Directory nonexistent

解决方案:直接在/usr/local/etc/php目录下面新建 conf.d目录和对应的docker-php-ext-pdo_msql.ini文件

其中docker-php-ext-pdo_msql.ini的内容为:

extension=pdo_mysql.so
Copy after login

2、thinkphp 报错 STORAGE_WRITE_ERROR:./Application/Runtime/Cache/Home/4e64ea6a2012f26b832b14cbc2152b28.php

是因为服务器缓存文件夹的操作权限不够,即Runtime没有权限,把缓存文件全部删除,再给Runtime777权限就行了

sudo chmod 777 Runtime 或者直接对代码库最外层设置777权限

3、thinkphp验证码图片显示不出来

缺少gd扩展,安装:

docker-php-ext-install gd
Copy after login
Copy after login

可能以下报错:

If configure fails try --with-webp-dir=


If configure fails try --with-jpeg-dir=
configure: error: png.h not found.

安装:

apt-get install libpng-dev libjpeg-dev
Copy after login

再次执行:

// 增加freetype配置
docker-php-ext-configure gd --enable-gd-native-ttf --with-freetype-dir=/usr/include/freetype2 --with-png-dir=/usr/include --with-jpeg-dir=/usr/include

// 安装
docker-php-ext-install gd
Copy after login

php.ini增加php_gd2.so

How Docker quickly builds a PHP+Nginx+Mysql environment

phpinfo中显示gd库

How Docker quickly builds a PHP+Nginx+Mysql environment

注意如果phpinfo的gd库中没有freetype的支持,验证码依然显示不出来, 会报错:

Call to undefined function Think\imagettftext()

如果gd库中没有freeType,则按照以下步骤进行:

docker-php-ext-configure gd --enable-gd-native-ttf --with-freetype-dir=/usr/include/freetype2 --with-png-dir=/usr/include
Copy after login

重新编译:

docker-php-ext-install gd
Copy after login
Copy after login

如果报错: configure: error: freetype-config not found. 运行:

apt-get -y install libfreetype6-dev
Copy after login

然后再继续运行上面的命令。gd库中有了freetype,则验证码显示正常了

The above is the detailed content of How Docker quickly builds a PHP+Nginx+Mysql environment. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to create a mirror in docker How to create a mirror in docker Apr 15, 2025 am 11:27 AM

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

How to read the docker version How to read the docker version Apr 15, 2025 am 11:51 AM

To get the Docker version, you can perform the following steps: Run the Docker command "docker --version" to view the client and server versions. For Mac or Windows, you can also view version information through the Version tab of the Docker Desktop GUI or the About Docker Desktop menu.

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

PHP: An Introduction to the Server-Side Scripting Language PHP: An Introduction to the Server-Side Scripting Language Apr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

How to save docker image How to save docker image Apr 15, 2025 am 11:54 AM

To save the image in Docker, you can use the docker commit command to create a new image, containing the current state of the specified container, syntax: docker commit [Options] Container ID Image name. To save the image to the repository, you can use the docker push command, syntax: docker push image name [: tag]. To import saved images, you can use the docker pull command, syntax: docker pull image name [: tag].

See all articles