[docker部署php环境]
apache模式
docker run -it --rm --name my-apache-php-app -p 8000:80 -v "$PWD":/var/www/html php:5.4-apache
fpm模式配置过程
docker run -it --rm --name my-fpm-php-app -p 9000:9000 -v "$PWD":/var/www/html php:5.4-fpm
配置nginx(物理机部署)
编辑/etc/nginx/sites-available/default ,添加如下行
location ~* \.php$ { fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; }
nginx -s reload
nginx container + fpm模式
1.启动fpm容器
docker run -d --name my-fpm-php-app -v /root/src:/var/www/html php:5.4-fpm
2.启动nginx服务器并link到fpm
docker run --name my-nginx -d -p 8080:80 --link my-fpm-php-app:fpm nginx
3.查看fpm的ip地址及端口号
docker exec -it my-nginx "/bin/bash"
进入后使用env命令查看环境变量,输出如下,可以看到fpm对应的ip及端口号
root@05cb669a483f:/# env
FPM_PORT_9000_TCP_ADDR=172.17.0.20
HOSTNAME=05cb669a483f
FPM_PORT_9000_TCP_PORT=9000
FPM_NAME=/my-nginx/fpm
FPM_PORT_9000_TCP_PROTO=tcp
FPM_ENV_PHP_INI_DIR=/usr/local/etc/php
FPM_ENV_PHP_VERSION=5.4.45
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
NGINX_VERSION=1.9.5-1~jessie
FPM_ENV_GPG_KEYS=F38252826ACD957EF380D39F2F7956BC5DA04B5D
FPM_PORT=tcp://172.17.0.20:9000
SHLVL=1
HOME=/root
FPM_PORT_9000_TCP=tcp://172.17.0.20:9000
FPM_ENV_PHP_EXTRA_CONFIGURE_ARGS=--enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data
_=/usr/bin/env
4.修改容器中/etc/nginx/conf.d/default.conf文件,在容器内不能修改,先复制到本地后修改
docker cp my-nginx:/etc/nginx/conf.d/default.conf ./default.conf
修改内容如下
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 172.17.0.20:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
docker cp ./default.conf my-nginx:/etc/nginx/conf.d/default.conf
5.reload 容器中的nginx
docker exec -it my-nginx "/bin/bash"
在容器中执行nginx -s reload
备注:访问过程中可能会报错:”File not found“,解决办法是吧default.conf 文件中
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
替换为绝对路径
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
思路一:link时 nignx容器内会有fpm相关的环境变量,如何利用环境变量?sed 控制变量值读取环境变量
思路二:以fpm为baseimage,集成nginx,使用Supervisor或CFEngine同时吊起多个进程
使用Supervisor,容器内同时启动nginx以及fpm
root用户下新建目录
mkdir nginx-fpm
cd nginx-fpm
创建文件default.conf用来取代nginx默认配置文件
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
创建文件supervisord.conf
[supervisord]
nodaemon=true
[program:sshd]
user=root
command=/usr/sbin/sshd -D
[program:php]
user=root
command=/usr/local/sbin/php-fpm
[program:nginx]
user=root
command=/usr/sbin/nginx -g "daemon off;"
创建文件Dockerfile
FROM php:5.4-fpm
MAINTAINER Jiashiwen "shiwen4@leju.com"
#Install Nginx
RUN apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
RUN echo "deb http://nginx.org/packages/mainline/debian/ jessie nginx" >> /etc/apt/sources.list
ENV NGINX_VERSION 1.9.5-1~jessie
RUN apt-get update && \
apt-get install -y ca-certificates nginx=${NGINX_VERSION}
# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log
RUN ln -sf /dev/stderr /var/log/nginx/error.log
COPY default.conf /etc/nginx/conf.d/default.conf
VOLUME ["/var/cache/nginx"]
#Install Supervisor
RUN apt-get update && apt-get install -y openssh-server supervisor && \
rm -rf /var/lib/apt/lists/*
RUN mkdir -p /var/run/sshd /var/run/nginx /var/log/supervisor /var/run/php-fpm
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
EXPOSE 80 443 9000 22
CMD ["/usr/bin/supervisord"]
构建docker image
docker build -t nginx-fpm .
启动docker 容器
docker run --rm -p 8888:80 -v /home/develop/Desktop:/var/www/html --name mynginxfpm nginx-fpm
-p参数用于映射端口号,格式为localport:containerport;-v用于映射目录,格式为localvolume:containervolume
nginx-fpm与mysql集成
root用户下新建目录
mkdir nginx-fpm
cd nginx-fpm
创建文件default.conf用来取代nginx默认配置文件
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
创建文件supervisord.conf
[supervisord]
nodaemon=true
[program:sshd]
user=root
command=/usr/sbin/sshd -D
[program:php]
user=root
command=/usr/local/sbin/php-fpm
[program:nginx]
user=root
command=/usr/sbin/nginx -g "daemon off;"
创建文件Dockerfile
在其中添加php mysql扩展包
FROM php:5.4-fpm
MAINTAINER Jiashiwen "shiwen4@leju.com"
#Install mysql module
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
&& docker-php-ext-install iconv mcrypt \
&& docker-php-ext-configure mysql \
&& docker-php-ext-install mysql
#Install Nginx
RUN apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
RUN echo "deb http://nginx.org/packages/mainline/debian/ jessie nginx" >> /etc/apt/sources.list
ENV NGINX_VERSION 1.9.5-1~jessie
RUN apt-get update && \
apt-get install -y ca-certificates nginx=${NGINX_VERSION}
# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log
RUN ln -sf /dev/stderr /var/log/nginx/error.log
COPY default.conf /etc/nginx/conf.d/default.conf
VOLUME ["/var/cache/nginx"]
#Install Supervisor
RUN apt-get update && apt-get install -y openssh-server supervisor && \
rm -rf /var/lib/apt/lists/*
RUN mkdir -p /var/run/sshd /var/run/nginx /var/log/supervisor /var/run/php-fpm
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
EXPOSE 80 443 9000 22
CMD ["/usr/bin/supervisord"]
构建docker image
docker build -t nginx-fpm .
root用户下载官方mysql镜像
docker pull mysql
启动mysql container
docker run -p 3306:3306 -v /my/own/datadir:/var/lib/mysql --name mydb -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.6
-p参数映射mysql端口号以便再外部通过GUI客户端操作mysql数据库;-v参数映射数据文件到本地目录;-e参数制定mysql数据库root用户的口令。
创建相应的数据库、表并添加数据
create database yueche_leju_com;
use yueche_leju_com;
CREATE TABLE `yueche_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`admin_id` int(11) DEFAULT NULL COMMENT '管理员ID',
`type` varchar(50) DEFAULT NULL COMMENT '操作标识',
`info` text COMMENT '内容',
`create_time` datetime DEFAULT NULL COMMENT '记录时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
SELECT * FROM yueche_leju_com.yueche_log;
insert into yueche_leju_com.yueche_log values(123,344,"fds","fds","20150509104509");
commit;
编写测试代码dbtest.php
<?php
$dbhost = getenv('MYDB_PORT_3306_TCP_ADDR');
$username = 'root';
$userpass = getenv('MYDB_ENV_MYSQL_ROOT_PASSWORD');
$dbdatabase = 'yueche_leju_com';
//生成一个连接
$db_connect=mysql_connect($dbhost, $username, $userpass) or die("Unable to connect to the MySQL!");
//选择一个需要操作的数据库
mysql_select_db($dbdatabase,$db_connect);
//执行MySQL语句
$result=mysql_query("SELECT * FROM yueche_log");
//提取数据
while($row = mysql_fetch_row($result)) {
echo '<pre>';
var_dump($row);
}
?>
启动nginx-fpm container
docker run --rm --link mydb -p 8888:80 -v /home/develop/Desktop:/var/www/html -e MYSQL_DBNAME=testdb --name mynginxfpm nginx-fpm
通过浏览器验证
http://localhost:8888/dbtest.php
nginx-fpm+mysql+redis集成
root用户下新建目录
mkdir nginx-fpm
cd nginx-fpm
创建文件default.conf用来取代nginx默认配置文件
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
创建文件supervisord.conf
[supervisord]
nodaemon=true
[program:sshd]
user=root
command=/usr/sbin/sshd -D
[program:php]
user=root
command=/usr/local/sbin/php-fpm
[program:nginx]
user=root
command=/usr/sbin/nginx -g "daemon off;"
创建文件Dockerfile
在其中添加php mysql扩展包以及redis扩展包
FROM php:5.4-fpm
MAINTAINER Jiashiwen "shiwen4@leju.com"
#Install mysql module
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
&& docker-php-ext-install iconv mcrypt \
&& docker-php-ext-configure mysql \
&& docker-php-ext-install mysql
#Install redis module
RUN curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/2.2.7.tar.gz \
&& tar xfz /tmp/redis.tar.gz \
&& rm -r /tmp/redis.tar.gz \
&& mv phpredis-2.2.7 /usr/src/php/ext/redis \
&& docker-php-ext-install redis
#Install Nginx
RUN apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
RUN echo "deb http://nginx.org/packages/mainline/debian/ jessie nginx" >> /etc/apt/sources.list
ENV NGINX_VERSION 1.9.5-1~jessie
RUN apt-get update && \
apt-get install -y ca-certificates nginx=${NGINX_VERSION}
# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log
RUN ln -sf /dev/stderr /var/log/nginx/error.log
COPY default.conf /etc/nginx/conf.d/default.conf
VOLUME ["/var/cache/nginx"]
#Install Supervisor
RUN apt-get update && apt-get install -y openssh-server supervisor && \
rm -rf /var/lib/apt/lists/*
RUN mkdir -p /var/run/sshd /var/run/nginx /var/log/supervisor /var/run/php-fpm
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
EXPOSE 80 443 9000 22
CMD ["/usr/bin/supervisord"]
构建docker image
docker build -t nginx-fpm .
root用户下载官方mysql镜像
docker pull mysql
启动mysql container
docker run -p 3306:3306 -v /my/own/datadir:/var/lib/mysql --name mydb -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.6
-p参数映射mysql端口号以便再外部通过GUI客户端操作mysql数据库;-v参数映射数据文件到本地目录;-e参数制定mysql数据库root用户的口令。
创建相应的数据库、表并添加数据
create database yueche_leju_com;
use yueche_leju_com;
CREATE TABLE `yueche_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`admin_id` int(11) DEFAULT NULL COMMENT '管理员ID',
`type` varchar(50) DEFAULT NULL COMMENT '操作标识',
`info` text COMMENT '内容',
`create_time` datetime DEFAULT NULL COMMENT '记录时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
SELECT * FROM yueche_leju_com.yueche_log;
insert into yueche_leju_com.yueche_log values(123,344,"fds","fds","20150509104509");
commit;
编写测试代码dbtest.php
<?php
//-------------------数据库测试----------------------
$dbhost = getenv('MYDB_PORT_3306_TCP_ADDR');
$username = 'root';
$userpass = getenv('MYDB_ENV_MYSQL_ROOT_PASSWORD');
$dbdatabase = 'yueche_leju_com';
//生成一个连接
$db_connect=mysql_connect($dbhost, $username, $userpass) or die("Unable to connect to the MySQL!");
//选择一个需要操作的数据库
mysql_select_db($dbdatabase,$db_connect);
//执行MySQL语句
$result=mysql_query("SELECT * FROM yueche_log");
//提取数据
while($row = mysql_fetch_row($result)) {
echo '<pre>';
var_dump($row);
}
//-----------------------redis测试---------------------
//$redis_config = getenv('SINASRV_CACHE_REDIS_HOST'); //形如 127.0.0.1:6379
//list($host, $port) = explode(":", $redis_config);
$config = array(
'host' => getenv('MYREDIS_PORT_6379_TCP_ADDR'),
'port' => getenv('MYREDIS_PORT_6379_TCP_PORT')
);
var_dump($config);
if(class_exists('Redis')){
try{
$redis = new Redis();
$redis->connect($config['host'], $config['port']);
$redis->ping();
}catch(Exception $e){
$redis = NULL;
}
}else{
die('can not to initial Redis!');
}
//设置
$redis->setex('test_key', 60, 'hello world!');
//获取
$test_value = $redis->get('test_key');
if($test_value){
echo 'The Redis is running! and the value of "test_key" is : "'.$test_value.'"';
}
?>
下载redis镜像
docker pull redis
启动redis container
docker run -d -p 6379:6379 --name myredis redis
启动nginx-fpm container
docker run --rm --link mydb --link myredis -p 8888:80 -v /home/develop/Desktop:/var/www/html -e MYSQL_DBNAME=testdb --name mynginxfpm nginx-fpm
通过浏览器验证
http://localhost:8888/dbtest.php