zoukankan      html  css  js  c++  java
  • Docker五分钟搭建Wordpress

    当你看到这篇文章的时候,表明你已经有docker的基础知识了,或者可以看上一篇文章 Docker 入门教程

    传统的使用wordpress搭建网站,意味着你需要搭建以下四个环境:

    • php;
    • apache / nginx;
    • mysql;
    • wordpress;

    这里面主要是php的搭建真心麻烦,各种依赖,版本不兼容,然后还有php跟mysql的插件,我是吃了它很大的苦,寻求让我快乐的方法,知道我发现了它。使用docker容器技术5分钟快速搭建wordpress,相信我,真的是五分钟。

    一台linux服务器

    你需要一台linux或者ubuntu服务器,阿里云或者腾讯云的都可以。

    新建并启动 MySQL 容器

    $ docker container run 
      -d 
      --rm 
      --name wordpressdb 
      --env MYSQL_ROOT_PASSWORD=123456 
      --env MYSQL_DATABASE=wordpress 
      mysql:5.7
    

    上面的命令会基于 MySQL 的 image 文件(5.7版本)新建一个容器。该命令的五个命令行参数的含义如下。

    • --rm:停止运行后,自动删除容器文件。
    • --name wordpress:容器的名字叫做wordpress。
    • --env MYSQL_ROOT_PASSWORD=123456:向容器进程传入一个环境变量MYSQL_ROOT_PASSWORD,该变量会被用作 MySQL 的根密码。
    • --env MYSQL_DATABASE=wordpress:向容器进程传入一个环境变量MYSQL_DATABASE,容器里面的 MySQL 会根据该变量创建一个同名数据库(本例是WordPress)。

    新建并启动 WordPress 容器

    基于官方的 WordPress image,新建并启动 WordPress 容器。

    docker container run 
      -d 
      -p 8080:80 
      --rm 
      --name wordpress 
      --env WORDPRESS_DB_PASSWORD=123456 
      --link wordpressdb:mysql 
      --volume "$PWD/wordpress":/var/www/html 
      wordpress
    

    该命令执行完后,会在当前目录下生成一个wordpress目录。

    -p 8080:80:将容器的 80 端口映射到当前物理机的8080端口。
    --volume "$PWD/wordpress":/var/www/html:将容器的`/var/www/html`目录映射到当前目录的wordpress子目录,操作$pwd/wordpress目录,相当于操作容器里面的/var/www/html目录了。
    

    这个时候你想浏览器输入公网ip : 8080端口,其实是访问不了的,有两个原因:

    • 有的人可能没有这个问题,比如我用的是阿里云的服务器,8080端口默认没有加入安全组,需要去阿里云控制台新建安全组,把8080端口加进去;
    • 容器里面的wordpress数据库配置文件没有修改,这里通过$pwd/wordpress映射,修改。

    修改WordPress配置文件

    目前有两个容器

    [root@iZbp180j96p8y98l1s1oucZ ~]# docker container ls
    CONTAINER ID   IMAGE       COMMAND                  CREATED        STATUS        PORTS                       NAMES
    f03c15abfe54   wordpress   "docker-entrypoint.s…"   17 hours ago   Up 17 hours   172.24.49.41:8080->80/tcp   wordpress
    f6274d57162a   mysql:5.7   "docker-entrypoint.s…"   18 hours ago   Up 18 hours   3306/tcp, 33060/tcp         wordpressdb
    

    进入wordpress容器,通过names名为wordpress,或者container id为f03c15abfe54,都可以进入容器,命令格式为:

    [root@iZbp180j96p8y98l1s1oucZ ~]# docker exec -it f03c15abfe54 /bin/bash
    root@f03c15abfe54:/var/www/html# cd wp-content/
    root@f03c15abfe54:/var/www/html/wp-content# ll
    bash: ll: command not found
    

    通过exit命令即可退出当前容器环境,返回到linux命令行。

    可以看到,在容器里面操作及其不方便,由于前面将容器的/var/www/html目录映射到当前目录的wordpress子目录,我们可以直接操作当前目录下的wordpress目录。

    [root@iZbp180j96p8y98l1s1oucZ ~]# ll
    total 4
    drwxr-xr-x 5 33 tape 4096 May 27 12:35 wordpress
    [root@iZbp180j96p8y98l1s1oucZ ~]# vim wordpress/wp-config.php 
    

    需要修改下面三个

    // ** MySQL settings - You can get this info from your web host ** //
    /** The name of the database for WordPress */
    define( 'DB_NAME', getenv_docker('WORDPRESS_DB_NAME', 'wordpress') );
    
    /** MySQL database username */
    define( 'DB_USER', getenv_docker('WORDPRESS_DB_USER', 'root') );
    
    /** MySQL database password */
    define( 'DB_PASSWORD', getenv_docker('WORDPRESS_DB_PASSWORD', '123456') );
    

    然后浏览器输入公网ip:8080,即可进去wordpress安装界面,跟着向导,几分钟就完成了。

    到这里,如果不出意外的话,你的网站应该已经可以访问了,接下来就是绑定到域名以及写文章去丰富内容了。


    如果想进入mysql容器,也是同样类似的命令:

    [root@iZbp180j96p8y98l1s1oucZ ~]# docker exec -it f6274d57162a /bin/bash
    root@f6274d57162a:/# mysql -u root -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 812
    Server version: 5.7.34 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2021, Oracle and/or its affiliates.
    
    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                |
    | wordpress          |
    +--------------------+
    5 rows in set (0.00 sec)
    
    mysql> exit;
    Bye
    root@f6274d57162a:/# exit
    exit
    
  • 相关阅读:
    Python socket 通信功能简介
    python2 && python3 的 input函数
    python 监听键盘输入
    std_msgs/String.msg
    python中string、json、bytes的转换
    python json与字典对象互相转换
    maven依赖关系中Scope的作用
    Maven项目下HttpServletRequest 或 HttpServletResponse需引用的依赖包
    Setup SS5 Socks Proxy
    Turn any Linux computer into SOCKS5 proxy in one command
  • 原文地址:https://www.cnblogs.com/data-magnifier/p/14827625.html
Copyright © 2011-2022 走看看