docker部署nginx+php+mysql

首先创建一个放置docker映射nginx容器和php容器得文件夹

拉取nginx镜像

docker pull nginx:1.18.0

创建挂载目录

mkdir -p /home/admin/nginx/conf
mkdir -p /home/admin/nginx/log
mkdir -p /home/admin/nginx/www

生成运行一个nginx容器

docker run -d --name nginxtest nginx

把容器里的nginx目录复制出来,配置是放在/etc/nginx

docker cp nginxtest:/etc/nginx /home/admin/nginx/conf

把容器里的log目录复制出来,日志是放在/var/log

docker cp nginxtest:/var/log /home/admin/nginx/log

把容器的程序目录映射出来

docker cp nginxtest:/usr/share/nginx/www /home/admin/nmp/www

删除测试的nginx-test

docker rm -f nginxtest

查看nginx配置结构

[root@localhost ~]# tree nginx
nginx
├── conf.d
│   └── default.conf
├── fastcgi_params
├── mime.types
├── modules -> /usr/lib/nginx/modules
├── nginx.conf
├── scgi_params
└── uwsgi_params

查看log日志结构

[root@localhost ~]# tree log
log
├── apt
│   ├── eipp.log.xz
│   ├── history.log
│   └── term.log
├── btmp
├── dpkg.log
├── faillog
├── lastlog
├── nginx
│   ├── access.log -> /dev/stdout
│   └── error.log -> /dev/stderr
└── wtmp

拉取php镜像

docker pull php:7.4.28-fpm

检查镜像是否拉取成功

docker images

运行一个测试的php容器

docker run -d --name phptest php:7.4.28-fpm

把容器里的php目录复制出来,进容器php -i | grep php.ini可查看配置文件目录

docker cp phptest:/usr/local/etc/php /home/admin/nmp/php

删除测试的php-test

docker rm -f phptest

查看php目录

[root@localhost ~]# tree php
php
├── conf.d
│   └── docker-php-ext-sodium.ini
├── php.ini-development(改名php.ini)
└── php.ini-production

拉取mysql镜像

docker pull mysql:5.7

#先运行一个测试mysql容器或者直接创建把容器里的mysql目录复制出来

docker cp mysqltest:/var/log/mysql /home/admin/nmp/mysql/log
docker cp mysqltest:/var/lib/mysql /home/admin/nmp/mysql/data
docker cp mysqltest:/etc/mysql /home/admin/nmp/mysql/conf

查看容器ip

docker inspect 容器ID

运行并创建nginx容器

docker run -p 80:80 -d --name nginx1180 -v /home/admin/nmp/www:/usr/share/nginx/www -v /home/admin/nmp/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /home/admin/nmp/log:/var/log/nginx c2c45d506085;

运行并创建php容器

docker run -p 9000:9000 -d --name php74 -v /home/admin/nmp/www:/usr/share/nginx/www -v /home/admin/nmp/php/conf.d/php.ini:/usr/local/etc/php/php.ini 08ccd6665390

运行并创建mysql容器

docker run -p 3306:3306 -d  --name mysql57 --restart unless-stopped -v  /home/admin/nmp/mysql/log:/var/log/mysql -v  /home/admin/nmp/mysql/data:/var/lib/mysql -v  /home/admin/nmp/mysql/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=123456 3147495b3a5c

由于在docker容器内转发php所以要使用php容得IP进行转发 nginx conf.d default.conf相关配置

server {
    listen 80;
    server_name zxft.daiying.net.cn;
    index index.php index.html index.htm default.php default.htm default.html;
    root   /usr/share/nginx/www/public;
  
    location / {
        index  index.html index.htm index.php;
          if (!-e $request_filename) {
             rewrite  ^(.*)$  /index.php?s=/$1  last;
             break;
         }
         #autoindex  on;
    }
    location ~ \.php(.*)$ {
       #这里的ip 通过 docker inspect php容器ID 查看 ipAddress
        fastcgi_pass   172.17.0.2:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include        fastcgi_params;
    }
 
 
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
     
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires 30d;
        error_log off;
        access_log off;
    }
     
    location ~ .*\.(js|css)?$
    {
        expires 12h;
        error_log off;
        access_log off; 
    } 
 }

nginx 反向代理设置

location ~ ^/ {
  proxy_pass http://10.4.123.99:80;
  proxy_set_header Host $host;
  proxy_set_header X-Real-Ip $remote_addr;
  proxy_read_timeout 120s;
}

会飞的鱼博客
请先登录后发表评论
  • latest comments
  • 总共0条评论