跳转至

使用 WordPress 部署官网

我司官网之前是手动开发、感觉有点繁琐、使用 wordpress 替换。

物理机部署

编写 playbook-wordpress.yml

- hosts: wordpress
  remote_user: root
  tasks:
    - name: Install Nginx
      apt:
        name:
          - nginx
        update_cache: no

    - name: Install PHP
      apt:
        name:
          - php-curl 
          - php-gd
          - php-intl 
          - php-mbstring 
          - php-soap 
          - php-xml 
          - php-xmlrpc 
          - php-zip
          - php-fpm
          - php-mysql
        update_cache: no

    - name: 上传 Nginx 配置文件
      ansible.builtin.copy:
        src: "wordpress/wordpress.conf"
        dest: "/etc/nginx/conf.d/wordpress.conf"
        mode: "0644"

    - name: 删除默认 Nginx 配置
      become: true
      file:
        path: /etc/nginx/sites-enabled/default
        state: absent

    - name: 解压 WordPress 文件,将文件下载至 files 中,将自动解压,下载地址 ( https://cn.wordpress.org/download/#download-install )
      unarchive:
        src: latest-zh_CN.tar.gz
        dest: /var/www/
        owner: www-data
        group: www-data

    - name: Start Nginx, if not started
      service:
        name: nginx
        state: restarted

    - name: Enable autostart
      shell: systemctl enable nginx 

编写 inventory.ini

[wordpress]

编写主站 nginx 文件

mkdir wordpress && vim wordpress/wordpress.conf

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/wordpress;

        index index.php;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi_params;
                fastcgi_intercept_errors on;
                # 按需选择 9000 sock
                # fastcgi_pass unix:/var/run/php/php-fpm.sock;
                fastcgi_pass 127.0.0.1:9000;
                #The following parameter can be also included in fastcgi_params file
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param HTTPS on;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}

执行以下命令安装 主站

pipenv run ansible-playbook playbook-wordpress.yml -i inventory.ini

修改 php 设置

  • 设置上传限制
sed -i "s/post_max_size = .M/post_max_size = 250M/ ; s/upload_max_filesize = .M/upload_max_filesize = 250M/" /etc/php/*/fpm/php.ini

迁移使用 docker 部署并配置安全组

编写 playbook-wordpress.yml

- hosts: wordpress
  remote_user: root
  vars:
    service_name: wordpress
    service_path: /root/wordpress
  environment:
    LC_ALL: C.UTF-8
    LANG: C.UTF-8
  tasks:
    - name: Synchronize directory
      ansible.posix.synchronize:
        src: "docker-compose/{{ service_name }}/"
        dest: "{{ service_path }}/"

    - name: Docker pull images
      ansible.builtin.shell:
        chdir: "{{ service_path }}"
        cmd: docker compose pull

    - name: Start service
      ansible.builtin.shell:
        chdir: "{{ service_path }}"
        cmd: docker compose up -d --remove-orphans --force-recreate

    - name: Prune container and image
      ansible.builtin.shell: docker system prune --force

编写 compose.yaml 文件

docker-compose/wordpress/compose.yaml

services:
  wordpress:
    image: wordpress:latest
    ports:
      - 80:80
    restart: always
    volumes:
      - ./official:/var/www/html
      - ./upload.ini:/usr/local/etc/php/conf.d/uploads.ini

修改 php 设置 upload.ini

file_uploads = On
memory_limit = 256M
upload_max_filesize = 300M
post_max_size = 300M
max_execution_time = 360

安全组 ( 不允许出站 )

  • 入方向

img

  • 出方向

img