运行docker:
sudo systemctl start docker
0.创建docker网络:
docker network create --subnet=172.18.0.0/24 mxd
1. 复制配置文件到宿主机:
# 创建宿主机目录结构 mkdir -p /www/docker/nginx/logs mkdir -p /www/docker/php/php74 mkdir -p /www/web # 创建 nginx 容器 docker run -d --name nginx --network mxd nginx # 复制 nginx 配置文件 docker cp nginx:/etc/nginx/conf.d /www/docker/nginx docker cp nginx:/etc/nginx/nginx.conf /www/docker/nginx # 创建 php 容器 docker run -d --name php74 --network mxd php:7.4-fpm # 复制 php 配置文件 docker cp php74:/usr/local/etc/php-fpm.d/www.conf /www/docker/php/php74 docker cp php74:/usr/local/etc/php/php.ini-development /www/docker/php/php74/php.ini # 复制完成后销毁容器 docker rm -vf nginx php74
2. 创建容器, 挂载目录:
# 创建 nginx 容器 # -v 说明: # 第1个: 挂载 nginx 主配置文件 # 第2个: 挂载 nginx 站点配置目录 # 第3个: 挂载日志 # 第4个: 挂载 html 文件目录 docker run -d --name nginx -p 80:80 -p 443:443 --network mxd --ip 172.18.0.2 \ -v /www/docker/nginx/nginx.conf:/etc/nginx/nginx.conf -v /www/docker/nginx/conf.d:/etc/nginx/conf.d -v /www/docker/nginx/logs:/var/log/nginx -v /www/web:/www/web nginx # 创建 php 容器 # -v 说明: # 第1个: 挂载 php-fpm 配置文件 # 第2个: 挂载 php 配置文件 # 第3个: 挂载 html 文件目录 docker run -d --name php74 -p 9000:9000 --network mxd --ip 172.18.0.4 \ -v /www/docker/php/php74/www.conf:/usr/local/etc/php-fpm.d/www.conf -v /www/docker/php/php74/php.ini:/usr/local/etc/php/php.ini -v /www/web:/www/web php:7.4-fpm
3. 安装php扩展gd iconv pdo_mysql
参考:https://hub.docker.com/_/php
https://blog.csdn.net/lggirls/article/details/89395827
https://www.jianshu.com/p/20fcca06e27e
# 进入容器 docker exec -it php74 bash # 安装`pdo_mysql`扩展: docker-php-ext-install pdo_mysql # 单独安装`gd`扩展 apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev && docker-php-ext-configure gd --with-freetype --with-jpeg \ && docker-php-ext-install -j$(nproc) gd # 安装`gd`和`iconv`扩展(如果失败, 请去官网复制): apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev && docker-php-ext-install -j$(nproc) iconv && docker-php-ext-configure gd --with-freetype --with-jpeg \ && docker-php-ext-install -j$(nproc) gd # 安装`zip`扩展: apt-get update && apt-get install -y --no-install-recommends libzip-dev \ && rm -r /var/lib/apt/lists/* && docker-php-ext-install -j$(nproc) zip # 安装`intl`扩展: apt-get update && apt-get install -y zlib1g-dev libicu-dev g++ && docker-php-ext-configure intl && docker-php-ext-install intl # 大一统命令(安装`pdo_mysql`,`gd`,`iconv`,`zip`,`intl`): apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev --no-install-recommends libzip-dev zlib1g-dev libicu-dev g++ && docker-php-ext-install pdo_mysql && docker-php-ext-install -j$(nproc) iconv && docker-php-ext-configure gd --with-freetype --with-jpeg && docker-php-ext-install -j$(nproc) gd && docker-php-ext-install -j$(nproc) zip && docker-php-ext-configure intl && docker-php-ext-install intl # 退出容器 exit
4. 配置站点:
4.1. 默认站点:
cd /www/docker/nginx/conf.d vi default.conf
完整代码:
server { listen 80; listen [::]:80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; root /www/web; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } location ~ \.php$ { # root html; fastcgi_pass php74:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #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 html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; } #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 html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache‘s document root # concurs with nginx‘s one # #location ~ /\.ht { # deny all; #} }
4.2. 默认其它站点:
在/www/docker/nginx/conf.d创建xxx.conf文件,修改为对应内容即可
4.3. 上传网站代码;
4.4. 重启nginx:
docker restart nginx # 测试配置文件是否正确 docker exec nginx nginx -t # 重载配置文件 docker exec nginx nginx -s reload
4.5 访问网站 ip/index.php
5. 安装mysql
5.1. 复制配置文件到宿主机:
# 创建目录(该目录在启动镜像之前要保证为空, 否则密码可能错误) mkdir -p /www/docker/mysql/mysql57/data # 创建容器 docker run -d --name mysql57 --network mxd -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7 # 复制配置文件 docker cp mysql57:/etc/mysql/conf.d /www/docker/mysql/mysql57 docker cp mysql57:/etc/mysql/mysql.conf.d /www/docker/mysql/mysql57 # 删除容器 docker rm -vf mysql57
5.2. 创建mysql容器,挂载目录:
docker run -d --name mysql57 --network mxd --ip 172.18.0.5 \ -v /www/docker/mysql/mysql57/data:/var/lib/mysql -v /www/docker/mysql/mysql57/conf.d:/etc/mysql/conf.d -v /www/docker/mysql/mysql57/mysql.conf.d:/etc/mysql/mysql.conf.d -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
5.3. 测试连接(密码:123456):
docker exec -it mysql57 mysql -uroot -p