目录

 一、实验

1.环境

2.OpenEuler 部署 docker-compose

3.docker-compose 部署 LNMP

二、问题

1.ntpdate未找到命令

2.timedatectl 如何设置时区与时间同步

3.php网页显示时区不对


 

 

 一、实验

1.环境

(1)主机

表1  主机

系统架构版本IP备注
LinuxopenEuler22.03 LTS SP2

192.168.204.144(动态)

192.168.204.141(静态)

192.168.204.142(静态)

 

 docker25.0.3  
 docker-compose2.24.6  

(2)查看系统版本

[root@localhost ~]# cat /etc/os-release

e4d8bd1e19174f7ebec18a7c53fb3c2a.png

(3) 查看网络ip

[root@localhost ~]# ip addr

d78e9538efb44978898b7c27f1d7e222.png

(4) 查看docker版本

[root@localhost ~]# docker --version

d00ab0ec09264683a2035688aee10fcd.png

(5)查看docker进程

[root@localhost ~]# docker ps

309f51314e7e4dfab8472d60427f47c1.png

(6)查看端口

80 、3306、9000端口被docker容器占用

[root@localhost ~]# netstat -antlp

8742c53d1f0249649cead03b751f80fa.png

[root@localhost ~]# netstat -antlp |grep docker-proxy

46c090b2420a4d1086b3df2359e061bd.png

(7)删除所有容器

[root@localhost ~]# docker rm -f $(docker ps -aq)

2154460fe31c4853b8c656f45b417b6f.png

再次查看

[root@localhost ~]# docker ps

4c4b4eea8c4b49dbb9f8888b42786362.png

 

2.OpenEuler 部署 docker-compose

(1) 官网

Releases · docker/compose · GitHub

(2)下载

wget https://github.com/docker/compose/releases/download/v2.24.6/docker-compose-linux-x86_64

(3) 移动赋权

[root@localhost ~]# sudo mv docker-compose-linux-x86_64 /usr/bin/docker-compose
[root@localhost ~]#  sudo chmod +x /usr/bin/docker-compose

71d16c0e713a4dfc9c4d474862da0dd7.png

(4)查看版本

[root@localhost ~]# docker-compose version

771de0f9812f4edba476aa2eb6d96d1e.png

 

3.docker-compose 部署 LNMP

(1)查看docker镜像

[root@localhost ~]# docker images

f449a55cbf974ec983cf799bd66a4654.png

(2)创建挂载目录


[root@localhost ~]# mkdir -p /docker/mysql/db /docker/www /docker/nginx/conf.d

[root@localhost ~]# cd /docker/ ; ls

40662f67ce6741efb198eac3d883bd62.png

(3)编辑配置文件

[root@localhost docker]# vim /docker/docker-compose.yml

dc832451850349f68e4981041d200226.png

version: "3.9"
services:
  mysql:
    restart: always
    image: mysql:8.0.35
    container_name: mysql
    command:
      --default-authentication-plugin=mysql_native_password
      --default-time-zone=+08:00
    environment:
      TZ: PRC
      MYSQL_ROOT_PASSWORD: Root_321
    ports:
      - "3306:3306"
    volumes:
      - /docker/mysql/db:/var/lib/mysql

  php-fpm:
    restart: always
    image: bitnami/php-fpm
    container_name: php-fpm
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - /docker/www:/usr/share/nginx/html
    depends_on:
      - mysql

  nginx:
    image: nginx:latest
    restart: always
    container_name: nginx
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 80:80
      - 443:443
    volumes:
      - /docker/nginx/conf.d/:/etc/nginx/conf.d
      - /docker/www:/usr/share/nginx/html
    depends_on:
      - php-fpm
~

10790c4a202347e4b9480c77ea1deeb3.png

(3)查看目录

[root@localhost docker]# ls
docker-compose.yml  mysql  nginx  www

bfdc879d658c46688b3df513fb70d21e.png

(4)添加nginx配置文件

[root@localhost docker]# cd nginx/conf.d/
[root@localhost conf.d]# ls
[root@localhost conf.d]# vim default.conf

33980d6e75f4429c8b76c1754097ef3a.png

server{
    listen 80;
    server_name localhost;
    location / {
        root /usr/share/nginx/html;
        index index.html index.htm index.php;
    }

    location ~ \.php$ {
        root /usr/share/nginx/html;
        fastcgi_pass php-fpm:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        include fastcgi_params;
    }
 }

4598357e8e7d4a5aad47d58bfae996ca.png

(5)查看目录

[root@localhost docker]# tree /docker

3efaba2f6d524577bd4f9b3a92002885.png

[root@localhost docker]# tree /docker -s

ed72ad801278427b87310e02d66ecd98.png

(6)运行

-d 在后台运行容器,没有提示信息

[root@localhost docker]# docker-compose up -d

1eba717052b240e0951b97e2666211e8.png

(7)查看进程

[root@localhost docker]# docker ps
CONTAINER ID   IMAGE             COMMAND                   CREATED          STATUS          PORTS                                                                      NAMES
0194b5301702   nginx:latest      "/docker-entrypoint.…"   47 seconds ago   Up 45 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   nginx
388b48d024e3   bitnami/php-fpm   "php-fpm -F --pid /o…"   47 seconds ago   Up 46 seconds   9000/tcp                                                                   php-fpm
3250c88c5e04   mysql:8.0.35      "docker-entrypoint.s…"   47 seconds ago   Up 46 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp                       mysql

de3a88914dee4ab495b20946780658c1.png

(8)访问

http://192.168.204.141

目前为空

189bb354788841c680676c47c61bdcde.png

(9)创建index.php

[root@localhost docker]# cd www/
[root@localhost www]# ls
[root@localhost www]# vim index.php

dd2fc44753fe48dea86ac5cb71dda661.png

<?php
phpinfo();

4ee592c9056846ec8fd9e37117de92d6.png

(10)再次访问

http://192.168.204.141

7ebfbfd03a4a4ad09c95a9f19646518e.png

(11)创建数据库连接db.php

[root@localhost www]# sudo vim /docker/www/db.php

2eb32c355d1a42209ee2dc03a06ac0ff.png

<?php
date_default_timezone_set("PRC");
#输出php版本号
echo PHP_VERSION;
printf("<br>");
echo date('Y-m-d H:i:s');
printf("<br>");
/*循环格式化输出*/
for ($i = 1; $i <= 5; $i++) {
    printf("<h3>%03d</h3>", $i);
}
//---pdo 连接数据库
$dsn = 'mysql:host=mysql;dbname=mysql';
$username = 'root';
$password = 'Root_321';
try {
    $db = new PDO($dsn, $username, $password);
    // 设置错误模式为异常
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "成功连接到数据库";
} catch (PDOException $e) {
    echo "连接数据库失败: " . $e->getMessage();
} catch (Exception $e) {
    echo "系统未知异常:" . $e->getMessage();
}
$st = $db->query("show databases");
while ($row = $st->fetch(3)) {
    echo $row[0] . '<br>';
}

b5e5d372ca594f5f8687a9cdbbcffcc7.png

(12) 访问

http://192.168.204.141/db.php

a8a32b2197984227858459f43f49ac1f.png

(13)删除项目

[root@localhost docker]# docker-compose rm -f -s

c2b03a154a0a49ccb00bf48efbebe71b.png

(14)查看进程

目前为空

[root@localhost docker]# docker ps

4b32b87f1dea4ce8a8cfb27721cc7a9b.png

 

二、问题

1.ntpdate未找到命令

(1)报错

-bash: ntpdate:未找到命令

01527d0e664c4141b1e3a9e538791f0f.png

(2)原因分析

未安装ntp

(3)解决方法

[root@localhost www]# dnf install ntp -y

537cdd8778014b3588d45c70a41567be.png

成功:

[root@localhost www]# ntpdate cn.pool.ntp.org

17ed09a8c1824a818c4d34764128d027.png

 

2.timedatectl 如何设置时区与时间同步

(1)设置时区

#设置时区
[root@localhost www]# timedatectl set-timezone Asia/Shanghai

#查看
[root@localhost www]# timedatectl

722c15f690f2474b94bc9588d2dcb35b.png

(2)设置时间同步

#开启同步
[root@localhost www]# timedatectl set-ntp true

#关闭同步
[root@localhost www]# timedatectl set-ntp false

#查看
[root@localhost www]# timedatectl

bc4984720b0841179febdb360f239ffc.png

 

3.php网页显示时区不对

(1)报错

f070438620564d2c9d68f2e8efd2958f.png

(2)原因分析

php文件未设置时区

(3)解决方法

修改db.php文件

添加如需代码:

date_default_timezone_set("PRC");

277b8bdb7c3c47bf980aad6adc3fd255.png

成功:

a8a32b2197984227858459f43f49ac1f.png

 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐