版本信息

宿主机系统 CentOS Linux release 7.6.1810 (Core)
docker Version 18.09.6
docker-compose version 1.18.0
MySQL 5.6.44
PHP 7.0.33 (cli)
PHP 7.0.33 (fpm-fcgi)

创建compose_lnmp目录

  • 结构如下
compose_lnmp/
├── docker-compose.yml
└── php
    └── Dockerfile

编辑 php/Dockerfile

  • 由于官方php-fpm镜像缺少一些扩展,所以要先用dockerfile构建新的镜像
  • 注意配置时区和mysql扩展
FROM php:7.0-fpm
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng-dev \
        libmemcached-dev \
        zlib1g-dev \
        libcurl4-openssl-dev \
        libxml2-dev \
        --no-install-recommends && rm -rf /var/lib/apt/lists/* \
    && docker-php-ext-install -j$(nproc) \
        iconv mcrypt gettext curl mysqli pdo pdo_mysql zip \
        mbstring bcmath opcache xml simplexml sockets hash soap \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd

CMD ["php-fpm", "-F"]

编辑docker-compose.yml

  • 官方推荐使用volume优于bind mount
  • volume默认存放在/var/lib/docker/volumes
version: "3"

services:
  mysql:
    hostname: mysql
    restart: always
    image: mysql:5.6
    container_name: mysql
    ports:
      - "3306:3306"
    volumes:
      - mysql-config:/etc/mysql
      - mysql-log:/var/log/mysql
      - mysql-data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_USER: user
      MYSQL_PASSWORD: user123
  php:
    hostname: php
    restart: always
    container_name: php
    build:
      context: ./php
      dockerfile: Dockerfile
    ports:
      - "9000:9000"
    links:
      - mysql:mysql
    volumes:
      - nginx-html:/var/www/html
      - php-config:/usr/local/etc
  nginx:
    hostname: nginx
    restart: always
    container_name: nginx
    image: nginx:1.17.0
    ports:
      - "80:80"
      - "443:443"
    links:
      - "php:php"
    volumes:
      - nginx-config:/etc/nginx
      - nginx-log:/var/log/nginx
      - nginx-html:/usr/share/nginx/html

volumes:
  mysql-config:
  mysql-log:
  mysql-data:
  nginx-html:
  php-config:
  nginx-config:
  nginx-log:

启动服务

cd compose_lnmp

docker-compose up -d 启动
docker-compose restart 重启

数据持久化目录

  • /var/lib/docker/volumes
docker volume ls #查看volume

composelnmp_mysql-config 数据库配置文件
composelnmp_mysql-data 数据库数据文件
composelnmp_mysql-log 数据库日志
composelnmp_php-config PHP配置文件
composelnmp_nginx-config nginx配置文件
composelnmp_nginx-html nginx web目录
composelnmp_nginx-log nginx日志目录
metadata.db volumes元数据信息

php配置文件

  • /var/lib/docker/volumes/composelnmp_php-config/_data
  • 默认的php.ini文件是没有的,需要手动把模板配置文件复制为php.ini
  • 同时修改时区
cd /var/lib/docker/volumes/composelnmp_php-config/_data/php
cp php.ini-production php.ini

修改php.ini
date.timezone = Asia/Shanghai

nginx配置

  • /var/lib/docker/volumes/composelnmp_nginx-config/_data/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ \.php$ {
        fastcgi_pass   php:9000; #这里php就是php容器的名字
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name; #此处配置要注意,有坑
        include        fastcgi_params;
    }
}

测试验证

查看容器状态

  • docker container ls
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                  PORTS                                                                  NAMES
b7531773beaa        nginx:1.17.0              "nginx -g 'daemon of…"   14 minutes ago      Up 7 minutes            0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp                               nginx
aaebabd0a32a        composelnmp_php           "docker-php-entrypoi…"   14 minutes ago      Up 7 minutes            0.0.0.0:9000->9000/tcp                                                 php
df7b8bd4975b        mysql:5.6                 "docker-entrypoint.s…"   14 minutes ago      Up 7 minutes            0.0.0.0:3306->3306/tcp                                                 mysql

验证mysql

[root@localhost ~]# docker container exec -it mysql bash 
root@mysql:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.44    |
+-----------+
1 row in set (0.00 sec)

mysql>
查mysql数据文件
[root@localhost _data]# pwd
/var/lib/docker/volumes/composelnmp_mysql-data/_data
[root@localhost _data]# ls
auto.cnf  ibdata1  ib_logfile0  ib_logfile1  mysql  performance_schema

查mysql配置文件
[root@localhost _data]# pwd
/var/lib/docker/volumes/composelnmp_mysql-config/_data
[root@localhost _data]# ls
conf.d  my.cnf  my.cnf.fallback  mysql.cnf  mysql.conf.d

查mysql日志
[root@localhost _data]# pwd
/var/lib/docker/volumes/composelnmp_mysql-log/_data
[root@localhost _data]# ls
error.log

验证php访问

  • /var/lib/docker/volumes/composelnmp_nginx-html/_data/test.php
<?php phpinfo();
  • 浏览器访问http://服务器IP/test.php
    在这里插入图片描述
    在这里插入图片描述

Primary script unknown错误处理

  • 最开始部署完后访问测试index.php会报404
  • nginx日志显示"Primary script unknown"
2019/07/11 10:51:25 [error] 7#7: *4 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.159.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.18.0.3:9000", host: "192.168.159.128"
192.168.159.1 - - [11/Jul/2019:10:51:25 +0000] "GET /index.php HTTP/1.1" 404 27 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "-"
  • 网上很多说改nginx配置是不对的
#修改为$document_root依然解决不了问题
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 

#正确应该是
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
  • 还有很多说是php和nginx运行的用户不一致,或者目录权限不对;这些经我测试都是不准的
GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐