搭建基于Docker的PHP开发环境的详细教程
这篇文章主要介绍了搭建基于Docker的PHP开发环境的详细教程,Docker是当下虚拟机技术的最佳选择,需要的朋友可以参考下
现在很多开发者都使用Vagrant来管理他们的虚拟机开发环境,Vagrant确实很酷, 不过也有不少缺点(最主要的是它占用太多的资源)。在容器技术、Docker和更多类Docker技术出现后,解决这个问题就变得简单了。
免责声明
由于boot2docker的工作方式,本文所述的方法在你的环境中可能无法正常运行。如果需要在非Linux环境下共享文件夹到Docker容器,还需要注意更多额外的细节。后续我会写篇文章专门来介绍实际遇到的问题。
怎样才算是好的开发环境
首先,我们得知道什么才是好的开发环境, 对于我而言,一个好的开发环境需要具备以下几个特点:
而Docker都支持以上这些特点,甚至更多。你几乎可以即时销毁和重建容器,,而更新环境只需要重建你当前使用的镜像即可。
什么是PHP开发环境
目前Web应用错综复杂,PHP开发环境需要很多的东西,为了保证环境的简单性,需要做各种各样的限制。
我们这次使用Nginx、PHP5-FPM、MySQL来运行Synmfony项目。
Pet 与 Cattle
另一个我们要讨论的重点是:我们要把开发环境部署在多容器还是单容器中。 两种方式各有优点:
因为我比较懒,加上我需要在我的笔记本上放点别的内容,所以,这里我们只介绍单个容器的方法。
初始化工程
首先要做的是初始化一个新的Symfony工程. 推荐的方法是用composer的create-project命令。本来可以在工作站上安装composer,但是那样太简单了。这次我们通过Docker来使用它。
我之前发过一篇关于Docker命令的文章:make docker commands(好吧,我说谎了,我本来把它写在这篇文章中了,然后觉得把它独立出来会比较好)。
不管怎么样,你可以读一下。接下来如果还没有composer命令的话,你可以创建一个属于自己的composer 别名。
$ alias composer="docker run -i -t -v \$PWD:/srv ubermuda/composer"
现在你可以初始化Symfony工程了:
$ composer create-project symfony/framwork-standard-edition SomeProject
帅呆了!下面来点实在的工作。
容器
构建一个运行标准Symfony项目且自给自足的容器相当容易,只需要安装好常用的Nginx、PHP5-FPM和MySQL-Server即可,然后把预先准备好的Nginx的虚拟主机配置文件扔进去,再复制一些配置文件进去就完事了。
本容器的源代码在GitHub上的 ubermuda/docker-symfony仓库中可以找到。 Dockerfile 是Docker构建镜像要用到的配置文件,我们来看一下:
FROM debian:wheezy ENV DEBIAN_FRONTEND noninteractive RUN apt-get update -y RUN apt-get install -y nginx php5-fpm php5-mysqlnd php5-cli mysql-server supervisor RUN sed -e 's/;daemonize = yes/daemonize = no/' -i /etc/php5/fpm/php-fpm.conf RUN sed -e 's/;listen\.owner/listen.owner/' -i /etc/php5/fpm/pool.d/www.conf RUN sed -e 's/;listen\.group/listen.group/' -i /etc/php5/fpm/pool.d/www.conf RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf ADD vhost.conf /etc/nginx/sites-available/default ADD supervisor.conf /etc/supervisor/conf.d/supervisor.conf ADD init.sh /init.sh EXPOSE 80 3306 VOLUME ["/srv"] WORKDIR /srv CMD ["/usr/bin/supervisord"]
我们通过扩展 debian:wheezy 这个基础镜像开始,然后通过一系列的sed命令来配置Nginx和PHP5-FPM。
复制代码 代码如下:
RUN sed -e 's/;daemonize = yes/daemonize = no/' -i /etc/php5/fpm/php-fpm.conf
RUN sed -e 's/;listen\.owner/listen.owner/' -i /etc/php5/fpm/pool.d/www.conf
RUN sed -e 's/;listen\.group/listen.group/' -i /etc/php5/fpm/pool.d/www.conf
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
这里我们要做两件事。 首先配置PHP5-FPM和Nginx让他们在前台运行以便supervisord可以追踪到他们。
然后,配置PHP5-FPM以指定的用户运行Web-Server,并处理好文件权限。
接下来需要安装一组配置文件,首先是Nginx的虚拟主机配置文件vhost.conf:
server { listen 80; server_name _; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; root /srv/web; index app_dev.php; location / { try_files $uri $uri/ /app_dev.php?$query_string; } location ~ [^/]\.php(/|$) { fastcgi_pass unix:/var/run/php5-fpm.sock; include fastcgi_params; } }
因为我们不需要域名,所以把server_name设成了_(有点像perl的$_占位符变量), 并配置根目录(document root)为/svr/web, 我们会把应用程序部署在/srv下,剩下的就是标准的Mginx + PHP5-FPM配置.
因为一个容器每次只能运行一个程序, 我们需要supervisord(或者任何别的进程管理器,不过我比较中意supervisord)。幸运的是, 这个进程管理器会产生我们需要的所有进程!下面是一小段supervisord的配置:
[supervisord] nodaemon=true [program:nginx] command=/usr/sbin/nginx [program:php5-fpm] command=/usr/sbin/php5-fpm [program:mysql] command=/usr/bin/mysqld_safe [program:init] command=/init.sh autorestart=false redirect_stderr=true redirect_stdout=/srv/app/logs/init.log
这里我们需要做的是定义所有的服务, 加上一个特殊的program:init进程,它不是一个实际的服务,而是一个独创的运行启动脚本的方式。
这个启动脚本的问题在于,它通常需要先启动某些服务。比如,你可能要初始化一些数据库表,但前提是你得先把MySQL跑起来,一个可能的解决办法 是,在启动脚本中启动MySQL,然后初始化表,然后为了防止影响到supervisord的进程管理,需要停掉MySQL,最后再启动 supervisord。
这样的脚本看起来类似下面这样:
/etc/init.d/mysql start app/console doctrine:schema:update --force /etc/init.d/mysql stop exec /usr/bin/supervisord
看起来丑爆了有木有,咱换种方式,让supervisor来运行它并且永不重启。
实际的init.sh脚本如下:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database
