使用docker-compose部署LNMP环境

1、创建相关compose存放目录

< 5 docker-test - [root]: > mkdir -p /apps/compose_lnmp/Nginx
< 5 docker-test - [root]: >  cd !$
< 5  docker-test - [root]: /apps/compose_lnmp/Nginx >

2、下载Nginx软件包,创建dockerfile

< 6  docker-test - [root]: /apps/compose_lnmp/Nginx > # wget http://Nginx.org/download/Nginx-1.14.2.tar.gz

< 7  docker-test - [root]: /apps/compose_lnmp/Nginx > # vim Dockerfile

FROM centos:7
MAINTAINER gujiwork
RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel
ADD Nginx-1.14.2.tar.gz /tmp
RUN cd /tmp/Nginx-1.14.2 && ./configure --prefix=/usr/local/Nginx && make -j 4 && make install
copY Nginx.conf  /usr/local/Nginx/conf

EXPOSE 80
workdir /usr/local/Nginx
CMD ["./sbin/Nginx", "-g", "daemon off;"]

3、这里面用到了Nginx.conf,放到compose_lnmp/Nginx目录下,将 fastcgi_pass 127.0.0.1:9000改成PHP容器名, fastcgi_pass php-cgi:9000;

#user  Nginx;
worker_processes  2;
worker_cpu_affinity 0101 1010;

#error_log  logs/error.log;
error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/Nginx.pid;

events {
    worker_connections  10240;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;  

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush on;
    tcp_nodelay on;
    server_names_hash_bucket_size 128;
    server_names_hash_max_size 512;
    client_header_timeout 15s;
    client_body_timeout 15s;
    send_timeout 60s;

    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   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   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   php-cgi: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;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
include vhost/*.conf;
}

4、创建mysql目录,数据和配置文件做持久化,这里我们使用MysqL官方镜像,因此不需要写dockerfile

< 39  docker-test - [root]: /apps/compose_lnmp > # tree  MysqL/
MysqL/
|-- conf
|   `-- my.cnf
`-- data
< 40  docker-test - [root]: /apps/compose_lnmp > # cat MysqL/conf/my.cnf 
[MysqLd]
user=MysqL
port=3306
datadir=/var/lib/MysqL
socket=/var/lib/MysqL/MysqL.socket
pid-file=/var/run/MysqL/MysqL.pid
log_error=/var/log/MysqL/error.log
character_set_server = utf8
max_connections=3600

5、创建PHP目录及dockerfile,PHP.ini主要修改了时区为shanghai

< 49  docker-test - [root]: /apps/compose_lnmp > # tree  PHP/
PHP/
|-- Dockfile
|-- PHP-5.6.31.tar.gz
`-- PHP.ini
< 50  docker-test - [root]: /apps/compose_lnmp > # cat PHP/Dockfile 
FROM centos:7
MAINTAINER gujiwork
RUN yum install -y gcc gcc-c++ gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel make
ADD PHP-5.6.31.tar.gz /tmp/
RUN cd /tmp/PHP-5.6.31 && \
    ./configure --prefix=/usr/local/PHP \
    --with-config-file-path=/usr/local/PHP/etc \
    --with-MysqL --with-MysqLi \
    --with-openssl --with-zlib --with-curl --with-gd \
    --with-jpeg-dir --with-png-dir --with-iconv \
    --enable-fpm --enable-zip --enable-mbstring && \
    make -j 4 && make install && \
    cp /usr/local/PHP/etc/PHP-fpm.conf.default /usr/local/PHP/etc/PHP-fpm.conf && \
    sed -i "s/127.0.0.1/0.0.0.0/" /usr/local/PHP/etc/PHP-fpm.conf && \
    cp ./sapi/fpm/init.d.PHP-fpm /etc/init.d/PHP-fpm && \
    chmod +x /etc/init.d/PHP-fpm 
copY PHP.ini /usr/local/PHP/etc

EXPOSE 9000
CMD /etc/init.d/PHP-fpm start && tail -F /var/log/messages

6、创建docker-compose维护文件

< 53  docker-test - [root]: /apps/compose_lnmp > # cat docker-compose.yml 
version: '3'
services:
  Nginx:
    hostname: Nginx
    build:
      context: ./Nginx
      dockerfile: Dockerfile
    ports:
      - 80:80
    links:
      - PHP:php-cgi
    volumes:
      - ./wwwroot:/usr/local/Nginx/html

  PHP:
    hostname: PHP
    build: ./PHP
    links:
      - MysqL:MysqL-db
    volumes:
      - ./wwwroot:/usr/local/Nginx/html

  MysqL:
    hostname: MysqL
    image: MysqL:5.6
    ports:
      - 3306:3306
    volumes:
      - ./MysqL/conf:/etc/MysqL/conf.d
      - ./MysqL/data:/var/lib/MysqL
    #command: --character-set-server=utf8
    environment:
      MysqL_ROOT_PASSWORD: 123456
      MysqL_DATABASE: wordpress
      MysqL_USER: user
      MysqL_PASSWORD: user123

7、创建web挂载目录,整体目录结构如下

< 56  docker-test - [root]: /apps/compose_lnmp > # tree .
.
|-- docker-compose.yml
|-- MysqL
|   |-- conf
|   |   `-- my.cnf
|   `-- data
|-- Nginx
|   |-- Dockerfile
|   |-- Nginx-1.14.2.tar.gz
|   `-- Nginx.conf
|-- PHP
|   |-- Dockfile
|   |-- PHP-5.6.31.tar.gz
|   `-- PHP.ini
`-- wwwroot

6 directories, 8 files

8、执行build构建镜像

< 81  docker-test - [root]: /apps/compose_lnmp > # docker-compose  build

9、出现success表示构建成功

make[1]: Leaving directory `/tmp/Nginx-1.14.2'
Removing intermediate container b7fb79c4671e
---> b756345aac5b
Step 6/9 : copY Nginx.conf /usr/local/Nginx/conf
---> cb180351db65
Step 7/9 : EXPOSE 80
---> Running in 20805a3b58a5
Removing intermediate container 20805a3b58a5
---> 9f75c3834969
Step 8/9 : workdir /usr/local/Nginx
---> Running in 6abf5341ee7b
Removing intermediate container 6abf5341ee7b
---> 0cb88354c8b8
Step 9/9 : CMD ["./sbin/Nginx", "-g", "daemon off;"]
---> Running in a2db489f2b5b
Removing intermediate container a2db489f2b5b
---> 76ae0759f3b1
Successfully built 76ae0759f3b1
Successfully tagged compose_lnmp_Nginx:latest

10、启动服务测试

< 82  docker-test - [root]: /apps/compose_lnmp > # docker-compose  up -d
Creating network "compose_lnmp_default" with the default driver
Pulling MysqL (MysqL:5.6)...
5.6: Pulling from library/MysqL
177e7ef0df69: Pull complete
cac25352c4c8: Pull complete
8585afabb40a: Pull complete
1e4af4996053: Pull complete
c326522894da: Pull complete
50ec9776c6b3: Pull complete
b81a89945365: Pull complete
80f5ab6567ca: Pull complete
5caf5e4c5eb0: Pull complete
9295ceea71e2: Pull complete
fb029976ca26: Pull complete
Creating compose_lnmp_MysqL_1_32333e982f89 ... done
Creating compose_lnmp_PHP_1_856b9f701287   ... done
Creating compose_lnmp_Nginx_1_c5360c9dc627 ... done

< 144  docker-test - [root]: /apps/compose_lnmp/wwwroot > # echo "111" >index.html

#测试
< 144  docker-test - [root]: /apps/compose_lnmp/wwwroot > # curl localhost/
111

相关文章

今天小编给大家分享一下excel图案样式如何设置的相关知识点,...
这篇文章主要讲解了“win10设置过的壁纸如何删除”,文中的讲...
这篇“Xmanager怎么显示远程linux程序的图像”文章的知识点大...
今天小编给大家分享一下xmanager怎么连接linux的相关知识点,...
这篇“如何重置Linux云服务器的远程密码”文章的知识点大部分...
本篇内容介绍了“Linux云服务器手动配置DNS的方法是什么”的...