docker安装
卸载
yum remove docker docker-common docker-selinux docker-engine
安装docker需要环境
yum install -y yum-utils device-mapper-persistent-data lvm2
配置安装reps
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
#更新yum包
yum makecache fast
安装docker 并启动
yum install docker-ce
systemctl start docker
docker mysql 容器
安装mysql
mkdir -p /web/docker/mysql/logs
mkdir -p /web/docker/mysql/pid
mkdir -p /web/docker/mysql/data
mkdir -p /web/docker/mysql/conf
docker pull mysql:5.6.37
宿主机上/web/docker/mysql/conf/my.cnf 配置文件
[mysqld]
slow_query_log = ON
slow_launch_time=5
#pid-file = /web/docker/mysql/pid/mysqld.pid
#socket = /web/docker/mysql/pid/mysqld.sock
datadir = /web/docker/mysql/data
log-error = /web/docker/mysql/logs/error.log
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
启动并自定义配置文件和数据目录
docker run -p 3306:3306 --name mysql-server -v /web/docker/mysql/conf:/etc/mysql/mysql.conf.d -v /web/docker/mysql:/web/docker/mysql -e MYSQL_ROOT_PASSWORD=123345 -d mysql:5.6.37
注意这里的–name mysql-server 后续有用,因为data是宿主机上,所以下次可以省略-e MYSQL_ROOT_PASSWORD=123345
docker php 容器
安装php
docker pull php:5.6.31-fpm
启动php-fpm
docker run --name php-fpm-1 --rm -p 9000:9000 -v /web/docker/nginx/www1:/var/www/html -d php:5.6.31-fpm
安装iconv、mcrypt、gd和pdo_mysql扩展
docker exec -it container_id /bin/bash
apt-get update
apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libmcrypt-dev libpng12-dev
docker-php-ext-install -j$(nproc) iconv mcrypt
docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
docker-php-ext-install -j$(nproc) gd
exit
container_id 是你php-fpm启动后的容器id
安装redis、xdebug、memcache扩展
pecl install redis-3.1.3
pecl install xdebug-2.5.5
docker-php-ext-enable redis xdebug
apt-get install -y libmemcached-dev zlib1g-dev
pecl install memcached-2.2.0
docker-php-ext-enable memcached
停止和重启php-fpm重启
docker stop container_id
docker start container_id
注意这里的/var/www/html是php-fpm的读取源码默认位置,请勿修改后续nginx里有配置相关对应
docker nginx 容器
安装nginx
docker pull nginx:1.12.1
启动nginx,并关联mysql和php容器
mkdir -p /web/docker/nginx/logs
mkdir -p /web/docker/nginx/pid
mkdir -p /web/docker/nginx/www1
mkdir -p /web/docker/nginx/conf
docker run --name nginx-server-1 --link php-fpm-1:php-fpm --link mysql-server:mysql-server -v /web/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /web/docker/nginx:/web/docker/nginx -p 80:80 -d nginx:1.12.1
注意这里的/etc/nginx/nginx.conf是nginx的读取源码默认配置文件
/web/docker/nginx/www1/index.php
<?php
phpinfo();
?>
/web/docker/nginx/www1/index.html
nginx is working! <br />
create by qklin(object); wechat: qklandy!
/web/docker/nginx/conf/nginx.conf 配置
user nginx;
worker_processes 2;
#error_log /var/log/nginx/error.log warn;
#pid /var/run/nginx.pid;
error_log /web/docker/nginx/logs/error.log warn;
pid /web/docker/nginx/pid/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
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;
include /web/docker/nginx/conf/vhost/*.conf;
}
vhost.example.conf
server {
listen 80;
server_name localhost;
root /web/docker/nginx/www1;
location / {
index index.html index.htm index.php;
}
# limit_conn crawler 20;
location ~ ^.+\.php$ {
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include /web/docker/nginx/conf/fastcgi.conf;
fastcgi_pass php-fpm:9000;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 1h;
}
}
fastcgi.conf
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#/var/www/html/ 为php-fpm容器里源码存放的路径,这个php-fpm能解析执行php文件的关键
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
try_files $fastcgi_script_name =404;
测试nginx
curl http://localhost/
curl http://localhost/index.php
docker redis 容器
安装redis
docker pull redis:4.0.1
启动redis
docker run -d -p 6379:6379 -v /web/docker/redis:/web/docker/redis --name redis-server-1 redis:4.0.1 redis-server /web/docker/redis/conf/redis1.conf
/web/docker/redis/data/redis1 redis数据库文件夹(保存rdb和aof)
/web/docker/redis/conf/redis.conf
bind 127.0.0.1
protected-mode yes
tcp-backlog 511
timeout 0
tcp-keepalive 300
#daemonize yes
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
supervised no
#pidfile /var/run/redis_6379.pid
#loglevel notice
#logfile "/web/docker/redis/logs/redis-server.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /web/docker/redis/data/redis1
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes