溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

docker-compose如何部署php項(xiàng)目

發(fā)布時(shí)間:2020-11-10 14:11:42 來(lái)源:億速云 閱讀:504 作者:小新 欄目:編程語(yǔ)言

這篇文章給大家分享的是有關(guān)docker-compose如何部署php項(xiàng)目的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。

 1.制定特定擴(kuò)展的PHP鏡像

sudo mkdir -p /www/docker
sudo cd /www/docker
sudo vi Dockerfile
FROM php:7.2-fpm-alpine
MAINTAINER diaocheweide
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk update && apk add --no-cache --virtual .build-deps \
        $PHPIZE_DEPS \
        curl-dev \
        imagemagick-dev \
        libtool \
        libxml2-dev \
        postgresql-dev \
        sqlite-dev \
        libmcrypt-dev \
        freetype-dev \
        libjpeg-turbo-dev \
        libpng-dev \
    && apk add --no-cache \
        curl \
        imagemagick \ mysql-client \
        postgresql-libs \
    && pecl install imagick \
    && pecl install mcrypt-1.0.1 \
    && docker-php-ext-install zip \
    && docker-php-ext-install pdo_mysql \
    && docker-php-ext-install opcache \
    && docker-php-ext-install mysqli \
    && docker-php-ext-enable mcrypt \
    && docker-php-ext-enable imagick \
    && docker-php-ext-install \
        curl \
        mbstring \
        pdo \
        pdo_mysql \
        pdo_pgsql \
        pdo_sqlite \
        pcntl \
        tokenizer \
        xml \
        zip \
        && docker-php-ext-install -j"$(getconf _NPROCESSORS_ONLN)" iconv \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j"$(getconf _NPROCESSORS_ONLN)" gd \
    && pecl install -o -f redis \
    && rm -rf /tmp/pear \
    && docker-php-ext-enable redis \
    && rm -r /var/cache/apk/*
EXPOSE 9000

2.編寫(xiě)yml文件

sudo vi docker-compose.yml
version: '3.1'
services:
  nginx:
    image: nginx
    container_name: nginx
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /www/data/nginx/conf.d:/etc/nginx/conf.d
      - /www/default:/www/default
    networks:
      csl:
        ipv4_address: 172.18.0.2
 
  php:
    image: php:7.2-fpm-alpine-dcwd
    container_name: php7.2
    restart: always
    volumes:
      - /www/default:/www/default
    networks:
      csl:
        ipv4_address: 172.18.0.3 mysql5:
    image: mysql:5.7
    container_name: mysql5
    restart: always
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: thisyourpassword
    volumes:
      - /www/data/mysql/mysql5:/var/lib/mysql #- /www/data/mysql/mysql5/conf/my.cnf:/etc/my.cnf
      #- /www/data/mysql/mysql5/init:/docker-entrypoint-initdb.d
    networks:
      csl:
        ipv4_address: 172.18.0.4 mysql8:
    image: mysql:8
    container_name: mysql8
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: thisyourpassword
    volumes:
      - /www/data/mysql/mysql8:/var/lib/mysql networks:
      csl:
        ipv4_address: 172.18.0.5
networks:
  csl:
    driver: bridge
    ipam:
      config:
        - subnet: 172.18.0.0/16

3.配置default.conf文件,拷貝這一步請(qǐng)參考上一篇文章

sudo vi /www/data/nginx/default.conf
server {
    listen       80;
    server_name  localhost;
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    location / {
        root   /www/default;
        index  index.php index.html index.htm;
    }
    #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           /www/default;
        fastcgi_pass   php7.2:9000;#php容器名或者php容器ip
        fastcgi_index  index.php;
        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;
    #}
}

4.修改mysql允許遠(yuǎn)程連接并新建數(shù)據(jù)庫(kù)

docker exec -it mysql5 bash
mysql -u root -p
use mysql;
update user set host='%' where user='root';
flush privileges;

5.創(chuàng)建并且啟動(dòng)容器

docker-compose up -d

6.新建index.php測(cè)試mysql連接

<?php
$con = mysqli_connect("172.18.0.4", "root", "thisyourpassword", "shop");
if ($con) {
    echo '連接 MYSQL 成功';
} else {
    echo "連接 MySQL 失敗: " . mysqli_connect_error();
} mysqli_close($con);

感謝各位的閱讀!關(guān)于docker-compose如何部署php項(xiàng)目就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI