欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker:Nginx-Redis-Mysql-PHP 部署

程序员文章站 2022-07-15 13:25:59
...

Docker:Nginx-Redis-Mysql-PHP 部署

网络桥接

Docker容器之间默认网络隔离,需要使用桥接网络进行互通

创建网络

docker network create net-local

docker network ls

NETWORK ID     NAME        DRIVER    SCOPE
da9c8fc3dc80   bridge      bridge    local
786414beb4b3   host        host      local
75b536a6e1ff   net-local   bridge    local
ac79ba73f784   none        null      local

容器关联网络: —network [网络名称]


依赖redis和mysql容器,优先启动

Redis

拉取镜像

docker pull redis:latest

启动容器

docker run -itd --name redis-local -p 6379:6379 --network net-local redis

Mysql

拉取镜像

docker pull mysql:latest

docker run -itd --name mysql-local -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --network net-local mysql

PHP

选用7.4-buster版本

docker pull php:7.4-fpm-buster

++注意:alpine版本为了减轻镜像体积,去除了很多命令和扩展,而buster版本中丰富了些基础命令,如:apt++

新建镜像配置文件

FROM php:7.4-fpm-buster
#COPY [--chown=www-data:www-data] install.sh kafka_start.sh /tmp/
#RUN chmod 777 /tmp/install.sh && /tmp/install.sh

RUN apt update \
    # 相关依赖必须手动安装
    && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng-dev \
        librdkafka-dev \
        autoconf \
        automake \
        libtool \
        curl \
        wget \
    # 生成php源码默认扩展目录
    && docker-php-source extract \
    # 下载predis扩展包
    && curl -L -o /tmp/reids.tar.gz "https://codeload.github.com/phpredis/phpredis/tar.gz/5.0.2" \
    && cd /tmp/ \
    && tar -zxvf reids.tar.gz \
    && mv phpredis-5.0.2 /usr/src/php/ext/phpredis \
    # 安装kafka
    && wget http://archive.apache.org/dist/kafka/0.10.2.0/kafka_2.11-0.10.2.0.tgz \
    && tar -zxvf kafka_2.11-0.10.2.0.tgz \
    && mv kafka_2.11-0.10.2.0/ /opt/kafka \
    # 安装php-kafka
    && wget https://github.com/arnaud-lb/php-rdkafka/archive/4.0.2.tar.gz \
    && tar -zxvf 4.0.2.tar.gz \
    && mv php-rdkafka-4.0.2 /usr/src/php/ext/kafka \
    # 安装扩展
    && cd /usr/src/php/ext \
    # 如果安装的扩展需要自定义配置时
    #&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install iconv phpredis pcntl pdo_mysql kafka \
    # 需要php.ini中配置"extension=mcrypt.so;"
    && pecl install mcrypt

按照配置文件建立镜像

docker build -f ~/Documents/Work/Docker/Php/Dockerfile -t php:7.4-fpm-local-buster ~/Documents/Work/Docker/Php/

运行容器

docker run --name php7.4-fpm-local-buster -v ~/Documents/Work/Project:/www -v ~/Documents/Work/Docker/Php/php.ini:/usr/local/etc/php/php.ini --link redis-local:redis --link mysql-local:mysql --network net-local -d php:7.4-fpm-local-buster

Nginx

docker pull nginx:latest

配置文件

server {
    listen 80;
    server_name local.api.spm.tool.wwwnet.com;
    #spm-tool/public是映射的主机目录
    root /usr/share/nginx/html/spm-tool/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        proxy_set_header Host $host; 
        proxy_set_header X-Real-IP $remote_addr; 
        index  index.html index.htm index.php;

        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME 
        #/www/spm-tool/public/是映射的容器内目录
        /www/spm-tool/public/$fastcgi_script_name;
        fastcgi_param  APP_ENV          local; 
        include fastcgi_params;
    }

}

运行

docker run --name nginx-local -p 80:80 -d -v ~/Documents/Work/Project:/usr/share/nginx/html:ro -v ~/Documents/Work/Docker/nginx/conf/conf.d:/etc/nginx/conf.d:ro -v ~/Documents/Work/Docker/nginx/logs:/var/log/nginx --link php7.4-fpm-local-buster:php --link redis-local:redis --link mysql-local:mysql --network net-local nginx

关于参数说明参考Docker教程

容器内安装PHP扩展

进入docker的php容器

docker exec -it php7.4-fpm-local-buster sh

安装扩展

# 生成php源码默认扩展目录
docker-php-source extract

# 下载源码包至/tmp目录
curl -L -o /tmp/reids.tar.gz https://codeload.github.com/phpredis/phpredis/tar.gz/5.0.2
cd /tmp/
tar -zxvf reids.tar.gz

# 将扩展包移至php源码扩展目录
mv phpredis-5.0.2 /usr/src/php/ext/phpredis

# 进入扩展目录
cd /usr/src/php/ext/

# 安装predis扩展
docker-php-ext-install phpredis pcntl pdo_mysql

# 检查php扩展
php -m | grep redis

系统置入命令别名

vi ~/.bash_profile

cat ~/.bash_profile

alias ll='ls $LS_OPTIONS -lha'

source ~/.bash_profile

Redis共享网络HOST

进入PHP容器,查看当前ip

/usr/src/php/ext # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:12:00:04  
          inet addr:172.18.0.4  Bcast:172.18.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:40736 errors:0 dropped:0 overruns:0 frame:0
          TX packets:34103 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:48118457 (45.8 MiB)  TX bytes:3098250 (2.9 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:18 errors:0 dropped:0 overruns:0 frame:0
          TX packets:18 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1606 (1.5 KiB)  TX bytes:1606 (1.5 KiB)
# 此IP为redis链接地址
/var/www/html # ping 172.18.0.2
PING 172.18.0.2 (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.583 ms
64 bytes from 172.18.0.2: seq=1 ttl=64 time=0.152 ms

对应env.local配置

REDIS_CLIENT=predis
REDIS_HOST=172.18.0.2
REDIS_PASSWORD=
REDIS_PORT=6379

TOKEN_REDIS_HOST=172.18.0.2
TOKEN_REDIS_PASSWORD=
TOKEN_REDIS_PORT=6379

检查运行php的用户

/usr/local/php/php/fpm # php -i |grep user
Configure Command =>  './configure'  '--build=x86_64-linux-musl' '--with-config-file-path=/usr/local/etc/php' '--with-config-file-scan-dir=/usr/local/etc/php/conf.d' '--enable-option-checking=fatal' '--with-mhash' '--with-pic' '--enable-ftp' '--enable-mbstring' '--enable-mysqlnd' '--with-password-argon2' '--with-sodium=shared' '--with-pdo-sqlite=/usr' '--with-sqlite3=/usr' '--with-curl' '--with-libedit' '--with-openssl' '--with-zlib' '--enable-fpm' '--with-fpm-user=www-data' '--with-fpm-group=www-data' '--disable-cgi' 'build_alias=x86_64-linux-musl'
ignore_user_abort => Off => Off
user_dir => no value => no value
user_ini.cache_ttl => 300 => 300
user_ini.filename => .user.ini => .user.ini
bytes_received_change_user_packet => 0
packets_received_change_user => 0
com_change_user => 0
Registered save handlers => files user redis rediscluster 
user_agent => no value => no value
PHP_EXTRA_CONFIGURE_ARGS => --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --disable-cgi
$_SERVER['PHP_EXTRA_CONFIGURE_ARGS'] => --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --disable-cgi
$_ENV['PHP_EXTRA_CONFIGURE_ARGS'] => --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --disable-cgi

将代码目录权限更换为www-data

chown -R www-data:www-data /www/spm-tool

将缓存目录赋予777权限

chmod -R 777 /www/spm-tool/storage/framework/cache

附录

关于参数说明参考Docker教程

如果用到 composer,建议主机安装和容器对应的php版本及扩展,docker-composer有些复杂;