Docker配置PHP开发环境教程

时间:2023-08-17 04:19:49 

前言

笔者用的是mac开发,但是mac自带的php功能安装十分不方便,并且和线上的linux开发环境不一致。在没有用docker之前一直用vagrant配置的centos的php开发环境,但是自从有了docker之后,就不再用vagrant了。

配置自己的php镜像

首先在自己的任意一个目录下创建如下三个文件

run.sh


#!/bin/bash
/usr/sbin/php-fpm7.0
/usr/sbin/nginx
tailf /etc/apt/sources.list

sources.list


# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse

Dockerfile


FROM ubuntu:16.04
# ===========================
# 配置虚拟主机
# -v default:/etc/nginx/sites-enabled/default
# 配置程序目录
# -v web:/var/www/html
# 配置映射端口
# -p 8008:80
# ===========================
MAINTAINER chengtao "751753158@qq.com"
ADD sources.list /etc/apt/sources.list
ADD run.sh /root/run.sh
RUN chmod +x /root/run.sh
RUN apt-get update
RUN apt-get install -y php-fpm php-mysql nginx
RUN sed -i 's/;date.timezone =/date.timezone = Asia\/Shanghai/' /etc/php/7.0/fpm/php.ini
RUN mkdir -p /run/php/
EXPOSE 80
CMD ["/bin/bash","/root/run.sh"]

执行命令


docker build -t d1studio:php-base:0.1 .

配置php mysql开发环境


mkdir -p ~/projects/php-app
cd ~/projects/php-app
mkdir mysql
mkdir www

www/index.php


<?php
phpinfo();

nginx.conf


server {
listen 80 default_server;
root /var/www/html;
index index.html index.htm index.php;
location / {
 try_files $uri $uri/ =404;
}
location ~ \.php$ {
 include snippets/fastcgi-php.conf;
 fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}

docker-compose.yml


version: '2'
services:
mysql:
image: mysql:5.6
volumes:
 - ./mysql/:/var/lib/mysql/
ports:
 - "3307:3306"
environment:
 - MYSQL_ROOT_PASSWORD=123456
php-app:
image: d1studio/php-base:0.1
ports:
 - "8009:80"
volumes:
 - ./nginx.conf:/etc/nginx/sites-enabled/default
 - ./www/:/var/www/html/
links:
 - mysql

开启php的测试项目


#开启
docker-compose up
#关闭
docker-compose down
标签:docker,php
0
投稿

猜你喜欢

  • Python+腾讯云服务器实现每日自动健康打卡

    2023-08-18 00:22:44
  • 一个比较实用的大数据量分页存储过程

    2024-01-13 16:11:29
  • 详解webpack进阶之loader篇

    2024-05-11 09:43:50
  • keras自动编码器实现系列之卷积自动编码器操作

    2023-12-31 18:33:15
  • python gdal安装与简单使用

    2022-06-19 00:03:25
  • 使用pandas读取文件的实现

    2022-10-20 14:00:42
  • SQL窗口函数之取值窗口函数的使用

    2024-01-27 06:53:37
  • influx+grafana自定义python采集数据和一些坑的总结

    2022-08-23 14:55:54
  • 基于python-pptx库中文文档及使用详解

    2023-11-30 12:06:13
  • python方向键控制上下左右代码

    2022-01-27 01:44:22
  • python向字符串中添加元素的实例方法

    2023-08-24 00:59:21
  • 基于Python实现微信自动回复功能

    2021-10-17 14:01:54
  • python3代码中实现加法重载的实例

    2023-07-13 12:01:31
  • 关于python的编码与解码decode()方法及zip()函数

    2022-04-04 15:15:54
  • python+Django实现防止SQL注入的办法

    2022-05-22 13:13:13
  • Python实现的knn算法示例

    2022-09-21 14:35:09
  • 深入理解python协程

    2021-04-06 09:31:53
  • vue使用代理解决请求跨域问题详解

    2024-05-10 14:15:33
  • pandas按行按列遍历Dataframe的几种方式

    2023-07-04 15:36:05
  • python元组和字典的内建函数实例详解

    2021-09-13 09:34:56
  • asp之家 网络编程 m.aspxhome.com