如何用docker搭建php环境

要使用 Docker 搭建 PHP 环境,可以按照以下步骤进行:

1. 安装 Docker

首先,确保你的系统上已经安装了 Docker。如果没有安装,可以参考 [Docker 官方文档](https://docs.docker.com/get-docker/) 进行安装。

2. 拉取 PHP 和 Nginx 镜像

使用以下命令拉取最新的 PHP 和 Nginx 镜像:

```sh

docker pull php:7.4-fpm

docker pull nginx:latest

```

3. 创建项目目录

创建一个目录来存放你的项目文件和 Docker 配置文件:

```sh

mkdir -p ~/docker/php

mkdir -p ~/docker/nginx/conf

mkdir -p ~/docker/nginx/www

```

4. 配置 Nginx

在 `~/docker/nginx/conf` 目录下创建一个 Nginx 配置文件 `default.conf`,内容如下:

```nginx

server {

   listen 80;

   server_name localhost;

   root /var/www/html;

   index index.php index.html index.htm;

   location / {

       try_files $uri $uri/ =404;

   }

   location ~ \.php$ {

       include snippets/fastcgi-php.conf;

       fastcgi_pass php:9000;

       fastcgi_index index.php;

       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

       include fastcgi_params;

   }

}

```

5. 创建 Docker 容器

使用以下命令创建并运行 PHP 和 Nginx 容器:

```sh

创建并运行 PHP 容器

docker run --name php-fpm -d -v ~/docker/php:/var/www/html php:7.4-fpm

创建并运行 Nginx 容器

docker run --name nginx -d -p 80:80 -v ~/docker/nginx/conf:/etc/nginx/conf.d -v ~/docker/nginx/www:/usr/share/nginx/html --link php-fpm:php nginx:latest

```

6. 测试 PHP 环境

在 `~/docker/php` 目录下创建一个测试文件 `index.php`,内容如下:

```php

phpinfo();

?>

```

然后在浏览器中访问 `http://localhost`,如果看到 PHP 信息页面,说明环境配置成功。

你可能感兴趣的:(docker,php,eureka)