ansible远程编译部署httpd和PHP
说明:
参考:https://blog.51cto.com/14012942/2444580
应该是能跑得起来的,不过还差的很远…
模块可拆分成多个文件
实现第一步:能用
远程执行shell脚本应使用script模块
src文件在远程主机,应使用copy模块的remote_src参数
httpd.conf应该配个域名
目录结构:
入口文件
[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和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 <<EOFProxyRequests 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 ~]# [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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。