这里主要介绍 docker 搭建
下载安装 docker
使用阿里云镜像
搭建前准备工作
# 新建一个网络,容器内的所有通讯走这个网络
docker network create -d bridge my-net
# 若无特殊说明,一般都是采用文件挂载的方式
# 如需指定版本,去 https://hub.docker.com 上面找
# 下文为了便于阅读和理解,将命令拆分成了多行,执行时请合并成一行
docker pull nginx
docker redis:6
docker mysql:8
docker php:7.4-fpm
安装 nginx
# 拉去最新的 nginx 镜像,也可以拉取指定版本
docker pull nginx
# 运行
docker run -dp 80:80 --name nginx
# 加入网络
--network my-net
# 日志文件目录
--mount type=bind,source=/Users/dwy/shuxiaoyuan/docker/nginx/logs,target=/var/log/nginx
# 主配置文件,要先搞一份配置文件,不让起不来的
--mount type=bind,source=/Users/dwy/shuxiaoyuan/docker/nginx/nginx.conf,target=/etc/nginx/nginx.conf
# conf.d 配置文件目录
--mount type=bind,source=/Users/dwy/shuxiaoyuan/docker/nginx/conf.d,target=/etc/nginx/conf.d
# 代码目录
--mount type=bind,source=/Users/dwy/shuxiaoyuan/mycode,target=/www
nginx:latest
nginx 主配置文件
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
nginx 多站点配置
# 默认站点:default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
#location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
#}
#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;
#}
}
# 站点一:test1.conf
server {
listen 80;
server_name test1-docker.com;
# 这个地方是容器内的目录地址,不要搞成宿主机的了
root /www/test1/public;
location / {
index index.php index.htm index.html;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
root /www/test1;
# 这个地方要填写容器内 php-fpm的IP地址,也可以直接使用容器名
fastcgi_pass 172.18.0.5:9000;
fastcgi_index index.php;
# 这个地方也要指到 public 目录下才行
fastcgi_param SCRIPT_FILENAME /www/test1/public/$fastcgi_script_name;
include fastcgi_params;
}
access_log /var/log/nginx/test1-docker.com-access.log main;
}
# 站点二:test2.conf
server {
listen 80;
server_name test2.com;
root /www/test2/public;
location / {
index index.php index.htm index.html;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
root /www/test2;
fastcgi_pass php74:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/test2/public/$fastcgi_script_name;
include fastcgi_params;
}
access_log /var/log/nginx/test2.com-access.log main;
}
安装 redis
# 地址:https://hub.docker.com/_/redis
docker pull redis:6
# 为了便于查看,下面将命令拆分成了多行,实际是一行
# 运行docker,取名,端口映射
docker run --name redis6 -dp 6379:6379
# 加入网络
--network my-net
# 挂载文件或目录
--mount type=bind,source=/Users/dwy/shuxiaoyuan/docker/redis/data,target=/data
--mount type=bind,source=/Users/dwy/shuxiaoyuan/docker/redis/data/redis.conf,target=/data/redis.conf
# 以redis:6为镜像,用配置文件启动
redis:6 redis-server /data/redis.conf
--appendonly yes
redis 配置文件说明
上面启动 Redis 时,用的配置文件启动的,配置文件挂载在本地的,后续可以在本地修改配置文件,然后重新启动容器即可,下面简单的写了两个配置,更详细的可以去找 Redis 的完整配置文件
port 6379
requirepass 123456
安装 mysql
# 设置密码 123456
docker run --name mysql -it -dp 3306:3306 --network my-net -e MYSQL_ROOT_PASSWORD=123456 --mount type=bind,source=/Users/dwy/shuxiaoyuan/docker/mysql,target=/var/lib/mysql mysql:latest
安装 PHP
https://hub.docker.com/_/php
# 支持的标签
https://github.com/docker-library/docs/blob/master/php/README.md#supported-tags-and-respective-dockerfile-links
# 运行,PHP无需映射端口
docker run -d --name php74 --network my-net --mount type=bind,source=/Users/dwy/shuxiaoyuan/mycode,target=/www php:7.4-fpm
后续安装扩展等
也可以用我制作的镜像,里面安装了composer和大部分扩展
docker pull shuxiaoyuan/php74:v1.3