Docker 如何安装PHP+Nginx

2020-10-04   阅读:2832   分类:后端    标签: Docker

一、Docker安装PHP

安装php镜像

docker pull php

或者

docker search php

image.png

我们这里来安装PHP7.2

docker pull php:7.2-fpm

image.png

等待下载完成后,输入docker images我们就可以在本地镜像列表里查到REPOSITORY为php,标签为7.2-fpm的镜像。

docker images

image.png

二、Docker 安装 Nginx

1、我们还可以用 docker search nginx 命令来查看可用版本

docker search nginx

image.png

2、取最新版的 Nginx 镜像

docker pull nginx:latest

image.png

3、查看本地镜像

使用以下命令来查看是否已安装了 nginx:

docker images

image.png

4、运行容器

安装完成后,我们可以使用以下命令来运行 nginx 容器:

docker run --name nginx-test -p 8080:80 -d nginx

image.png

参数说明:

--name nginx-test:容器名称。

-p 8080:80: 端口进行映射,将本地 8080 端口映射到容器内部的 80 端口。

-d nginx: 设置容器在在后台一直运行。

5、安装成功

最后我们可以通过浏览器可以直接访问 8080 端口的 nginx 服务:

image.png

Nginx + PHP 部署

1、 启动 PHP:

docker run --name myphp-fpm -v ~/nginx/www:/www -d php:7.2-fpm

image.png

命令说明:

--name myphp-fpm : 将容器命名为 myphp-fpm。

-v ~/nginx/www:/www : 将主机中项目的目录 www 挂载到容器的 /www

创建 ~/nginx/conf/conf.d 目录:

mkdir ~/nginx/conf/conf.d

进入到该目录创建文件

vi test.conf
server {
    listen       8080;
    server_name  127.0.0.1;
    location / {
        root   /home/nginx/www;
        index  index.html index.htm index.php;
    }
 
    error_page   500 502 503 504  /50x.html;
    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

配置文件说明:

php:9000: 表示 php-fpm 服务的 URL,下面我们会具体说明。

/www/: 是 myphp-fpm 中 php 文件的存储路径,映射到本地的 ~/nginx/www 目录。

进入到/home/nginx/www/目录。创建文件index.php

输入以下代码并保存

<?php
echo phpinfo();
?>

image.png

【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://tpxhm.com/adetail/458.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×