千家信息网

使用docker-compose部署LNMP环境

发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,1、创建相关compose存放目录< 5 docker-test - [root]: > mkdir -p /apps/compose_lnmp/nginx< 5 docker-test - [roo
千家信息网最后更新 2025年12月02日使用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 DockerfileFROM centos:7MAINTAINER gujiworkRUN yum install -y gcc gcc-c++ make openssl-devel pcre-develADD nginx-1.14.2.tar.gz /tmpRUN cd /tmp/nginx-1.14.2 && ./configure --prefix=/usr/local/nginx && make -j 4 && make installCOPY nginx.conf  /usr/local/nginx/confEXPOSE 80WORKDIR /usr/local/nginxCMD ["./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=mysqlport=3306datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.socketpid-file=/var/run/mysql/mysql.pidlog_error=/var/log/mysql/error.logcharacter_set_server = utf8max_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:7MAINTAINER gujiworkRUN yum install -y gcc gcc-c++ gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel makeADD 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/etcEXPOSE 9000CMD /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`-- wwwroot6 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---> b756345aac5bStep 6/9 : COPY nginx.conf /usr/local/nginx/conf---> cb180351db65Step 7/9 : EXPOSE 80---> Running in 20805a3b58a5Removing intermediate container 20805a3b58a5---> 9f75c3834969Step 8/9 : WORKDIR /usr/local/nginx---> Running in 6abf5341ee7bRemoving intermediate container 6abf5341ee7b---> 0cb88354c8b8Step 9/9 : CMD ["./sbin/nginx", "-g", "daemon off;"]---> Running in a2db489f2b5bRemoving intermediate container a2db489f2b5b---> 76ae0759f3b1Successfully built 76ae0759f3b1Successfully tagged compose_lnmp_nginx:latest

10、启动服务测试

< 82  docker-test - [root]: /apps/compose_lnmp > # docker-compose  up -dCreating network "compose_lnmp_default" with the default driverPulling mysql (mysql:5.6)...5.6: Pulling from library/mysql177e7ef0df69: Pull completecac25352c4c8: Pull complete8585afabb40a: Pull complete1e4af4996053: Pull completec326522894da: Pull complete50ec9776c6b3: Pull completeb81a89945365: Pull complete80f5ab6567ca: Pull complete5caf5e4c5eb0: Pull complete9295ceea71e2: Pull completefb029976ca26: Pull completeCreating compose_lnmp_mysql_1_32333e982f89 ... doneCreating compose_lnmp_php_1_856b9f701287   ... doneCreating 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
0