服务文件模板#httpd.service.ubuntu.j2
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
Environment=APACHE_STARTED_BY_SYSTEMD=true
ExecStart={{INSTALL_DIR}}/apachectl start
ExecStop={{INSTALL_DIR}}/apachectl stop
ExecReload={{INSTALL_DIR}}/apachectl graceful
PrivateTmp=true
Restart=on-abort
[Install]
WantedBy=multi-user.target
服务文件模板#httpd.service.centos.j2
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
ExecStart={{ INSTALL_DIR }}/bin/apachectl start
ExecReload={{ INSTALL_DIR }}/bin/apachectl graceful
ExecStop={{ INSTALL_DIR }}/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
install_httpd .yml
---
- hosts: websrvs
vars:
HTTPD_VERSION: 2.4.46
APR_VERSION: 1.7.0
APR_UTIL_VERSION: 1.6.1
HTTPD_URL: https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-{{HTTPD_VERSION}}.tar.gz
APR_URL: https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-{{APR_VERSION}}.tar.gz
APR_UTIL_URL: https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-{{APR_UTIL_VERSION}}.tar.gz
SRC_DIR: /usr/local/src/
INSTALL_DIR: /apps/httpd
tasks:
- name: install pkgs
yum:
name: "{{ item }}"
state: latest
loop:
- gcc
- make
- pcre-devel
- openssl-devel
- expat-devel
when: ansible_os_family == "RedHat"
- name: install pkgs
apt:
pkg:
- libpcre3
- libpcre3-dev
- openssl
- libssl-dev
- gcc
- make
- libexpat1-dev
state: latest
when: ansible_os_family == "Debian"
- name: download httpd pkg
unarchive:
src: "{{ item.name }}"
dest: "{{ item.dir }}"
owner: root
remote_src: yes
loop:
- { name: "{{HTTPD_URL}}",dir: "{{SRC_DIR}}" }
- { name: "{{APR_URL}}",dir: "{{SRC_DIR}}" }
- { name: "{{APR_UTIL_URL}}",dir: "{{SRC_DIR}}" }
- name: mv src togather
shell: chdir={{SRC_DIR}} mv apr-{{APR_VERSION}} httpd-{{HTTPD_VERSION}}/srclib/apr
- name: mv src togather
shell: chdir={{SRC_DIR}} mv apr-util-{{APR_UTIL_VERSION}} httpd-{{HTTPD_VERSION}}/srclib/apr-util
- name: build httpd ./config
shell: chdir={{SRC_DIR}}/httpd-{{HTTPD_VERSION}} ./configure --prefix={{INSTALL_DIR}} --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
- name: build httpd make
shell: chdir={{SRC_DIR}}/httpd-{{HTTPD_VERSION}} make -j {{ansible_processor_vcpus}}
- name: buid httpd make install
shell: chdir={{SRC_DIR}}/httpd-{{HTTPD_VERSION}} make install
- name: create group
group:
name: apache
gid: 80
state: present
- name: create user
user:
name: apache
uid: 80
group: apache
shell: /sbin/nologin
system: yes
create_home: no
home: "{{INSTALL_DIR}}/conf/httpd"
state: present
- name: set httpd user
replace:
path: "{{INSTALL_DIR}}/conf/httpd.conf"
regexp: "{{ item.name }}"
replace: "{{ item.replace }}"
with_items:
- {name: "^User",replace: "'User apache'"}
- {name: "^Group",replace: "'Group apache'"}
- name: set variable PATH
shell: echo PATH={{INSTALL_DIR}}/bin:$PATH >> /etc/profile.d/httpd.sh
- name: prepare service file on {{ansible_os_family}}
template:
src: httpd.service.centos.j2
dest: /usr/lib/systemd/system/httpd.service
when: ansible_os_family == "RedHat"
- name: prepare service file on {{ansible_os_family}}
template:
src: httpd.service.ubuntu.j2
dest: /lib/systemd/system/apache2.service
when: ansible_os_family == "Debian"
- name: start service on {{ansible_os_family}}
service:
name: httpd
state: started
enabled: yes
when: ansible_os_family == "RedHat"
- name: start service on {{ansible_os_family}}
service:
name: apache2
state: started
enabled: yes
when: ansible_os_family == "Debian"