一、安装docker
1.删除旧版本
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2.安装依赖,设置软件源
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
3.安装docker
yum -y install docker-ce
4.启动服务
systemctl start docker
5.查看版本
docker version
二、拉取nginx、php-fpm5.6、redis、mysql等镜像
根据需求拉取不同版本的镜像
docker pull mysql:5.6
docker pull redis
docker pull php-fpm:5.6
docker pull nginx
三、安装mysql、redis
1.安装mysql5.6
docker run --name mysql_docker -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.6
2.安装redis
docker run --name redis_docker -p 6379:6379 -d redis
docker命令
--name//容器命名
-p//设置端口
-e//传递环境变量
-d//后台运行
--link :alias//建立容器间链接 --link 容器名(id):别名
四、安装php
1.生成宿主机挂载目录
mkdir -p /usr/docker/php/www
2.生成容器
docker run --name php-fpm5.6_docker -p 9000:9000 -v /usr/docker/php/www/:/var/www/html -d docker.io/bitnami/php-fpm:5.6
五、安装nginx
1.生成宿主机挂载目录
mkdir -p /usr/docker/nginx/{conf.d,html,log}
2.生成容器
docker run --name nginx_docker --link php-fpm5.6_docker:php -p 80:80 -p 81:81 -p 82:82 -v /usr/docker/nginx/log:/var/log/nginx -v /usr/docker/nginx/conf.d:/etc/nginx/conf.d -v /usr/docker/nginx/html:/usr/share/nginx/html -d docker.io/nginx
3.进入nginx容器
docker exec -it nginx_docker bash
nginx -t//检查nginx.conf配置文件的正确性,后面会用到
4.查看nginx配置文件
cat /etc/nginx/nginx.conf
看到include /etc/nginx/conf.d/*.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;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
5.在宿主机/usr/docker/nginx/conf.d下生成*.conf文件
vim /usr/docker/nginx/conf.d/nginx.conf
server {
listen 80;
server_name 域名;
root /usr/share/nginx/html/项目1路径;
index index.php index.html index.htm;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|map|svg|ttf|woff|ico)$
{
expires 1d;
access_log off;
}
location ~ .*\.(php|php5)?$
{
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/项目2路径/$fastcgi_script_name;
include fastcgi_params;
}
location ~ .* {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?$1 last;
}
}
}
server {
listen 80;
server_name 域名;
root /usr/share/nginx/html/项目2路径;
index index.php index.html index.htm;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|map|svg|ttf|woff|ico)$
{
expires 1d;
access_log off;
}
location ~ .*\.(php|php5)?$
{
proxy_pass http://127.0.0.1:81;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/项目2路径/$fastcgi_script_name;
include fastcgi_params;
}
location ~ .* {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?$1 last;
}
}
}
6.重启nginx容器或重启服务
重启容器
docker restart nginx_docker
//在nginx容器中重启服务
nginx -s reload
7.访问
image.png