Docker builds LNMP environment (New)

Posted by trauch on Mon, 24 Jan 2022 09:23:34 +0100

preface

Tip: Here you can add the general contents to be recorded in this article:
For example, with the continuous development of artificial intelligence, machine learning technology is becoming more and more important. Many people have started learning machine learning. This paper 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 mysql image

docker pull mysql:5.6

2. Run and start the mysql container

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


Parameter Description:
-d let the container move in the background
-p add host to container port mapping
-e set the mysql environment variable and set the initial password of root
– name name the container
The last parameter is the image name, which is the name of the pulled image

2, Install PHP FPM

1. Pull 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 exist in the warehouse. Don't worry

2. Run and 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 let the container move in the background
-p add host to container port mapping
-v add directory mapping. D:/docker/nginx on the host is mapped to / var/www/html, that is, synchronize the contents under the directory. ' D:/docker/nginx 'you can directly copy the directory under the host you want
The directory where the code is written can be used. If $PWD is valid, it refers to the current directory. Because I reported an error using this parameter, I directly copied and pasted the path
– name name the container
– 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 pulled image

3. Enter PHP FPM container

docker exec -it xy_phpfpm bash

Parameter description
-t produce a pseudo terminal in the container
-i interact with the standard input (STDIN) in the container

Create an index under / var/www/html / After the PHP file, you will find that there will also be local files, which is synchronized, because the corresponding directories of the host and container have been synchronized when starting the container
Mapped;

4. Install pdo_mysql module

Since the pdo module will be used for testing later, pdo is installed_ MySQL module

docker-php-ext-install pdo_mysql

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

In this way, the extension will not necessarily be displayed after phpinfo() printing after installation, and the test will have modification steps later

3, Installing nginx

1. Pull nginx image

docker pull nginx:1.10.3

2. Run 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

It is recommended to go to / etc / nginx / conf.d/default Conf change;
fastcgi_ Write php:7.0-fpm:9000 in pass. The alias you don't write may become invalid;
root is written as the code directory set by the php container before;

4. Test whether the installation is successful

Modify index PHP code

<?php

phpinfo();


After using the previously installed command, phpinfo() does not have mysql and needs to enter PHP Ini to change

5. Modify configuration

docker exec -it xy_phpfpm bash

This container contains PHP Ini in / usr/local/etc/php ini-development php.ini-production
These two files are PHP INI files as like as two peas. What do you mean by name?
Put extension=php_pdo_mysql.dll extension can be released

5. Link 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

summary

If you are successful, the primary environment will be ok. You should be careful in the whole process. Check more data when configuring files

Topics: PHP Linux Docker Nginx