安装docker这里就不说了,不会的看我的博客
安装docker:https://blog.csdn.net/dabao87/article/details/103277956
docker的常用命令:https://blog.csdn.net/dabao87/article/details/86682204
安装mysql这里也不说了,在安装docker这一片有很详细的说明
安装php
这里以php版本的5.6.30为例
#拉取镜像
docker pull php:5.6.30-fpm
#在宿主机创建文件夹以便于后面的挂载
mkdir -p /mynginx/html
#运行,挂载,这里记住docker这个目录:/var/www/html,后面要用到
docker run -d -v /my/nginx/html:/var/www/html -p 9000:9000 --name phpfpm php:5.6.30-fpm
#参数说明
-d 让容器在后台运行
-p 添加主机到容器的端口映射
-v 添加目录映射,即主机上的/my和phpfpm容器中/var/www/html(phpfpm服务访问php文件目录)目录是同步的
–name 容器的名字
#查看php容器是否启动好
docker ps
#进入phpfpm容器安装mysqli扩展(只需要这一步即可)
docker-php-ext-install mysqli
#在容器里查看扩展模块
php -m
安装nginx
docker pull nginx
这时候要在宿主机上将nginx的配置文件写好才能启动nginx容器
在 vim /my/nginx/conf/nginx.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;
}
在 vim /my/nginx/conf.d/default.conf 中
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /my/nginx/html;
index index.html index.htm;
autoindex on;
#try_files $uri /index/index/page.html;
#try_files $uri /index/map/page.html;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
启动和挂载nginx
docker run --name dk_nginx -d -p 8100:8100 -v /my/nginx/html:/usr/share/nginx/html -v /my/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /my/nginx/logs:/var/log/nginx -v /my/nginx/conf.d:/etc/nginx/conf.d -d nginx:latest
查看启动的容器
docker ps -a
#如果没有启动起来,有可能是在宿主机上创建的目录没有访问权限
到此为止,安装已经完成,但是,要想完全运行起来的php,我们的路还能长,下面我们来踩坑
由于我们以及挂载好了配置文件和目录文件,接下来只要在宿主机操作就可以了
1:访问index.html页面
http://域名/index.html 访问一直是访问不上,知道肯定是配置文件不对,就改配置文件,经过一番查找后发现是端口号错了,将上面的default.conf文件中的80端口改成了8001,server_name localhost 中的localhost改成域名,访问成功了
2:修改配置可以访问php文件
在网上查找之后都说是修改
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
我将这些打开后去访问,访问不了
开始了漫漫长路啊,百度上都没有说这里面的参数对应的是哪些参数,经过我一番思索后终于解决了
root:就是刚才我让记住的那个路径
fastcgi_pass:要运行命令:docker inspect phpfpm,在最后有一个IPAddress,将这个填上去
fastcgi_param:和root一样的路径
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 172.17.0.3:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
这样就可以了