一、在Docker中安装环境(PHP、Nginx、MySQL、Redis、memcached、MongoDB)
1、安装Docker
2、查看安装软件版本
# docker -v / docker --version
# docker-compose -v
3、在代码根目录新建docker安装环境所属目录:/data1/project/docker
4、在 /data1/project/docker 新建 init.sh 脚本
5、# chmod a+x init.sh
6、添加以下命令加入到 /data1/project/docker/init.sh 文件中保存(#####中的所有内容)
##########################################################################
mkdir app
mkdir app/memcached
mkdir app/mysql
mkdir app/nginx
mkdir app/nginx/conf
mkdir app/php
mkdir app/redis
mkdir app/mongo
mkdir db
mkdir logs
mkdir tools
mkdir tools/phpmyadmin
mkdir tools/console
mkdir web
touch app/memcached/Dockerfile
touch app/mysql/Dockerfile
touch app/nginx/Dockerfile
touch app/php/Dockerfile
touch app/redis/Dockerfile
touch app/mongo/Dockerfile
touch tools/phpmyadmin/Dockerfile
touch tools/console/Dockerfile
touch app/nginx/conf/default.conf
touch app/php/opcache-recommended.ini
touch app/php/php.ini
touch app/php/proxychains.conf
touch app/php/proxychains-ng
touch docker-compose.yaml
curl -L -o ./app/php/redis.tar.gzhttps://github.com/phpredis/phpredis/archive/3.0.0.tar.gz
curl -L -o ./app/php/memcached.tar.gz "https://codeload.github.com/php-memcached-dev/php-memcached/tar.gz/v3.0.2"
curl -L -o ./app/php/mongodb.tgzhttps://github.com/mongodb/mongo-php-driver/releases/download/1.3.2/mongodb-1.3.2.tgz
cat /dev/null > docker-compose.yaml
cat >> docker-compose.yaml <
version: "2"
services:
mysql:
build:
context: ./app/mysql
dockerfile: Dockerfile
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: "app"
MYSQL_USER: "app"
MYSQL_PASSWORD: "111111"
volumes:
- ./db:/var/lib/mysql
php:
build:
context: ./app/php
dockerfile: Dockerfile
volumes:
- ./web:/opt/web
nginx:
build:
context: ./app/nginx
dockerfile: Dockerfile
ports:
- 8080:80
depends_on:
- php #此处代表nginx依赖php,php会在nginx之前启动
volumes_from:
- php
volumes:
- ./app/nginx/conf:/etc/nginx/conf.d
redis:
build:
context: ./app/redis
dockerfile: Dockerfile
# ports:
# - 6379:6379
memcached:
build:
context: ./app/memcached
dockerfile: Dockerfile
# ports:
# - 11211:11211
mongo:
build:
context: ./app/mongo
dockerfile: Dockerfile
# ports:
# - 27017:27017
phpmyadmin:
build:
context: ./tools/phpmyadmin
dockerfile: Dockerfile
ports:
- "8081:80"
environment:
PMA_HOST: "mysql"
PMA_USER: "root"
PMA_PASSWORD: "root"
console:
build:
context: ./tools/console
dockerfile: Dockerfile
volumes_from:
- php
tty: true
#networks:
volumes:
db:
driver: local
EOF
cat /dev/null > ./app/php/Dockerfile
cat >> ./app/php/Dockerfile <
FROM php:7.1.11-fpm
# 以下几行为安装代理工具,如不需要,可将其屏蔽
# COPY proxychains-ng /opt/proxychains-ng
# COPY proxychains.conf /etc/proxychains.conf
# WORKDIR /opt/proxychains-ng
# RUN ./configure && make && make install
# 替换为阿里云源
RUN echo "debhttp://mirrors.aliyun.com/debian/jessie main non-free contrib\n\\
debhttp://mirrors.aliyun.com/debian/jessie-proposed-updates main non-free contrib\n\\
deb-srchttp://mirrors.aliyun.com/debian/jessie main non-free contrib\n\\
deb-srchttp://mirrors.aliyun.com/debian/jessie-proposed-updates main non-free contrib" > /etc/apt/sources.list
# 安装php扩展
WORKDIR /tmp
ENV PHPREDIS_VERSION 3.0.0
COPY ./redis.tar.gz /tmp/redis.tar.gz
COPY ./memcached.tar.gz /tmp/memcached.tar.gz
COPY ./mongodb.tgz /tmp/mongodb.tgz
COPY ./yar.tgz /tmp/yar.tgz
RUN tar xfz /tmp/redis.tar.gz \
&& rm -r /tmp/redis.tar.gz \
&& tar xf /usr/src/php.tar.xz \
&& mv php-7.1.11 /usr/src/php \
&& mv phpredis-$PHPREDIS_VERSION /usr/src/php/ext/redis \
&& mkdir -p /usr/src/php/ext/memcached \
&& tar -C /usr/src/php/ext/memcached -zxf /tmp/memcached.tar.gz --strip 1 \
&& rm /tmp/memcached.tar.gz \
&& mkdir -p /usr/src/php/ext/mongodb \
&& tar -C /usr/src/php/ext/mongodb -zxf /tmp/mongodb.tgz --strip 1 \
&& rm -r /tmp/mongodb.tgz \
&& mkdir -p /usr/src/php/ext/yar \
&& tar -C /usr/src/php/ext/yar -zxf /tmp/yar.tgz --strip 1 \
&& rm -r /tmp/yar.tgz
# 安装php
RUN apt-get clean \
&& apt-get update \
&& apt-get -y remove libssl1.0.0 \
&& apt-get install -y libpng12-dev libjpeg-dev libmemcached-dev libssl-dev libcurl4-openssl-dev \
&& rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr \
&& docker-php-ext-install gd mysqli pdo_mysql zip opcache redis \
&& docker-php-ext-configure memcached \
&& docker-php-ext-install memcached \
&& docker-php-ext-configure mongodb \
&& docker-php-ext-install mongodb \
&& docker-php-ext-configure yar \
&& docker-php-ext-install yar \
&& docker-php-ext-configure bcmath \
&& docker-php-ext-install bcmath
# 生成配置文件
COPY ./php.ini /usr/local/etc/php/conf.d/
COPY ./opcache-recommended.ini /usr/local/etc/php/conf.d/
EOF
echo "FROM nginx:1.12.1" > ./app/nginx/Dockerfile
echo "FROM mysql:5.7" > ./app/mysql/Dockerfile
echo "FROM memcached:1.5" > ./app/memcached/Dockerfile
echo "FROM redis:4.0.0" > ./app/redis/Dockerfile
echo "FROM mongo:3.5.13" > ./app/mongo/Dockerfile
echo "FROM phpmyadmin/phpmyadmin:4.7" > ./tools/phpmyadmin/Dockerfile
cat >> ./tools/console/Dockerfile <
FROM php:7.1.11
WORKDIR /mnt/app
# 替换为阿里云源
RUN echo "debhttp://mirrors.aliyun.com/debian/jessie main non-free contrib\n\\
debhttp://mirrors.aliyun.com/debian/jessie-proposed-updates main non-free contrib\n\\
deb-srchttp://mirrors.aliyun.com/debian/jessie main non-free contrib\n\\
deb-srchttp://mirrors.aliyun.com/debian/jessie-proposed-updates main non-free contrib" > /etc/apt/sources.list
# 常用工具
RUN apt-get update && apt-get install -y git curl wget cron vim locales libfreetype6-dev mariadb-client \\
&& rm -rf /var/lib/apt/lists/* \\
&& docker-php-ext-install zip \\
&& docker-php-ext-install mysqli pdo_mysql
# 把语言设置成简体中文
RUN dpkg-reconfigure locales && \\
locale-gen C.UTF-8 && \\
/usr/sbin/update-locale LANG=C.UTF-8
RUN echo 'zh_CN.UTF-8 UTF-8' >> /etc/locale.gen && \\
locale-gen
ENV LC_ALL C.UTF-8
ENV LANG zh_CN.UTF-8
ENV LANGUAGE zh_CN.UTF-8
RUN php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');" \\
&& php composer-setup.php \\
&& php -r "unlink('composer-setup.php');" \\
&& mv ./composer.phar /usr/local/bin/composer \\
&& chmod +x /usr/local/bin/composer
RUN echo 'export PATH="\$PATH:\$HOME/.composer/vendor/bin"' >> ~/.bashrc \\
&& . ~/.bashrc \\
&& composer config -g repo.packagist composerhttps://packagist.phpcomposer.com
EOF
echo "opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=1
opcache.fast_shutdown=1
opcache.enable_cli=1
opcache.enable=0" > ./app/php/opcache-recommended.ini
echo "memory_limit = 256M
post_max_size = 100M
upload_max_filesize = 100M" > ./app/php/php.ini
cat >> ./app/nginx/conf/default.conf <
server {
listen 80;
server_name localhost;
root /opt/web;
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;
}
}
EOF
##########################################################################
7、当前 /data1/project/docker 目录下运行init.sh脚本
# ./init.sh
8、若下载超时 或 失败 ,多次执行 # ./init.sh 命令即可
9、执行docker-compose up -d 来启动环境、并安装
10、确保Mac/Linux环境端口8080未被占用,执行期间若error报错端口已占用,则更改Mac/Linux环境端口,不被占用即可
11、Mac/Linux环境端口改完重启当前系统,执行
# docker-compose down
12、再次执行编译安装
# docker-compose up -d
13、查看已存在镜像
# docker images
14、查看服务是否已启动
# docker ps -a
此时可以查看到已经安装:nginx、mongo、php、mysql、redis、phpmyadmin、memcached
15、如果后续增加PHP扩展,vi /data1/project/docker/app/php/Dockerfile 增加扩展,然后关闭,再启动并且编译
# docker-compose down
# docker-compose up -d --build
16、若要远程连接mysql、memcache、redis ........ 则编辑 vi /data1/project/docker/docker-compose.yaml
(截图范围有限,若要远程连接,都增加端口开放即可)
17、关闭容器,在重启容器,查看当前所有容器的端口均已开启
# docker-compose down
# docker-compose up -d
# docker ps -a
18、测试mysql连接虚拟机Docker中的MySQL
打开Navicat(此处的3307端口是 /data1/project/docker/docker-compose.yaml 中MySQL增加)
IP:10.0.1.145
port:3307
username:root
password:root
二、在Docker内配置Nginx开发环境设置(此处配置laravel框架路由)
注意:默认安装的容器访问根目录为:/opt/web/
1、Nginx配置文件默认为:/data1/project/docker/app/nginx/conf/default.conf
server {
listen 80;
server_name docker.laravel.com;
root /opt/web/laravel/public;
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、重启docker Nginx 环境
# docker restart docker_nginx_1
17、本地主机配置host域名:127.0.0.1 docker.laravel.com ,所有配置文件访问均已映射到宿主机:8080端口去访问docker:80端口,本地浏览器访问
eg:docker.laravel.com:8080
18、本地docker目录:/data1/project/docker/web 与任意容器目录:/opt/web/形成互相映射,在本地目录或者容器目录操作项目文件即可
19、进入docker_php_1容器
# docker exec -it docker_php_1 /bin/bash
或
# docker exec -it docker_php_1 bash