ansible遠程編譯部署httpd和php

ansible遠程編譯部署httpd和php

說明:

參考:https://blog.51cto.com/14012942/2444580php

應該是能跑得起來的,不過還差的很遠…html

模塊可拆分紅多個文件node

不少都是shell模塊實現的,yum裝包,修改配置文件等mysql

實現第一步:能用web

遠程執行shell腳本應使用script模塊sql

src文件在遠程主機,應使用copy模塊的remote_src參數shell

httpd.conf應該配個域名apache

相關文件壓縮包:下載:https://www.lanzous.com/i6xbouj 密碼:6vubsocket

修改後:下載:https://www.lanzous.com/i72hdcb 密碼:gnbeide

修改後的main.yml文件:

roles/httpd/tasks/main.yml


- name: create group
  group: 
    name: apache
    gid: 48
    system: yes
    state: present
- name: create user
  user: 
    name: apache
    uid: 48
    group: apache
    comment: "Apache"
    state: present
    createhome: no
    system: yes
    shell: /sbin/nologin
- name: yum install dependency package
  yum:
    name:
      - gcc
      - openssl-devel
      - pcre-devel
      - libnghttp2-devel
      - ncurses-devel
      - lbzip2
      - bzip2 
      - expat-devel
      - libtool
    state: present
- name: unarchive httpd.tar.gz to remote server
  unarchive: 
    src: roles/httpd/files/httpd-2.4.41.tar.gz
    dest: "{{ SRC }}"
- name: unarchive apr.util.tar.gz to remote server
  unarchive: 
    src: roles/httpd/files/apr-util-1.6.1.tar.gz
    dest: "{{ SRC }}/httpd-2.4.41/srclib/"
- name: unarchive apr.tar.gz to remote server
  unarchive: 
    src: roles/httpd/files/apr-1.7.0.tar.gz
    dest: "{{ SRC }}/httpd-2.4.41/srclib/"
- name: rename  
  shell: |
      mv {{ SRC }}/httpd-2.4.41/srclib/apr-1.7.0 {{ SRC }}/httpd-2.4.41/srclib/apr
      mv {{ SRC }}/httpd-2.4.41/srclib/apr-util-1.6.1 {{ SRC }}/httpd-2.4.41/srclib/apr-util
- name: configure
  shell: ./configure --prefix={{ PREFIX }} --sysconfdir={{ SYSCONFDIR }} --enable-http2 --enable-ssl --enable-so  --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
  args:
    chdir: "{{ SRC }}/httpd-2.4.41/"
- name: Build the default target
  make: 
    target: install
    chdir: "{{ SRC }}/httpd-2.4.41/"
- name: PATH
  shell: echo "PATH={{ PREFIX }}/bin:$PATH" >> /etc/profile.d/http.sh
- name: copy service file
  template: 
    src: roles/httpd/templates/httpd.service.j2
    dest: /usr/lib/systemd/system/httpd.service
- name: httpd conf
  shell: |
      sed  '/^Group/ s/daemon/apache/' {{ SYSCONFDIR }}/httpd.conf  -i
      sed  '/^User/ s/daemon/apache/' {{ SYSCONFDIR }}/httpd.conf  -i
      sed '$a LoadModule proxy_module modules/mod_proxy.so\nLoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so\nLoadModule proxy_fdpass_module modules/mod_proxy_fdpass.so' {{ SYSCONFDIR }}/httpd.conf  -i
- name: systemreload
  systemd: 
    name: httpd
    state: started
    daemon_reload: yes
    enabled: yes

roles/php/tasks/main.yml

- name: create group
  group: 
    name: apache
    gid: 48
    system: yes
    state: present
- name: create user
  user: 
    name: apache
    uid: 48
    group: apache
    comment: "Apache"
    state: present
    createhome: yes
    system: yes
    shell: /sbin/noshell
- name: yum install
  yum: 
    name:
      - gcc
      - openssl-devel
      - pcre-devel
      - libnghttp2-devel
      - ncurses-devel
      - lbzip2
      - bzip2
      - expat-devel
      - libxml2-devel
      - libxml2
      - libtool
- name: copy php
  unarchive: 
    src: roles/php/files/php-7.3.10.tar.gz 
    dest: "{{ SRC }}"
- name: compile
  shell: |
    ./configure --prefix=/usr/local/php --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-config-file-path=/usr/loca/php/etc --with-config-file-scan-dir=/usr/local/php/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo
  args: 
    chdir: "{{ SRC }}/php-7.3.10/"
- name: make install
  make: 
    target: install
    chdir: "{{ SRC }}/php-7.3.10/"
    params:
      NUM_THREADS: 4
- name: copy php-fpm.conf
  copy: 
    src: roles/php/files/php-fpm.conf 
    dest: /usr/local/php/etc/php-fpm.conf
- name: copy www.conf
  copy: 
    src: roles/php/files/www.conf 
    dest: /usr/local/php/etc/php-fpm.d/www.conf
- name: copy init file
  copy: 
    src: roles/php/files/php-fpm 
    dest: /etc/init.d/php-fpm 
    mode: 0755
- name: system reload
  systemd: 
    name: php-fpm 
    state: started 
    daemon_reload: yes 
    enabled: yes





目錄結構:

image.png

入口文件

[root@node1 test_playbook]# cat deploy.yml - hosts: web
  gather_facts: true
  remote_user: root
  roles:
    - httpd
    - php

清單文件

[root@node1 test_playbook]# cat  inventory/testenv [web]
192.168.38.145

[web:vars]
PREFIX=/usr/local/httpd2.4.41
SYSCONFDIR=/etc/httpd
SRC=/usr/local/src
SYSCONFDIR=/etc/httpd

httpd主任務文件

[root@node1 test_playbook]# cat roles/httpd/tasks/main.yml 
- name: create group
  group: name=apache gid=48 system=yes state=present
- name: create user
  user: name=apache uid=48 group=apache comment="Apache" state=present createhome=no system=yes shell=/sbin/noshell
- name: yum install
  shell: yum install gcc openssl-devel pcre-devel libnghttp2-devel ncurses-devel  lbzip2  bzip2 expat-devel autoconf libtool -y
- name: copy httpd
  unarchive: src=roles/httpd/files/httpd-2.4.41.tar.gz  dest={{ SRC }}
- name: copy apr-utils
  unarchive: src=roles/httpd/files/apr-util-1.6.1.tar.gz dest={{ SRC }}/httpd-2.4.41/srclib/
- name: cpoy apr
  unarchive: src=roles/httpd/files/apr-1.7.0.tar.gz dest={{ SRC }}/httpd-2.4.41/srclib/
- name: rename  
  shell: |
      mv {{ SRC }}/httpd-2.4.41/srclib/apr-1.7.0 {{ SRC }}/httpd-2.4.41/srclib/apr
      mv {{ SRC }}/httpd-2.4.41/srclib/apr-util-1.6.1 {{ SRC }}/httpd-2.4.41/srclib/apr-util
- name: compile
  shell: |
      cd {{ SRC }}/httpd-2.4.41/     
      ./configure --prefix={{ PREFIX }} --sysconfdir={{ SYSCONFDIR }} --enable-http2 --enable-ssl --enable-so  --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
      make -j 4 && make install
#- name:  compile
#  shell: chdir=/usr/local/httpd-2.4.41/ make -j 4
#- name: install 
#  shell: make install 
#  PATH變量看狀況處理下
- name: PATH
  shell: echo "PATH={{ PREFIX }}/bin:$PATH" >> /etc/profile.d/http.sh
- name: copy service file
  template: 'src=roles/httpd/templates/httpd.service.j2 dest=/usr/lib/systemd/system/httpd.service'
- name: httpd conf
  shell: |
      sed  '/^Group/ s/daemon/apache/' {{ SYSCONFDIR }}/httpd.conf  -i
      sed  '/^User/ s/daemon/apache/' {{ SYSCONFDIR }}/httpd.conf  -i
      sed '$a LoadModule proxy_module modules/mod_proxy.so\nLoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so\nLoadModule proxy_fdpass_module modules/mod_proxy_fdpass.so' {{ SYSCONFDIR }}/httpd.conf  -i
- name: systemreload
  systemd: daemon_reload=yes name=httpd

httpd的service文件

[root@node1 test_playbook]# cat roles/httpd/files/httpd.service [Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=simple
EnvironmentFile=${SYSCONFDIR}/httpd.conf
ExecStart=${PREFIX}/bin/apachectl -k start  -DFOREGROUND
ExecReload=${PREFIX}/bin/apachectl  -k graceful
ExecStop=/usr/bin/kill -WINCH ${MAINPID}PrivateTmp=true[Install]
WantedBy=multi-user.target

php主任務文件

[root@node1 test_playbook]# cat roles/php/tasks/main.yml - name: create group
  group: name=apache gid=48 system=yes state=present
- name: create user
  user: name=apache uid=48 group=apache comment="Apache" state=present createhome=no system=yes shell=/sbin/noshell
- name: yum install
  shell: yum install gcc openssl-devel pcre-devel libnghttp2-devel ncurses-devel  lbzip2  bzip2 expat-devel libxml2-devel libxml2  autoconf libtool -y
- name: copy php
  unarchive: src=roles/php/files/php-7.3.10.tar.gz dest={{ SRC }}
- name: compile
  shell: |      cd {{ SRC }}/php-7.3.10/
      ./configure --prefix=/usr/local/php --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-config-file-path=/usr/loca/php/etc --with-config-file-scan-dir=/usr/local/php/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo
      make -j 4 && make install 
- name: copy php-fpm.conf
  copy: 'src=roles/php/files/php-fpm.conf dest=/usr/local/php/etc/php-fpm.conf'- name: copy www.conf
  copy: 'src=roles/php/files/www.conf dest=/usr/local/php/etc/php-fpm.d/www.conf'- name: copy init file
  copy: 'src=roles/php/files/php-fpm dest=/etc/init.d/php-fpm mode=0755'- name: system reload
  systemd: daemon_reload=yes name=php-fpm

php啓動文件

#php程序生成的
[root@node1 test_playbook]# ll roles/php/files/php-fpm
-rwxr-xr-x 1 root root 2401 Oct 23 06:01 roles/php/files/php-fpm

php配置文件

改的東西很少:進程用戶,監聽套接字,php進程數量沒改

安裝完成,沒太大問題

中途報錯單步排錯:

# php和httpd應該加入開機啓動# httpd能夠選擇安裝目錄
# 編譯php不建議改安裝目錄了,否則後面還要改腳本
# 例如
ansible web  -i ../../../inventory/testenv -m template -a 'src=../../httpd/templates/httpd.service.j2 dest=/usr/lib/systemd/system/httpd.service'
[root@node1 test_playbook]# ansible web -i inventory/testenv -m unarchive -a 'src=roles/php/files/php-7.3.10.tar.gz dest=/usr/local/src'
[root@node1 test_playbook]# ansible web -i inventory/testenv -m systemd -a 'name=httpd state=started daemon_reload=yes enabled=yes'
[root@node1 test_playbook]# ansible web -i inventory/testenv -m systemd -a 'name=php-fpm state=started daemon_reload=yes enabled=yes'
#使用ansible直接操做主機IP時主機應存在於/etc/ansible/hosts,#開啓密鑰驗證就用-k了#playbook腳本中管道|能夠多行執行shell命令

安裝完成後測試

[root@node1 ~]# cat >> /etc/httpd/httpd.conf <<EOF
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ unix:/var/run/php-fpm.sock|fcgi://localhost/var/www/html
EOF
[root@node1 ~]# sed  's/DirectoryIndex index.html/DirectoryIndex index.php index.html/' /etc/httpd/httpd.conf -i
[root@node1 ~]# sed -r 's@/usr/local/httpd2.4.41/htdocs@/var/www/html@' /etc/httpd/httpd.conf -i
[root@node1 ~]# mkdir /var/www/html -p
[root@node1 ~]# cat > /var/www/html/index.php <<EOF
<?
    phpinfo();
?>
EOF
[root@node1 ~]# httpd -t[root@node1 ~]# systemctl restart httpd

相關文章
相關標籤/搜索