windows下linux+docker+lnmp php开发环境

docker 专栏收录该内容
6 篇文章 0 订阅

Docker环境

以下三种自行选择

  • vmware 安装自行google 安装完docker和docker-compose之后跳到Git部分
  • docker desktop 直接跳到Git部分
  • WSL2 Docker (这里详细介绍WSL2)

搭建WSL2

1.管理员身份运行powershell.exe 并执行

  • powershell.exe 在 C:\Windows\System32\WindowsPowerShell\v1.0 目录下,运行完毕重启电脑
  dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

2.下载适用于 x64 计算机的 WSL2 Linux 内核更新包

  • https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
  • 安装 wsl_update_x64.msi

3.应用商店(Microsoft Store)下载软件

  • 下载Ubuntu,安装完毕点击安装 设置用户和密码
  • 下载 Windows Terminal (有自己喜欢的工具可以忽略这步)

4.设置WSL版本

  • 回到第1步打开powershell.exe执行下面命令
  • 查看WSL版本命令
  wsl -l -v 
  • 设置wsl版本
  wsl --set-version Ubuntu 2
  wsl --set-default-version 2

5.登陆ubuntu

  • 打开 Windows Terminal 点击 +号旁边的向下符号 ↓ ,选择Ubuntu
  • 根据提示设置一个非root用户
  • 设置 root 用户密码
  sudo passwd
  • 关闭防火墙
  sudo ufw disable

Docker

安装Docker和Docker-compose

  • 添加Docker源
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
  "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
  $(lsb_release -cs) \
  stable"
  • 更新软件包
sudo apt update  
  • 安装docker
sudo apt install -y docker-ce
  • 启动docker
sudo service docker start
sudo docker -v
  • docker命令免sudo,执行命令之后记得关掉终端再次打开
sudo usermod -aG docker $USER
  • 拉取自定义php-fpm镜像
echo '1VRKQcr9ojqP' | docker login --username=sz99com --password-stdin registry.cn-hongkong.aliyuncs.com
docker pull registry.cn-hongkong.aliyuncs.com/sz99/rep:php-fpm
  • 修改Tag
docker tag 4fcebc58ea14 laradock_php-fpm:latest
  • 安装docker-compose
sudo curl -L "https://get.daocloud.io/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo docker-compose --version
  • 配置docker自动脚本
sudo vi /etc/init.wsl
  • 将以下内容添加至/etc/init.wsl
sudo service docker start
  • 设置可执行权限
sudo chmod +x /etc/init.wsl
  • Windows开机自动启动Docker
win+r 输入 shell:startup
  • 在该目录下创建wsl-docker-start.vbs脚本 文件名字随意,.vbs扩展结尾就行 输入以下内容
  • 如果安装的是 Ubuntu 18.04 LTS 或者 Ubuntu 20.04 LTS 用Ubuntu-18.04 或者 Ubuntu-20.04 替换Ubuntu名称即可
Set ws = WScript.CreateObject("WScript.Shell")
ws.run "wsl -d Ubuntu -u root /etc/init.wsl"

Git

Git和laradock

  • 安装Git
sudo apt install -y git
  • 新建目录
cd /mnt/d/ && sudo mkdir wwwroot && cd wwwroot
  • 克隆laradock项目
git clone https://github.com/Laradock/laradock.git
  • 创建.evn文件
cd laradock/ && cp .env.example .env
  • 替换PHP版本
sed -i 's/PHP_VERSION=7.3/PHP_VERSION=7.4/g' .env
  • 修改docker-compose.yml
### NGINX Server #########################################
    nginx:
      build:
        context: ./nginx
        args:
          - CHANGE_SOURCE=${CHANGE_SOURCE}
          - PHP_UPSTREAM_CONTAINER=${NGINX_PHP_UPSTREAM_CONTAINER}
          - PHP_UPSTREAM_PORT=${NGINX_PHP_UPSTREAM_PORT}
          - http_proxy
          - https_proxy
          - no_proxy
      volumes:
        - ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}${APP_CODE_CONTAINER_FLAG}
        - ${NGINX_HOST_LOG_PATH}:/var/log/nginx
        - ${NGINX_SITES_PATH}:/etc/nginx/sites-available
        - ${NGINX_SSL_PATH}:/etc/nginx/ssl
      ports:
        - "${NGINX_HOST_HTTP_PORT}:80"
        - "${NGINX_HOST_HTTPS_PORT}:443"
        - "${VARNISH_BACKEND_PORT}:81"
      depends_on:
        - php-fpm
+++     - redis
+++     - mysql
        
      networks:
        - frontend
        - backend
+++   restart: always
### PHP-FPM ##############################################
    php-fpm:
      build:
        context: ./php-fpm
        args:
          - CHANGE_SOURCE=${CHANGE_SOURCE}
          - BASE_IMAGE_TAG_PREFIX=${PHP_FPM_BASE_IMAGE_TAG_PREFIX}
          - LARADOCK_PHP_VERSION=${PHP_VERSION}
          - LARADOCK_PHALCON_VERSION=${PHALCON_VERSION}
          - INSTALL_BZ2=${PHP_FPM_INSTALL_BZ2}
          - INSTALL_ENCHANT=${PHP_FPM_INSTALL_ENCHANT}
          - INSTALL_GMP=${PHP_FPM_INSTALL_GMP}
          - INSTALL_GNUPG=${PHP_FPM_INSTALL_GNUPG}
          - INSTALL_XDEBUG=${PHP_FPM_INSTALL_XDEBUG}
          - INSTALL_PCOV=${PHP_FPM_INSTALL_PCOV}
          - INSTALL_PHPDBG=${PHP_FPM_INSTALL_PHPDBG}
          - INSTALL_BLACKFIRE=${INSTALL_BLACKFIRE}
          - INSTALL_SSH2=${PHP_FPM_INSTALL_SSH2}
          - INSTALL_SOAP=${PHP_FPM_INSTALL_SOAP}
          - INSTALL_XSL=${PHP_FPM_INSTALL_XSL}
          - INSTALL_SMB=${PHP_FPM_INSTALL_SMB}
          - INSTALL_IMAP=${PHP_FPM_INSTALL_IMAP}
          - INSTALL_MONGO=${PHP_FPM_INSTALL_MONGO}
          - INSTALL_AMQP=${PHP_FPM_INSTALL_AMQP}
          - INSTALL_CASSANDRA=${PHP_FPM_INSTALL_CASSANDRA}
          - INSTALL_GEARMAN=${PHP_FPM_INSTALL_GEARMAN}
          - INSTALL_MSSQL=${PHP_FPM_INSTALL_MSSQL}
          - INSTALL_BCMATH=${PHP_FPM_INSTALL_BCMATH}
          - INSTALL_PHPREDIS=${PHP_FPM_INSTALL_PHPREDIS}
          - INSTALL_MEMCACHED=${PHP_FPM_INSTALL_MEMCACHED}
          - INSTALL_OPCACHE=${PHP_FPM_INSTALL_OPCACHE}
          - INSTALL_EXIF=${PHP_FPM_INSTALL_EXIF}
          - INSTALL_AEROSPIKE=${PHP_FPM_INSTALL_AEROSPIKE}
          - INSTALL_OCI8=${PHP_FPM_INSTALL_OCI8}
          - INSTALL_MYSQLI=${PHP_FPM_INSTALL_MYSQLI}
          - INSTALL_PGSQL=${PHP_FPM_INSTALL_PGSQL}
          - INSTALL_PG_CLIENT=${PHP_FPM_INSTALL_PG_CLIENT}
          - INSTALL_POSTGIS=${PHP_FPM_INSTALL_POSTGIS}
          - INSTALL_INTL=${PHP_FPM_INSTALL_INTL}
          - INSTALL_GHOSTSCRIPT=${PHP_FPM_INSTALL_GHOSTSCRIPT}
          - INSTALL_LDAP=${PHP_FPM_INSTALL_LDAP}
          - INSTALL_PHALCON=${PHP_FPM_INSTALL_PHALCON}
          - INSTALL_SWOOLE=${PHP_FPM_INSTALL_SWOOLE}
          - INSTALL_TAINT=${PHP_FPM_INSTALL_TAINT}
          - INSTALL_IMAGE_OPTIMIZERS=${PHP_FPM_INSTALL_IMAGE_OPTIMIZERS}
          - INSTALL_IMAGEMAGICK=${PHP_FPM_INSTALL_IMAGEMAGICK}
          - INSTALL_CALENDAR=${PHP_FPM_INSTALL_CALENDAR}
          - INSTALL_FAKETIME=${PHP_FPM_INSTALL_FAKETIME}
          - INSTALL_IONCUBE=${PHP_FPM_INSTALL_IONCUBE}
          - INSTALL_APCU=${PHP_FPM_INSTALL_APCU}
          - INSTALL_CACHETOOL=${PHP_FPM_INSTALL_CACHETOOL}
          - INSTALL_YAML=${PHP_FPM_INSTALL_YAML}
          - INSTALL_RDKAFKA=${PHP_FPM_INSTALL_RDKAFKA}
          - INSTALL_GETTEXT=${PHP_FPM_INSTALL_GETTEXT}
          - INSTALL_ADDITIONAL_LOCALES=${PHP_FPM_INSTALL_ADDITIONAL_LOCALES}
          - INSTALL_MYSQL_CLIENT=${PHP_FPM_INSTALL_MYSQL_CLIENT}
          - INSTALL_PING=${PHP_FPM_INSTALL_PING}
          - INSTALL_SSHPASS=${PHP_FPM_INSTALL_SSHPASS}
          - INSTALL_MAILPARSE=${PHP_FPM_INSTALL_MAILPARSE}
          - INSTALL_PCNTL=${PHP_FPM_INSTALL_PCNTL}
          - ADDITIONAL_LOCALES=${PHP_FPM_ADDITIONAL_LOCALES}
          - INSTALL_FFMPEG=${PHP_FPM_FFMPEG}
          - INSTALL_AUDIOWAVEFORM=${PHP_FPM_AUDIOWAVEFORM}
          - INSTALL_WKHTMLTOPDF=${PHP_FPM_INSTALL_WKHTMLTOPDF}
          - INSTALL_XHPROF=${PHP_FPM_INSTALL_XHPROF}
          - INSTALL_XMLRPC=${PHP_FPM_INSTALL_XMLRPC}
          - INSTALL_PHPDECIMAL=${PHP_FPM_INSTALL_PHPDECIMAL}
          - INSTALL_ZOOKEEPER=${PHP_FPM_INSTALL_ZOOKEEPER}
          - DOWNGRADE_OPENSSL_TLS_AND_SECLEVEL=${PHP_DOWNGRADE_OPENSSL_TLS_AND_SECLEVEL}
          - PUID=${PHP_FPM_PUID}
          - PGID=${PHP_FPM_PGID}
          - IMAGEMAGICK_VERSION=${PHP_FPM_IMAGEMAGICK_VERSION}
          - LOCALE=${PHP_FPM_DEFAULT_LOCALE}
          - PHP_FPM_NEW_RELIC=${PHP_FPM_NEW_RELIC}
          - PHP_FPM_NEW_RELIC_KEY=${PHP_FPM_NEW_RELIC_KEY}
          - PHP_FPM_NEW_RELIC_APP_NAME=${PHP_FPM_NEW_RELIC_APP_NAME}
          - INSTALL_DOCKER_CLIENT=${PHP_FPM_INSTALL_DOCKER_CLIENT}
          - http_proxy
          - https_proxy
          - no_proxy
      volumes:
        - ./php-fpm/php${PHP_VERSION}.ini:/usr/local/etc/php/php.ini
        - ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}${APP_CODE_CONTAINER_FLAG}
        - docker-in-docker:/certs/client
      ports:
        - "${PHP_FPM_XDEBUG_PORT}:9003"
      expose:
        - "9000"
      extra_hosts:
        - "dockerhost:${DOCKER_HOST_IP}"
      environment:
        - PHP_IDE_CONFIG=${PHP_IDE_CONFIG}
        - DOCKER_HOST=tcp://docker-in-docker:2376
        - DOCKER_TLS_VERIFY=1
        - DOCKER_TLS_CERTDIR=/certs
        - DOCKER_CERT_PATH=/certs/client
        - FAKETIME=${PHP_FPM_FAKETIME}
+     # depends_on:
+     #   - workspace
      networks:
        - backend
+     # links:
+     #   - docker-in-docker
+++   restart: always
### MySQL ################################################
    mysql:
      build:
        context: ./mysql
        args:
          - MYSQL_VERSION=${MYSQL_VERSION}
      environment:
        - MYSQL_DATABASE=${MYSQL_DATABASE}
        - MYSQL_USER=${MYSQL_USER}
        - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
        - TZ=${WORKSPACE_TIMEZONE}
      volumes:
        - ${DATA_PATH_HOST}/mysql:/var/lib/mysql
        - ${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
      ports:
        - "${MYSQL_PORT}:3306"
      networks:
        - backend
+++   restart: always
  ### Redis ################################################
    redis:
      build: ./redis
      volumes:
        - ${DATA_PATH_HOST}/redis:/data
      ports:
        - "${REDIS_PORT}:6379"
      networks:
        - backend
+++   restart: always
  • 启动容器
sudo docker-compose up -d nginx
  • 配置域名,linux如果无效,请到C:\Windows\System32\drivers\etc\hosts 配置
sudo vi /etc/hosts
127.0.0.1       api.local
  • 配置Vhost文件
cd /mnt/d/wwwroot/laradock/nginx/sites && sudo cp laravel.conf.example api.local.conf
  • 修改配置文件
sed -i 's/laravel.test/api.local/g' api.local.conf
sed -i 's/laravel\/public/sz99-api\/public/g' api.local.conf
  • 平滑重启nginx
  cd /mnt/d/wwwroot/laradock
  sudo docker-compose exec nginx nginx -s reload
  • 测试PHP环境是否搭建成功,执行以下命令后访问 http://api.local 如果出现Hello Docker则成功
  sudo mkdir -p /mnt/d/wwwroot/sz99-api/public && cd /mnt/d/wwwroot/sz99-api/public
  echo "<php" > index.php && echo "echo 'Hello Docker';" >> index.php
  • 删除测试代码
  sudo rm -rf /mnt/d/wwwroot/sz99-api/public
  • 生成ssh秘钥 并将公钥添加到gitlab 生成 public key 文件可以看 [03.代码发布规范.md] 章节
  ssh-keygen -t ed25519 -N ''
  cat ~/.ssh/id_ed25519.pub
  • 拉取项目
  cd /mnt/d/wwwroot
  git clone git@git.sz99.com:root/sz99-api.git
  • Navicat连接mysql
 host:      mysql
 user:      root
 password : root
  • 新建数据库 .sql 可以看 [03.代码发布规范.md] 章节登陆研发机导出

  • 复制配置文件 修改数据库配置

  cd /mnt/d/wwwroot/sz99-api && cp env-example .env
  # MySQL 配置
  DB_CONNECTION=mysql
  DB_HOST=mysql
  DB_PORT=3306
  DB_DATABASE=sz99_api
  DB_USERNAME=root
  DB_PASSWORD=root
  • 访问 http://api.local 显示时间则大功告成

全剧终

  • 1
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值