docker-compose部署php項(xiàng)目實(shí)例詳解
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.編寫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文件
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ù)庫
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)文章
Laravel框架定時(shí)任務(wù)2種實(shí)現(xiàn)方式示例
這篇文章主要介紹了Laravel框架定時(shí)任務(wù)2種實(shí)現(xiàn)方式,結(jié)合實(shí)例形式較為詳細(xì)的分析了Laravel框架定時(shí)任務(wù)相關(guān)實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下2018-12-12
CodeIgniter擴(kuò)展核心類實(shí)例詳解
這篇文章主要介紹了CodeIgniter擴(kuò)展核心類,結(jié)合實(shí)例形式分析了CodeIgniter針對(duì)核心類的擴(kuò)展方法與擴(kuò)展CI類庫與輔助函數(shù)的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01
PHP程序中使用adodb連接不同數(shù)據(jù)庫的代碼實(shí)例
這篇文章主要介紹了PHP程序中使用adodb連接不同數(shù)據(jù)庫的代碼實(shí)例,具體的用法示例中用switch語句寫了一個(gè)匯總式的支持,需要的朋友可以參考下2015-12-12
推薦幾款用 Sublime Text 開發(fā) Laravel 所用到的插件
Sublime Text2 是一款具有代碼高亮、語法提示、自動(dòng)完成且反應(yīng)快速的編輯器軟件,不僅具有華麗的界面,還支持插件擴(kuò)展機(jī)制,用她來寫代碼,絕對(duì)是一種享受。所以本人也是用她來做Laravel開發(fā)的,這里給大家推薦幾款她的插件2014-10-10
PHP設(shè)計(jì)模式之解釋器(Interpreter)模式入門與應(yīng)用詳解
這篇文章主要介紹了PHP設(shè)計(jì)模式之解釋器(Interpreter)模式,結(jié)合實(shí)例形式詳細(xì)分析了PHP解釋器模式的概念、原理、基本應(yīng)用與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-12-12
Laravel 5.5 的自定義驗(yàn)證對(duì)象/類示例代碼詳解
Laravel 5.5 將提供一個(gè)全新的自定義驗(yàn)證規(guī)則的對(duì)象,以作為原來的 Validator::extend 方法的替代。接下來通過示例代碼給大家介紹laravel 自定義驗(yàn)證對(duì)象和類,需要的朋友參考下吧2017-08-08

