lnmp
lnmp的容器編排衣吠。
實(shí)現(xiàn)對(duì)多個(gè)容器的編排:nginx茶敏、mysql、php-fpm缚俏。
支持一鍵構(gòu)建啟動(dòng)和銷(xiāo)毀惊搏。
支持配置的修改和日志的查看。方便開(kāi)發(fā)和測(cè)試忧换。
支持快速修改構(gòu)建中的一些參數(shù)恬惯,直接修改 .env 文件
目前版本只合適作為日常開(kāi)發(fā)和測(cè)試環(huán)境。不適用于生產(chǎn)環(huán)境亚茬。
參考:忘了酪耳。。。待會(huì)兒找到補(bǔ)上=碗暗。=
算了不補(bǔ)了
可以直接使用laradock集成的很好:https://github.com/laradock/laradock
版本的選擇
- centos latest
- nginx latest
- mysql latest //mysql 8.0
- php latest
安裝使用說(shuō)明
1颈将、安裝 docker 百度三連.jpg
2、安裝 docker-compose 百度三連.jpg
3言疗、構(gòu)建環(huán)境:在當(dāng)前文件文件目錄執(zhí)行:docker-compose up --build
根據(jù)個(gè)人網(wǎng)絡(luò)情況一般20分鐘內(nèi)
4晴圾、開(kāi)始使用
完整資源下載:https://pan.baidu.com/s/1WLVSOFgTvLkWcgN-MAqY8w 提取碼:vmxv
ps:
1、因?yàn)閿?shù)據(jù)庫(kù)使用的是mysql 8.0.16噪奄,mysql8和之前的版本密碼加密方式不一樣死姚,從 mysql_native_password 改為了 caching_sha2_password ,連接數(shù)據(jù)庫(kù)時(shí)可能會(huì)報(bào)錯(cuò)勤篮。
解決方式:方法1都毒、使用 navcat mysql 12 等支持 caching_sha2_password 加密的高版本連接工具
方法2、docker 內(nèi)部用root登錄數(shù)據(jù)后叙谨,依次執(zhí)行下面3條命令:
select Host,User,plugin,authentication_string from mysql.user;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
FLUSH PRIVILEGES;
readme.md說(shuō)明:
##目錄說(shuō)明
docker20190621
|- db_data //mysql數(shù)據(jù)存放目錄
|- log //日志存放目錄
|- mysql //mysql日志
|- nginx //nginx日志
|- php //php日志
|- php-fpm //php-fpm日志
|- mysql
|- mysql.conf.d
|- mysqld.cnf //mysql 配置文件
|- Dockerfile //mysql 構(gòu)建dockerfile
|- nginx
|- conf //nginx vhost配置文件目錄
|- default.conf //nginx 默認(rèn)配置
|- Dockerfile //nginx 構(gòu)建dockerfile
|- nginx.conf //nginx 配置
|- php-fpm
|- Dockerfile //php 構(gòu)建dockerfile
|- php.ini //php.ini
|- sources.list //資源列表
|- www //web項(xiàng)目存放路徑
|- .env //docker-compose構(gòu)建鏡像時(shí)加載的配置
|- docker-compose.yml //yml文件沒(méi)啥好說(shuō)的
|- README.md //說(shuō)明
## 相關(guān)命令
開(kāi)發(fā)中常用的命令温鸽。
- 拷貝nginx的配置文件到 宿主電腦桌面。
docker cp 容器id:/etc/nginx/nginx.conf /Users/Win用戶名/Desktop
- 拷貝php-fpm的配置文件到 宿主電腦桌面手负。
docker cp 容器id:/usr/local/etc/php-fpm.d/www.conf /Users/Win用戶名/Desktop
- php 安裝擴(kuò)展涤垫。目前只安裝了一個(gè)mysqli的擴(kuò)展。其他擴(kuò)展參考下面的地址竟终。后續(xù)本版本也不斷豐富蝠猬。
https://store.docker.com/images/php
- 拷貝mysql的配置文件到宿主電腦桌面。
docker cp 容器id:/etc/mysql/mysql.conf.d/ /Users/Win用戶名/Desktop
- 以下命令都需要在 .yml 文件所在目錄執(zhí)行
啟動(dòng)所有容器
docker-compose up
重新編譯并啟動(dòng)所有容器
docker-compose up --build
停止所有容器
docker-compose stop
銷(xiāo)毀所有容器
docker-compose down
- 重新編譯服務(wù)鏡像
docker-compose build --force-rm --no-cache
- 重新啟動(dòng)服務(wù)
docker-compose up -d --force-recreate
- 進(jìn)入容器內(nèi).例如進(jìn)入容器內(nèi)
docker exec 容器id -it /bin/bash
- 查看某個(gè)容器的日志统捶。查看容器運(yùn)行的日志榆芦。
docker logs 容器id
## 數(shù)據(jù)庫(kù)
- 外部工具連接容器數(shù)據(jù)庫(kù)的默認(rèn)配置是:
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USERNAME=root
DB_PASSWORD=root
下面上配置 :
- /.env
#存放項(xiàng)目的文件夾
SOURCE_DIR=./www
# http地址
HTTP_HOST=80
# NGINX配置
NGINX_VERSION=alpine
NGINX_CONFD_DIR=./nginx/conf
NGINX_CONF_FILE=./nginx/conf
NGINX_LOG_DIR=./log/nginx
# PHP配置
PHP_VERSION=7.2
PHP_CONFIG_FILE=./php/conf/php.ini
PHP_FPM_CONFIG_FILE=./php/conf/php-fpm.conf
PHP_FPM_LOG=./log/php-fpm
PHP_LOG=./log/php
# MYSQL配置
MYSQL_PORT=3306
MYSQL_ROOT_USER=root
MYSQL_ROOT_PASSWORD=root
MYSQL_TEST_USER=test
MYSQL_TEST_PASSWORD=test
MYSQL_DATA_DIR=./db_data
MYSQL_LOG=./log/mysql
# MYSQL_CONF=./mysql/conf
# MYSQL_MY_CNF=./mysql/my.cnf
- /docker-compose.yml
#版本號(hào)
version: "3"
#服務(wù)
services:
#mysql服務(wù)
db:
#使用mysql目錄下的Dockerfile構(gòu)建鏡像
#image: mysql:5.7
build: ./mysql
command: --default-authentication-plugin=mysql_native_password
#磁盤(pán)映射。./db_data 是宿主的目錄喘鸟。后者是容器里的目錄匆绣。
volumes:
- ${MYSQL_DATA_DIR}:/var/lib/mysql
- ${MYSQL_LOG}:/var/log/mysql
restart: always
# 端口映射。前者是宿主的什黑。后者是容器里端口
ports:
- "${MYSQL_PORT}:3306"
#環(huán)境變量
environment:
#mysql的root密碼
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
#容器會(huì)創(chuàng)建的數(shù)據(jù)庫(kù)
MYSQL_DATABASE: dockerdb
#test用戶
MYSQL_USER: ${MYSQL_TEST_USER}
#test用戶的密碼
MYSQL_PASS: ${MYSQL_TEST_PASSWORD}
#=======================================================
#php-fpm服務(wù)
php:
#服務(wù)器鏡像構(gòu)建的目錄崎淳。會(huì)在該目錄下尋找dockerfile構(gòu)建鏡像。
build: ./php-fpm
#依賴的服務(wù)愕把。會(huì)在容器里的host文件里添加 一條記錄拣凹。訪問(wèn)db,即可訪問(wèn)mysql的服務(wù)恨豁。
depends_on:
- db
#目錄映射
volumes:
- ${SOURCE_DIR}:/www
- ${PHP_FPM_LOG}:/var/log/php-fpm
- ${PHP_LOG}:/var/log/php
restart: always
#=======================================================
#nginx服務(wù)
nginx:
#使用nginx目錄下的Dockerfile構(gòu)建鏡像
build: ./nginx
depends_on:
- php
#目錄映射
volumes:
- ${SOURCE_DIR}:/www
- ${NGINX_LOG_DIR}:/var/log/nginx
- ${NGINX_CONFD_DIR}:/etc/nginx/conf.d
#端口映射
ports:
- "${HTTP_HOST}:80"
restart: always
- /mysql/Dockerfile:
# 基礎(chǔ)鏡像
FROM mysql:latest
# 設(shè)置時(shí)區(qū)
RUN echo "Asia/shanghai" > /etc/timezone;
RUN rm -rf /etc/localtime;
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 將本地mysql.conf.d下的文件復(fù)制到鏡像中
ADD ./mysql.conf.d/mysqld.cnf /etc/mysql/mysql.conf.d/
- /mysql/mysql.conf.d
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
# 時(shí)區(qū)
default-time_zone = '+8:00'
log_timestamps = SYSTEM
# 是否啟用通用查詢?nèi)罩?general-log = on
general_log_file = /var/log/mysql/mysql.log
# 錯(cuò)誤日志地址
log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# 慢查詢?nèi)罩?slow_query_log = 1
long-query-time = 1 #慢查詢時(shí)間 超過(guò)1秒則為慢查詢
slow_query_log_file = /var/log/mysql/slow.log
# 默認(rèn)使用“mysql_native_password”插件認(rèn)證
default_authentication_plugin=mysql_native_password
#default_authentication_plugin=caching_sha2_password
- /nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
- /nginx/Dockerfile
# 基礎(chǔ)鏡像
FROM nginx:latest
# 設(shè)置時(shí)區(qū)
RUN echo "Asia/shanghai" > /etc/timezone;
RUN rm -rf /etc/localtime;
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#修改配置嚣镜。站點(diǎn)目錄配置和php 請(qǐng)求。
ADD nginx.conf /etc/nginx/
ADD /conf/default.conf /etc/nginx/conf.d/
- /nginx/conf/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log /var/log/nginx/log/host.access.log main;
location / {
root /www;
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 /usr/share/nginx/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 /www;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www$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;
#}
}
- /php-fpm/Dockerfile
#基礎(chǔ)鏡像
FROM php:7.2-fpm
# 設(shè)置時(shí)區(qū)
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
# 將本地源文件復(fù)制到鏡像
ADD sources.list /etc/apt/sources.list
# 更新系統(tǒng) && 安裝依賴庫(kù)
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
# 安裝一些常用擴(kuò)展
&& docker-php-ext-install -j$(nproc) iconv \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install bcmath zip opcache iconv pdo pdo_mysql mysqli
# apt-get安裝擴(kuò)展
RUN apt-get install -y libmcrypt-dev \
&& pecl install mcrypt-1.0.1 \
&& docker-php-ext-enable mcrypt
# pecl安裝擴(kuò)展
RUN pecl install redis-4.0.1 \
&& pecl install xdebug-2.6.0 \
&& docker-php-ext-enable redis xdebug
# 將本地php.ini復(fù)制到鏡像中
ADD php.ini /usr/local/etc/php/php.ini
- /php-fpm/sources.list
# 默認(rèn)第一個(gè)方案可能會(huì)報(bào)zlib1g-dev依賴的錯(cuò)誤
# deb http://mirrors.ustc.edu.cn/debian/ stretch main non-free contrib
# deb http://mirrors.ustc.edu.cn/debian/ stretch-updates main non-free contrib
# deb http://mirrors.ustc.edu.cn/debian/ stretch-backports main non-free contrib
# deb-src http://mirrors.ustc.edu.cn/debian/ stretch main non-free contrib
# deb-src http://mirrors.ustc.edu.cn/debian/ stretch-updates main non-free contrib
# deb-src http://mirrors.ustc.edu.cn/debian/ stretch-backports main non-free contrib
# deb http://mirrors.ustc.edu.cn/debian-security/ stretch/updates main non-free contrib
# deb-src http://mirrors.ustc.edu.cn/debian-security/ stretch/updates main non-free contrib
#=================================================
# 方案二:使用中科大源處理zlib1g-dev依賴的錯(cuò)誤
deb http://mirrors.ustc.edu.cn/debian stable main contrib non-free
deb-src http://mirrors.ustc.edu.cn/debian stable main contrib non-free
deb http://mirrors.ustc.edu.cn/debian stable-proposed-updates main contrib non-free
deb-src http://mirrors.ustc.edu.cn/debian stable-proposed-updates main contrib non-free
#=================================================
# 鏡像源
# 163源 http://mirrors.163.com/
# 阿里源 http://mirrors.aliyun.com/
# 中科大源 http://mirrors.ustc.edu.cn/