docker搭建 nginx+mysql+php-fpm开发环境
文章目录
- 使用到的目录结构及说明
- 创建docker内容器网络环境
- nginx环境搭建
- 创建容器
- 将nginx容器加入网络 nginx-php56 网络
- php环境搭建
- 创建临时容器
- 复制配置文件
- 删除临时容器
- 创建正式容器
- 将php56-fpm容器加入网络 nginx-php56 网络
- mysql环境搭建
- 创建my.cnf
- 创建正式容器
- 配置远程登录用户
- 将mysql57容器加入网络 nginx-php56 网络
- 配置nginx与php的结合
- php安装扩展
- pdo_mysql
- mysqli
- gd库
- 重启所有容器
使用到的目录结构及说明
创建环境搭建的根目录D:\docker
D:\docker
|--nginx
|--1.23.3 //版本
|--conf //配置文件目录 映射到 /etc/nginx/conf.d
|--log //日志目录 映射到 /var/log/nginx
|--php-fpm
|--5.6.40
|--conf //配置文件目录 映射到 /usr/local/etc
|--log //日志目录 映射到 /var/log
|--mysql
|--5.7.40
|--data //数据库存放目录 映射到 /var/lib/mysql
|--conf //配置文件目录 映射到 /etc/mysql/conf.d
|--log //日志目录 映射到 /var/log
创建docker内容器网络环境
docker network create nginx-php56
nginx环境搭建
创建容器
docker run -d --name nginx -p 8081:80 \
-v D:\docker\nginx\1.23.3\conf:/etc/nginx/conf.d \
-v D:\docker\nginx\1.23.3\log:/var/log/nginx \
-v D:\www:/app \
nginx:1.23.3
将nginx容器加入网络 nginx-php56 网络
docker network connect nginx-php56 nginx
D:\www 是代码所在的根目录
php环境搭建
创建临时容器
docker run -d --name php56-temp php:5.6.40-fpm
复制配置文件
docker cp php56-temp:/usr/local/etc D:\docker\php-fpm\5.6.40
注意复制到宿主机的是etc目录,修改宿主机目录名称etc为conf
删除临时容器
docker stop php56-temp
docker rm php56-temp
创建正式容器
docker run -d --name php56-fpm \
-v D:\www:/app \
-v D:\docker\php-fpm\5.6.40\conf:/usr/local/etc \
-v D:\docker\php-fpm\5.6.40\log:/var/log \
php:5.6.40-fpm
将php56-fpm容器加入网络 nginx-php56 网络
docker network connect nginx-php56 php56-fpm
mysql环境搭建
创建my.cnf
将my.cnf放入D:\docker\mysql\mysql57\conf
[mysql]
default-character-set=utf8
[mysqld]
character-set-server=utf8
default-time_zone='+8:00'
log-error=/var/log/mysqld.log
[client]
default-character-set=utf8
创建正式容器
docker run -d --name mysql57 -p 8306:3306 -e MYSQL_ROOT_PASSWORD=123456 \
-v D:\docker\mysql\5.7.40\data:/var/lib/mysql \
-v D:\docker\mysql\5.7.40\log:/var/log \
-v D:\docker\mysql\5.7.40\conf:/etc/mysql/conf.d \
mysql:5.7
配置远程登录用户
#登录mysql
mysql -u root -p
#添加远程登录用户
CREATE USER 'user1'@'%' IDENTIFIED WITH mysql_native_password BY 'user123!';
GRANT ALL PRIVILEGES ON *.* TO 'user1'@'%';
将mysql57容器加入网络 nginx-php56 网络
docker network connect nginx-php56 mysql57
配置nginx与php的结合
注意:将配置文件放入指定目录
default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /app;
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php/$1 last;
break;
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root /app;
# fastcgi_pass php56-fpm:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
# include fastcgi_params;
#}
location ~ .+\.php($|/) {
root /app;
fastcgi_pass php56-fpm:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /app/$fastcgi_script_name;
fastcgi_split_path_info ^((?U).+.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME /app/$fastcgi_script_name;
include fastcgi_params;
#include snippets/fastcgi-php.conf;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
php安装扩展
pdo_mysql
docker exec -it php56-fpm bash
docker-php-ext-install pdo pdo_mysql
mysqli
docker exec -it php56-fpm bash
docker-php-ext-install mysqli
gd库
apt update
#1.安装依赖包
apt install -y libfreetype6-dev libwebp-dev libjpeg-dev libpng-dev libvpx-dev libxpm-dev
#2.解压扩展文件
docker-php-source extract
#3.进入扩展目录
cd /usr/src/php/ext
#通过 docker-php-ext-configure gd --help 可以查看配置参数
#4.进行配置参数
docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include --with-vpx-dir=/usr/include --with-xpm-dir=/usr/include
#5.安装
docker-php-ext-install gd
重启所有容器
docker restart nginx php56-fpm mysql57