远离搭建环境的烦恼,使用docker官方镜像也能简单搭建本地开发环境(可配置多站点,可灵活切换多个php版本)。本文全是干货,无脑按步骤操作即可。
第一步:在本地创建一个工作目录,目录结构如下:
lnmp
nginx
conf
vhost
www.test.cc # 虚拟域名
default.conf
logs
redis
data
log
redis.conf # 需要自己去下载redis.conf文件内容https://redis.io/topics/config,我这边是6.2版本 protected-mode no 设置成no,logfile 改为"/dev/log",# bind 127.0.0.1注释掉,requirepass 'xxxx'设置密码
www # 项目代码存放目录
docker-compose.yml
docker-compose.yml的内容:
services:
nginx:
image: nginx
restart: always
ports:
- "8000:80"
volumes:
# 本地路径:容器中的路径
- ~/www/docker/lnmp/www:/usr/share/nginx/html
- ~/www/docker/lnmp/nginx/conf:/etc/nginx/conf.d
- ~/www/docker/lnmp/nginx/logs:/var/log/nginx
networks:
- dnmp
container_name: nginx_test
php:
image: php:7.3-fpm
restart: always
volumes:
# 本地路径:容器中的路径
- ~/www/docker/lnmp/www:/www
# - ~/www/docker/lnmp/php/conf:/usr/local/etc/php
networks:
- dnmp
container_name: php_test
redis:
image: redis:6.2
restart: always
ports:
- "6378:6379"
volumes:
# 本地路径:容器中的路径
- ~/www/docker/lnmp/redis/data:/data
# 需要自己去下载redis.conf文件内容https://redis.io/topics/config,我这边是6.2版本
- ~/www/docker/lnmp/redis/redis.conf:/etc/redis.conf
- ~/www/docker/lnmp/redis/log:/dev/log #默认redis的日志路径
networks:
dnmp:
ipv4_address: 172.20.3.4 # 为redis容器指定ip
container_name: redis_test
networks:
dnmp: #可手动创建网络 docker network create --subnet=172.20.3.0/24 lnmp_dnmp
ipam:
config:
- subnet: 172.20.3.0/24 # 本机ip的网络段
nginx的default.conf内容:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$query_string;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;
include fastcgi_params;
}
}
第二步:打开终端,执行:
cd ~/www/docker/lnmp # 进入工作目录(docker-composer.yml所在目录)
docker-compose up -d # 执行docker-compose.yml内容
再执行
docker ps
可以看到我们本地多了三个容器分别是nginx_test、php_test、redis_test。
此时,最基础的开发环境就已经搭建好了,可以在lnmp/www目录中新建index.php,浏览器打开网址:localhost:8000,查看效果。
第三步:为php容器安装项目运行所需扩展:
完成前两步,虽然可以解析一些简单的PHP脚本,但是对于网校的开发还缺少一些扩展。所以我们还要手动安装部分扩展。
docker exec -it php_test /bin/bash
docker-php-ext-install sockets pdo_mysql
至此,本地的开发环境虽然可以用,但还不满足我们的日常开发需求。接下来就简单的处理一下nginx的配置,以支持多站点运行。
第四步:nginx多站点配置
在default.conf文件中加入
include /etc/nginx/conf.d/vhost/*.conf;
在vhost目录中新建文件www.xxx.com.conf
server {
listen 80; # 监听端口
server_name www.xxx.com; # 站点域名
location / {
root /usr/share/nginx/html/www.xxx.com/public;
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$query_string;
}
# PHP配置
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/www.xxx.com/public/$fastcgi_script_name;
include fastcgi_params;
}
}
在工作目录下的www目录中,新建www.xxx.com目录,并把网校API项目放入其中(当然最好是git clon下来)。例如:(不要忘记修改env中redis的连接地址,redis的host可设置为redis的ip或redis容器名称)
在宿主机/etc/hosts配置 www.xxx.com
127.0.0.1 www.xxx.com
请求www.xxx.com:8000,可查看效果。
第五步:切换php版本
docker pull想要的php版本,例如
docker pull php:7.2-fpm
启动一个php7.2版本的容器,加入到前面创建的网络并绑定www目录:
docker run -itd --name php72 --net lnmp_dnmp -v ~/www/docker/lnmp/www:/www php:7.2-fpm
执行成功后,修改nginx配置中的fastcgi_pass参数,改为php容器名称:端口号9000:
location ~ \.php$ {
fastcgi_pass php72:9000;
}
重启nginx容器:
docker restart nginx_test
请求本次修改的nginx配置对应的站点域名,查看php版本切换效果。
此环境能够满足日常开发需求,目录结构和其他配置都可以DIY,大家可以添加更多丰富的功能或配置,也许以后可以成为一套标准的开发环境模型。
结尾:
操作过程中有可能用到的命令:
# 修改nginx配置,记得重启nginx
# update读取所有源的软件列表
apt-get update
# 安装vim
apt-install vim