拉取官方php8.0镜像
docker pull php:8.0-fpm
// --name php8 将php的容器命名为php8
// ~/Desktop/wwwroot/:/www 本地~/Desktop/wwwroot/目录映射至容器/www目录
// -d 后台运行
docker run --name php8 -v ~/Desktop/wwwroot/:/www -d php:8.0-fpm
拉取官方最新nginx镜像
docker pull nginx:latest
创建本地nginx配置目录
~/Desktop/workspacce/docker/nginx/conf
在上面配置目录里创建demo.conf
server {
listen 8801;
server_name localhost;
location / {
root /usr/share/nginx/html/demo;
index index.html index.htm index.php;
}
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/demo/$fastcgi_script_name;
include fastcgi_params;
}
}
// -p 8801:8801: 端口映射,把 nginx 中的 8801 映射到本地的 8801 端口。前面是本地端口
// ~/Desktop/wwwroot: 是本地www的存储目录,/usr/share/nginx/html 是容器内 html 文件的存储目录。
// ~/Desktop/workspacce/docker/nginx/conf: 是上面创建的本地 nginx 配置文件的存储目录,/etc/nginx/conf.d 是容器内 nginx 配置文件的存储目录。
// --link php8:php: 把 php8 的网络并入 nginx,并通过修改 nginx 的 /etc/hosts,把域名 php 映射成 127.0.0.1,让 nginx 通过 php:9000 访问 php-fpm。
docker run --name nginx-php8 -p 8801:8801 -d \
-v ~/Desktop/wwwroot:/usr/share/nginx/html:ro \
-v ~/Desktop/workspacce/docker/nginx/conf:/etc/nginx/conf.d:ro \
--link php8:php \
nginx
扩展:
安装composer
进入php8容器
docker exec -it php8 /bin/bash
安装composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
composer install 可能会出现错误提示 ”Failed to download symfony/polyfill-mbstring from dist: The zip extension and unzip/7z commands are both missing, skipping.“
执行以下命令 或者 自行安装git
apt-get update
apt-get install zip unzip
安装php-redis扩展
打开官方php redis扩展页面,选择合适的redis源码扩展包
https://pecl.php.net/package/redis
curl -O https://pecl.php.net/get/redis-5.3.4.tgz
tar xfz redis-5.3.4.tgz
mv redis-5.3.4 /usr/src/php/ext/redis
docker-php-ext-install redis
使用php -m可查看redis扩展是否安装成功
装完扩展后需要重启php容器
docker restart php8
扩展:
pdp-mysql 直接执行 docker-php-ext-install pdo pdo_mysql就行
docker容器内使用本机的mysql,redis等host为 host.docker.internal