Analyze how docker builds lnmp environment (php7.4 + nginx)

青灯夜游
Release: 2022-02-15 10:25:08
forward
6450 people have browsed it

This article will introduce you to the installation method of docker under Linux, and learn more about the steps of building lnmp environment (php7.4 nginx) with docker. I hope it will be helpful to you!

Analyze how docker builds lnmp environment (php7.4 + nginx)

1. Docker basics

1.1 Docker installation

The installation of docker is very simple, we can directly Use the yum command in the centos system to install:

yum install -y docker
Copy after login

After completion, check the docker version information:

[root@localhost docker]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-204.git0be3e21.el7.x86_64
 Go version:      go1.10.3
 Git commit:      0be3e21/1.13.1
 Built:           Fri Mar 19 13:57:09 2021
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-204.git0be3e21.el7.x86_64
 Go version:      go1.10.3
 Git commit:      0be3e21/1.13.1
 Built:           Fri Mar 19 13:57:09 2021
 OS/Arch:         linux/amd64
 Experimental:    false
[root@localhost docker]#
Copy after login

Prove that the installation is complete. The docker command is as follows:

systemctl start docker   # 启动docker
systemctl stop docker    # 停止docker
systemctl status docker  # 查看docker状态
systemctl restart docker # 重新启动docker
Copy after login

sudo systemctl enable docker #Set docker to start automatically at boot

Before starting, we need to modify the docker image source:

[root@localhost docker]# vim /etc/docker/daemon.json

{
  "registry-mirrors": ["https://ftnejmh3.mirror.aliyuncs.com"]
}
Copy after login

After completion, run systemctl start docker to start the docker service.

2. Docker builds lnmp environment

Introduction to environment software:

1. docker

2. nginx

3. mysql

4. php7.4 [Recommended learning: "PHP Video Tutorial"]

lnmp directory structure built by docker:

docker
│   └── nginx
│   │   └── default.conf #nginx配置文件
│   └── www
│       └── ...  #项目代码
Copy after login

2.1 Docker builds nginx

We can use the docker search nginx command to find the nginx image on Docker Hub. Here we directly pull the official image

[root@localhost ~]# docker pull nginx
Copy after login

After the download is completed, we can The image whose REPOSITORY is nginx is found in the local image list.

[root@localhost www]# docker images
REPOSITORY       TAG       IMAGE ID        CREATED        SIZE
docker.io/nginx  latest    62d49f9bab67    12 days ago    133 MB
Copy after login

Create nginx configuration

[root@localhost ~]# cd /docker/nginx
[root@localhost ~]# touch default.conf
[root@localhost ~]# vim default.conf

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    root /docker/www/lmrs-2008/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /docker/www/lmrs-2008/public;
    }

    location ~ \.php$ {
        root /docker/www/lmrs-2008/public;
        fastcgi_pass   172.17.0.3:9000;    //容器的ip地址
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
Copy after login
fastcgi_pass   172.17.0.3:9000;
Copy after login

View ip information

[root@localhost docker]# docker inspect php | grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",
Copy after login

Use nginx image to open nginx application container

[root@localhost ~]# docker run -p 80:80 -d --name nginx -v /docker/nginx/default.conf:/etc/nginx/conf.d/default.conf -v /docker/www:/docker/www  --privileged=true nginx

-p 80:80:将容器的80端口映射到主机的80端口
-d 后台运行(守护进程)
--name nginx:将容器命名为nginx
-v 将主机中当前目录下的www挂载到容器的www目录

#查看docker目前的容器
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
8781afd1bf13        nginx               "/docker-entrypoin..."   About an hour ago   Up 17 minutes       0.0.0.0:80->80/tcp                  nginx
Copy after login

2.2 docker installation php

Like nginx, we can first find the image through docker search php. Here we directly pull the official image, labeled 7.4-fpm. You can choose other versions by yourself

[root@localhost ~]# docker pull php:7.4-fpm
Copy after login

After the download is completed, we can find the image with the REPOSITORY of php and the label of 7.4-fpm in the local image list.

[root@localhost docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/nginx     latest              62d49f9bab67        12 days ago         133 MB
docker.io/php       7.4-fpm             41b17b0f90e6        2 weeks ago         405 MB
Copy after login

Use php mirror to open the php-frm application container

[root@localhost docker]# docker run -p 9000:9000 -d --name php -v /docker/www:/docker/www --privileged=true php:7.4-fpm
-p 9000:9000 :将容器的9000端口映射到主机的9000端口
-d 后台运行(守护进程)
--name php:将容器命名为php
-v 将主机中当前目录下的www挂载到容器的www目录
Copy after login

Check the container startup status

[root@localhost docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
cbf73ca0f17c        php:7.4-fpm         "docker-php-entryp..."   About an hour ago   Up About an hour    0.0.0.0:9000->9000/tcp              php
8781afd1bf13        nginx               "/docker-entrypoin..."   About an hour ago   Up 23 minutes       0.0.0.0:80->80/tcp                  nginx
Copy after login

At this point, you can see that nginx and php are running successfully (STATUS is up Indicates that it is running)

Check whether the php-fpm service IP configured by nginx is consistent:

location ~ \.php$ {
    root /docker/www/lmrs-2008/public;
    fastcgi_pass   172.17.0.3:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
Copy after login

In the above configurationfastcgi_pass 172.17.0.3:9000 ;Needs to be consistent with the IP of the php container we are viewing.

After completing the above configuration and confirming that there is no problem starting the container, you can test it:

There may be directory permission problems when accessing laravel. The following is the solution:

[root@localhost docker]# cd /docker/www
[root@localhost www]# chmod -R 777  文件名
Copy after login

Recommended learning: "docker video tutorial"

The above is the detailed content of Analyze how docker builds lnmp environment (php7.4 + nginx). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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 [email protected]
Latest issues
Popular Tutorials
More>
Latest downloads
More>
web effects
Website source code
Website materials
Front end template
About us Disclaimer Sitemap
PHP Chinese website:Public welfare online PHP training,Help PHP learners grow quickly!