Now containerization technology is more and more mature and popular, compared to the traditional virtualization technology is indeed a great advantage, so the popularity is inevitable. Use Docker dockerfile to customize LNMP environment.

Environment configuration

CentOS Linux release 7.6.1810 Docker version: 19.03.5 Nginx version: 1.15.5 PHP version: 7.2.26 MySQL version: 8.0.18 Redis version: 5.0.5Copy the code

Creating a directory structure

Docker directory: //docker related configuration [root@zhangdeTalk data]# tree dockerDocker ├ ─ ─ bin │ └ ─ ─ docker - compose - Linux. Yml / / dockerfile yml ├ ─ ─ the config │ ├ ─ ─ mysql │ │ └ ─ ─ mysqld. CNF / / database configuration file │ ├ ─ ─ Nginx │ │ ├ ─ ─ the conf. D. │ │ │ └ ─ ─ the default. The main configuration file conf / / nginx │ │ └ ─ ─ nginx. Conf / / nginx basic configuration file │ ├ ─ ─ PHP │ │ ├ ─ ─ PHP. Ini │ ├─ ├─ └─ www.conf // PHP └─ ├─ ├─ ├─ ├─ 3.0.18 │ ├─ ├─ 1.3.0.18 │ ├─ 1.3.0.18 │ ├─ 1.3.0.18 │ ├─ 1.3.0.18 │ │ ├─ 1.3.0.18 │ │ ├─ 1.3.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 ├ ─ ─ the README. En. The md └ ─ ─ the README, mdCopy the code
WWW directory: // site directory [root@zhangdeTalk data]# tree www├─ WWW └─ 1.html ├─ index.phpCopy the code
// Log directory [root@zhangdeTalk data]# tree logsLogs ├ ─ ─ mysql ├ ─ ─ nginx │ ├ ─ ─ access. Log │ └ ─ ─ the error. The log ├ ─ ─ PHP └ ─ ─ redisCopy the code
Mysql directory: // Database data directoryCopy the code
Redis directory: // Database data directoryCopy the code

Docker installation

Install Docker on CentOS first

Construct LNMP image Dockerfile

PHP Dockerfile

The From PHP: 7.2 - FPM# Maintainer information
MAINTAINER zhangdeTalk [email protected]
# time zone
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_mysqlWORKDIR /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 Phpphpmongodb_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
Execute instructions when the container is started
CMD ["php-fpm"]
Copy the code

Nginx Dockerfile

The From nginx: 1.15.5# Maintainer information
MAINTAINER zhangdeTalk [email protected]
# time zone
ENV TZ Asia/Shanghai
RUN date -R
Execute instructions when the container is started
CMD ["nginx"."-g"."daemon off;"]
Copy the code

Mysql Dockerfile

The From mysql: 8.0.18# Maintainer information
MAINTAINER zhangdeTalk [email protected]
# time zone
ENV TZ Asia/Shanghai
RUN date -R
Execute instructions when the container is started
CMD ["mysqld"]
Copy the code

Redis Dockerfile

The From redis: 5.0.5# Maintainer information
MAINTAINER zhangdeTalk [email protected]
# time zone
ENV TZ Asia/Shanghai
RUN date -R
Execute instructions when the container is started
CMD ["redis-server"]
Copy the code

Dockerfile. Yml configuration

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:
Copy the code

Nginx configuration file

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; // Project directory location / {#try_files $uri $uri/ =404;
                 index    index.php index.htm index.html;
                 if (!-e $request_filename) { rewrite ^(.*)$ /index.php? s=The $1  last;
                         break; } } location ~ \.php$ { include fastcgi_params; fastcgi_index index.php; fastcgi_pass php7:9000; // Container: port number fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name; }}Copy the code

Docker – compose the installation

1. The 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 // set executable permissionsCopy the code

Create and start the container

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

The container to see

docker ps
Copy the code

test

vim /data/www/zhangdetalk_blog_admin/index.php <? phpecho "Hello World";
$conn = mysqli_connect('Database container Name'.'dbuser'.'dbpw');
if($conn) {echo 'Database connection successful! ';
}else{
 echo 'Database connection failed! '; } phpinfo(); ? >Copy the code

Through access to the site: http://www.zhangdetalk.com/ can see shows the database connection is successful, and print out the PHP related information To this step, our LNMP environment structures, congrats!Copy the code

Making the source code

Relevant source download address: https://github.com/zhangdejian/docker_lnmp.gitCopy the code

Problems encountered

Mysql > select host from '127.0.0.1'; mysql > select host from '127.0.0.1'Copy the code

Mysql_native_password = mysql_native_password = mysql_native_password = mysql_native_password = mysql_native_password = mysql_native_password = mysql_native_password = mysql_native_password * use mysql; * select host,user,plugin from user; * alter user'root'@The '%' identified with mysql_native_password by '123456';
Copy the code

See this article: Docker installs MySQL8.0