docker搭建lnmp(nginx+mysql57+php7.4)

docker搭建lnmp(nginx+mysql57+php7.4)


1. 准备工作 docker安装

$ sudo yum update
$ sudo sudo yum install -y yum-utils device-mapper-persistent-data lvm2
$ sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
$ sudo yum install -y docker-ce
$ sudo systemctl start docker
$ sudo systemctl enable docker
$ sudo docker run hello-world

2.开始搭建

2.1安装docker-compose

curl -SL https://github.com/docker/compose/releases/download/v2.19.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

2.2创建项目目录

mkdir -p /home/docker/lnmp
cd /home/docker/lnmp

2.3在项目目录下面创建docker-compose.yml

version: '3'
services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx:/etc/nginx/conf.d
      - ./web:/var/www/html
    links:
      - php

  php:
    image: php:7.4-fpm
    volumes:
      - ./web:/var/www/html

  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_DATABASE: demo
      MYSQL_USER: demo
      MYSQL_PASSWORD: 123456
    volumes:
      - mysql-data:/var/lib/mysql

volumes:
  mysql-data:

2.4在该目录下面创建nginx目录,然后创建 default.conf 配置文件

server {
    listen       80;
    server_name  localhost;
    root   /var/www/html;
    index  index.php index.html index.htm;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

2.5在该目录下面创建web目录,然后创建 index.php 文件

<?php
echo phpinfo();

2.6启动docker-compose

docker-compose up -d

2.7访问

http://localhost

2.8停止docker-compose

docker-compose down

2.9删除docker-compose

docker-compose rm

3.0查看docker-compose日志

docker-compose logs

3.1查看 nginx 容器日志

docker-compose logs nginx

你可能感兴趣的:(docker,nginx,容器,运维开发,php)