Table of Contents
Add environment variables during build
Home Backend Development PHP Tutorial Tutorial on deploying LNMP working environment under Docker (detailed steps)

Tutorial on deploying LNMP working environment under Docker (detailed steps)

Jan 28, 2019 am 09:44 AM
docker php

This article brings you a tutorial (detailed steps) on deploying LNMP working environment under Docker. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Ordinary PC environment can also use the following configuration, just replace MYSQL with the official version of mysql.

Docker installation

$ curl -sSL get.docker.com | sudo sh
Copy after login

Working directory

/lnmp/conf stores virtual host configuration
/lnmp/data /mysql Stores mysql database data
/lnmp/log Stores nginx and mysql logs
/lnmp/workspace Stores various project files

/lnmp/.env Environment variable configuration when starting docker
/lnmp/docker-compose.yml Build configuration file
/lnmp/php/Dockerfile Compile PHP configuration file

Build Nginx

Create project

First create a static file index.html
/lnmp/workspace/default/index.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 after login

Add environment variables during build

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

SOURCE_DIR=./workspace workspace文件夹挂载到nginx下的 /var/www/html
HTTP_HOST=80           宿主机的80端口映射到nginx下的80端口
NGINX_VERSION=alpine   nginx镜像使用alpine版本
NGINX_CONFD_DIR=./conf/conf.d   conf.d文件夹挂载到nginx下的/etc/nginx/conf.d
NGINX_CONF_FILE=./conf/nginx.conf   nginx.conf挂载nginx的 /etc/nginx/nginx.conf
NGINX_LOG_DIR=./log
Copy after login


Nginx.conf

Create /lnmp/conf/nginx.conf file

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 after login

Configure virtual host

Create /lnmp/conf/conf.d/default.conf file
Note: The path under root must start with /var/www/html, default is our local A folder under the workspace

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 after login

Build Nginx configuration

Edit docker-composer.yml
The following ${} are all configured in the above .env file The variable

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 after login

Start Nginx

After running the command, access the IP of the Raspberry Pi. Normally, Hello, Docker!

$ docker-compose up
Copy after login
Copy after login
Copy after login

# will be displayed. ##Build PHP

Edit /lnmp/.env to add PHP build variables

PHP_CONFIG_FILE=./conf/php.ini
PHP_FPM_CONFIG_FILE=./conf/php-fpm.conf
Copy after login
Because the php.ini configuration is a bit long, you can copy it at the following address

php.ini
php-fpm.conf

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 after login

Add build PHP

Edit /lnmp/docker-compose.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_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 after login

Virtual host supports PHP parsing

/lnmp/conf/conf.d/default. conf

The php of fastcgi_pass php:9000 below refers to a service name in docker-compose.yml above, which can be used to specify the PHP version for a certain project

#...其它代码

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

Add files

/lnmp/workspace/default/index.php

<?php

echo ‘Hello, PHP!’;
Copy after login

Start preview

If you have not terminated the process before, you can terminate it with ctrl c Run the following command.

If nothing unexpected happens, the IP when accessing the Raspberry Pi will display Hello, PHP!.

$ docker-compose up
Copy after login
Copy after login
Copy after login

Add PHP extension

/lnmp/.env Add variables, The following is a swoole extension demonstration. For other extensions, please refer to the following operations.

PHP_SWOOLE_VERSION=4.2.10
Copy after login
/lnmp/docker-compose.yml Add variables

php:
    build: 
      context: ./php
      args:
        PHP_VERSION: 7.2
        PHP_SWOOLE: ${PHP_SWOOLE_VERSION}
Copy after login
/lnmp/php/Dockerfile Download and compile swoole

# ...其它代码
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 after login
Recompile

Note: If the Dockerfile is changed, you must add --build and recompile it once, otherwise it will not take effect when docker-compose up

$ docker-compose up --build
Copy after login
Preview

Edit /lnmp/workspace/default/index.php to save, access the IP of the Raspberry Pi, and as expected you can find the swoole extension

<?php

phpinfo();
Copy after login

Build MYSQL

MYSQL persistence

The Docker container service has been deleted to ensure that data will not be lost

Create the /lnmp/data/mysql folder

Build-time variables

Edit the /lnmp/.env file and add the mysql service port, login password and data persistence folder.

MYSQL_HOST_PORT=3306
MYSQL_PASSWORD=1234
MYSQL_DATA_DIR=./data/mysql
Copy after login
Build configuration

version: "3"
services:
    #... 其它代码
    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 after login

Startup

After starting, use the data connection tool to test, link address: Raspberry Pi IP Port: 3306 Username: root Password: 1234

$ docker-compose up
Copy after login
Copy after login
Copy after login
So far, your own LNMP working environment is ready.

The above is the detailed content of Tutorial on deploying LNMP working environment under Docker (detailed steps). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How to install deepseek How to install deepseek Feb 19, 2025 pm 05:48 PM

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

See all articles