千家信息网

docker怎么配置nginx+php+mysql

发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,本篇内容主要讲解"docker怎么配置nginx+php+mysql",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"docker怎么配置nginx+php+
千家信息网最后更新 2025年12月01日docker怎么配置nginx+php+mysql

本篇内容主要讲解"docker怎么配置nginx+php+mysql",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"docker怎么配置nginx+php+mysql"吧!

首先了解一个方法:

使用docker exec进入Docker容器

  docker在1.3.X版本之后还提供了一个新的命令exec用于进入容器,这种方式相对更简单一些,下面我们来看一下该命令的使用:

sudo docker exec --help

接下来我们使用该命令进入一个已经在运行的容器

$ sudo docker ps  $ sudo docker exec -it 775c7c9ee1e1 /bin/bash

一. 配置nginx

查找 Docker Hub 上的 nginx 镜像

runoob@runoob:~/nginx$ docker search nginxNAME                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATEDnginx                     Official build of Nginx.                        3260      [OK]       jwilder/nginx-proxy       Automated Nginx reverse proxy for docker c...   674                  [OK]richarvey/nginx-php-fpm   Container running Nginx + PHP-FPM capable ...   207                  [OK]million12/nginx-php       Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS...   67                   [OK]maxexcloo/nginx-php       Docker framework container with Nginx and ...   57                   [OK]webdevops/php-nginx       Nginx with PHP-FPM                              39                   [OK]h4nrik/nginx-ldap         NGINX web server with LDAP/AD, SSL and pro...   27                   [OK]bitnami/nginx             Bitnami nginx Docker Image                      19                   [OK]maxexcloo/nginx           Docker framework container with Nginx inst...   7                    [OK]...

这里我们拉取官方的镜像

runoob@runoob:~/nginx$ docker pull nginx

等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORY 为 nginx 的镜像。

runoob@runoob:~/nginx$ docker images nginxREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEnginx               latest              555bbd91e13c        3 days ago          182.8 MB创建并运行容器:
docker run --name mynginx -p 80:80 -v /var/www:/var/www -v /usr/local/nginx/conf/conf.d:/etc/nginx/conf.d -d nginx

注意:

-v 添加文件映射关系,这样在宿主机上更改的文件可以直接映射到容器中。这里的目录根据自己实际情况进行映射。

创建并运行容器后,docker内的nginx即启动成功,无需进入docker内部再次启动nginx, 否则会提示80等端口被占用,因为nginx已经启动。

这时候便可以访问nginx配置的域名验证了。

我这里映射的conf.d主要包含nginx的配置文件,php的配置信息为:

复制代码

# phpserver {    charset utf-8;    client_max_body_size 128M;    listen 80; ## listen for ipv4    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6    server_name www.baidu.com;    root        /var/www;    index       index.php;    location / {        #-e表示只要filename存在,则为真        if (!-e $request_filename){            rewrite  ^(.*)$  /index.php?s=$1  last;            break;        }        # Redirect everything that isn't a real file to index.php        try_files $uri $uri/ /index.php$is_args$args;    }    # uncomment to avoid processing of calls to non-existing static files by Yii    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {    #    try_files $uri =404;    #}    #error_page 404 /404.html;    # deny accessing php files for the /assets directory    location ~ ^/assets/.*\.php$ {        deny all;    }    location ~ \.php$ {        include fastcgi_params;        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;        fastcgi_pass 172.17.0.3:9000;        #fastcgi_pass unix:/var/run/php5-fpm.sock;        try_files $uri =404;    }    location ~* /\. {        deny all;    }}

注意最后面的fastcgi_pass的ip地址,在php配置常见问题有详细介绍。

二. php配置

查找Docker Hub上的php镜像

runoob@runoob:~/php-fpm$ docker search phpNAME                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATEDphp                       While designed for web development, the PH...   1232      [OK]       richarvey/nginx-php-fpm   Container running Nginx + PHP-FPM capable ...   207                  [OK]phpmyadmin/phpmyadmin     A web interface for MySQL and MariaDB.          123                  [OK]eboraas/apache-php        PHP5 on Apache (with SSL support), built o...   69                   [OK]php-zendserver            Zend Server - the integrated PHP applicati...   69        [OK]       million12/nginx-php       Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS...   67                   [OK]webdevops/php-nginx       Nginx with PHP-FPM                              39                   [OK]webdevops/php-apache      Apache with PHP-FPM (based on webdevops/php)    14                   [OK]phpunit/phpunit           PHPUnit is a programmer-oriented testing f...   14                   [OK]tetraweb/php              PHP 5.3, 5.4, 5.5, 5.6, 7.0 for CI and run...   12                   [OK]webdevops/php             PHP (FPM and CLI) service container             10                   [OK]...

这里我们拉取官方的镜像,标签为5.6-fpm

runoob@runoob:~/php-fpm$ docker pull php:5.6-fpm

等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为php,标签为5.6-fpm的镜像。

runoob@runoob:~/php-fpm$ docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEphp                 5.6-fpm             025041cd3aa5        6 days ago          456.3 MB

创建并运行php容器:

docker run -p 9000:9000 --name  phpfpm -v /var/www:/var/www -d php:5.6-fpm

注意这里一定要创建文件映射,或者php容器内有对应的php代码。上一步nginx的文件映射,在这里是找不到的。所以如果没有文件映射,127.0.0.1:9000 在此容器内就找不到文件 。

常见问题:

启动php容器后,如果访问nginx为:502 Bad Gateway

尝试以下方法:

查看php镜像的ip地址

docker inspect --format='{{.NetworkSettings.IPAddress}}' phpfpm


如:192.168.4.202

那么修改nginx的conf配置文件,使fastcgi_pass的值为 192.168.4.202:9000
vim /docker/nginx/conf.d/default.conf
fastcgi_pass 192.168.4.202:9000;


重启nginx容器后,就可以正常访问。

三. mysql配置

查找Docker Hub上的mysql镜像

runoob@runoob:/mysql$ docker search mysqlNAME                     DESCRIPTION                                     STARS     OFFICIAL   AUTOMATEDmysql                    MySQL is a widely used, open-source relati...   2529      [OK]       mysql/mysql-server       Optimized MySQL Server Docker images. Crea...   161                  [OK]centurylink/mysql        Image containing mysql. Optimized to be li...   45                   [OK]sameersbn/mysql                                                          36                   [OK]google/mysql             MySQL server for Google Compute Engine          16                   [OK]appcontainers/mysql      Centos/Debian Based Customizable MySQL Con...   8                    [OK]marvambass/mysql         MySQL Server based on Ubuntu 14.04              6                    [OK]drupaldocker/mysql       MySQL for Drupal                                2                    [OK]azukiapp/mysql           Docker image to run MySQL by Azuki - http:...   2                    [OK]...

这里我们拉取官方的镜像,标签为5.6

runoob@runoob:~/mysql$ docker pull mysql:5.6

等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为mysql,标签为5.6的镜像。

runoob@runoob:~/mysql$ docker images |grep mysqlmysql               5.6                 2c0964ec182a        3 weeks ago         329 MB

创建并运行MySQL容器:

docker run -p 3306:3306 --name mysql -v /usr/local/mysql:/etc/mysql/sqlinit -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6

这里的文件映射主要目的是把宿主机的sql数据库数据文件映射到docker mysql容器,方便导入,注意这里mysql容器的目录不能是已有的目录,否则会覆盖。

注意:

这里创建容易已经有了my.cnf,无需自己添加。

拓展

使用外部工具navicat连接docker 内mysql

mysql的host 填写docker内的IP,获取方式为:

1 docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql

即可连接成功!

注意:

docker的容器启动顺序问题会导致容器的IP地址不一致,如果在连接数据库和fastcgi处有用到容器的IP,要注意容器的启动顺序。

重启容器:docker restart 容器名/容器ID

关闭容器:docker stop xxx

开启容器:docker start xxx

查看正在运行的容器:docker ps

查看所有容器(包括未运行的容器): docker ps -a

创建并运行容器: docker run

---------------------------------------

常见报错:

1. thinkphp报错 Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'

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

找到php.ini,docker中在/usr/local/etc/php中,复制一份php.ini,增加 extension=pdo_mysql.so ,重启phpfpm。

如果还不行,访问phpinfo页面,查看是否有pdo_mysql

缺少gd扩展,安装:

docker-php-ext-install gd

可能以下报错:

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

再次执行:

// 增加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

php.ini增加php_gd2.so

phpinfo中显示gd库

注意如果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重新编译:docker-php-ext-install gd

如果报错:

configure: error: freetype-config not found.

运行: apt-get -y install libfreetype6-dev ,然后再继续运行上面的命令。

gd库中有了freetype,则验证码显示正常了:

到此,相信大家对"docker怎么配置nginx+php+mysql"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

容器 镜像 配置 文件 运行 命令 数据 标签 地址 官方 数据库 方法 目录 问题 验证 成功 内容 再次 实际 宿主 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 域控服务器默认域管理员组 数据库管理技术主要有哪些模型 软件开发广东哪个大学好 信息网络安全包括什么安全 应用程序是如何读取数据库数据 上海科技出版社网络技术应用 雷兔跨境服务器不能适用国外 雷霆行动揭开网络安全渗透 信息网络安全相关法律法规ppt 浙江正规戴尔服务器云服务器 数据库如何插入重复键 怎么看域名服务器价格 皮皮网络技术服务有限公司 吉林放心软件开发服务参考价格 wifi认证服务器 软件开发哪些学校比较好 重庆专业技术人员网络安全 桓台人力资源软件开发公司 虹口区网络技术产业化 制作网络安全宣传册 数据库基础之sql操作数据 管理ip登录服务器 数据库表都需要唯一标识符 lce服务器操作员 小文 电子科大软件开发 大航海时代ol 数据库 网络安全iis目录遍历 php 判断数据库连接 战舰世界如何链接香港服务器 维护网络安全 提高风险意识
0