Table of Contents
作者:Mike Ebinum
译者:叶可强
Dockerfile
使用 Cent OS 6.x
更新镜像
安装包
配置 Nginx 、 HHVM 和 Supervisord
下一步?
这篇文章由 Mike Ebinum 撰写,叶可强 翻译。点击 这里 阅读原文。
The article was contributed by Mike Ebinum, click here to read the original publication.
Home php教程 php手册 创建一个运行 PHP 、NGINX 和 Hip Hop VM(HHVM) 的 Docker 容器

创建一个运行 PHP 、NGINX 和 Hip Hop VM(HHVM) 的 Docker 容器

Jun 06, 2016 pm 08:12 PM
nginx php create run

作者:Mike Ebinum 译者:叶可强 对于 Docker,我感到非常的兴奋。作为一个很早就进行 .NET 开发的开发人员,我工作中不喜欢的事情之一就是在不同的环境中部署和测试。部署一个 web 应用程序的过程绝对是一个噩梦般的经历。即便之后我迁移到基于 UNIX 平台开

作者:Mike Ebinum
译者:叶可强

alt

对于 Docker,我感到非常的兴奋。作为一个很早就进行 .NET 开发的开发人员,我工作中不喜欢的事情之一就是在不同的环境中部署和测试。部署一个 web 应用程序的过程绝对是一个噩梦般的经历。即便之后我迁移到基于 UNIX 平台开发,并且使用开源的工具/语言,如 Node 、 Java 、 Scala 、 PHP 等等,我发现同样的部署问题一次又一次的发生。

使用如 Docker 这样的工具,你可以让开发环境的配置精确得如生产环境的镜像一样。部署好 web 应用程序的容器,所有东西都被配置,也就无需担心关于部署的那些麻烦事。

如果你是一个 Docker 的新手,并且不是十分确定它是什么,以下文章会是一个完美的学习纲要。。

  • Docker Lightweight linux containers for consistent development and deployment
  • Docker: Using Linux Containers to Support Portable Application deployment

作为一个懒惰的程序员,我的梦想成真了,做好一次然后就无后顾之忧(在一定程度上)。通过这篇文章,我将展示如何基于下列开发环境去创建并且运行一个 Docker 容器。

  • CentOS
  • Nginx web server
  • PHP with Hip Hop VM (HHVM)

Dockerfile

准备开始,我们创建一个 Dockerfile —— Dockerfile 包含如何创建所需镜像的指令。

FROM    centos:centos6
MAINTAINER Mike Ebinum, hello@seedtech.io  
Copy after login

使用 Cent OS 6.x

告知 Docker 使用官方社区最新版本的 CentOS 6.x 可用镜像。

更新镜像

安装所有最新版本的包更新,并且把 Red Hat EPEL 的仓库加入可用的仓库列表。

RUN yum update -y >/dev/null && yum install -y http://ftp.riken.jp/Linux/fedora/epel/6/i386/epel-release-6-8.noarch.rpm  && curl -L -o /etc/yum.repos.d/hop5.repo "http://www.hop5.in/yum/el6/hop5.repo"  
Copy after login

安装包

安装 supervisord —— 我们将使用这个配置和控制运行在容器中的进程 - 、 nginx 、 php 、一些 PHP 的开发包以及 Facebook 的 hhvm 。

RUN yum install -y python-meld3 http://dl.fedoraproject.org/pub/epel/6/i386/supervisor-2.1-8.el6.noarch.rpm
RUN ["yum", "-y", "install", "nginx", "php", "php-mysql", "php-devel", "php-gd", "php-pecl-memcache", "php-pspell", "php-snmp", "php-xmlrpc", "php-xml","hhvm"]  
Copy after login

配置 Nginx 、 HHVM 和 Supervisord

为 nginx 创建目录,并且把 index.php 文件加入 nginx 来展现。

RUN mkdir -p /var/www/html && chmod a+r /var/www/html && echo "<?php phpinfo(); ?>" > /var/www/html/index.php  
Copy after login

下一组指令是:

  • 为 HHVM 添加一个配置文件,然后重启我们的 HHVM 服务
  • 为 Supervisord 添加一个配置文件,然后启动 Nginx 和 HHVM
  ADD config.hdf /etc/hhvm/config.hdf 
  RUN service hhvm restart
  ADD nginx.conf /etc/nginx/conf.d/default.conf
  ADD supervisord.conf /etc/supervisord.conf
  RUN chkconfig supervisord on && chkconfig nginx on
Copy after login
  • 添加一个 shell 脚本 /run.sh ,在 Docker 容器运行时启动。

run.sh

#!/bin/bash
set -e -x  
echo "starting supervisor in foreground"  
supervisord -n  
Copy after login
 ADD scripts/run.sh /run.sh
 RUN chmod a+x /run.sh 
 EXPOSE 22 80
 ENTRYPOINT ["/run.sh"]
Copy after login

构建容器,并且打 tag

docker build -t centos-nginx-php5-hhvm .  
Copy after login

现在我们有一个全功能的容器,我们可以像下面这样运行:

docker run -d -p 80:80 centos-nginx-php5-hhvm  
Copy after login

如果你已经有本地的服务已经在运行并且占用了 80 端口,你能很容易的的改变容器的对外端口。

alt

docker registry 提供这个 Docker 镜像的可用版本。

Dockerfile

完整的 Dockerfile 如下

# DOCKER-VERSION 1.0.0
FROM    centos:centos6
MAINTAINER Mike Ebinum, hello@seedtech.io
# Install dependencies for HHVM
# yum update -y >/dev/null && 
RUN yum install -y http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm  && curl -L -o /etc/yum.repos.d/hop5.repo "http://www.hop5.in/yum/el6/hop5.repo"
# Install supervisor
RUN yum install -y python-meld3 http://dl.fedoraproject.org/pub/epel/6/i386/supervisor-2.1-8.el6.noarch.rpm
#install nginx, php, mysql, hhvm
RUN ["yum", "-y", "install", "nginx", "php", "php-mysql", "php-devel", "php-gd", "php-pecl-memcache", "php-pspell", "php-snmp", "php-xmlrpc", "php-xml","hhvm"]
# Create folder for server and add index.php file to for nginx
RUN mkdir -p /var/www/html && chmod a+r /var/www/html && echo "<?php phpinfo(); ?>" > /var/www/html/index.php
#Setup hhvm - add config for hhvm
ADD config.hdf /etc/hhvm/config.hdf 
RUN service hhvm restart
# ADD Nginx config
ADD nginx.conf /etc/nginx/conf.d/default.conf
# ADD supervisord config with hhvm setup
ADD supervisord.conf /etc/supervisord.conf
#set to start automatically - supervisord, nginx and mysql
RUN chkconfig supervisord on && chkconfig nginx on
ADD scripts/run.sh /run.sh
RUN chmod a+x /run.sh 
EXPOSE 22 80  
#Start supervisord (which will start hhvm), nginx 
ENTRYPOINT ["/run.sh"]  
Copy after login

在这篇文章中提到的其他的可用文件在 Github 上。

下一步?

太棒了!我们现在有了一个环境配置,但我如何运行 PHP 应用程序?我将做后续的文章介绍如何使用这个容器来安装和配置 PHP 应用程序。欢迎订阅这个 博客,也可以在在 twitter 关注 @mikeebinum 和 @SEEDtechio 来获得更新。


这篇文章由 Mike Ebinum 撰写,叶可强 翻译。点击 这里 阅读原文。
The article was contributed by Mike Ebinum, click here to read the original publication.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve nginx403 How to solve nginx403 Apr 14, 2025 am 10:33 AM

How to fix Nginx 403 Forbidden error? Check file or directory permissions; 2. Check .htaccess file; 3. Check Nginx configuration file; 4. Restart Nginx. Other possible causes include firewall rules, SELinux settings, or application issues.

How to solve nginx304 error How to solve nginx304 error Apr 14, 2025 pm 12:45 PM

Answer to the question: 304 Not Modified error indicates that the browser has cached the latest resource version of the client request. Solution: 1. Clear the browser cache; 2. Disable the browser cache; 3. Configure Nginx to allow client cache; 4. Check file permissions; 5. Check file hash; 6. Disable CDN or reverse proxy cache; 7. Restart Nginx.

How to solve the problem of nginx cross-domain How to solve the problem of nginx cross-domain Apr 14, 2025 am 10:15 AM

There are two ways to solve the Nginx cross-domain problem: modify the cross-domain response header: add directives to allow cross-domain requests, specify allowed methods and headers, and set cache time. Use CORS modules: Enable modules and configure CORS rules that allow cross-domain requests, methods, headers, and cache times.

How to start nginx in Linux How to start nginx in Linux Apr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to solve nginx403 error How to solve nginx403 error Apr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to check the running status of nginx How to check the running status of nginx Apr 14, 2025 am 11:48 AM

The methods to view the running status of Nginx are: use the ps command to view the process status; view the Nginx configuration file /etc/nginx/nginx.conf; use the Nginx status module to enable the status endpoint; use monitoring tools such as Prometheus, Zabbix, or Nagios.

How to clean nginx error log How to clean nginx error log Apr 14, 2025 pm 12:21 PM

The error log is located in /var/log/nginx (Linux) or /usr/local/var/log/nginx (macOS). Use the command line to clean up the steps: 1. Back up the original log; 2. Create an empty file as a new log; 3. Restart the Nginx service. Automatic cleaning can also be used with third-party tools such as logrotate or configured.

See all articles