docker搭建lnmp环境基于centos

1、下载相应镜像到本地

[root@localhost lnmp]# docker pull php:7.1-fpm
[root@localhost lnmp]# docker pull mysql:5.7
[root@localhost lnmp]# docker pull nginx:1.13
[root@localhost lnmp]# docker pull redis:3.2

2、创建相应容器

# 创建php:7.1-fpm的容器并命名php7.1,将容器的9000端口映射到主机的9000端口。把主机的/home/lnmp/www/目录挂载到容器的/www目录(这个目录用于存放php脚本文件)
[root@localhost lnmp]# docker run -d -p 9000:9000 --name php7.1 -v /home/lnmp/www/:/www php:7.1-fpm

# 创建nginx:1.13的容器并命名nginx1.13,将容器的80端口映射到主机的80端口。把主机的/home/lnmp/app/nginx1.13/conf/目录挂载到容器的/etc/nginx/conf.d目录;/home/lnmp/www/目录挂载到容器的/www目录。
[root@localhost lnmp]# docker run -d -p 80:80 --name nginx1.13 -v /home/lnmp/app/nginx1.13/conf/:/etc/nginx/conf.d -v /home/lnmp/www/:/www nginx:1.13 

# 创建mysql:5.7的容器并命名mysql5.7,将容器的3306端口映射到主机的3306端口。把主机的/home/lnmp/data/mysql目录挂载到容器的/var目录。设置root的密码为123456。
[root@localhost lnmp]# docker run -d -p 3306:3306 --name mysql5.7 -v /home/lnmp/data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7

# 创建redis:3.2的容器并命名redis3.2,将容器的6379端口映射到主机的6379端口。把主机的/home/lnmp/data/redis目录挂载到容器的/data目录。设置redis的持久化保存。
[root@localhost lnmp]# docker run -d -p 6379:6379 --name redis3.2 -v /home/lnmp/data/redis:/data redis:3.2 redis-server --appendonly yes

3、相关文件的配置

3.1 nignx的配置

下面以绝对地址说明:在/home/lnmp/app/nginx1.13/conf/目录下新建nginx.conf文件,保持nginx原有文件的面貌,配置如下:

server {
    listen       80;
    server_name  192.168.0.228;
    index  index.php index.html index.htm;
    root   /www;
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
    try_files $uri $uri/ /index.php?$query_string;
    index  index.php index.html index.htm;
        # root   /usr/share/nginx/html;
        root   /www/testweb/project/ac;
    }

    #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   /www;
    }

    # 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(/|$) {
        fastcgi_pass   172.17.0.1:9000; # 这里的ip是对应PHP版本的容器的ip地址,这个为容器之间的通信,下节会有介绍
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$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;
    #}
}

在/home/lnmp/app/nginx1.13/conf/目录下新建fastcgi_params文件,配置如下:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  PATH_INFO          $fastcgi_script_name;  
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

3、容器之间的通信

1、获取各个容器的ip地址:两种方法

a、利用docker命令

[root@localhost ~]# docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
/redis3.2 - 172.17.0.2
/mysql5.7 - 172.17.0.4
/nginx1.13 - 172.17.0.3
/php7.1 - 172.17.0.1

b、装完容器后,利用iptables

[root@localhost conf]# iptables -L --line-number
num  target     prot opt source               destination         
Chain DOCKER (1 references)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  anywhere             172.17.0.1           tcp dpt:cslistener
2    ACCEPT     tcp  --  anywhere             172.17.0.2           tcp dpt:6379
3    ACCEPT     tcp  --  anywhere             172.17.0.3           tcp dpt:http
4    ACCEPT     tcp  --  anywhere             172.17.0.4           tcp dpt:mysql

上面已经说过nginx配置需要的php-fpm的ip地址就是dpt:cslistener对应的地址。其它扩展对应的地址在代码中填写连接即可,例我的代码如下:

[
        'type'=>'file',
        'debug'=>true,
        'pconnect'=>0,
        'autoconnect'=>0,
    ],
    'memcache'=>[
        'hostname'=>'172.17.0.6',
        'port'=>11211,
        'type'=>'memcache',
        'debug'=>true,
        'pconnect'=>0,
        'autoconnect'=>0,
        'pre'=>'@ns_ls_',
        'ismaster'=>true,
    ],
    'memcached'=>[
        'hostname'=>'172.17.0.6',
        'port'=>11211,
        'type'=>'memcached',
        'debug'=>true,
        'pconnect'=>0,
        'autoconnect'=>0,
        'pre'=>'@ns_ls_',
        'ismaster'=>true,
    ],
    'redis'=>[
        'hostname'=>'172.17.0.5',
        'port'=>6379,
        'timeout'=>0,
        'type'=>'redis',
        'debug'=>true,
        'pconnect'=>0,
        'autoconnect'=>0,
        'pre'=>'@ns_ls_',
        'isusecluster'=>false,
    ],
    'apc'=>[
        'type'=>'apc',
        'pre'=>'@ns_ls_',
    ],
    'xcache'=>[
        'type'=>'xcache',
        'pre'=>'@ns_ls_',
    ],
    'eaccelerator'=>[
        'type'=>'eaccelerator',
        'pre'=>'@ns_ls_',
    ],
    '__pre'=>'TnPGcc_',
];

 

你可能感兴趣的:(docker搭建lnmp环境基于centos)