How to build lnmp environment in docker

How to build lnmp environment in docker

Create a project directory

mkdir php

Create the following project structure

  • The sites directory is where the project files are placed
  • Services directory places service-related configuration
  • script Place custom script
├── Readme.md
├── docker-compose.yml
├── script
├── services
│ ├── mariadb
│ │ └── Dockerfile
│ ├── nginx
│ │ ├── Dockerfile
│ │ ├── conf.d
│ │ │ └── default.conf
│ │ └── nginx.conf
│ ├── php
│ │ └── Dockerfile
│ └── redis
│ └── Dockerfile
└── sites
 ├── index.html
 └── index.php

 
8 directories, 10 files

Edit the docker-compose file

version: "3"
services: 
 php:
 build: ./services/php
 # ports: 
 # - "9001:9000"
 container_name: lnmp-php
 restart: always
 volumes: 
  - ./sites:/www
 networks:
  lnmp_net:
  ipv4_address: 101.11.11.10

 nginx:
 build: ./services/nginx
 ports:
  - "81:80"
  - "444:443"
 container_name: lnmp-nginx
 restart: always
 volumes: 
  - ./sites:/www
  - ./services/nginx/nginx.conf:/etc/nginx/nginx.conf
  - ./services/nginx/conf.d:/etc/nginx/conf.d:rw
 networks:
  lnmp_net:
  ipv4_address: 101.11.11.11

networks:
 lnmp_net:
 driver: bridge
 ipam:
  config:
  - subnet: 101.11.11.0/20

Edit services/nginx file

FROM nginx:1.17.0-alpine

# Update the installation source RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories

# Set the time zone to Shanghai RUN apk update && apk add --upgrade \
 && apk add tzdata \
 && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
 && echo "Asia/Shanghai" > /etc/timezone \
 && apk del tzdata

Edit services/php file

FROM php:7.3.6-fpm-alpine3.9

# Update the installation source RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories

# Set the time zone to Shanghai RUN apk update && apk add --no-cache tzdata autoconf gcc g++ make zlib-dev curl-dev\
 && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
 && echo "Asia/Shanghai" > /etc/timezone \
 && apk del tzdata \
 && docker-php-ext-install mysqli pdo_mysql opcache \
 && pecl install grpc protobuf xdebug yaf yar swoole \
 && docker-php-ext-enable xdebug yaf yar swoole grpc protobuf 

Our lnmp environment is basically built above. Now we will configure Php parsing.

Modify services/nginx/nginx.conf, you can modify it according to your needs

user nginx;
worker_processes auto;

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


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/access.log main;

 sendfile on;
 #tcp_nopush on;

 keepalive_timeout 65;

 #gzip on;

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

Modify the files in the services/nginx/conf.d directory

This directory is the configuration file for each project, which can configure single or multiple services according to needs.

server {
 listen 80;
 server_name localhost;

 #charset koi8-r;
 #access_log /var/log/nginx/host.access.log main;

  #root /usr/share/nginx/html;
 root /www;
 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 html;
  fastcgi_pass 101.11.11.10:9000;
  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;
 #}
}

Start the service

cd php && docker-compose up --build -d

The execution time of the above steps varies depending on the network speed and docker configuration. Because some extensions are compiled in PHP, you can also delete them according to your own needs. Visit http://127.0.0.1:81 through the browser. If you see the output information of phpinfo, then congratulations, your lnmp environment has been deployed.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to quickly build an LNMP environment with Docker (latest)
  • Use Docker to create a distributed lnmp image
  • How to deploy LNMP architecture in docker
  • Use Docker to create an integrated service lnmp environment
  • Detailed explanation of using Docker to build LNMP environment
  • Example of compiling LNMP in Docker container

<<:  How to implement an array lazy evaluation library in JavaScript

>>:  A brief discussion on MySQL index optimization analysis

Recommend

Summary of various common join table query examples in MySQL

This article uses examples to describe various co...

Detailed explanation of the loading rules of the require method in node.js

Loading rules of require method Prioritize loadin...

Let you understand the working principle of JavaScript

Table of contents Browser kernel JavaScript Engin...

Remote development with VSCode and SSH

0. Why do we need remote development? When develo...

A brief analysis of controlled and uncontrolled components in React

Table of contents Uncontrolled components Control...

Five practical tips for web form design

1. Mobile selection of form text input: In the te...

How to set up the terminal to run applications after Ubuntu starts

1. Enter start in the menu bar and click startup ...

Docker installs and runs the rabbitmq example code

Pull the image: [mall@VM_0_7_centos ~]$ sudo dock...

Simple implementation of vue drag and drop

This article mainly introduces the simple impleme...

How to view and close background running programs in Linux

1. Run the .sh file You can run it directly using...