当前位置: 首页 > 工具软件 > Docker-PHP > 使用案例 >

docker配置php+mysql+nginx

唐晗昱
2023-12-01

docker基础

1.安装docker

yum install -y docker

2.查看配置版本

docker version

3.docker基础操作

#非root用户使用 前面加 sudo
systemctl start docker    # 启动docker
systemctl stop docker     # 停止docker
systemctl status docker   # 查看docker状态
systemctl restart docker  # 重新启动docker

4.启动docker之前修改docker镜像源

vim /etc/docker/daemon.json
​
{
"registry-mirrors": ["https://ftnejmh3.mirror.aliyuncs.com"]
}

5.启动docker服务。

systemctl restart docker

docker搭建nginx

1.查找 Docker Hub 上的 nginx 镜像,这里直接拉取官方的镜像

docker search nginx

2.拉取nginx的docker镜像

docker pull nginx

3.查看镜像列表

docker images

4.创建nginx配置

mkdir /docker/nginx
​
mkdir /docker/nginx/conf.d
​
mkdir /docker/nginx/conf.d/http
​
mkdir /docker/nginx/conf.d/tcp
​
#注意,此处创建的nginx.conf会覆盖docker容器默认的nginx.conf
#所以此处需要设置系统默认的配置信息,覆盖创建的目的是配置自定义的文件格式,引入nginx代理tcp的功能
vim /docker/nginx/nginx.conf

5.nginx.conf配置信息如下

user  nginx;
worker_processes  1;
​
pid        /var/run/nginx.pid;
error_log  /var/log/nginx/error.log warn;
​
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 /dev/null;
    #access_log  /var/log/nginx/access.log  main;
​
    sendfile        on;
    #tcp_nopush     on;
​
    keepalive_timeout  65;
​
    gzip  on;
    
    #引入http配置文件
    include /etc/nginx/conf.d/http/*.conf;
}
​
#引入tcp配置文件
stream {
    include /etc/nginx/conf.d/tcp/*.conf;
}

6.http的配置模板

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    root /usr/share/nginx/html;
    index index.html index.php;
    location / {
    }
​
    location /api/test/ {
           proxy_set_header Host $host; #防止nginx修改请求头的host为代理host
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Port $http_port;
           proxy_pass http://172.17.0.3:9619/;
    }
​
}

7.tcp的配置模板

upstream config-ssh {
   hash $remote_addr consistent;
   server 172.17.0.3:9501 weight=5 max_fails=3 fail_timeout=30s;
}
server {
   #监听端口
   listen 9501;
   proxy_connect_timeout 10s;
   #设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
   proxy_timeout 300s;
   proxy_pass config-ssh;
}
​

8.创建nginx容器

#注意:此处若是想要用到其他端口,就需要映射指定的端口,默认给定 9501 - 9509
docker run -d --name nginx \
-p 80:80 -p 9501:9501 -p 9502:9502 -p 9503:9503 -p 9504:9504 -p 9505:9505 \
-p 9506:9506 -p 9507:9507 -p 9508:9508 -p 9509:9509 \
-v /docker/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /docker/nginx/conf.d:/etc/nginx/conf.d \
-v /docker/www:/docker/www \
--privileged=true nginx:latest

docker搭建php

1.先通过docker search php查找镜像,这里直接拉取官方的镜像,标签为7.4-fpm,其他版本的可自行选择

docker pull php:7.4-fpm

2.使用php镜像开启php-frm应用容器

docker run -p 9000:9000 -d --name php -v/docker/www:/docker/www --privileged=true php:7.4-fpm

3.查看php的docker地址

docker inspect php | grep "IPAddress"

4.nginx转发php代理

server {
    listen 80;
    listen [::]:80;
    server_name localhost;
    root /docker/www/lmrs-2008/public;
    index index.php index.html;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    error_page 500 502 503 504 /50x.html;
    
    location = /50x.html {
        root /docker/www/lmrs-2008/public;
    }
    
    location ~ \.php$ {
        root /docker/www/lmrs-2008/public;
        fastcgi_pass 172.17.0.3:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

docker 搭建mysql

1.先通过docker search mysql查找镜像,这里直接拉取官方的镜像

docker search mysql

2.拉取镜像

docker pull mysql

3.配置宿主机的mysql配置文件

vim /etc/my_docker.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
​
[mysqld]
secure_file_priv=/var/lib/mysql
port = 3306
socket = /tmp/mysql.sock
datadir = /usr/local/mysql/data
default_storage_engine = InnoDB
performance_schema_max_table_instances = 400
table_definition_cache = 400
skip-external-locking
key_buffer_size = 32M
max_allowed_packet = 100G
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 4K
read_buffer_size = 768K
read_rnd_buffer_size = 256K
myisam_sort_buffer_size = 8M
thread_cache_size = 16
tmp_table_size = 32M
default_authentication_plugin = mysql_native_password
lower_case_table_names = 1
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
explicit_defaults_for_timestamp = true
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
​
server-id=1
log-bin=mysql-bin
relay-log=relay-log
​
binlog_format=mixed
​
binlog_expire_logs_seconds = 600000
slow_query_log=1
slow-query-log-file=/usr/local/mysql/data/mysql-slow.log
long_query_time=3
early-plugin-load = ""
innodb_data_home_dir = /usr/local/mysql/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/data
innodb_buffer_pool_size = 128M
innodb_log_file_size = 64M
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 1
innodb_write_io_threads = 1
​
[mysqldump]
quick
max_allowed_packet = 500M
​
[mysql]
no-auto-rehash
​
[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 768K
read_buffer = 2M
write_buffer = 2M
​
[mysqlhotcopy]
interactive-timeout

4.创建镜像容器

docker run -p 3306:3306 -d --name mysql \
-v /etc/my_docker.cnf:/etc/mysql/my.cnf --privileged=true \
-e MYSQL_ROOT_PASSWORD=root mysql

5.进入容器,连接mysql配置一个自己的用户,用于项目使用。(以下操做非必要)

create user `starsky`@`%` identified by "root";
​
grant all on *.* to `starsky`@`%` with grant option;

 类似资料: