DNMP:基于docker搭建集成LNMP(nginx+mysql+php)

有一天,主机商跑路了。迫于无奈之下,我需要进行数据迁移。原先用的主机我可以什么都不管,直接用就行。而现在,只有一个centos服务器。一切从零开始...

我曾试过一个一个安装,从docker入手,一步一步搭建mysql,nginx,php。正当我即将看到胜利的曙光时,现实又给了我当头一棒。php扩展没有pdo_mysql,php.ini配置文件找不到等等。各种问题接踵而至。我曾多次想过放弃。不知道到底哪个环节出错了,如果让我从新再来,想想就让人心生疲惫。直到我看到了这个: DNMP(LNMP)一键安装程序 。注意:这不是广告,亲测,亲测。

我们先初步了解下他的强大之处:

DNMP:基于docker搭建集成LNMP(nginx+mysql+php)_第1张图片

 好家伙,口气还真不小。事实是否真就如此呢。我们就来动手实操一番。是骡子是马拉出来溜溜。我们就按照他的要求,一步步操作。

1.准备工作(docker+docker-compose 1.7以上+git)

docker安装就不多加叙述,根据服务器系统的不同,百度一下对应的安装命令即可。

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

 国内 daocloud 一键安装命令:

curl -sSL https://get.daocloud.io/docker | sh

 DNMP:基于docker搭建集成LNMP(nginx+mysql+php)_第2张图片

安装完之后可以输入:docker -version查看版本。 

docker-compose安装命令:先确定安装目录。我这里打算安装到home目录下的dnmp子目录

下载docker-compose命令:要安装其他版本的 Compose,请替换 v2.2.2。

$ sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

DNMP:基于docker搭建集成LNMP(nginx+mysql+php)_第3张图片

 至此docker和docker-compose已安装完成,而git只是为了clone项目。我们可以直接选择去git下载即可。这里就不安装了。下载完之后的目录结构如下:

DNMP:基于docker搭建集成LNMP(nginx+mysql+php)_第4张图片

 我们将文件上传到dnmp目录下,这里我是通过xftp上传的。

DNMP:基于docker搭建集成LNMP(nginx+mysql+php)_第5张图片

 2.启动项目

DNMP:基于docker搭建集成LNMP(nginx+mysql+php)_第6张图片

 第一步和第二步我们已经完成,第三步不用管,现在我们直接进行第四步,继续终端:依次输入命令,别忘了启动docker,启动命令:start docker.service

[root@localhost dnmp]# docker-compose --version
Docker Compose version v2.2.2
[root@localhost dnmp]# ls
bash.alias.sample  data  docker-compose.sample.yml  env.sample  LICENSE  logs  README-en.md  README.md  services  snapshot.png  www
[root@localhost dnmp]# cp env.sample .env
[root@localhost dnmp]# cp docker-compose.sample.yml docker-compose.yml
[root@localhost dnmp]# docker-compose up

DNMP:基于docker搭建集成LNMP(nginx+mysql+php)_第7张图片

 至此,基于docker的lnmp集成环境就安装完成啦~~,让我们奔放一下~

DNMP:基于docker搭建集成LNMP(nginx+mysql+php)_第8张图片

 very good !!!接下来就是自由发挥时间了,根据说明,我们可以更换mysql版本和php版本以及php扩展,另外,该集成还自备https。这就很nice了。下面我们继续深入一下。

3.数据库

数据库一共有两个版本,第一个是mysql8.0,第二个是mysql5.7,集成环境默认搭建的配置是:

DNMP:基于docker搭建集成LNMP(nginx+mysql+php)_第9张图片

 如果要下载其他的php版本或者mysql版本,找到dnmp目录下的docker-compose.yml配置文件,把前面的#号去掉皆可,再使用命令 docker-compose up创建启动一下就行了。数据库的默认用户名root,密码123456。

注意:另外下载的mysql5.7对应的端口为3305

 通过docker进入mysql,修改密码sql语句如下:两个用户都修改

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                                                      NAMES
9d4ece6148f1   dnmp_nginx     "/docker-entrypoint.…"   13 minutes ago   Up 13 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   nginx
89b79105ce9c   mysql:8.0.13   "docker-entrypoint.s…"   13 minutes ago   Up 13 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp                       mysql
a5333f2cfde3   dnmp_php       "docker-php-entrypoi…"   13 minutes ago   Up 13 minutes   9000/tcp, 9501/tcp                                                         php
[root@localhost ~]# docker exec -it 89b79105ce9c /bin/bash
root@89b79105ce9c:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.13 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 
#mysql5.7修改密码
use mysql;
update user set authentication_string=password('liuqing') where user='root' and host='localhost';
update user set authentication_string=password('liuqing') where user='root' and host='%';

#mysql8.0修改密码
use mysql;
ALTER user 'root'@'localhost' IDENTIFIED BY 'liuqing';
ALTER user 'root'@'%' IDENTIFIED BY 'liuqing';

4.nginx静态配置

lz的项目是thinkphp5.0的,有两个,分别位于站点根目录www/api/tp5和www/dream。

DNMP:基于docker搭建集成LNMP(nginx+mysql+php)_第10张图片DNMP:基于docker搭建集成LNMP(nginx+mysql+php)_第11张图片

dnmp/services/nginx/conf.d文件夹下的localhost.conf配置如下:

server {
    listen       80  default;
    server_name  localhost;
    root   /www/localhost;
    index  index.php index.html index.htm;
    #charset koi8-r;
    
    access_log /dev/null;
    #access_log  /var/log/nginx/nginx.localhost.access.log  main;
    error_log  /var/log/nginx/nginx.localhost.error.log  warn;
    
    #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($|/){
        fastcgi_pass   php:9000;
        include        fastcgi-php.conf;
        include        fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
location /api/tp5/public/ {
        index index.php;
        #如果文件不存在则尝试TP解析
        if (!-e $request_filename) {
           rewrite  ^/api/tp5/public/(.*)$  /api/tp5/public/index.php?s=/$1  last;
           break;
        }
    }
location /dream/public/ {
        index index.php;
        #如果文件不存在则尝试TP解析
        if (!-e $request_filename) {
           rewrite  ^/dream/public/(.*)$  /dream/public/index.php?s=/$1  last;
           break;
        }
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

server {
    listen 443  default ssl http2;
    server_name  localhost;
    root   /www/localhost;
    index  index.php index.html index.htm;
    #charset koi8-r;

    access_log /dev/null;
    #access_log  /var/log/nginx/nginx.localhost.access.log  main;
    error_log  /var/log/nginx/nginx.localhost.error.log  warn;

    #error_page  404              /404.html;

    ssl_certificate /ssl/localhost/localhost.crt;
    ssl_certificate_key /ssl/localhost/localhost.key;

    # 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$ {
        fastcgi_pass   php:9000;
        include        fastcgi-php.conf;
        include        fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
location /api/tp5/public/ {
        index index.php;
        #如果文件不存在则尝试TP解析
        if (!-e $request_filename) {
           rewrite  ^/api/tp5/public/(.*)$  /api/tp5/public/index.php?s=/$1  last;
           break;
        }
    }
location /dream/public/ {
        index index.php;
        #如果文件不存在则尝试TP解析
        if (!-e $request_filename) {
           rewrite  ^/dream/public/(.*)$  /dream/public/index.php?s=/$1  last;
           break;
        }
    }
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

 5.配置SSL

这个就不多说了dnmp/services/ngin/ssl/localhost文件下的两个证书文件替换成自己的就行。

DNMP:基于docker搭建集成LNMP(nginx+mysql+php)_第12张图片

 

以上,就是docker安装集成环境的全部步骤,lz一路摸索一路爬坑。索性,幸不辱命!!

你可能感兴趣的:(php,linux,mysql,php,nginx,docker,docker-compose)