使用ansible-play部署wordpress

发布于 2024-06-11  423 次阅读


ansible主机清单

[root@ansible ~]# cat /etc/ansible/hosts 
[databases]
192.168.1.52    name="db"

[webserver]
192.168.1.53    name="web" 

nginx配置文件

[root@ansible ~]# cat nginx.conf 
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user {{ NGINX_USER }};
worker_processes {{ NGINX_FORKS }};
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       {{ NGINX_PORT | default(80) }};
        server_name  _;
        root     {{ BASE_DIR }}; 
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            root {{ BASE_DIR }}; 
            index index.php;
        }
        location ~ \.php$ {
            root          {{ BASE_DIR }};  #指定网站目录
            fastcgi_pass   unix:///var/opt/remi/php80/run/php-fpm/www.sock; 
            fastcgi_index  index.php;       #指定默认文件
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; #站点根目录,取决于root配置项
            include        fastcgi_params;  #包含nginx常量定义
                }
        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

wordpress.yaml

[root@ansible ~]# cat wordpress.yaml 
---
- hosts: databases,webserver
  remote_user: root
  gather_facts: no
  vars:
    MYSQL_DB_NAME: wordpress
    MYSQL_USER: wordpress
    MYSQL_PASSWORD: "123456"
    MYSQL_HOST: "%"
    BASE_DIR: /usr/share/nginx/wordpress/
    NGINX_USER: nginx
    NGINX_PORT: 80
    NGINX_FORKS: auto
  tasks:
    - name: Install MySQL
      yum: name=mariadb-server,mariadb state=present
      when: name == "db"
    - name: Start and enable MySQL service
      service: name=mariadb state=started enabled=yes
      when: name == "db"

    - name: Create MySQL database
      shell: mysql -e "CREATE DATABASE if not exists {{ MYSQL_DB_NAME }};grant all privileges on {{ MYSQL_DB_NAME }}.* to '{{ MYSQL_USER }}'@'{{ MYSQL_HOST }}' identified by '{{ MYSQL_PASSWORD }}';flush privileges;"
      when: name == "db"

    - name: Install PHP packages and epel-release
      yum: name={{ item }} state=present disable_gpg_check=yes
      with_items:
        - epel-release
        - http://rpms.remirepo.net/enterprise/remi-release-9.rpm
      when: name == "web"

    - name: Install PHP and nginx
      yum: name=php80-php-xsl,php80-php,php80-php-cli,php80-php-devel,php80-php-gd,php80-php-pdo,php80-php-mysql,php80-php-fpm,nginx state=present
      when: name == "web"

    - name: Configure nginx
      template: src=/root/nginx.conf dest=/etc/nginx/nginx.conf
      when: name == "web"

    - name: Start and enable services PHP and nginx
      service: name={{ item }} state=started enabled=yes
      with_items:
        - nginx
        - php80-php-fpm
      when: name == "web"

    - name: Configure PHP-FPM
      shell: chmod 777 /var/opt/remi/php80/run/php-fpm/www.sock;
      when: name == "web"

    - name: Copy wordpress files to webserver
      #  /usr/share/nginx/wordpress/
      unarchive: src=/opt/latest-zh_CN.zip dest=/usr/share/nginx/ mode=777
      when: name == "web"
[root@ansible ~]# ansible-playbook wordpress.yaml

注意

1、php的www.conf配置文件中的listen的监听方式是什么
1)listen 127.0.0.1:9000; 那么nginx中的配置为fastcgi_pass 127.0.0.1:9000;
2) listen = /var/opt/remi/php80/run/php-fpm/www.sock; 那么nginx中的配置为fastcgi_pass unix:///var/opt/remi/php80/run/php-fpm/www.sock;

2、注意准备的nginx配置文件,在哪个目录。 playbook中nginx的配置文件在/root目录中

3、注意下载的wordpress项目包的名字及所放到目录是哪个,还有就是梳理清晰nginx到网站发布目录是哪里。


一沙一世界,一花一天堂。君掌盛无边,刹那成永恒。