首页 > 其他 > 详细

系统综合实践第三次实践

时间:2020-05-09 01:23:16      阅读:55      评论:0      收藏:0      [点我收藏+]

1、先安装docker-compose,安装后验证

技术分享图片

 

 

 2、编写框架环境中的Dockerfile

编写依照上节课的内容,但由于这次目的在于实现框架的连接搭建,所以并不用在用在Dockerfile上写太多特制。

FROM mysql

#MAINTAINER IFORMATION
MAINTAINER jayer@xiajibaxie.com
FROM nginx

#MAINTAINER IFORMATION
MAINTAINER jayer@xiajibaxie.com
FROM php:7.4-fpm

#MAINTAINER IFORMATION
MAINTAINER jayer@xiajibaxie.com

#Configure PHP Core Extensions
RUN apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev && docker-php-ext-configure gd --with-freetype --with-jpeg && docker-php-ext-install -j$(nproc) gd  

文件目录

技术分享图片不明白为何挂载mysql下的data的时候多出那么多文件

配置文件

index.php——执行PHP脚本指令的文件

index.html——网页文件

default.conf——nginx下的配置文件,需要进行配置来连接nginx和php。

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/nginx-2/html;
        index  index.html  index.htm  index.php;
    }

    #error_page  404              /404.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;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
     location ~ \.php$ {
         root           /usr/nginx-2/php; #php容器下的工作路径
         fastcgi_pass   lnmp_php-container:9000; #container name
         fastcgi_index  index.php; 
         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 
         include        fastcgi_params;
     }

    # deny access to .htaccess files, if Apache‘s document root
    # concurs with nginx‘s one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

  其中重点在于加入location ~ \.php${}的匹配信息(大致了解就是nginx在匹配路径文件时,如果后缀是php文件,即向容器端口去发送请求,从而得到运行php容器中相应路径下的index.php文件)

  同时了解PHP的也应该知道,该脚本语言与HTML是兼容的。

  前面3个root、fastcgi_pass、fastcgi_index容易理解,后面两个就不明白为啥了(直接写上就完事)

3、使用docker-compose.yml——可以创建相互依赖的镜像容器,从而搭建成可互通的框架。

version: "3"
services:
 nginx:
   image: lnmp_nginx
   container_name: lnmp_nginx-container
   build: ./nginx
   ports:
     - "80:80"
   links:
     - "php"
   volumes:
     - ./nginx/html/:/usr/nginx-2/html/
     - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
 php:
   image: lnmp_php
   container_name: lnmp_php-container
   build: ./php
   ports:
     - "9000:9000"
   volumes:
     - ./php/phpfile/:/usr/nginx-2/php/
   links:
     - "mysql"
   stdin_open: true
   tty: true
 mysql:
   image: lnmp_mysql
   container_name: lnmp_mysql-container
   build: ./mysql
   ports:
     - "3306:3306"
   volumes:
     - ./mysql/data/:/var/lib/mysql/
   environment:
     MYSQL_ROOT_PASSWORD : 123

  这里用到的几个基本的就是,image和container_name定义镜像名与容器名,build相对路径下寻找Dockerfile定制镜像,ports端口映射。

  volumes挂载数据卷或环境(稍微了解一下就是将主机的某个文件或路径补充到容器下的文件路径之后,所体现就是形成同一文件环境,主机内修改文件信息可永久性反馈到容器内)

  ...

构建成功

技术分享图片

4、服务测试

1、nginx与php

<?php phpinfo();?>

  技术分享图片  说明端口访问PHP环境成功

 

  

2、数据库连接

  搭建完之后PHP没有mysqli,有pdo但不支持mysql,于是就去安装一下呗

  技术分享图片技术分享图片

 

  写index.php执行php文件

<?php
$servername = "lnmp_mysql-container";
$username = "root";
$password = "123";
 
// 创建连接
$conn = new mysqli($servername, $username, $password);
 
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
echo "连接成功";
?>

  结果:

技术分享图片

 

系统综合实践第三次实践

原文:https://www.cnblogs.com/jay-home/p/12853554.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!