94 create craft cms site using docker - xiaoxin01/Blog GitHub Wiki
本文介绍如何使用 Nginx, Mysql, Php 的Docker镜像快速搭建 CraftCMS站点
Craft CMS 是一个PHP开发的,以内容导向的内容管理系统,号称是让开发者和内容管理人员用起来会觉得很愉快,官方网站在这里:https://craftcms.com/
它的环境主要是依赖PHP的一些扩展包,详情在这里:https://craftcms.com/docs/requirements
作为一个优秀的程序猿,我们当然不会愿意慢慢的手动搭建这些环境,利用Docker的镜像服务,可以很方便的构建出Craft CMS站点,组成如下:
- 代理服务器用Nginx
- 运行环境用 php-fpm
- 数据库用Mysql
直接可以从官网上找到最新版本的下载地址,这里以2.6版本为例:
wget https://download.craftcdn.com/craft/2.6/2.6.2986/Craft-2.6.2986.zip
unzip -d craftwww Craft-2.6.2986.zip
chmod -R 777 craftwww/craft/app
chmod -R 777 craftwww/craft/config
chmod -R 777 craftwww/craft/storage
我们把Nginx的主配置文件和站点的配置文件分开
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 2048;
multi_accept on;
use epoll;
}
http {
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log off;
error_log off;
gzip on;
gzip_disable "msie6";
client_max_body_size 500M;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-available/*;
open_file_cache max=100;
}
daemon off;
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/craftwww/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
# cache.appcache, your document html and data
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
expires -1;
access_log /var/log/nginx/static.log;
}
# Feed
location ~* \.(?:rss|atom)$ {
expires 1h;
add_header Cache-Control "public";
}
# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
# CSS and Javascript
location ~* \.(?:css|js)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}
# WebFonts
location ~* \.(?:ttf|ttc|otf|eot|woff|woff2)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
}
FROM nginx:alpine
MAINTAINER Mahmoud Zalt <[email protected]>
ADD nginx.conf /etc/nginx/
ADD www.conf /etc/nginx/sites-available/
RUN apk update \
&& apk upgrade \
&& apk add --no-cache bash \
&& adduser -D -H -u 1000 -s /bin/bash www-data \
&& rm /etc/nginx/conf.d/default.conf \
&& echo "upstream php-upstream { server php:9000; }" > /etc/nginx/conf.d/upstream.conf
CMD ["nginx"]
EXPOSE 80 443
FROM php:7.0-fpm
MAINTAINER Mahmoud Zalt <[email protected]>
RUN apt-get update && apt-get install \
libpq-dev -y \
curl \
libmemcached-dev \
mysql-client \
libpng12-dev \
libfreetype6-dev \
libcurl4-gnutls-dev \
libmcrypt-dev
# configure gd library
RUN docker-php-ext-configure gd \
--enable-gd-native-ttf \
--with-freetype-dir=/usr/include/freetype2
# Install extensions using the helper script provided by the base image
RUN docker-php-ext-install \
pdo_mysql \
pdo_pgsql \
gd \
curl \
mcrypt
# Install xdebug for php 7
RUN pecl install xdebug && \
docker-php-ext-enable xdebug
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
RUN usermod -u 1000 www-data
# Install nvm (A Node Version Manager)
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash && \
. ~/.nvm/nvm.sh && \
nvm install stable && \
nvm use stable && \
nvm alias stable && \
npm install -g gulp bower
RUN echo "" >> ~/.bashrc && \
echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bashrc && \
echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> ~/.bashrc
# Clean up
USER root
RUN apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
WORKDIR /var/www/craftwww/public
CMD ["php-fpm"]
EXPOSE 9000
version: '2'
services:
nginx:
build: ./nginx
volumes_from:
- volumes_source
volumes:
- ./logs/nginx/:/var/log/nginx
ports:
- "8001:80"
links:
- php
php:
container_name: "craftwww"
build:
context: ./php
dockerfile: Dockerfile-php-70
volumes_from:
- volumes_source
expose:
- "9000"
mysql:
image: mysql:5.7.17
volumes:
- ./mysql/mysql.cnf:/etc/mysql/conf.d/mysql.cnf:ro
volumes_from:
- volumes_data
ports:
- "3308:3306"
environment:
- MYSQL_ROOT_PASSWORD=craft
links:
- php
volumes_source:
image: tianon/true
volumes:
- ../:/var/www/craftwww
volumes_data:
image: tianon/true
volumes:
- ./data/mysql:/var/lib/mysql
进入docker目录,运行
docker-compose up -d -p craftwww
进入 mysql 容器,建立db:
CREATE DATABASE IF NOT EXISTS craftwww CHARACTER SET utf8 COLLATE utf8_unicode_ci;
修改 craft/config/db.php文件,配置为 mysql 容器
上述一切完成之后,访问下面链接会对站点进行初始化:
看到如下界面就说明成功了: