实验:Docker部署LNMP

椰子ya Linux 2020-11-29

双11活动买了三年腾讯云服务器,因为阿里云只有一年租期,所以想把博客迁移到腾讯云。刚好最近对Docker比较感兴趣,所以尝试以Docker部署LNMP。

一、部署Docker

安装过程可参考,Docker官网
安装完成后启动docker,并通过运行hello-world 映像来验证是否正确安装了Docker Engine 。

[root@cloud ~]#systemctl start docker
[root@cloud ~]# sudo docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

docker 常用命令

[root@cloud ~]# docker ps -a   #查看所有容器
[root@cloud ~]# docker stop nginx 停止正在运行的容器
[root@cloud ~]# docker start nginx 启动一个停止的容器
[root@cloud ~]# docker rm nginx 删除一个容器

二、部署LNMP
1、部署Nginx

[root@cloud ~]# docker pull nginx  #从Docker公共仓库拉取Nginx镜像
[root@cloud ~]# docker run --name nginx -p 80:80 -d docker #启动nginx,详细参考docker run命令 

通过浏览器localhost或本机IP访问,可以看到nginx的欢迎界面。
run:创建一个容器
--name:指定容器的名称,name为空,docker则自动分配一个名称
-p:导出容器端口到本地服务器,格式-p local-port:container-port
-d: 后台启动
nginx:是Dockerhub上下载的nginx镜像名称。

映射HTML路径:默认情况下Docker nginx服务器HTML根目录在容器/usr/share/nginx/html下,现在则需要把这个目录映射到本地的/usr/www/html目录,具体命令如下
-v local-path:container-path

[root@cloud ~]# docker stop nginx  #停止容器
[root@cloud ~]# docker rm nginx    #删除容器
[root@cloud ~]# docker --name nginx -p 80:80 -v /usr/www/html:/usr/share/nginx/html/ -d nginx
可以在local-path下新建index.html测试

配置Nginx
Nginx的强大部分体现在配置文件,所以自定义Nginx非常重要。所以我们要把Nginx配置文件复制到本地服务器目录

[root@cloud ~]# cd /usr/www/html/config/
[root@cloud ~]# docker cp nginx:/etc/nginx/conf.d/default.conf default.conf
[root@cloud ~]# docker run --name nginx -p 80:80 -p 443:443 -v /usr/www/html/:/usr/share/nginx/html/ -v /usr/www/html/config/default.conf:/etc/nginx/conf.d/default.conf -d nginx
由于后期考虑ssl 所以顺便把443端口也映射了出来
修改配置文件后,可通过以下命令重启容器,使配置生效。
[root@cloud ~]# docker restart nginx

2、部署Mysql

[root@cloud ~]# docker pull mysql
[root@cloud ~]# docker run mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql

-e username="ritchie": 设置环境变量;
-e MYSQL_ROOT_PASSWORD=123456

3、部署php-fpm

[root@cloud ~]# docker pull php:fpm
[root@cloud ~]# docker run --name php-fpm -p 9000:9000 -d php-fpm
[root@cloud ~]# cd /www/html/config/
[root@cloud config]# docker cp php-fpm:/usr/local/etc/php-fpm.d/www.conf www.conf
 docker pull php:fpm

[root@cloud ~]# docker ps -a   #查看容器的  containerID

[root@cloud ~]# docker exec -it containerID /bin/bash   #进入到正在运行的php-fpm容器中
root@e8f61e487bb6:/usr/src#cd /usr/src/
root@e8f61e487bb6:/usr/src#ls -ll   #进入到src中会看到2个压缩包
-rw-r--r-- 1 root root 11928820 Dec  4 18:40 php.tar.xz
-rw-r--r-- 1 root root      850 Dec  4 18:40 php.tar.xz.asc
root@e8f61e487bb6:/usr/src# xz -d php.tar.xz   #解压tar.xz文件:先 xz -d xxx.tar.xz 将 xxx.tar.xz解压成 xxx.tar 然后,再用 tar xvf xxx.tar来解包。
root@e8f61e487bb6:/usr/src#ls -ll
-rw-r--r-- 1 root root 139786240 Dec  4 18:40 php.tar
-rw-r--r-- 1 root root       850 Dec  4 18:40 php.tar.xz.asc
root@e8f61e487bb6:/usr/src#tar xvf php.tar
root@e8f61e487bb6:/usr/src#ls -ll
drwxr-xr-x 14 1000 1000      4096 Dec  4 16:12 php-7.3.0
-rw-r--r--  1 root root 139786240 Dec  4 18:40 php.tar
-rw-r--r--  1 root root       850 Dec  4 18:40 php.tar.xz.asc
exit;
#回到宿主机的config文件夹中,把容器里面的php.ini复制下来
[root@cloud config]#  docker cp php-fpm:/usr/src/php-7.4.12/php.ini-production php.ini
在本地服务器修改 php.ini 的内容,设置cgi.fix_pathinfo=0(要先删除前面的;注释符):

cgi.fix_pathinfo=0
然后重新启动容器:

[root@cloud config]#  docker stop php-fpm
[root@cloud config]#  docker rm php-fpm
[root@cloud config]#  docker run --name php-fpm --link mysql:mysql -v /usr/www/html/:/usr/share/nginx/html -v /usr/www/html/config/www.conf:/usr/local/etc/php-fpm.d/www.conf -v /usr/www/html/config/php.ini:/usr/local/etc/php-7.4.12/php.ini -d php:fpm

php-fpm容器部署完成。

4、配置nginx配置文件,使nginx支持php

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    root   html;
    gzip on;                     !!!!!!!!!!!!!!!!!!!!
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.php;         !!!!!!!!!!!!!!!!!!!!!!!!
    }

    #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    /usr/share/nginx/html;    !!!!!!!!!!!!!!!!!!!!!!!!!!!!
        fastcgi_pass   php-fpm:9000;      !!!!!!!!!!!!!!!!!!!!!!!!!!!!
        fastcgi_index  index.php;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    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;
    #}
}

使nginx与php-fpm建立连接

[root@cloud config]#docker stop nginx
[root@cloud config]#docker rm nginx
[root@cloud config]#docker run --name nginx -p 80:80 -p 443:443 --link php-fpm -v /usr/www/html/:/usr/share/nginx/html/ -v /usr/www/html/config/default.conf:/etc/nginx/conf.d/default.conf -d nginx

到这里已经可以正常显示php界面了

PREV
KVM-qeum安装虚拟机
NEXT
实验:华为VXLAN