Docker 再探 — Win10 使用 docker-compose 搭建 lnmp 开发环境(2/3)
接上次使用 docker-compose 安装完成 nginx 之后,这次使用 docker-compose 安装 php 与 mysql,并与 nginx 做到联动!
docker-compose 安装 php8.0.1-fpm
首先依然是把 php 包 Pull 下来
docker pull php8.0.1-fpm
然后编写之前的docker-compose.yml
文件。
需要注意的是 php 的 volumes 目录和 nginx 的代码目录需要保持一致,不然会找不到文件,这里也是有点纳闷。。。
version: "3" services: web1: container_name: test_com_nginx image: nginx privileged: true restart: always networks: - test_com_network ports: - 80:80 - 443:443 volumes: - C:\Users\Administrator\code\docker\nginx:/etc/nginx/conf.d - C:\Users\Administrator\code\www:/usr/share/nginx/html environment: - NGINX_HOST=test.com - NGINX_PORT=80 depends_on: - php8 php8: container_name: test_com_php image: php:8.0.1-fpm privileged: true restart: always volumes: - C:\Users\Administrator\code\www:/usr/share/nginx/html environment: - TZ=Asia/Shanghai networks: - test_com_network expose: - "9000" networks: test_com_network: name: test_com_network
修改 default.conf 加入 php 文件的 location 块
server { listen 80; server_name test.com; root /usr/share/nginx/html; location / { index index.html index.php index.htm; } error_page 500 502 503 504 /50x.html; #注意:fastcgi_pass 链接名需要填写 yml 文件中 php 块的 container_name location ~ \.php$ { fastcgi_pass test_com_php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $DOCUMENT_ROOT$fastcgi_script_name; include fastcgi_params; } }
接着我们在项目目录(www)中写入一个 php 文件(info.php)
<?php phpinfo(); ?>
然后启动该项目
docker-compose run -f .\docker-compose.yml
访问http://test.com/info.php
docker 的 php 安装完成!
docker-compose 安装 mysql8
1.创建一个 mysql 目录,该目录下有 conf 和 data 两个目录分别用来存放 mysql 配置文件和 data 文件
mkdir mysql cd mysql mkdir conf mkdir data
2.继续编辑我们的docker-compose.yml
文件
mysql8: container_name: test_com_mysql image: mysql privileged: true restart: always volumes: - C:\Users\Administrator\code\docker\mysql\conf:/etc/mysql/conf.d - C:\Users\Administrator\code\docker\mysql\data:/var/lib/mysql ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: 123456 MYSQL_DATABASE: test_com MYSQL_USER: user MYSQL_PASSWORD: user123 networks: - test_com_network expose: - "3306"
3.执行docker-compose up
等待初始化 mysql(初始化启动较慢)
初始化完毕之后可以执行如下命令进入 docker 里的 mysql
docker run -it --rm --network test_com_network mysql mysql -hmysql8 -uroot -p
PS C:\Users\Administrator> docker run -it --rm --network test_com_network mysql mysql -hmysql8 -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.0.22 MySQL Community Server - GPL Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test_com | +--------------------+ 5 rows in set (0.01 sec) mysql>
至此 mysql8 安装完毕!
php 连接 mysql
1.首先需要在 docker-compose 的 php 下加一个 links
links: - mysql8
在 www 目录下创建一个mysql.php
<?php function test_conn_mysql(){ $path = "mysql:dbname=mysql;host=mysql8;port=3306"; $username = "root"; $password = "123456"; try{ $db = new PDO($path,$username,$password); }catch(PDOException $e){ die("Could not connect to the database:".$e); } $sql = "select * from user"; $res = $db->prepare($sql); $res->execute(); $result = $res->fetchAll(); print_r($result); } test_conn_mysql(); ?>
浏览器地址访问http://test.com/mysql.php
发现连接不上 mysql,错误日志为没有该 diver,果然,php 没有 pdo_mysql 扩展,可以说很多扩展都没有
安装之~
docker php 安装 pdo_mysql 扩展
执行如下命令进入 php 环境终端
docker exec -it test_com_php /bin/bash
安装 pdo_mysql 扩展
docker-php-ext-install pdo_mysql
退出终端
exit
重启 php
docker restart test_com_php
然后再访问 mysql.php
Array ( [0] => Array ( [Host] => % [0] => % [User] => root [1] => root [Select_priv] => Y [2] => Y [Insert_priv] => Y [3] => Y [Update_priv] => Y [4] => Y [Delete_priv] => Y [5] => Y [Create_priv] => Y ... ...
已经可以连接上 mysql 了,至此 nginx,php 和 mysql 都已安装成功,下面就来安装其他一些软件,并对 lnmp 环境做出更个性化定制,毕竟还需要安装很多 php 的扩展!