Docker使用docker-composer.yml部署PHP环境

6个月前 Linux 1127

常用命令:
docker rm -f mysql php-fpm nginx 删除容器

docker-compose up -d 执行docker-composer.yml

docker images 查看镜像

docker start nginx php-fpm mysql 重启容器

docker ps -a 查看运行中的容器

一、docker环境准备

安装docker环境 和 docker-composer 自习百度。

$ sudo yum install docker
$ sudo groupadd docker
$ sudo usermod -aG docker $USER
$ sudo systemctl start docker.service

二、lnmp环境准备

#默认最新   
$ docker pull nginx
$ docker pull php:fpm
$ docker pull mysql
$ docker pull redis

三、准备配置文件

Php-fpm 记得解压

#准备宿主机与容器映射用于配置的文件目录

#mysql配置
#准备文件夹
$ mkdir -p /www/dnmp/conf/mysql
$ mkdir -p /www/dnmp/conf/mysql/conf.d
$ mkdir -p /www/dnmp/data/mysql
#复制mysql配置 到本地
$ docker run --name mysql -d mysql
$ docker cp mysql:/etc/mysql/my.cnf /www/dnmp/conf/mysql
$ docker cp mysql:/etc/mysql/conf.d /www/dnmp/conf/mysql
#删除容器
$ docker rm -f mysql

#nginx配置
#准备文件夹
$ mkdir -p /www/dnmp/conf/nginx
$ mkdir -p /www/dnmp/www
$ mkdir -p /www/dnmp/conf/nginx/letsencrypt
$ docker run --name nginx -d nginx
$ docker cp nginx:/etc/nginx/nginx.conf /www/dnmp/conf/nginx
$ docker cp nginx:/etc/nginx/conf.d /www/dnmp/conf/nginx
$ docker rm -f nginx

#php配置 
#php.ini/php-fpm.conf配置
#准备文件夹
$ mkdir -p /www/dnmp/conf/php
$ mkdir -p /www/dnmp/www
$ docker run --name php-fpm -d php:fpm
#php容器 里面的php.tar.xz  需要解压才能使用
$ docker exec -it php-fpm tar Jxvf /usr/src/php.tar.xz
#这里注意php的版本。  如果不清楚 可以进入php-fpm  看一下  我这里是最新的php-7.3.0
#复制php.ini 到宿主机来
$ docker cp php-fpm:/var/www/html/php-7.3.4/php.ini-production /www/dnmp/conf/php/php.ini
$ docker cp php-fpm:/usr/local/etc/php-fpm.conf /www/dnmp/conf/php/
$ docker cp php-fpm:/usr/local/etc/php-fpm.d /www/dnmp/conf/php/
#方便下次使用 不用再解压  这里可以把解压后的php-fpm 容器打包成新的镜像
$ docker commit php-fpm php7.3
$ docker rm -f php-fpm

找到本地的php.ini 修改配置

cgi.fix_pathinfo=0

四、修改环境配置

在本地新建docker-composer.yml

version: '3' 
services:   
    php-fpm:
        image: php7.3.4
        expose:
            - 9000
        ports:
           - 9000:9000
        container_name: php_1
        volumes:
            - ./conf/php/php-fpm.conf:/usr/local/etc/php-fpm.conf 
            - ./conf/php/php-fpm.d:/usr/local/etc/php-fpm.d
            - ./conf/php/php.ini:/var/www/html/php-7.3.4/php.ini
            - ./www:/usr/share/nginx/html
            - ./conf/logs/php:/www/dnmp/conf/logs/php
    
    nginx:
        image: nginx
        container_name: nginx_1
        ports:
            - "${NGINX_HTTP_HOST_PORT}:80"
            -  443:443
        volumes:
            - ./www:/usr/share/nginx/html
            - ./conf/nginx/nginx.conf:/etc/nginx/nginx.conf
            - ./conf/nginx/conf.d:/etc/nginx/conf.d
            - ./conf/nginx/letsencrypt:/etc/letsencrypt
            - ./conf/logs/nginx:/www/dnmp/conf/logs/nginx
    
    mysql:
        image: mysql_my
        ports:
           - 3306:3306
        container_name: mysql_1
        environment:
            - MYSQL_ROOT_PASSWORD=qwert123yuiop
        volumes:
            #-  /www/dnmp/config/mysql/my.cnf:/etc/mysql/my.cnf:ro
            -  /data/mysql:/var/lib/mysql  
            
 
    # redis:
    #     image: redis:latest
    #     #expose:
    #     #    - 6379
    #     networks:
    #         - rdb
 
    #     restart: always
    #     container_name: redis
    #     command: redis-server --appendonly yes 
    #     volumes:
    #         - /docker/data/redis:/data

nginx 配置

default.conf

server {
    listen       80;
  
    root   /usr/share/nginx/html;
    index  index.php index.html;
    server_name  localhost;
    
 
    location ~ \.php$ {
        fastcgi_pass   php-fpm:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;#这里注意
        include        fastcgi_params;

    }
}



# server {
#     listen       80;
#     server_name  localhost;

#     #charset koi8-r;
#     #access_log  /var/log/nginx/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;
#     #}

#     # deny access to .htaccess files, if Apache's document root
#     # concurs with nginx's one
#     #
#     #location ~ /\.ht {
#     #    deny  all;
#     #}
# }

多站点

server {
    listen 80;
    root /usr/share/nginx/html/demo;
    index index.php index.html index.htm;
    server_name demo.com;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
        fastcgi_pass   php-fpm:9000;
        fastcgi_index  index.php;
    #   fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/demo/$fastcgi_script_name;#这里注意
        include        fastcgi_params;

    }
}

在nginx.conf

添加

include /etc/nginx/conf.d/demo.com.conf;

参考:

https://www.yupengsir.com/topic/content?i=163



本文为脚本叔叔原创文章,转载无需和我联系,但请注明来自脚本叔叔http://jianghaihao.com

目前有1条留言
  1. 脚本叔叔:

    123

    2019-05-16 09:33:10 [回复]

发表评论

全站搜索

    Table of Contents