The following configuration can also be used in common PC environments, but the MYSQL version can be changed back to the official MYSQL version.

Docker installation

$ curl -sSL get.docker.com | sudo sh 
Copy the code

Working directory

LNMP /conf stores virtual host configurations, LNMP /data/mysql stores mysql database data, LNMP /log stores Nginx and mysql logs, and LNMP /workspace stores various project files

/ LNMP /. Env Docker environment variable configuration/LNMP/docker-comemage. yml build configuration file/LNMP/PHP /Dockerfile compile PHP configuration file

Build Nginx

Create a project

First create a static file index. The HTML/LNMP/workspace/default/index. The HTML

<! DOCTYPE html> <html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="Width = device - width, initial - scale = 1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    Hello, Docker!
</body>
</html>
Copy the code

Add build-time environment variables

Edit. Env file

SOURCE_DIR=./workspace HTTP_HOST=80 NGINX_VERSION=alpine NGINX_CONFD_DIR=./conf/conf.d NGINX_CONF_FILE=./conf/nginx.conf  NGINX_LOG_DIR=./log
Copy the code

/var/ WWW/HTML HTTP_HOST=80 Port 80 of the host is mapped to port 80 of the nginx port NGINX_VERSION=alpine The Nginx image uses the Alpine version

NGINX_CONFD_DIR=./conf/conf.d conf.d NGINX_CONF_FILE=./conf/nginx.conf Nginx. conf Nginx /etc/nginx/nginx.conf NGINX_LOG_DIR=./log

Nginx.conf

create/lnmp/conf/nginx.conffile
user  nginx;
worker_processes  1;

pid        /var/run/nginx.pid;
error_log  /var/log/nginx/nginx.error.log warn;

events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/nginx.access.log  main;

    sendfile        on;
    #tcp_nopush on;

    keepalive_timeout  65;

    #gzip on;

    include /etc/nginx/conf.d/*.conf;
}


Copy the code

Configuring a Virtual Host

Create/LNMP/conf/conf. D/default. The conf file note: under the root path must begin by/var/WWW/HTML, default is our local workspace of a folder

server {
    listen       80;
    server_name  localhost default-server;
    root   /var/www/html/default;
    index  index.php index.html index.htm;

    access_log  /var/log/nginx/nginx.localhost.access.log  main;
    error_log  /var/log/nginx/nginx.localhost.error.log  warn;

    #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; }}Copy the code

Build the Nginx configuration

Edit docker-composer. Yml. ${} below are all variables configured in. Env above

version: "3"
services:
  nginx:
    image: nginx:${NGINX_VERSION}
    ports:
      - ${HTTP_HOST}:80
    volumes:
      - ${SOURCE_DIR}:/var/www/html/:rw
      - ${NGINX_CONFD_DIR}:/etc/nginx/conf.d/:rw
      - ${NGINX_CONF_FILE}:/etc/nginx/nginx.conf:ro
      - ${NGINX_LOG_DIR}:/var/log/nginx/:rw
    restart: always
Copy the code

Start the Nginx

After running the command, access the raspberry PI’S IP, which normally displays Hello, Docker!

$ docker-compose up
Copy the code

Build the PHP

Edit/LNMP /.env to add PHP build variables

PHP_CONFIG_FILE=./conf/php.ini
PHP_FPM_CONFIG_FILE=./conf/php-fpm.conf
Copy the code

Because the php.ini configuration is a bit long, you can copy php.ini php-fpm.conf at the following address

Add Dockerfile

/lnmp/php/Dockerfile

ARG PHP_VERSION
FROM php:${PHP_VERSION}-fpm
RUN apt update
RUN apt-get install -y \
		libfreetype6-dev \
		libjpeg62-turbo-dev \
		libmcrypt-dev \
		libpng-dev \
        unixodbc-dev \
        gcc g++ make autoconf libc-dev pkg-config
Copy the code

Add build PHP

Edit/LNMP/docker-comemage. yml file

version: "3"
services:
  nginx:
    image: nginx:${NGINX_VERSION}
    ports:
      - ${HTTP_HOST}:80
    volumes:
      - ${SOURCE_DIR}:/var/www/html/:rw
      - ${NGINX_CONFD_DIR}:/etc/nginx/conf.d/:rw
      - ${NGINX_CONF_FILE}:/etc/nginx/nginx.conf:ro
      - ${NGINX_LOG_DIR}:/var/log/nginx/:rw
    restart: always
  php:
    build: 
      context: ./php
      args:
        PHP_VERSION: 7.2
    volumes:
      - ${SOURCE_DIR}:/var/www/html/:rw
      - ${PHP_CONFIG_FILE}:/usr/local/etc/php/php.ini:ro
      - ${PHP_FPM_CONFIG_FILE}:/usr/local/etc/php-fpm.d/www.conf:rw
    cap_add:
      - SYS_PTRACE
    restart: always
    networks:
      - default
Copy the code

Virtual hosts support PHP parsing

/ LNMP/conf/conf. D/default. Under the conf fastcgi_pass PHP: 9000 PHP, refers to the docker – compose. Yml a service name, specify the PHP version can be used for a project

#... Other code

location ~ \.php$ {
    fastcgi_pass   php:9000;
    fastcgi_index  index.php;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
}
Copy the code

Add files

/lnmp/workspace/default/index.php

<? phpecho'Hello, PHP! ";Copy the code

Start the preview

If the process has not been terminated before, you can terminate it with CTRL + C and then run the following command. Otherwise, the IP accessing the Raspberry PI will display Hello, PHP! .

$ docker-compose up
Copy the code

Add PHP extensions

/ LNMP /.env adds the variable, as demonstrated by the swoole extension below, for other extensions refer to the following operations

PHP_SWOOLE_VERSION = 4.2.10Copy the code

/ LNMP/docker-comemage. yml add variable

/ PHP args: PHP_VERSION: 7.2 PHP_SWOOLE:${PHP_SWOOLE_VERSION}
Copy the code

/ LNMP/PHP /Dockerfile Download and compile swoole

#... Other code
ARG PHP_SWOOLE=false
RUN if [ ${PHP_SWOOLE}! =false ]; then \
    curl -O http://pecl.php.net/get/swoole-${PHP_SWOOLE}.tgz -L \
    && pecl install swoole-${PHP_SWOOLE}.tgz \
    && docker-php-ext-enable swoole \
;fi
Copy the code

recompile

Docker-compose compose: docker-compose compose: docker-compose compose: docker-compose compose: docker-compose compose: docker-compose compose compose: docker-compose compose compose: docker-compose compose compose: docker-compose compose compose compose: docker-compose compose compose compose

$ docker-compose up --build
Copy the code

preview

Edit/LNMP/workspace/default/index. The PHP preservation, IP access raspberry pie, not surprisingly, you can find swoole extension

<? php phpinfo();Copy the code

To build the MYSQL

MYSQL persistence

Create/LNMP /data/mysql folder

Build-time variables

Edit/LNMP /. Env to add mysql service port, login password, and data persistence folder.

MYSQL_HOST_PORT=3306
MYSQL_PASSWORD=1234
MYSQL_DATA_DIR=./data/mysql
Copy the code

Build configuration

version: "3"
services:
    #... Other code
    mysql:
      image: tobi312/rpi-mysql
      ports:
        - ${MYSQL_HOST_PORT}:3306
      volumes:
        - ${MYSQL_DATA_DIR}:/var/lib/mysql:rw
      environment:
        MYSQL_ROOT_PASSWORD: "${MYSQL_PASSWORD}"
Copy the code

Start the

After startup, use data connection tool to test, link address: Raspberry PI IP port: 3306 Username: root Password: 1234

$ docker-compose up
Copy the code

At this point, your own LNMP working environment is ready.