Docker容器

centos下docker-compose搭建lnmp环境

本文主要是介绍centos下docker-compose搭建lnmp环境,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

所有操作均在root权限下进行

sudo -i

 

1、新建文件夹【/root/lnmp】和文件【/root/lnmp/docker-compose.yml】

mkdir /root/lnmp
vi /root/lnmp/docker-compose.yml

 

2、写入yml文件内容

version: "3"  
services:   
  nginx:    
     image: nginx:latest
     container_name: c_nginx
     ports:
      - "80:80"
     #centos文件夹【/root/lnmp/nginx/html】和容器文件夹【/usr/share/nginx/html】进行绑定,并设rw权限
     #centos文件【/root/lnmp/nginx/conf/default.conf】和容器文件【/etc/nginx/conf.d/default.conf】进行绑定
     volumes:
      - /root/lnmp/nginx/html/:/usr/share/nginx/html/:rw
      - /root/lnmp/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf
     #设置上海时区
     environment:
      TZ: "Asia/Shanghai"
     #容器自启
     restart: always
     #加入lnmp的网络
     networks:
      - lnmp
  php:
    image: php:7.3.29-fpm
    container_name: php_01
    volumes:
      - /root/lnmp/nginx/html/:/var/www/html/:rw
    restart: always
    cap_add:
      - SYS_PTRACE
    networks:
      - lnmp

  mysql:
    image: mysql:5.6
    container_name: mysql56
    ports:
      - "3306:3306"
    volumes:
      - /root/lnmp/mysql/data:/var/lib/mysql/:rw
    restart: always
    networks:
      - lnmp
    environment:
      MYSQL_ROOT_PASSWORD: "123456"
      TZ: "Asia/Shanghai"
networks:   
  #新建自定义网络【lnmp】
  lnmp:

  

 

 

3、新建文件夹【/root/lnmp/nginx】、【/root/lnmp/nginx/conf】和文件【/root/lnmp/nginx/conf/default.conf】

mkdir /root/lnmp/nginx
mkdir /root/lnmp/nginx/conf
vi /root/lnmp/nginx/conf/default.conf

  

4、写入conf文件内容

server {
    listen       80;
    root   /usr/share/nginx/html;
    index   index.html index.php;


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

    location / {
        index  index.html index.php ;
        try_files $uri $uri/ /index.php?$query_string;
        autoindex  on;
    }


    location ~ \.php$ {
        #php_01是yml里的容器名
        fastcgi_pass   php_01:9000;
        fastcgi_index  index.php;
        include        fastcgi_params;
        fastcgi_param  PATH_INFO $fastcgi_path_info;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
    }

}

  

5、进入lnmp文件夹启动yml文件

cd /root/lnmp
docker-compose up -d

  

 

这篇关于centos下docker-compose搭建lnmp环境的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!