How to quickly build an LNMP environment with Docker (latest)

How to quickly build an LNMP environment with Docker (latest)

Preface

Tip: Here you can add the approximate content to be recorded in this article:
For example: With the continuous development of artificial intelligence, machine learning technology has become more and more important. Many people have started to learn machine learning. This article introduces the basic content of machine learning.

Tip: The following is the main content of this article. The following cases can be used for reference

1. Mysql?

1. Pull the mysql image

docker pull mysql:5.6 

insert image description here

2. Run to start the mysql container

docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=xy123456 --name xy_mysql mysql:5.6 

insert image description here

Parameter Description:
-d makes the container run in the background
-p Add port mapping from host to container
-e sets the mysql environment variable and the initial root password. –name gives the container a name. The last parameter is the image name, which is the name of the image to be pulled.

2. Install php-fpm

1. Pull the php-fpm image

docker pull php:7.0-fpm

You can also pull docker pull php:7.4.20-fpm docker pull php:7.3.28-fpm These versions are in the warehouse, feel free to pull

insert image description here

2. Run to start the php-fpm container

docker run -d -v D:/docker/nginx:/var/www/html -p 9000:9000 --link xy_mysql:mysql --name xy_phpfpm php:7.0-fpm

Parameter Description:
-d makes the container run in the background
-p Add port mapping from host to container
-v adds directory mapping, D:/docker/nginx on the host is mapped to /var/www/html, that is, the contents of the directory are synchronized. You can directly copy the directory where you want to write code under the host'D:/docker/nginx'. If $PWD is valid, it refers to the current directory. I used this parameter to report an error, so I copied and pasted the path directly.
–name gives the container a name –link establishes a connection with another container so that the service of another container can be used in the current container. The last parameter is the image name, which is the name of the image to be pulled.

3. Enter the php-fpm container

docker exec -it xy_phpfpm bash

Parameter Description
-t creates a pseudo terminal in the container
-i Interact with the container's standard input (STDIN)

After creating an index.php file in /var/www/html/, you will find that it also exists locally, which means it is synchronized because the corresponding directories of the host and the container have been mapped when the container is started;

insert image description here
insert image description here

4. Install the pdo_mysql module

Since we will use the pdo module for testing later, we will install the pdo_mysql module.

docker-php-ext-install pdo_mysql

php -m prints to see if the installation is successful;

insert image description here

After installation, the extension may not be displayed after printing in phpinfo(). There will be modified steps in the subsequent test.

3. Install nginx

1. Pull the nginx image

docker pull nginx:1.10.3

2. Run the nginx container

docker run -d -p 80:80 -v D:/docker/nginx:/var/www/html --link xy_phpfpm:phpfpm --name xy_nginx nginx:1.10.3

3. Enter the nginx container and modify the nginx configuration file to support PHP

docker exec -it xy_nginx bash 

insert image description here

It is recommended to change it in /etc/nginx/conf.d/default.conf;
fastcgi_pass write php:7.0-fpm:9000. The alias you write may become invalid.
root is written as the code directory set by the previous PHP container;

4. Test whether the installation is successful

Modify index.php code

<?php

phpinfo(); 

insert image description here

After using the previously installed command, phpinfo() does not have mysql, so you need to go into php.ini to change it.

5. Modify the configuration

docker exec -it xy_phpfpm bash 

insert image description here

In this container, php.ini is in /usr/local/etc/php php.ini-development php.ini-production
These two files are the php.ini files. They are exactly the same. You can tell what they mean by looking at the names. Just open the extension=php_pdo_mysql.dll extension.

6. Link to mysql test

Modify index.php code

try {
    $con = new PDO('mysql:host=xy_mysql;dbname=mysql', 'root', 'xy123456');
    $con->query('SET NAMES UTF8');
    $res = $con->query('select * from user');
    while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
      // echo "id:{$row['id']} name:{$row['name']}";
	  print_r($row);
    }
} catch (PDOException $e) {
     echo 'Error reason:' . $e->getMessage();
}

If no error is reported, it is successful

Summarize

If successful, the primary environment is set up. Be careful during the entire process and check more information when configuring the file.

The above is the details of how to build an LNMP environment with Docker. For more information about how to build an LNMP environment with Docker, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Use Docker to create a distributed lnmp image
  • How to deploy LNMP architecture in docker
  • How to build lnmp environment 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

<<:  Is your website suitable for IE8?

>>:  Pure CSS to achieve the text description of semi-transparent effect when the mouse is placed on it (must read for novices)

Recommend

Vue+js realizes video fade-in and fade-out effect

Vue+js realizes the fade in and fade out of the v...

zabbix custom monitoring nginx status implementation process

Table of contents Zabbix custom monitoring nginx ...

Why should MySQL fields use NOT NULL?

I recently joined a new company and found some mi...

MySQL learning database operation DML detailed explanation for beginners

Table of contents 1. Insert statement 1.1 Insert ...

MySQL data type details

Table of contents 1. Numeric Type 1.1 Classificat...

Vite+Electron to quickly build VUE3 desktop applications

Table of contents 1. Introduction 2. Create a Vit...

The principle and application of MySQL connection query

Overview One of the most powerful features of MyS...

Implementation code of using select to select elements in Vue+Openlayer

Effect picture: Implementation code: <template...

MySql index detailed introduction and correct use method

MySql index detailed introduction and correct use...

How to use less in WeChat applet (optimal method)

Preface I am used to writing less/sass, but now I...

Detailed examples of using JavaScript event delegation (proxy)

Table of contents Introduction Example: Event del...

How to handle images in Vue forms

question: I have a form in Vue for uploading blog...

MySQL stored functions detailed introduction

Table of contents 1. Create a stored function 2. ...

10 bad habits to avoid in Docker container applications

There is no doubt that containers have become an ...