docker部署php环境

[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及端口号

 
 
  1. root@05cb669a483f:/# env
  2. FPM_PORT_9000_TCP_ADDR=172.17.0.20
  3. HOSTNAME=05cb669a483f
  4. FPM_PORT_9000_TCP_PORT=9000
  5. FPM_NAME=/my-nginx/fpm
  6. FPM_PORT_9000_TCP_PROTO=tcp
  7. FPM_ENV_PHP_INI_DIR=/usr/local/etc/php
  8. FPM_ENV_PHP_VERSION=5.4.45
  9. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  10. PWD=/
  11. NGINX_VERSION=1.9.5-1~jessie
  12. FPM_ENV_GPG_KEYS=F38252826ACD957EF380D39F2F7956BC5DA04B5D
  13. FPM_PORT=tcp://172.17.0.20:9000
  14. SHLVL=1
  15. HOME=/root
  16. FPM_PORT_9000_TCP=tcp://172.17.0.20:9000
  17. FPM_ENV_PHP_EXTRA_CONFIGURE_ARGS=--enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data
  18. _=/usr/bin/env




4.修改容器中/etc/nginx/conf.d/default.conf文件,在容器内不能修改,先复制到本地后修改

docker cp my-nginx:/etc/nginx/conf.d/default.conf ./default.conf


修改内容如下

 
 
  1. location ~* \.php$ {
  2.    fastcgi_index   index.php;
  3.    fastcgi_pass    172.17.0.20:9000;
  4.    include         fastcgi_params;
  5.    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
  6.    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
  7. }




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 文件中

 
 
  1. fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;

替换为绝对路径

 
 
  1. fastcgi_param   SCRIPT_FILENAME    /var/www/html$fastcgi_script_name;


思路一:link时 nignx容器内会有fpm相关的环境变量,如何利用环境变量?sed 控制变量值读取环境变量


思路二:以fpm为baseimage,集成nginx,使用Supervisor或CFEngine同时吊起多个进程


使用Supervisor,容器内同时启动nginx以及fpm

root用户下新建目录

 
 
  1. mkdir nginx-fpm

  2. cd nginx-fpm

创建文件default.conf用来取代nginx默认配置文件


 
 
  1. server {
  2.    listen       80;
  3.    server_name  localhost;
  4.    #charset koi8-r;
  5.    #access_log  /var/log/nginx/log/host.access.log  main;
  6.    location / {
  7.        root   /usr/share/nginx/html;
  8.        index  index.html index.htm;
  9.    }
  10.    #error_page  404              /404.html;
  11.    # redirect server error pages to the static page /50x.html
  12.    #
  13.    error_page   500 502 503 504  /50x.html;
  14.    location = /50x.html {
  15.        root   /usr/share/nginx/html;
  16.    }
  17.    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  18.    #
  19.    #location ~ \.php$ {
  20.    #    proxy_pass   http://127.0.0.1;
  21.    #}
  22.    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  23.    #
  24.    #location ~ \.php$ {
  25.    #    root           html;
  26.    #    fastcgi_pass   127.0.0.1:9000;
  27.    #    fastcgi_index  index.php;
  28.    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  29.    #    include        fastcgi_params;
  30.    #}
  31.     location ~* \.php$ {
  32.         fastcgi_index   index.php;
  33.         fastcgi_pass    127.0.0.1:9000;
  34.         include         fastcgi_params;
  35.         fastcgi_param   SCRIPT_FILENAME    /var/www/html$fastcgi_script_name;
  36.         fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
  37.     }
  38.    # deny access to .htaccess files, if Apache's document root
  39.    # concurs with nginx's one
  40.    #
  41.    #location ~ /\.ht {
  42.    #    deny  all;
  43.    #}
  44. }

创建文件supervisord.conf


 
 
  1. [supervisord]
  2. nodaemon=true
  3. [program:sshd]
  4. user=root
  5. command=/usr/sbin/sshd -D
  6. [program:php]
  7. user=root
  8. command=/usr/local/sbin/php-fpm
  9. [program:nginx]
  10. user=root
  11. command=/usr/sbin/nginx -g "daemon off;"





创建文件Dockerfile  


 
 
  1. FROM php:5.4-fpm
  2. MAINTAINER Jiashiwen "shiwen4@leju.com"
  3. #Install Nginx
  4. RUN apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
  5. RUN echo "deb http://nginx.org/packages/mainline/debian/ jessie nginx" >> /etc/apt/sources.list
  6. ENV NGINX_VERSION 1.9.5-1~jessie
  7. RUN apt-get update && \
  8.    apt-get install -y ca-certificates nginx=${NGINX_VERSION}
  9. # forward request and error logs to docker log collector
  10. RUN ln -sf /dev/stdout /var/log/nginx/access.log
  11. RUN ln -sf /dev/stderr /var/log/nginx/error.log
  12. COPY  default.conf /etc/nginx/conf.d/default.conf
  13. VOLUME ["/var/cache/nginx"]
  14. #Install Supervisor
  15. RUN apt-get update && apt-get install -y openssh-server supervisor && \
  16.    rm -rf /var/lib/apt/lists/*
  17. RUN mkdir -p /var/run/sshd /var/run/nginx /var/log/supervisor /var/run/php-fpm
  18. COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
  19. EXPOSE 80 443 9000 22
  20. CMD ["/usr/bin/supervisord"]

构建docker image


 
 
  1. docker build -t nginx-fpm .


启动docker 容器

 
 
  1. 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用户下新建目录

 
 
  1. mkdir nginx-fpm

  2. cd nginx-fpm

创建文件default.conf用来取代nginx默认配置文件


 
 
  1. server {
  2.    listen       80;
  3.    server_name  localhost;
  4.    #charset koi8-r;
  5.    #access_log  /var/log/nginx/log/host.access.log  main;
  6.    location / {
  7.        root   /usr/share/nginx/html;
  8.        index  index.html index.htm;
  9.    }
  10.    #error_page  404              /404.html;
  11.    # redirect server error pages to the static page /50x.html
  12.    #
  13.    error_page   500 502 503 504  /50x.html;
  14.    location = /50x.html {
  15.        root   /usr/share/nginx/html;
  16.    }
  17.    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  18.    #
  19.    #location ~ \.php$ {
  20.    #    proxy_pass   http://127.0.0.1;
  21.    #}
  22.    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  23.    #
  24.    #location ~ \.php$ {
  25.    #    root           html;
  26.    #    fastcgi_pass   127.0.0.1:9000;
  27.    #    fastcgi_index  index.php;
  28.    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  29.    #    include        fastcgi_params;
  30.    #}
  31.     location ~* \.php$ {
  32.         fastcgi_index   index.php;
  33.         fastcgi_pass    127.0.0.1:9000;
  34.         include         fastcgi_params;
  35.         fastcgi_param   SCRIPT_FILENAME    /var/www/html$fastcgi_script_name;
  36.         fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
  37.     }
  38.    # deny access to .htaccess files, if Apache's document root
  39.    # concurs with nginx's one
  40.    #
  41.    #location ~ /\.ht {
  42.    #    deny  all;
  43.    #}
  44. }

创建文件supervisord.conf


 
 
  1. [supervisord]
  2. nodaemon=true
  3. [program:sshd]
  4. user=root
  5. command=/usr/sbin/sshd -D
  6. [program:php]
  7. user=root
  8. command=/usr/local/sbin/php-fpm
  9. [program:nginx]
  10. user=root
  11. command=/usr/sbin/nginx -g "daemon off;"






创建文件Dockerfile  

在其中添加php mysql扩展包

 
 
  1. FROM php:5.4-fpm
  2. MAINTAINER Jiashiwen "shiwen4@leju.com"
  3. #Install mysql module
  4. RUN apt-get update && apt-get install -y \
  5.        libfreetype6-dev \
  6.        libjpeg62-turbo-dev \
  7.        libmcrypt-dev \
  8.        libpng12-dev \
  9.    && docker-php-ext-install iconv mcrypt \
  10.    && docker-php-ext-configure mysql \
  11.    && docker-php-ext-install mysql
  12. #Install Nginx
  13. RUN apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
  14. RUN echo "deb http://nginx.org/packages/mainline/debian/ jessie nginx" >> /etc/apt/sources.list
  15. ENV NGINX_VERSION 1.9.5-1~jessie
  16. RUN apt-get update && \
  17.    apt-get install -y ca-certificates nginx=${NGINX_VERSION}
  18. # forward request and error logs to docker log collector
  19. RUN ln -sf /dev/stdout /var/log/nginx/access.log
  20. RUN ln -sf /dev/stderr /var/log/nginx/error.log
  21. COPY  default.conf /etc/nginx/conf.d/default.conf
  22. VOLUME ["/var/cache/nginx"]
  23. #Install Supervisor
  24. RUN apt-get update && apt-get install -y openssh-server supervisor && \
  25.    rm -rf /var/lib/apt/lists/*
  26. RUN mkdir -p /var/run/sshd /var/run/nginx /var/log/supervisor /var/run/php-fpm
  27. COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
  28. EXPOSE 80 443 9000 22
  29. CMD ["/usr/bin/supervisord"]



构建docker image


 
 
  1. docker build -t nginx-fpm .


root用户下载官方mysql镜像

 
 
  1. docker pull mysql

启动mysql container

 
 
  1. 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用户的口令。


创建相应的数据库、表并添加数据

  
  
  1. create database yueche_leju_com;
  2. use yueche_leju_com;
  3. CREATE TABLE `yueche_log` (
  4.  `id` int(11) NOT NULL AUTO_INCREMENT,
  5.  `admin_id` int(11) DEFAULT NULL COMMENT '管理员ID',
  6.  `type` varchar(50) DEFAULT NULL COMMENT '操作标识',
  7.  `info` text COMMENT '内容',
  8.  `create_time` datetime DEFAULT NULL COMMENT '记录时间',
  9.  PRIMARY KEY (`id`)
  10. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  11. SELECT * FROM yueche_leju_com.yueche_log;
  12. insert into yueche_leju_com.yueche_log values(123,344,"fds","fds","20150509104509");
  13. commit;


编写测试代码dbtest.php

 
 
  1. <?php
  2. $dbhost = getenv('MYDB_PORT_3306_TCP_ADDR');
  3. $username = 'root';
  4. $userpass = getenv('MYDB_ENV_MYSQL_ROOT_PASSWORD');
  5. $dbdatabase = 'yueche_leju_com';
  6. //生成一个连接
  7. $db_connect=mysql_connect($dbhost, $username, $userpass) or die("Unable to connect to the MySQL!");
  8. //选择一个需要操作的数据库
  9. mysql_select_db($dbdatabase,$db_connect);
  10. //执行MySQL语句
  11. $result=mysql_query("SELECT * FROM yueche_log");
  12. //提取数据
  13. while($row = mysql_fetch_row($result)) {
  14. echo '<pre>';
  15. var_dump($row);
  16. }
  17. ?>


启动nginx-fpm container

  
  
  1. 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用户下新建目录

 
 
  1. mkdir nginx-fpm

  2. cd nginx-fpm

创建文件default.conf用来取代nginx默认配置文件


 
 
  1. server {
  2.    listen       80;
  3.    server_name  localhost;
  4.    #charset koi8-r;
  5.    #access_log  /var/log/nginx/log/host.access.log  main;
  6.    location / {
  7.        root   /usr/share/nginx/html;
  8.        index  index.html index.htm;
  9.    }
  10.    #error_page  404              /404.html;
  11.    # redirect server error pages to the static page /50x.html
  12.    #
  13.    error_page   500 502 503 504  /50x.html;
  14.    location = /50x.html {
  15.        root   /usr/share/nginx/html;
  16.    }
  17.    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  18.    #
  19.    #location ~ \.php$ {
  20.    #    proxy_pass   http://127.0.0.1;
  21.    #}
  22.    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  23.    #
  24.    #location ~ \.php$ {
  25.    #    root           html;
  26.    #    fastcgi_pass   127.0.0.1:9000;
  27.    #    fastcgi_index  index.php;
  28.    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  29.    #    include        fastcgi_params;
  30.    #}
  31.     location ~* \.php$ {
  32.         fastcgi_index   index.php;
  33.         fastcgi_pass    127.0.0.1:9000;
  34.         include         fastcgi_params;
  35.         fastcgi_param   SCRIPT_FILENAME    /var/www/html$fastcgi_script_name;
  36.         fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
  37.     }
  38.    # deny access to .htaccess files, if Apache's document root
  39.    # concurs with nginx's one
  40.    #
  41.    #location ~ /\.ht {
  42.    #    deny  all;
  43.    #}
  44. }

创建文件supervisord.conf


 
 
  1. [supervisord]
  2. nodaemon=true
  3. [program:sshd]
  4. user=root
  5. command=/usr/sbin/sshd -D
  6. [program:php]
  7. user=root
  8. command=/usr/local/sbin/php-fpm
  9. [program:nginx]
  10. user=root
  11. command=/usr/sbin/nginx -g "daemon off;"


创建文件Dockerfile  

在其中添加php mysql扩展包以及redis扩展包

  
  
  1. FROM php:5.4-fpm
  2. MAINTAINER Jiashiwen "shiwen4@leju.com"
  3. #Install mysql module
  4. RUN apt-get update && apt-get install -y \
  5.        libfreetype6-dev \
  6.        libjpeg62-turbo-dev \
  7.        libmcrypt-dev \
  8.        libpng12-dev \
  9.    && docker-php-ext-install iconv mcrypt \
  10.    && docker-php-ext-configure mysql \
  11.    && docker-php-ext-install mysql
  12. #Install redis module
  13. RUN curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/2.2.7.tar.gz \
  14.    && tar xfz /tmp/redis.tar.gz \
  15.    && rm -r /tmp/redis.tar.gz \
  16.    && mv phpredis-2.2.7 /usr/src/php/ext/redis \
  17.    && docker-php-ext-install redis
  18. #Install Nginx
  19. RUN apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
  20. RUN echo "deb http://nginx.org/packages/mainline/debian/ jessie nginx" >> /etc/apt/sources.list
  21. ENV NGINX_VERSION 1.9.5-1~jessie
  22. RUN apt-get update && \
  23.    apt-get install -y ca-certificates nginx=${NGINX_VERSION}
  24. # forward request and error logs to docker log collector
  25. RUN ln -sf /dev/stdout /var/log/nginx/access.log
  26. RUN ln -sf /dev/stderr /var/log/nginx/error.log
  27. COPY  default.conf /etc/nginx/conf.d/default.conf
  28. VOLUME ["/var/cache/nginx"]
  29. #Install Supervisor
  30. RUN apt-get update && apt-get install -y openssh-server supervisor && \
  31.    rm -rf /var/lib/apt/lists/*
  32. RUN mkdir -p /var/run/sshd /var/run/nginx /var/log/supervisor /var/run/php-fpm
  33. COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
  34. EXPOSE 80 443 9000 22
  35. CMD ["/usr/bin/supervisord"]


构建docker image


 
 
  1. docker build -t nginx-fpm .


root用户下载官方mysql镜像

 
 
  1. docker pull mysql


启动mysql container

 
 
  1. 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用户的口令。


创建相应的数据库、表并添加数据

  
  
  1. create database yueche_leju_com;
  2. use yueche_leju_com;
  3. CREATE TABLE `yueche_log` (
  4.  `id` int(11) NOT NULL AUTO_INCREMENT,
  5.  `admin_id` int(11) DEFAULT NULL COMMENT '管理员ID',
  6.  `type` varchar(50) DEFAULT NULL COMMENT '操作标识',
  7.  `info` text COMMENT '内容',
  8.  `create_time` datetime DEFAULT NULL COMMENT '记录时间',
  9.  PRIMARY KEY (`id`)
  10. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  11. SELECT * FROM yueche_leju_com.yueche_log;
  12. insert into yueche_leju_com.yueche_log values(123,344,"fds","fds","20150509104509");
  13. commit;



编写测试代码dbtest.php

  
  
  1. <?php
  2. //-------------------数据库测试----------------------
  3. $dbhost = getenv('MYDB_PORT_3306_TCP_ADDR');
  4. $username = 'root';
  5. $userpass = getenv('MYDB_ENV_MYSQL_ROOT_PASSWORD');
  6. $dbdatabase = 'yueche_leju_com';
  7. //生成一个连接
  8. $db_connect=mysql_connect($dbhost, $username, $userpass) or die("Unable to connect to the MySQL!");
  9. //选择一个需要操作的数据库
  10. mysql_select_db($dbdatabase,$db_connect);
  11. //执行MySQL语句
  12. $result=mysql_query("SELECT * FROM yueche_log");
  13. //提取数据
  14. while($row = mysql_fetch_row($result)) {
  15. echo '<pre>';
  16. var_dump($row);
  17. }
  18. //-----------------------redis测试---------------------
  19. //$redis_config = getenv('SINASRV_CACHE_REDIS_HOST');  //形如 127.0.0.1:6379
  20. //list($host, $port) = explode(":", $redis_config);
  21. $config = array(
  22. 'host' => getenv('MYREDIS_PORT_6379_TCP_ADDR'),
  23.                 'port' => getenv('MYREDIS_PORT_6379_TCP_PORT')
  24. );
  25. var_dump($config);
  26. if(class_exists('Redis')){
  27. try{
  28. $redis  = new Redis();
  29. $redis->connect($config['host'], $config['port']);
  30. $redis->ping();
  31. }catch(Exception $e){
  32. $redis = NULL;
  33. }
  34. }else{
  35. die('can not to initial Redis!');
  36. }
  37. //设置
  38. $redis->setex('test_key', 60, 'hello world!');
  39. //获取
  40. $test_value = $redis->get('test_key');
  41. if($test_value){
  42. echo 'The Redis is running! and the value of "test_key" is : "'.$test_value.'"';
  43. }
  44. ?>


下载redis镜像

  
  
  1. docker pull redis

启动redis container

  
  
  1. docker run -d -p 6379:6379  --name myredis redis


启动nginx-fpm container

  
  
  1. 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


















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值