win 系统使用 docker 快速搭建 centos 8 的lnmp
win 10 系统使用 docker 快速搭建 centos 8 的 php7.4 和 mysql 完整开发环境和本系列的另外几篇文章差异:这里直接给出了 docker 下的构建文件。本次没有使用 docker-compose 工具,只用了最简单的单个镜像和几个简单配置,但效果还不错!因为主要使用了阿里的镜像,构建速度超级快~各个软件版本:centos 8php 7.4mysql 8nginx 1
win 10 系统使用 docker 快速搭建 centos 8 的 php7.4 和 mysql 完整开发环境
和本系列的另外几篇文章差异:这里直接给出了 docker 下的构建文件。
本次没有使用 docker-compose 工具,只用了最简单的单个镜像和几个简单配置,但效果还不错!
因为主要使用了阿里的镜像,构建速度超级快~
各个软件版本:
centos 8
php 7.4
mysql 8
nginx 1.18,现在的稳定版。
redis 5 ,(新的稳定版是6,不讲究了)
git 2.18
主要的 Dockerfile 文件内容:
FROM centos:8
ENV TZ Asia/Shanghai
WORKDIR /root
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo \
&& rm -f /etc/yum.repos.d/CentOS-centosplus.repo \
&& rm -f /etc/yum.repos.d/CentOS-PowerTools.repo \
&& rm -f /etc/yum.repos.d/CentOS-Extras.repo \
&& rm -f /etc/yum.repos.d/CentOS-AppStream.repo \
&& dnf makecache \
&& dnf install -y epel-release \
&& sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel* \
&& sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel* \
&& dnf makecache \
&& dnf install -y https://mirrors.aliyun.com/remi/enterprise/remi-release-8.rpm \
&& sed -i 's/https*:\/\/rpms.remirepo.net/https:\/\/mirrors.aliyun.com\/remi/g' /etc/yum.repos.d/remi* \
&& sed -i 's/#baseurl/baseurl/g' /etc/yum.repos.d/remi* \
&& sed -i 's|^mirrorlist|#mirrorlist|' /etc/yum.repos.d/remi* \
&& dnf makecache \
&& dnf install -y yum-utils \
&& dnf install -y php74 php74-php-devel php74-php-fpm php74-php-mbstring php74-php-memcache \
&& dnf install -y php74-php-memcached php74-php-redis php74-php-mysqlnd php74-php-pdo php74-php-bcmath php74-php-xml \
&& dnf install -y php74-php-gd php74-php-gmp php74-php-igbinary php74-php-imagick php74-php-mcrypt php74-php-pdo_mysql \
&& dnf install -y php74-php-posix php74-php-simplexml php74-php-opcache php74-php-xsl php74-php-xmlwriter php74-php-xmlreader php74-php-swoole \
&& dnf install -y php74-php-zip php74-php-phalcon php74-php-yaml php74-php-yar php74-php-yaf php74-php-uuid
RUN ln -s /usr/bin/php74 /usr/bin/php \
&& curl -o /usr/local/bin/composer https://mirrors.aliyun.com/composer/composer.phar \
&& chmod +x /usr/local/bin/composer \
&& composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ \
&& echo $'[nginx-stable] \n\
name=nginx stable repo \n\
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ \n\
gpgcheck=1 \n\
enabled=1 \n\
gpgkey=https://nginx.org/keys/nginx_signing.key \n\
module_hotfixes=true \n'\
> /etc/yum.repos.d/nginx.repo \
&& dnf makecache \
&& dnf install -y nginx \
&& systemctl enable nginx \
&& systemctl enable php74-php-fpm \
&& sed -i 's/user\ =\ apache/user\ =\ nginx/g' /etc/opt/remi/php74/php-fpm.d/www.conf \
&& sed -i 's/group\ =\ apache/group\ =\ nginx/g' /etc/opt/remi/php74/php-fpm.d/www.conf \
&& sed -i 's/listen\ =\ \/var\/opt\/remi\/php74\/run\/php-fpm\/www.sock/listen=9000/g' /etc/opt/remi/php74/php-fpm.d/www.conf \
&& dnf install -y git wget vim redis zip unzip p7zip rsync crontabs supervisor net-tools python3 \
&& systemctl enable redis
RUN ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone \
&& dnf install -y langpacks-zh_CN \
&& sed -i 's/en_US/zh_CN/g' /etc/locale.conf \
&& dnf install -y @mysql \
&& sed -i 's/\/var\/lib\/mysql\/mysql.sock/\/tmp\/mysql.sock/g' /etc/my.cnf.d/mysql-server.cnf \
&& systemctl enable mysqld
CMD "/usr/sbin/init"
安装 Docker Desktop 和 git
网上有很多教程,需设置一下镜像。但不设置也没关系。
镜像有很多,如 hub-mirror.c.163.com
windows 系统 安装 git 到 git 官网下载即可。
git-scm.com/downloads
用 git 下载构建文件到本地。
假设我想下载到本机 D 盘的 centos8 目录。
d:
cd \
git clone https://github.com/xieye114/centos8_php74.git centos8
构建镜像,启动容器
cd centos8\build
# 构建镜像,第一次大约2分钟。因为尽量使用了阿里的镜像,速度超级快。
build
# 启动容器前需修改 up.bat,只需改这一次
# 一样就不用改
# 必须 window 的换行符结尾,不能 linux 换行符,因为这是bat文件。
set mypath=d:/temp/centos8_php74
set path2=d:\temp\centos8_php74
# 改成
set mypath=d:/centos8
set path2=d:\centos8
# 启动容器,右下角docker会不停弹窗,让你确认,点击是,共享。
# 如 up 失败,就先执行 down,再 up
up
进入容器
exec
cd /code
# 安装一个laravel 8 的项目。
composer create-project --prefer-dist laravel/laravel blog "8.*"
# 安装结束后(大约1分钟多),按住CTRL+d 可以退出容器。
测试安装结果
把 d:\centos8\code\web.php 拷贝到 d:\centos8\code\blog\routes\web.php, 覆盖之。
打开浏览器。输入 localhost
自行点击 测试 redis 安装,和 mysql 安装。
如果成功,会各自显示安装成功的字样。
退出容器。
如不想使用了,进入 build 目录,执行 down 命令。
每次关电脑前最好关闭容器。
mysql 的数据存放
其中 mysql 的数据都保存在 windows 上,就算容器关闭,下次再打开,数据依然保留。
nginx 的配置修改。
修改 nginx_config 里的配置文件后,只需进入容器,systemctl restart nginx ,即可生效。
共享目录
其实可以事先在 docker Desktop 里设置共享目录,只需设置 d:\centos8 即可,不设置也没关系,弹窗同意也行。
常用命令
build 是日常维护目录,进入此目录可以执行以下命令。
build 构建镜像
remove 删除镜像
up 启动容器(会自动创建)
down 关闭容器
exec 进入容器
按住CTRL+d 可以退出容器。
更多推荐
所有评论(0)