溫馨提示×

溫馨提示×

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

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

Dockerfile 方式定制 lnmp 環(huán)境

發(fā)布時(shí)間:2020-06-07 04:33:32 來源:網(wǎng)絡(luò) 閱讀:1119 作者:zhangdeTalk 欄目:云計(jì)算

現(xiàn)在容器化技術(shù)越來越成熟和流行,比傳統(tǒng)的虛擬化技術(shù)確實(shí)是有很大的優(yōu)勢,所以流行是必然的。今天來實(shí)踐一把,使用Docker的dockerfile方式定制lnmp環(huán)境。

環(huán)境配置

操作系統(tǒng):CentOS Linux release 7.6.1810
Docker 版本:19.03.5
Nginx 版本:1.15.5
PHP 版本:7.2.26
MySQL 版本:8.0.18
Redis 版本:5.0.5  

創(chuàng)建目錄結(jié)構(gòu)

docker目錄://docker相關(guān)配置
[root@zhangdeTalk data]# tree docker
docker
├── bin
│?? └── docker-compose-linux.yml //dockerfile.yml
├── config
│?? ├── mysql
│?? │?? └── mysqld.cnf //數(shù)據(jù)庫配置文件
│?? ├── nginx
│?? │?? ├── conf.d
│?? │?? │?? └── default.conf //nginx主要配置文件
│?? │?? └── nginx.conf //nginx基礎(chǔ)配置文件
│?? ├── php
│?? │?? ├── php.ini //php基礎(chǔ)配置文件
│?? │?? └── www.conf //php主要配置文件
│?? └── redis
│?? └── redis.conf //redis配置文件
├── dockerfile
│?? ├── mysql-8.0.18
│?? │?? └── Dockerfile //mysql的dockerfile
│?? ├── nginx-1.15.5
│?? │?? └── Dockerfile //nginx的dockerfile
│?? ├── php-7.2-fpm
│?? │?? └── Dockerfile //php的dockerfile
│?? └── redis-5.0.5
│?? └── Dockerfile //redis的dockerfile
├── README.en.md
└── README.md
www目錄://站點(diǎn)目錄
[root@zhangdeTalk data]# tree www
www
└── zhangdetalk_blog_admin
├── 1.html
└── index.php
logs目錄://日志目錄
[root@zhangdeTalk data]# tree logs
logs
├── mysql
├── nginx
│   ├── access.log
│   └── error.log
├── php
└── redis
mysql目錄://數(shù)據(jù)庫數(shù)據(jù)目錄
redis目錄://數(shù)據(jù)庫數(shù)據(jù)目錄

Docker安裝

先在CentOS安裝Docker,可參考另外一篇文章:CentOS安裝Docker

構(gòu)建lnmp鏡像Dockerfile

PHP Dockerfile

From php:7.2-fpm
#維護(hù)者信息
MAINTAINER zhangdeTalk 2393222021@qq.com
#時(shí)區(qū)
ENV TZ Asia/Shanghai
RUN date -R
#RUN docker-php-ext-install bcmath dom fileinfo filter ftp gd gmp hash iconv imap json mbstring mysqli odbc opcache pdo pdo_mysql pdo_odbc phar reflection session snmp soap sockets zip
#RUN docker-php-ext-install mysqli opcache pdo_mysql
WORKDIR /working
RUN apt-get update --fix-missing && apt-get install -y libpng-dev libjpeg-dev libfreetype6-dev  \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include --with-jpeg-dir=/usr/include/jpeg \
    && docker-php-ext-install gd mysqli opcache pdo_mysql gd zip
ENV PHPREDIS_VERSION 4.0.1
ENV PHPXDEBUG_VERSION 2.6.0
ENV PHPSWOOLE_VERSION 4.2.13
ENV PHPMONGODB_VERSION 1.5.3
RUN pecl install redis-$PHPREDIS_VERSION \
    && pecl install xdebug-$PHPXDEBUG_VERSION \
    && pecl install swoole-$PHPSWOOLE_VERSION \
    && pecl install mongodb-$PHPMONGODB_VERSION \
    && docker-php-ext-enable redis xdebug swoole mongodb
# install composer new
# https://getcomposer.org/installer | https://install.phpcomposer.com/installer
 RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
     && php composer-setup.php \
     && php -r "unlink('composer-setup.php');" \
     && mv composer.phar /usr/local/bin/composer \
     && composer config -g repo.packagist composer https://packagist.laravel-china.org
RUN apt-get install -y git
# clear
RUN rm -rf /var/cache/apt/* \
    && rm -rf /var/lib/apt/lists/*
RUN mkdir /var/lib/sessions \
    && chmod o=rwx -R /var/lib/sessions
#容器啟動(dòng)時(shí)執(zhí)行指令
CMD ["php-fpm"]

Nginx Dockerfile

From nginx:1.15.5
#維護(hù)者信息
MAINTAINER zhangdeTalk 2393222021@qq.com
#時(shí)區(qū)
ENV TZ Asia/Shanghai
RUN date -R
#容器啟動(dòng)時(shí)執(zhí)行指令
CMD ["nginx", "-g", "daemon off;"]

Mysql Dockerfile

From mysql:8.0.18
#維護(hù)者信息
MAINTAINER zhangdeTalk 2393222021@qq.com
#時(shí)區(qū)
ENV TZ Asia/Shanghai
RUN date -R
#容器啟動(dòng)時(shí)執(zhí)行指令
CMD ["mysqld"]

Redis Dockerfile

From redis:5.0.5
#維護(hù)者信息
MAINTAINER zhangdeTalk 2393222021@qq.com
#時(shí)區(qū)
ENV TZ Asia/Shanghai
RUN date -R
#容器啟動(dòng)時(shí)執(zhí)行指令
CMD ["redis-server"]

dockerfile.yml配置

version: '3.3'
services:
  nginx:
    build: ../dockerfile/nginx-1.15.5
    ports:
      - "80:80" #nginx
    restart: always
    tty: true
    container_name: nginx
    volumes:
      - /data/www:/var/www/html
      - /data/logs/nginx:/var/log/nginx
      - /data/docker/config/nginx/conf.d:/etc/nginx/conf.d
      - /data/docker/config/nginx/nginx.conf:/etc/nginx/nginx.conf
      - /etc/letsencrypt:/etc/letsencrypt
    networks:
     - lnmp-networks
  php7:
    build: ../dockerfile/php-7.2-fpm
    tty: true
    restart: always
    container_name: php7
    volumes:
      - /data/www:/var/www/html
      - /data/logs/php:/var/log/php
      - /data/docker/config/php/php.ini:/usr/local/etc/php/php.ini
      - /data/docker/config/php/www.conf:/usr/local/etc/php-fpm.d/www.conf
    depends_on:
      - nginx
    networks:
     - lnmp-networks
  redis:
    build: ../dockerfile/redis-5.0.5
    container_name: redis
    tty: true
    restart: always
    volumes:
      - /data/docker/config/redis/redis.conf:/etc/redis/redis.conf
      - /data/redis:/var/lib/redis
      - /data/logs/redis:/var/log/redis
    networks:
     - lnmp-networks
  mysql:
    build: ../dockerfile/mysql-8.0.18
    container_name: mysql
    tty: true
    restart: always
    ports:
      - "3306:3306" #mysql
    volumes:
      - /data/mysql:/var/lib/mysql
      - /data/docker/config/mysql/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
      - /data/logs/mysql:/var/log/mysql
      - /data/mysqlback:/data/mysqlback
    environment:
      MYSQL_ROOT_PASSWORD: root
    networks:
      - lnmp-networks
networks:
 lnmp-networks:

Nginx配置文件

vim /data/docker/config/nginx/conf.d/default.conf

server {
         listen 80;
         listen [::]:80;
         # Add index.php to the list if you are using PHP
         index index.html index.htm index.nginx-debian.html index.php;
         charset utf-8;
         server_name zhangdetalk.com www.zhangdetalk.com;
         location ~ \.md$ {
             default_type 'text/plain';
         }
         root /var/www/html/zhangdetalk_blog_admin;//項(xiàng)目目錄
         location / {
                 #try_files $uri $uri/ =404;
                 index    index.php index.htm index.html;
                 if (!-e $request_filename) {
                         rewrite  ^(.*)$  /index.php?s=$1  last;
                         break;
                 }
         }
         location ~ \.php$ {
                 include fastcgi_params;
                 fastcgi_index index.php;
                 fastcgi_pass php7:9000;//容器:端口號(hào)
                 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         }
 }

docker-compose安裝

1. curl -L https://get.daocloud.io/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
2. chmod +x /usr/local/bin/docker-compose //設(shè)置可執(zhí)行權(quán)限

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

docker-compose -f docker-compose-linux.yml up -d --force-recreate --remove-orphans

容器查看

docker ps

Dockerfile 方式定制 lnmp 環(huán)境

測試

vim /data/www/zhangdetalk_blog_admin/index.php
<?php
echo "Hello World";
$conn = mysqli_connect('數(shù)據(jù)庫容器名字','dbuser','dbpw');
if($conn){
?echo '數(shù)據(jù)庫連接成功!';
}else{
?echo '數(shù)據(jù)庫連接失??!';
}
phpinfo();
?>

Dockerfile 方式定制 lnmp 環(huán)境

通過訪問站點(diǎn):http://www.zhangdetalk.com/
可以看到顯示數(shù)據(jù)庫連接成功,并打印出PHP的相關(guān)信息
到此步,我們的lnmp環(huán)境搭建大功告成!

github源碼

相關(guān)的源碼下載地址:https://github.com/zhangdejian/docker_lnmp.git

遇到的問題

1、PHP代碼連接數(shù)據(jù)庫,host不能寫成127.0.0.1或localhost,需要填寫數(shù)據(jù)庫容器名字

Dockerfile 方式定制 lnmp 環(huán)境

2、mysql8連接,連接失敗,因?yàn)榧用芊绞接凶兓?解決方式:主機(jī)登錄mysql,修改成舊的加密方式(mysql_native_password),并重置密碼 
* mysql -uroot -p;
* use mysql;
* select host,user,plugin from user;
* alter user 'root'@'%' identified with mysql_native_password by '123456';

可參考這篇文章:Docker 安裝 MySQL8.0

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

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

AI