一余寥、安裝docker
1.刪除舊版本
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2.安裝依賴领铐,設(shè)置軟件源
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.啟動(dòng)服務(wù)
systemctl start docker
5.查看版本
docker version
二、拉取nginx宋舷、php-fpm5.6绪撵、redis、mysql等鏡像
根據(jù)需求拉取不同版本的鏡像
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//設(shè)置端口
-e//傳遞環(huán)境變量
-d//后臺(tái)運(yùn)行
--link <name or id>:alias//建立容器間鏈接 --link 容器名(id):別名
四、安裝php
1.生成宿主機(jī)掛載目錄
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.生成宿主機(jī)掛載目錄
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.進(jìn)入nginx容器
docker exec -it nginx_docker bash
nginx -t//檢查nginx.conf配置文件的正確性改艇,后面會(huì)用到
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.在宿主機(jī)/usr/docker/nginx/conf.d下生成*.conf文件
vim /usr/docker/nginx/conf.d/nginx.conf
server {
listen 80;
server_name 域名;
root /usr/share/nginx/html/項(xiàng)目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/項(xiàng)目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/項(xiàng)目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/項(xiàng)目2路徑/$fastcgi_script_name;
include fastcgi_params;
}
location ~ .* {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?$1 last;
}
}
}
6.重啟nginx容器或重啟服務(wù)
重啟容器
docker restart nginx_docker
//在nginx容器中重啟服務(wù)
nginx -s reload
7.訪問