JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

使用docker-compose安装php+nginx+memcached

wys521 2024-10-04 06:12:14 精选教程 31 ℃ 0 评论

要在Docker上部署PHP 8.1、Memcached和Nginx,你可以按照以下步骤进行操作:

1. 首先,安装Docker和Docker Compose,确保你的系统上已经安装了它们。

2. 创建一个新的项目目录,在该目录下创建一个名为`docker-compose.yml`的文件。

3. 打开`docker-compose.yml`文件,并添加以下内容:

version: '3'

services:

nginx:

image: nginx

ports:

- 8100:80

volumes:

- ~/docker/nginx/conf.d/nginx.conf:/etc/nginx/nginx.conf

- ~/docker/www:/var/www/html

dns:

- 8.8.8.8

- 8.8.4.4

depends_on:

- php

php:

build:

context: .

dockerfile: Dockerfile

volumes:

- ~/docker/www:/var/www/html

dns:

- 8.8.8.8

- 8.8.4.4

depends_on:

- memcached

memcached:

image: memcached

dns:

- 8.8.8.8

- 8.8.4.4

4. 在项目目录下创建一个名为`nginx.conf`的文件,用于配置Nginx。在该文件中,你可以根据自己的需求进行配置。以下是一个基本的示例:

worker_processes 1;

events {

worker_connections 1024;

}

http {

server {

listen 80;

root /var/www/html;

index index.php index.html;

location / {

try_files $uri $uri/ /index.php?$query_string;

}

location ~ \.php$ {

fastcgi_pass php:9000;

fastcgi_index index.php;

include fastcgi_params;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

}

}

}

5. 在项目目录下创建一个名为`Dockerfile`的文件,用于构建自定义的PHP镜像。以下是一个基本的示例:

FROM php:8.1-fpm

RUN docker-php-ext-install pdo_mysql

WORKDIR /var/www/html

CMD ["php-fpm"]

6. 确保你的项目目录下有一个名为`html`的文件夹,用于存放PHP应用程序的代码文件。

7. 打开终端或命令提示符,导航到项目目录,然后运行以下命令启动Docker容器:

docker-compose up -d

这将根据`docker-compose.yml`文件中的配置启动Nginx、PHP和Memcached容器。

现在,你的PHP 8.1、Memcached和Nginx环境已经在Docker中成功部署了。你可以将你的PHP应用程序代码放置在项目目录下的`html`文件夹中,并通过浏览器访问。

http://主机ip:8100/

例如我的主机是 http://192.168.10.188:8100/

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表