LNMP部署wordpress

php镜像制作

  • php使用5.6版本
  • php使用 php-fpm 的方式
  • 需要使用 pdo_mysql 扩展,官方镜像无法满足,需要定制 dockerfile

dockerfile:

$ cat Dockerfile
FROM php:5.6-fpm

RUN docker-php-ext-install pdo_mysql \
        && docker-php-ext-install mysql \
        && docker-php-ext-install mysqli \
        && curl -fsSL 'http://pecl.php.net/get/redis-2.2.8.tgz' -o redis-2.2.8.tgz \
        && tar zxf redis-2.2.8.tgz \
        && rm redis-2.2.8.tgz \
        && ( \
                cd redis-2.2.8 \
                && phpize \
                && ./configure --with-php-config=/usr/local/bin/php-config \
                && make && make install \
        ) \
        && rm -rf ../redis-2.2.8 \
        && docker-php-ext-enable redis

生成镜像 docker build -t local/php:5.6-fpm

[info] 没有做私有仓库,stack的时候需要指定容器运行的节点。正常需要上传到仓库通过pull来执行

创建overlay网络

所有的服务都加入到该网络,内部使用该网络通信,只需要将 80 端口暴露给用户

docker network create --driver overlay lnmp

创建YAML文件

$ cat lnmp.yaml 
version: "3"
services:
  nginx:
    image: nginx:1.12.2-alpine
    ports:
      - "80:80"
    volumes:
      - /data:/data/wwwroot
      - /root/lnmp/nginx.conf:/etc/nginx/nginx.conf:ro 
    deploy:
      replicas: 3
    networks:
      - backend
    depends_on:
      - php

  php:
    image: local/php:5.6-fpm
    volumes:
      - /data:/data/wwwroot
    deploy:
      replicas: 2
      placement:
        constraints:
          - node.hostname == node2
    networks:
      - backend
    depends_on:
      - redis
      - mysql

  redis:
    image: redis:3.2.11
    deploy:
      replicas: 1
    networks:
      - backend
    depends_on:
      - mysql

  mysql:
    image: mysql:5.6
    deploy:
      replicas: 1
    networks:
      - backend
    environment:
      MYSQL_ROOT_PASSWORD: mysqlpasswd
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

networks:
  backend:
    external:
      name: lnmp

创建数据目录

  • 每台节点创建/data/目录, 作为web站点目录
  • 创建/root/lnmp目录,作为配置文件存放

[info] nginx配置文件

$ cat nginx.conf
#user nginx;
worker_processes auto;

error_log /var/log/nginx/error_nginx.log crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;

events {
  use epoll;
  worker_connections 51200;
  multi_accept on;
}

http {
  include mime.types;
  default_type application/octet-stream;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 1024m;
  client_body_buffer_size 10m;
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 120;
  server_tokens off;
  tcp_nodelay on;

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  fastcgi_intercept_errors on;

  #Gzip Compression
  gzip on;
  gzip_buffers 16 8k;
  gzip_comp_level 6;
  gzip_http_version 1.1;
  gzip_min_length 256;
  gzip_proxied any;
  gzip_vary on;
  gzip_types
    text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
    text/javascript application/javascript application/x-javascript
    text/x-json application/json application/x-web-app-manifest+json
    text/css text/plain text/x-component
    font/opentype application/x-font-ttf application/vnd.ms-fontobject
    image/x-icon;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  #If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
  open_file_cache max=1000 inactive=20s;
  open_file_cache_valid 30s;
  open_file_cache_min_uses 2;
  open_file_cache_errors on;

  log_format main '$remote_addr $remote_user [$time_local] '
                  '"$request" $status $bytes_sent '
                  '"$http_referer" "$http_user_agent"';

######################## default ############################
  server {
  listen 80;
  server_name _;
  access_log /var/log/nginx/access_nginx.log combined;
  root /data/wwwroot;
  index index.html index.htm index.php;
  location ~ [^/]\.php(/|$) {
    fastcgi_pass php:9000;
    #fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
  }

########################## vhost #############################
  include vhost/*.conf;
}

[info] phpinfo测试文件

$ cat phpinfo.php 
<?php
echo phpinfo();

[info] mysql连接测试文件
主机名和用户名等根据yaml配置文件填写

$ cat mysql.php 
<?php
$servername = "mysql";
$username = "wordpress";
$password = "wordpress";
$dbname = "wordpress";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    echo "连接成功"; 
}
catch(PDOException $e)
{
    echo $e->getMessage();
}
?>

[info] redis连接测试文件

$ cat redis.php
<?php

$redis = new Redis();
try {
    $redis->connect('redis', 6379);
    echo var_dump($redis->info());
}
catch(RedisException $e)
{
    echo '连接失败: ' . $e->getMessage();
}

运行lnmp服务

docker stack deploy -c lnmp.yaml lnmp

访问服务,通过任何节点的http都可以访问你的服务.

[info] http://{节点ip}/phpinfo.php

phpinfo信息

[info] http://{节点ip}/redis.php

redis info 信息

[info] http://{节点ip}/mysql.php

mysql 连接成功

部署wordpress

  • 下载 wordpress-4.9.4-zh_CN.zip
  • 将包解压并放置到/data目录
  • 访问 http://{节点ip}/wordpress/index.php 进行安装配置

results matching ""

    No results matching ""