docker-compose部署php项目实例详解(2)

3.配置default.conf文件

sudo vi /www/data/nginx/default.conf
server {

  listen    80;

  server_name localhost;

  #charset koi8-r;

  #access_log /var/log/nginx/host.access.log main;

  location / {

    root  /www/default;

    index index.php index.html index.htm;

  }

  #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      /www/default;

    fastcgi_pass  php7.2:9000;#php容器名或者php容器ip

    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;

  #}

}

4.修改mysql允许远程连接并新建数据库

docker exec -it mysql5 bash

mysql -u root -p

use mysql;

update user set host='%' where user='root';

flush privileges;

5.创建并且启动容器

docker-compose up -d

6.新建index.php测试mysql连接

<?php

$con = mysqli_connect("172.18.0.4", "root", "thisyourpassword", "shop");

if ($con) {

  echo '连接 MYSQL 成功';

} else {

  echo "连接 MySQL 失败: " . mysqli_connect_error();

} mysqli_close($con);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/5274.html