Docker-compose 一键部署 Nginx+PHP+MySql+Redis 环境
经过上面docker分别单独部署,nginx,PHP,mysql,redis服务。今天经过半天终于用docker-compose集成了Nginx+PHP+MySql+Redis环境,一键部署,确实方便太多了。
好了,回归正题,下面我部署环境的流程。
准备工作:
安装 Docker(参考之前文章)
安装 Docker-compose (参考之前文章)
大家可以用我集成好的docker-compose文件,部署分为三步。
1、下载文件 docker-compose 部署包
下载地址:https://www.aliyundrive.com/s/AwX2ff4ZLyu 提取码: 2f8x
Gitee仓库地址 :https://gitee.com/lingfeng9527/docker-compose.git
2、解压上传到服务器任务一个位置
3、执行命令:
docker-compose up -d
好了,已经部署好了。访问地址就可以看到结果。
HTML:
PHP文件访问
测试MySQL连接:
测试Redis:
可以看到redis也是没什么问题了。
上面已经验证了结果,接下来我们分别说明集成过程,嫌麻烦的朋友或者熟手可以不用看下面的内容!
首先准备工作,准备Nginx+PHP+MySql+Redis环境所需要的文件夹和文件:
docker-compose 文件:
version: '3' #docker-compose语法的版本
services:
php: #创建 php的容器
container_name: php74
image: php:7.4.3-fpm
ports:
- "9000:9000"
restart: always
privileged: true
links:
- "mysql"
environment:
- TZ=Asia/Shanghai
build: ./php #直接到 ./php文件下找Dockerfile
volumes:
- ./www:/www #把容器的/var/www 映射到./www
nginx: #创建 nginx容器
container_name: nginx
image: nginx:latest
ports: #映射 80和443端口到本机
- 80:80
- 8001:8001
- 4433:443
restart: always
depends_on:
- "php"
links:
- "php:php74"
environment:
- TZ=Asia/Shanghai
privileged: true
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf #把容器的nginx.conf映射到 ./nginx/nginx.conf
- ./nginx/conf.d:/etc/nginx/conf.d #把容器的conf.d映射到 ./nginx/conf.d
- ./www:/usr/share/nginx/html #把容器的/usr/share/nginx/html 映射到./www
- ./nginx/logs:/var/log/nginx #把容器的/var/log/nginx 映射到./nginx/logs
mysql:
container_name: mysql
image: mysql:latest
volumes:
- ./mysql/data:/var/lib/mysql #把容器的数据文件夹映射到 ./mysql/data
- ./mysql/logs:/var/log/mysql #把容器的日志文件夹映射到 ./mysql/log
- ./mysql/conf:/etc/mysql/conf.d #把容器的my.conf文件映射到 ./mysql/my.conf 暂时去掉
ports:
- 3306:3306 #把容器的3306端口映射到本机的3306
restart: always
privileged: true
environment:
- TZ=Asia/Shanghai
- MYSQL_ROOT_PASSWORD=469ge9g449646he #root密码(我这里乱输的,自己可改)
- MYSQL_USER=test #另一个帐号
- MYSQL_PASS=123456 #另一个帐号的密码
redis: #创建 reids的容器
container_name: redis
image: redis:latest
restart: always
ports:
- "6379:6379"
environment:
- TZ=Asia/Shanghai
volumes:
- ./redis/conf/redis.conf:/etc/redis/redis.conf #把容器的配置文件夹映射到 ./redis/config/redis.conf
- ./redis/data:/data
command: /bin/sh -c "echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
&& redis-server /etc/redis/redis.conf --appendonly yes" # 指定配置文件并开启持久化
privileged: true # 使用该参数,container内的root拥有真正的root权限。否则,container内的root只是外部的一个普通用户权限
docker-compose 文件就是用来管理各个docker容器服务。
www:这个主要是放所有的网站项目,html文件或者PHP文件,如果需要放其他语言的文件,可以参考我的方法集成其他环境。
Nginx:Nginx所有相关的配置文件、日志文件
MySQL:相关的配置文件,数据文件,日志文件
PHP:Dockerfile文件,主要是安装PHP的相关东西,包括PHP扩展。
Redis:redis配置和数据文件
mkdir /docker-compose/mysql
mkdir /docker-compose/mysql/conf
touch /docker-compose/mysql/conf/my.conf
mkdir /docker-compose/mysql/data
mkdir /docker-compose/mysql/logs
mkdir /docker-compose/nginx
mkdir /docker-compose/nginx/conf.d
mkdir /docker-compose/nginx/conf.d/www.test.com.conf
mkdir /docker-compose/nginx/conf.d/www.testphp.com.conf
mkdir /docker-compose/nginx/logs
touch /docker-compose/nginx/nginx.conf
mkdir /docker-compose/php
touch /docker-compose/php/Dockerfile
mkdir /docker-compose/redis
mkdir /docker-compose/redis/conf
touch /docker-compose/redis/conf/redis.conf
mkdir /docker-compose/redis/data
mkdir /docker-compose/www
touch /docker-compose/www/index.html #可以放入一些html文件
touch /docker-compose/www/php_dome/index.php #可以放入一些php文件
touch docker-compose.yml
my.conf 文件内容:
[mysqld]
max_allowed_packet = 20M
innodb_buffer_pool_size=128M
query_cache_size=56M
key_buffer_size = 16M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
log-bin=/var/lib/mysql/binlog
server-id=344
binlog_format=ROW
www.test.com.conf 文件内容:
server {
listen 80;
server_name 192.168.11.146;
location / {
#注意这里的root目录是容器的网站目录
root /usr/share/nginx/html/;
index index.html index.htm index.php;
}
}
www.testphp.com.conf 文件内容:
server {
listen 8001;
server_name 192.168.11.146;
location / {
root /usr/share/nginx/html/php_demo;
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;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;
include fastcgi_params;
}
}
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;
client_max_body_size 66M;
gzip on;
include /etc/nginx/conf.d/*.conf;
}
PHP Dockerfile 文件内容:
FROM php:7.4.3-fpm
# packages
RUN apk --update add \
autoconf \
build-base \
linux-headers \
libaio-dev \
zlib-dev \
curl \
git \
subversion \
freetype-dev \
libjpeg-turbo-dev \
libmcrypt-dev \
libpng-dev \
libtool \
libbz2 \
bzip2 \
bzip2-dev \
libstdc++ \
libxslt-dev \
openldap-dev \
imagemagick-dev \
make \
unzip \
wget && \
docker-php-ext-install bcmath mcrypt zip bz2 pdo_mysql mysqli simplexml opcache sockets mbstring pcntl xsl && \
docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && \
pecl install imagick && \
docker-php-ext-enable imagick && \
pecl install swoole && \
docker-php-ext-enable swoole && \
docker-php-ext-install gd && \
docker-php-ext-enable opcache && \
apk del build-base \
linux-headers \
libaio-dev \
&& rm -rf /var/cache/apk/*
ENV COMPOSER_ALLOW_SUPERUSER 1
ENV COMPOSER_HOME /tmp
ENV COMPOSER_VERSION 1.5.1
RUN curl -s -f -L -o /tmp/installer.php https://raw.githubusercontent.com/composer/getcomposer.org/da290238de6d63faace0343efbdd5aa9354332c5/web/installer \
&& php -r " \
\$signature = '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410'; \
\$hash = hash('SHA384', file_get_contents('/tmp/installer.php')); \
if (!hash_equals(\$signature, \$hash)) { \
unlink('/tmp/installer.php'); \
echo 'Integrity check failed, installer is either corrupt or worse.' . PHP_EOL; \
exit(1); \
}" \
&& php /tmp/installer.php --no-ansi --install-dir=/usr/bin --filename=composer --version=${COMPOSER_VERSION} \
&& rm /tmp/installer.php \
&& composer --ansi --version --no-interaction
VOLUME /var/www
WORKDIR /var/www
CMD php-fpm
好了,到这里基本上都介绍完了。
还没有评论,快来发表第一个评论吧