docker部署PHP项目
docker通过文件映射方式部署PHP项目
自从用了docker进行项目部署后,发现越来越香,前面有跟大家分享过如何通过将项目跟环境进行打包的方式部署php项目,今天跟大家分享另一种部署方式,直接将PHP项目映射到PHP容器,同时将nginx跟php进行关联的方式进行部署,这种部署方式就少了构建镜像的步骤。
这里我们PHP镜像就用之前分享过自己构建的镜像,大家可以看这一篇Docker构建PHP镜像,假设服务器PHP项目是在/www/wwwroot目录下,如果我们的PHP项目有定时调度任务,我这里用到的是laravel项目,项目刚好需要用到定时调度,我们可以创建一份定时调度配置映射到PHP容器里,因为我之前构建的PHP镜像默认是开启了crontab功能。
我们在服务器home目录下创建一个php目录用来存放我们的启动文件,我们先创建一份文件名为crontab的文件,存放我们定时调度的任务,配置如下
*/30 * * * * /usr/local/bin/php /www/wwwroot/personSpaces/artisan schedule:run */30 * * * * /usr/local/bin/php /www/wwwroot/personBlog/artisan schedule:run */30 * * * * /usr/local/bin/php /www/wwwroot/onedrive/one.php cache:refresh
这里我配置了三个定时调度任务。
我们可以创建我们的启动文件(docker-compose.yml)文件,配置如下
version: "3" services: php: image: hongzhuangxian/php-fpm7.3-diy container_name: docker_php_compose restart: always ports: - 9101:9000 volumes: - /www/wwwroot:/www/wwwroot - /home/php/crontab:/var/spool/cron/crontabs/root
这里我们将网站目录映射到容器的/www/wwwroot目录,同时将定时调度配置映射到容器/var/spool/cron/crontabs/root目录下,然后我们输入以下命令启动
docker-compose up -d
最后我们配置Nginx配置,假设服务器内网ip是127.0.1.1,连接PHP容器可以用
127.0.1.1:9101
配置如下
server {
listen 80;
server_name personSpace.test.com;
#charset koi8-r;
location / {
root /var/www/html/personSpace/public;
index index.php index.html index.htm;
# 如果没有以下4行,laravel将只能访问首页,其他页面都是404
try_files $uri $uri/ /index.php?$query_string;
if (!-e $request_filename){
rewrite ^/(.*) /index.php last;
}
# 如果没有以上4行,laravel将只能访问首页,其他页面都是404
}
#
location ~ \.php$ {
root /var/www/html/personSpaces/public;
index index.php index.html;
# 坑在这里,需将原有的127.0.0.1:9000替换成phpfpm:9000
fastcgi_pass 127.0.1.1:9101;
# 坑在这里,需将原有的127.0.0.1:9000替换成phpfpm:9000
fastcgi_index index.php;
# 下面这行也改了 中间的$document_root
fastcgi_param SCRIPT_FILENAME $document_root$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;
#}
}大家可以根据自己服务器的内网ip,自行替换127.0.1.1,配置完成之后,记得重载nginx配置。
这样我们就成功的通过映射方式部署PHP项目。
0条评论