windows下docker安装hyperf框架和lnmp环境搭建
docker安装hyperf框架和lnmp环境搭建!–优化了下环境搭建
giee地址
没什么事情,闲暇时间研究了下docker环境搭建。
1. 一. docker-compose.ymal文件
#镜像版本根据自己需要切换
#在该文件所在的目录下 安装:docker-compose up -d 移除:docker-compose down
#网关和文件挂载根据自己的ip自行配置
#一些挂载目录和配置文件需要自己提前在主机上创建好
version: "2"
services:
php:
#image: php:8-fpm
container_name: php #容器名称
build:
context: ./php # Dockerfile文件所在目录
dockerfile: Dockerfile
ports:
- "9100:9000"
volumes:
- D:/docker/nginx/www/:/var/www/
- D:/docker/php/php.ini:/usr/local/etc/php/php.ini
links: # php文件中 连接下面程序的host 设置为 'reids' 'mysql' 项目中配置连接host就需要改为 mysql 和 redis
- mysql
- redis
depends_on: #指定在哪个容器后启动
- mysql
- redis
networks:
mynginx:
# 网络段使用155.155 主机段指定155.3
ipv4_address: 155.155.14.3
nginx:
image: nginx:1.20.2
container_name: nginx
# 挂载宿主机端口8080
ports:
- "8080:80"
#- "443:443"
volumes:
# 挂载nginx目录
- D:/docker/nginx/www/:/usr/share/nginx/html/
# 挂载nginx日志
- D:/docker/nginx/log/:/var/log/nginx/
# 挂载nginx配置文件
- D:/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
# 挂载虚拟主机配置文件
- D:/docker/nginx/conf/conf.d/:/etc/nginx/conf.d/
links:
- php:php
depends_on:
- php
networks:
mynginx:
# 网络段使用155.155 主机段指定155.2
ipv4_address: 155.155.14.2
mysql:
image: mysql:8.0.12
container_name: mysql
# 暴露端口3306
ports:
- "3309:3306"
# 数据库数据挂载
volumes:
- D:/docker/mysql/data:/var/lib/mysql #mysql数据目录
- D:/docker/mysql/conf/my.cnf:/etc/mysql/my.cnf #mysql配置文件
- D:/docker/mysql/mysql-files/:/var/lib/mysql-files/ #映射配置文件 需要加上此目录 不然启动不了
environment:
# 创建默认homestead数据库
#- "MYSQL_DATABASE=homestead"
# 创建用户homestead
#- "MYSQL_USER=homestead"
# 设置homestead用户密码
#- "MYSQL_PASSWORD=secret"
# 设置root用户密码
- "MYSQL_ROOT_PASSWORD=root"
networks:
mynginx:
# 网络段使用155.155 主机段指定155.4
ipv4_address: 155.155.14.4
redis:
image: redis:7.0.4
container_name: redis
ports:
- "6390:6379"
volumes:
- D:/docker/redis/data:/data #持久化的数据目录
- D:/docker/redis/conf/redis.conf:/etc/redis/redis.conf #redis配置文件
privileged: true
command: redis-server /etc/redis/redis.conf --appendonly yes
networks:
mynginx:
# 网络段使用155.155 主机段指定155.5
ipv4_address: 155.155.14.5
# 同样创建mynginx网络
networks:
mynginx:
driver: bridge
ipam:
driver: default
# 子网掩码为 155.155.15.0/16
# 网关为 155.155.15.1
config:
- subnet: 155.155.14.0/16
gateway: 155.155.14.1
二. Dockerfile安装php并安装相应扩展
FROM php:8-fpm
RUN apt-get update && apt-get install -y zip procps
RUN curl -L -o /tmp/redis.tar.gz https://pecl.php.net/get/redis-5.3.7.tgz \
&& cd /tmp \
&& tar -zxvf /tmp/redis.tar.gz \
&& rm -r /tmp/redis.tar.gz \
&& mkdir -p /usr/src/php/ext \
&& mv /tmp/redis-5.3.7 /usr/src/php/ext/redis \
&& docker-php-ext-install redis \
&& pecl install swoole && docker-php-ext-enable swoole
RUN docker-php-ext-install pdo pdo_mysql mysqli pcntl bcmath sockets
#RUN docker-php-ext-enable mbstring
#安装composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
&& composer config -g repo.packagist composer https://mirrors.huaweicloud.com/repository/php/ \
&& composer self-update
#RUN cd /var/www && composer create-project hyperf/hyperf-skeleton
#RUN cd /var/www/hyperf-skeleton && composer install
#命令
CMD ["php-fpm"]
php.ini配置文件就不加了,可以自己到网上去copy一份,或者复制php.ini-production改为php.ini也可
NGINX配置文件
nginx.conf
#创建进程的用户和用户组,多个时用空格隔开
#user nobody;
#服务进程数量,一般等于 CPU 数量
worker_processes 1;
#全局错误日志定义,建议开启error级别日志.[ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log notice;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#记录进程 ID 的文件
pid /var/run/nginx.pid;
events {
#一个 worker_processe 允许的最大并发连接数量
worker_connections 1024;
}
http {
include 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 0;
#http连接的持续时间
keepalive_timeout 65;
#gzip压缩设置,开启或关闭
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
default.conf 文件 已经配置好转发PHP9501端口和websocket端口
server
{
listen 80;
server_name localhost;
#这段是我自己的laravel项目 可以忽略
location / {
root /usr/share/nginx/html/new_admin/public; #如果是laravel项目这段需要改成index文件路径
index index.html index.htm index.php;
include /usr/share/nginx/html/new_admin/public/nginx.htaccess;
autoindex off;
}
#这段是我自己的laravel项目 可以忽略
#系统的php转发规则
location ~ \.php$ {
root /var/www/new_admin/public; #如果是laravel项目这段需要改成index文件路径
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#反向代理设置
location /swoole/ { #需要在swoole后面加上 / 才能匹配所有路由 此处swoole可以根据自己随意切换,只是个名称而已,只要访问地址 后面 /name 就会走此处代理
proxy_pass http://php:9501/; #也需要加 /
#跨域配置 可以在nginx配置 也可在项目里配置
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,Keep-Alive,User-Agent,Cache-Control,Content-Type,Authorization';
if ($request_method = 'OPTIONS') {
return 204;
}
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#支持websocket链接
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
#配置代理socket.io服务
location ^~/socket.io/ {
# 执行代理访问真实服务器
proxy_pass http://php:9501;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
mysql配置 my.cnf
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
server_id=1
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
default-time-zone = '+8:00'
default-storage-engine=InnoDB
#最大链接数
max_connections=1024
#是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names=1
log_bin_trust_function_creators=1
#启用log-bin
log-bin=mysql-bin
#设置日志格式
binlog_format=mixed
#设置binlog清理时间
expire_logs_days=7
# 数据表默认时区
default-time-zone='+08:00'
# Custom config should go here
!includedir /etc/mysql/conf.d/
redis配置 redis.conf
bind 0.0.0.0
port 6379
timeout 65
maxclients 10000
databases 16
maxmemory 1048576000
appendonly yes
protected-mode no
最后附上一张自己的目录结构:
本作品采用《CC 协议》,转载必须注明作者和本文链接