在 Web 服务器上安装 php 时 Ansible Haproxy 不工作(错误 503 服务不可用)

Ansible Haproxy not working when php installed on webserver (error 503 Service Unavailable)

所以我已经被这个问题困扰了几个小时,我似乎无法弄清楚。

Web 服务器任务文件

---
- name: (APACHE) Install Apache Web server and PHP
  package:
    name: "{{ item }}"
    state: latest
  with_items:
    - apache2
    - ufw
    - php # Removing this package will make it work.
- name: (APACHE) Remove the default Websites
  file:
    path: /etc/apache2/sites-enabled/000-default.conf
    state: absent
  notify: reload apache

- name: (APACHE) DEBUG - Allow RSYNC for new super user without SUDO password
  lineinfile:
    path: /etc/sudoers
    state: present
    insertafter: '^%sudo'
    line: "{{ ansible_user }} ALL=NOPASSWD: /usr/bin/rsync"
  changed_when: false

- name: (APACHE) Copy some template for Web servers
  synchronize:
    src: files/apache_templates/
    dest: /root/
    links: yes
    recursive: yes

- name: (APACHE) DEBUG - Disallow RSYNC for new super user without SUDO password
  lineinfile:
    path: /etc/sudoers
    state: absent
    insertafter: '^%sudo'
    line: "{{ ansible_user }} ALL=NOPASSWD: /usr/bin/rsync"
  changed_when: false

- name: (APACHE) Copy the Diffie-Hellmann parameters
  copy:
    src: dhparams.pem
    dest: /etc/ssl/dhparams.pem
    owner: root
    group: root
    mode: 0644

- name: (APACHE) Check enabled modules
  stat:
    path: /etc/apache2/mods-enabled/{{ item }}
  with_items:
    - ssl.load
    - actions.load
    - proxy_fcgi.load
    - env.load
    - rewrite.load
    - headers.load
  register: modules

- name: (APACHE) Enable missing modules
  command: a2enmod {{ item.item }}
  with_items: "{{ modules.results }}"
  notify: restart apache
  when: not item.stat.exists

- name: (APACHE) Open the HTTP port on the firewall
  ufw:
    rule: allow
    port: 80
    proto: tcp
    direction: in
  notify: reload ufw

- name: (APACHE) Open the HTTPS port on the firewall
  ufw:
    rule: allow
    port: 443
    proto: tcp
    direction: in
  notify: reload ufw

- name: (USERSKEL) DEBUG - Allow RSYNC for new super user without SUDO password
  lineinfile:
    path: /etc/sudoers
    state: present
    insertafter: '^%sudo'
    line: "{{ ansible_user }} ALL=NOPASSWD: /usr/bin/rsync"
  changed_when: false

- name: (USERSKEL) Copy the user's skeleton template for Web servers
  synchronize:
    src: files/user_skel/
    dest: /etc/skel/
    links: yes
    recursive: yes

- name: (USERSKEL) DEBUG - Disallow RSYNC for new super user without SUDO password
  lineinfile:
    path: /etc/sudoers
    state: absent
    insertafter: '^%sudo'
    line: "{{ ansible_user }} ALL=NOPASSWD: /usr/bin/rsync"
  changed_when: false

- name: Delete default page
  file:
    path: /var/www/html/index.html
    state: absent

- name: Delete html directory
  when: apache_use_repo is defined
  file:
    path: /var/www/html
    state: absent

- name: Copy index.php
  template:
    src: index.php.j2
    dest: /var/www/html/index.php

删除 PHP 包将使其工作..

任务文件 Haproxy

---
- name: Ensure HAProxy is installed.
  package: name=haproxy state=present

- name: Ensure HAProxy is enabled 
  lineinfile:
    dest: /etc/default/haproxy
    regexp: "^ENABLED.+$"
    line: "ENABLED=1"
    state: present


- name: Get HAProxy version.
  command: haproxy -v
  register: haproxy_version_result
  changed_when: false
  check_mode: false

- name: Set HAProxy version.
  set_fact:
    haproxy_version: "{{ '1.5' if '1.5.' in haproxy_version_result.stdout else '1.4' }}"

- name: Copy HAProxy configuration in place.
  template:
    src: haproxy.cfg.j2
    dest: /etc/haproxy/haproxy.cfg
    mode: 0644
    validate: haproxy -f %s -c -q
  notify: restart haproxy

- name: Ensure HAProxy is started and enabled on boot.
  service: name=haproxy state=started enabled=yes

和默认文件

---
haproxy_socket: /var/lib/haproxy/stats
haproxy_chroot: /var/lib/haproxy
haproxy_user: haproxy
haproxy_group: haproxy

# Frontend settings.
haproxy_frontend_name: 'hafrontend'
haproxy_frontend_bind_address: '*'
haproxy_frontend_port: 80
haproxy_frontend_mode: 'http'

# Backend settings.
haproxy_backend_name: 'habackend'
haproxy_backend_mode: 'http'
haproxy_backend_balance_method: 'roundrobin'
haproxy_backend_httpchk: 'HEAD / HTTP/1.1\r\nHost:localhost'

# List of backend servers.
haproxy_backend_servers: 
 - name: app1
   address: 10.0.10.21:80
 - name: app2
   address: 10.0.10.22:80

# Extra global vars (see README for example usage).
haproxy_global_vars: []

它在没有 PhP 标记的情况下也能正常工作,但它只会重定向到第一台服务器,而不会重定向到第二台服务器。有人知道如何解决这个问题吗?

它给出的错误是503 Service Unavailable 没有服务器可用于处理此请求。

不确定是否有人遇到过这个问题,但这是我的 HAproxy.cfg.js2 文件中的错误。我重做了整个角色并且成功了。