1:安装docker
sudo apt install docker.io
2:安装mysql
sudo docker run --privileged=true -di --restart=always --name MySqlForPhp01 -p 55506:3306 -e MYSQL_ROOT_PASSWORD=1234qwer -e --character-set-server=utf8mb4 -e --collation-server=utf8mb4_unicode_ci mysql:8.0.30
3:安装php
PHP的几种运行模式cli、fpm、apache、zts比较:https://tongfu.net/home/35/blog/513287.html#4.
Docker的几种精简版本Buster、Alpine、Stretch比较:https://www.tongfu.net/home/35/blog/513289.html
pdo连接mysql数据库(简洁明了) :https://blog.csdn.net/m_nanle_xiaobudiu/article/details/79250344
sudo docker pull php:7.4.33-apache-buster
#运行一个测试容器复制配置文件
sudo docker run -itd --privileged=true --name MyPhpTest -v /opt/Docker/project:/var/www/html php:7.4.33-apache-buster
sudo mkdir -p /opt/Docker/project/docker/etc && sudo docker cp MyPhpTest:/etc/apache2 /opt/Docker/project/docker/etc/apache2 && sudo chmod -R 777 /opt/Docker/project/docker/etc/
#修改ports.conf
Listen 80
Listen 8855
Listen 8866
#修改sites-available/000-default.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html/public"
ServerName crp.com
AddHandler fcgid-script .php
<Directory "/var/www/html/public">
Options FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
DirectoryIndex crp.php index.php index.html error/index.html
</Directory>
</VirtualHost>
sudo docker run -itd --privileged=true --restart=always --name MyPhpForTest01 -p 56680:80 -p 8855:8855 -p 8866:8866 -v /opt/Docker/project:/var/www/html -v /opt/Docker/project/docker/etc/apache2:/etc/apache2 --link MySqlForPhp01 php:7.4.33-apache-buster
#进入php容器中
sudo docker exec -it MyPhpForTest01 /bin/bash
#安装扩展pdo_mysql、mysqli和pdo
docker-php-ext-install pdo_mysql && docker-php-ext-install mysqli && docker-php-ext-install pdo
#安装扩展gd和zip
apt update && apt install zlib1g-dev libpng-dev libzip-dev libfreetype6-dev libjpeg62-turbo-dev libpng-dev
docker-php-ext-install gd --with-freetype --with-jpeg && docker-php-ext-install -j$(nproc) gd && docker-php-ext-install zip
#重启php容器中
sudo docker restart MyPhpForTest01
sudo docker commit MyPhpForTest01 php:20230907x86_64
sudo docker save -o php20230907x86_64.docker php:20230907x86_64
http://127.0.0.1:56680/index.php
bdm项目
sudo docker pull php:7.1.33-apache-buster
sudo docker run --privileged=true -di --restart=always --name MySqlForBdm01 -p 59906:3306 -e MYSQL_ROOT_PASSWORD=1234qwer -e --character-set-server=utf8mb4 -e --collation-server=utf8mb4_unicode_ci mysql:8.0.30
sudo docker run -itd --privileged=true --restart=always --name MyPhpForBdm01 -p 56680:80 -v /opt/Docker/project:/var/www/html -v /opt/Docker/project/docker/etc/apache2:/etc/apache2 --link MySqlForBdm01 php:7.1.33-apache-buster
#进入php容器中
sudo docker exec -it MyPhpForBdm01 /bin/bash
#安装扩展pdo_mysql、mysqli和pdo
docker-php-ext-install pdo_mysql && docker-php-ext-install mysqli && docker-php-ext-install pdo
#安装扩展gd和zip
apt update && apt install zlib1g-dev libpng-dev libzip-dev libfreetype6-dev libjpeg62-turbo-dev libpng-dev
docker-php-ext-configure gd --with-freetype --with-jpeg && docker-php-ext-install -j$(nproc) gd && docker-php-ext-install zip
#重启php容器中
sudo docker restart MyPhpForBdm01
sudo docker commit MyPhpForBdm01 bdm:20230911x86_64
sudo docker save -o bdm20230911x86_64.docker bdm:20230911x86_64
测试
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>显示系统时间</title>
<style>
/* 将系统时间渲染到div盒子中 */
div {
margin: 100px auto;
width: 300px;
height: 40px;
border: 1px solid skyblue;
text-align: center;
line-height: 40px;
}
</style>
</head>
<body>
<!-- 创建一个div -->
<div></div>
<script>
// 获取div
const div = document.querySelector('div')
// 获取系统时间的函数
function getNowDate() {
// 获取日期对象
const date = new Date()
// 获取系统当前的小时,分钟,秒,分别赋值给:h , m , s 三个变量
let h = date.getHours()
let m = date.getMinutes()
let s = date.getSeconds()
// 实现输出格式为: xx:xx:xx (时:分:秒)
h = h < 10 ? '0'+h :h
m = m < 10 ? '0'+m :m
s = s < 10 ? '0'+s :s
return `今天是:${date.getFullYear()}年${date.getMonth()+1}月${date.getDate()}日 ${h}:${m}:${s}` //getMonth() 获取月份取值从0开始,故实际月份要加1
}
// 先调用一次,刷新页面直接就会显示系统时间,否则会有延迟一秒钟的等待。
div.innerHTML = getNowDate()
// 设置时间间隔,1000毫秒执行一次,也就是一秒执行一次。
setInterval(function(){
// 调用函数,将函数返回值写入到div标签中
div.innerHTML = getNowDate()
},1000)
</script>
</body>
</html>
http://127.0.0.1:56680/test.html
phpinfo.php
<?php
phpinfo();
?>
http://127.0.0.1:56680/phpinfo.php
mysqli_test.php
<?php
$DB_USER = "root";
$DB_PASS = "1234qwer";
$link = new mysqli('172.17.0.1:55506', $DB_USER, $DB_PASS, 'mysql');
if($link->connect_error) {
die("连接失败: ".$link->connect_error);
}
$sql="select * from user;";
$res=$link->query($sql);
$data=$res->fetch_all();
var_dump($data);
?>
http://127.0.0.1:56680/mysqli_test.php
PDO_test.php
<?php
$servername = "MySqlForPhp01";
$username = "root";
$password = "1234qwer";
try
{
$pdo = new PDO("mysql:host=$servername;dbname=mysql", $username, $password);
echo "success!\n";
//查询操作
$stmt = $pdo->query("select * from user;");
$data = $stmt->fetch(2);
var_dump($data);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
http://127.0.0.1:56680/PDO_test.php
标签:&&,--,sudo,坏境,install,mysql,apache,docker,php
From: https://www.cnblogs.com/yuanhaoblog/p/17856723.html