How to deploy LNMP & phpMyAdmin in docker

How to deploy LNMP & phpMyAdmin in docker

Environmental preparation:

Deploy lnmp on a host based on multiple containers:
nginx service: 172.16.10.10
mysql service:172.16.10.20
php service:172.16.10.30

Solve the problem of fixed container IP address:

Note: When the container is stopped or deleted, and the same container is run again, its IP address is no longer the original address, so we need to customize a network segment to specify the container's IP address.

Project Operation:

(1) First, customize a network:

[root@sqm-docker01 ~]# docker network create -d bridge --subnet 172.16.10.0/24 --gateway 172.16.10.1 mynet1

(2) Download nginx, php:fpm, mysql-5.7 image:

[root@sqm-docker01 ~]# docker pull nginx 
[root@sqm-docker01 ~]# docker pull php:7.2-fpm <!--Using the image of php:7.2-fpm-->
[root@sqm-docker01 ~]# docker pull mysql:5.7 <!--Using the image of mysql:5.7--> 

(3)####Run a container based on the nginx image#####

#First run the nginx service to copy the nginx configuration file:

Parameter explanation:

  • run: Create a new container
  • -d: Run in the background
  • --name: Custom name is test1

Create a directory on the host where you want to mount the files:

Website main directory: /wwwroot;

nginx configuration file: /docker;

[root@sqm-docker01 ~]# mkdir /wwwroot
[root@sqm-docker01 ~]# mkdir /docker

##Use the docker cp command to copy the directory where nginx configuration files are stored to the local machine.

Note: The docker cp command can copy files from the host to the container, or it can copy files or directories in the container to the local machine.

[root@sqm-docker01 ~]# docker cp test1:/etc/nginx /docker/ #Use docker cp to copy the directory where nginx configuration files are stored to the host .

[root@sqm-docker01 ~]# ls /docker/nginx/conf.d/ 

default.conf

#注意:需要修改的是nginx conf.d目錄下的default.conf文件,而不是nginx.conf 文件。

Copy the nginx web directory:

[root@sqm-docker01 ~]# docker cp test1:/usr/share/nginx/html /wwwroot/
[root@sqm-docker01 ~]# ls /wwwroot/html/
50x.html index.html

Modify the default test web page of nginx:

[root@sqm-docker01 html]# echo "<h1>hello welcome to nginx web</h1>" > index.html 


(4) Run the nginx container:

Mount the directories in the container to the local directory and specify the IP address

[root@sqm-docker01 ~]# docker run -itd --name nginx --network my_net1 --ip 172.16.10.10 -p 80:80 -v /docker/nginx:/etc/nginx -v /wwwroot/html:/usr/share/nginx/html nginx:latest

Parameter explanation:

  • run: Run a container
  • -itd: i: interactive
  • t: pseudo terminal
  • d: Keep the container running in the background
  • --network : Based on which network card to create the network
  • --ip: defines the container's ip address
  • -v = --volume data volume, perform a mount
  • Mount format: Host: In container
  • p: mapped port, host port: port in container

(5) Run the mysql container:

[root@sqm-docker01 ~]# docker run --name mysql -e MYSQL_ROOT_PASSWORD=123.com -d -p 3306:3306 --network my_net1 --ip 172.16.10.20 mysql:5.7
-e is to set the environment variable in the container. We set the mysql password environment variable, which will be passed into the container to set the mysql password

Test whether the root user can log in to mysql on this machine:

First you need to download the mysql client:

[root@sqm-docker01 ~]# yum -y install mysql 

(6) Run the PHP container:

[root@sqm-docker01 ~]# docker run -itd -p 9000:9000 --name phpfpm -v /wwwroot/html:/usr/share/nginx/html --network my_net1 --ip 172.16.10.30 php:7.2-fpm

Notice:
Ensure that nginx and PHP share a directory for storing web pages, and that PHP creates the same default web root directory as nginx (when -v is mounted, if the directory does not exist in the container, it will be created automatically).

(7) Configure nginx and php-fpm:

We need to configure PHP to parse nginx:

Create a simple test web page:

Next you need to modify the nginx configuration file:

[root@sqm-docker01 html]# vim /docker/nginx/conf.d/default.conf 


(8) Configure mysql:

Setting up phpMyadmin:

phpMyAdmin is a PHP-based, Web-Base-based MySQL database management tool on a website host, allowing administrators to manage MySQL databases using a Web interface. This web interface can be a better way to input complex SQL syntax in an easy way, especially for importing and exporting large amounts of data. One of the biggest advantages is that since phpMyAdmin runs on a web server like other PHP programs, you can use the HTML pages generated by these programs anywhere, that is, to remotely manage MySQL databases and easily create, modify, and delete databases and tables.
[root@sqm-docker01 html]# pwd
/wwwroot/html

Unzip to the current directory:

[root@sqm-docker01 html]# unzip phpMyAdmin-4.9.1-all-languages.zip

Rename:

[root@sqm-docker01 html]# mv phpMyAdmin-4.9.1-all-languages ​​phpmyadmin

Modify the nginx configuration file--configure the connection with phpMyAdmin:

[root@sqm-docker01 html]# vim /docker/nginx/conf.d/default.conf 

Copy the original location configuration item and add the following two location configuration sections.

After modifying the nginx configuration file, restart nginx:

Visit the test web page:

The port in the container has been mapped to the host, so directly access the host address: 172.16.1.30

Access PHP to parse the nginx web page:

Next, test logging into phpMyAdmin:

Visit url : http://172.16.1.30/phpmyadmin/index.php

The mysqli module is missing, so it cannot be accessed, so you need to add php to support the mysql configuration module:

How to add extension modules to the container, we can log in to dockerhub to query the relevant documents:

Link path: https://hub.docker.com/


Copy the above Dockerfile script and install it locally:

Note that some additional content needs to be added:

[root@sqm-docker01 ~]# vim Dockerfile
FROM php:7.2-fpm
RUN apt-get update && apt-get install -y \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
  && docker-php-ext-install -j$(nproc) iconv \
  && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
  && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install mysqli pdo pdo_mysql

Build Dockerfile:

[root@sqm-docker01 ~]# docker build -t phpfpm-mysqli .

Parameter explanation:

  • build: Build
  • -t : Specify the tag
  • . indicates the Dockerfile in the current directory

After the build is complete, delete the original PHP image and run the image that just successfully built and supports the mysqli module:

[root@sqm-docker01 ~]# docker stop phpfpm 
phpfpm
[root@sqm-docker01 ~]# docker rm phpfpm 
phpfpm
[root@sqm-docker01 ~]# docker run --name phpfpm -d -p 9000:9000 -v /wwwroot/html:/usr/share/nginx/html --network my_net1 --ip 172.16.10.30 phpfpm-mysqli:latest

#Mirror specifies the image name generated by the Dockerfile just built.

Modify the default sample (sample file) of phpMyAdmin:

If you want to use it in the configuration file, you must rename it and remove the sample.

[root@sqm-docker01 phpmyadmin]# pwd
/wwwroot/html/phpmyadmin
[root@sqm-docker01 phpmyadmin]# cp config.sample.inc.php config.inc.php

Modify the configuration file:

[root@sqm-docker01 phpmyadmin]# vim config.inc.php 

After modifying the configuration file, restart PHP:

[root@sqm-docker01 phpmyadmin]# docker restart phpfpm 
phpfpm

Access the phpMyAdmin web page:

Enter the URL: http://172.16.1.30/phpmyadmin/index.php

#Username and password are the login password of the database

Successfully accessed the mysql database. . . . . .

At this point, the deployment of lnmp between multiple containers in docker is complete.

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 configure PHP development environment through docker on Mac
  • Docker installation of PHP and deployment example with Nginx
  • Explanation of the steps to install PHP extension in Docker
  • How to use Docker to build OpenLDAP+phpLDAPadmin unified user authentication
  • Docker build PHP environment tutorial detailed explanation
  • Detailed tutorial on installing php-fpm service/extension/configuration in docker
  • Docker's flexible implementation of building a PHP environment
  • PHP uses docker to run workerman case explanation

<<:  MySQL 8.0.15 installation and configuration graphic tutorial under Win10

>>:  A brief discussion on the use of Web Storage API

Recommend

Windows DNS server exposed "worm-level" vulnerability, has existed for 17 years

Vulnerability Introduction The SigRed vulnerabili...

How to configure virtual user login in vsftpd

yum install vsftpd [root@localhost etc]# yum -y i...

Zabbix WEB monitoring implementation process diagram

Take zabbix's own WEB interface as an example...

How to use vw+rem for mobile layout

Are you still using rem flexible layout? Does it ...

Linux configuration SSH password-free login "ssh-keygen" basic usage

Table of contents 1 What is SSH 2 Configure SSH p...

Common properties of frameset (dividing frames and windows)

A frame is a web page screen divided into several ...

MySql8 WITH RECURSIVE recursive query parent-child collection method

background When developing a feature similar to c...

How to change the encoding to utf-8 in mysql version 5.7 under windows

Preface I just started learning MySQL and downloa...

How to set up Spring Boot using Docker layered packaging

The Spring Boot project uses docker containers, j...

Docker Basic Tutorial: Detailed Explanation of Dockerfile Syntax

Preface Dockerfile is a script interpreted by the...

How to determine if the Linux system is installed on VMware

How to determine whether the current Linux system...

Detailed explanation of Tomcat's Server Options

1. Configuration By default, the first two are no...