docker 类似虚拟机,但不是虚拟机。
首先在你的服务器上安装docker,以我的服务器ubuntu server系统为例安装docker
sudo apt-get install docker.io
安装docker-compose,用以快速部署多个docker
sudo apt-get install docker-compose
由于docker的仓库在国外,对国内来说速度太慢,我们需要安装国内的加速器。
在 /etc/docker/daemon.json 中写入如下:
{"registry-mirrors":["https://docker.mirrors.ustc.edu.cn/"]}
然后重启服务
sudo systemctl daemon-reloadsudo systemctl restart docker
到此,我们的docker就完全安装完毕了。
开始安装web套件
创建一个名为www的文件夹,里面的文件结构如下图:(tree命令)
docker-compose.yml 文件内容如下:
version: '3'services: nginx: image: nginx:1.21 restart: always ports: - "80:80" links: - "php" volumes: - ./nginx/conf/default.conf:/etc/nginx/conf.d/default.conf - ./html/:/usr/share/nginx/html/ container_name: "nginx-1" php: build: ./php restart: always expose: - "9000" links: - "mysql" volumes: - ./html/:/var/www/html/ container_name: "php-1" mysql: image: mysql:8 restart: always command: --default-authentication-plugin=mysql_native_password expose: - "3306" volumes: - ./mysql/data/:/var/lib/mysql/ environment: MYSQL_ROOT_PASSWORD: admin123123 container_name: "mysql-1"
上面的内容设定了3个容器服务,注意格式缩进,冒号:后面有一个空格。基于I386/amd64.
如果是arm64平台,mysql的镜像需要修改为如下
image: arm64v8/mysql:oracle
default.conf文件的内容如下:
server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; # 代码目录 index index.html index.htm index.php; # 增加index.php } error_page 404 /404.html; location = /40x.html { root /usr/share/nginx/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; } location ~ \.php$ { root /var/www/html; # 代码目录 fastcgi_pass php:9000; # compose文件中nginx段设置的links的php名称 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 修改为$document_root include fastcgi_params; }}
上面内容是nginx的配置信息
dockerfile文件的内容如下:
FROM php:7.3-fpmRUN mv /etc/apt/sources.list /etc/apt/sources.list.bak \&& echo "deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib" >/etc/apt/sources.list \&& echo "deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib" >>/etc/apt/sources.list \&& echo "deb http://mirrors.aliyun.com/debian-security/ bullseye-security main" >>/etc/apt/sources.list \&& echo "deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main" >>/etc/apt/sources.list \&& echo "deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >>/etc/apt/sources.list \&& echo "deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >>/etc/apt/sources.list \&& echo "deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >>/etc/apt/sources.list \&& echo "deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >>/etc/apt/sources.listRUN apt-get update && apt-get install -y libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev libzip-devRUN docker-php-ext-install pdo pdo_mysql mysqli bcmath exif zipRUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \&& docker-php-ext-install gd
上面内容是构建容器的详细命令,其中更换aliyun的源和安装了一些PHP扩展,如 bcmath exif zip gd。
然后cd到www目录,只需要执行
docker-compose up -d
复制代码
-d为后台启动。
程序开始自动下载docker 镜像并按照我们docker-compose.yml中设置的参数启动容器。
nginx+php+mysql有了。
找一个PHP网站的源码,放到www/html目录内,然后用chown命令修改www-data权限。
cd www/htmlchown -R www-data:www-data *
通过docker-compose.yml文件的信息我们知道,安装时,mysql服务器的地址直接填mysql,密码为admin123123.
OK,网站搭建完毕。
来源地址:https://blog.csdn.net/tengboy_top/article/details/126550816