nginx+proxy+nginx+php+redis

[root@61 ansible_admin]# tree

├── ansible.cfg ├── conf │ ├── memcached.j2 │ ├── nginx.conf.j2 │ ├── phpadmin.com.conf.j2 │ ├── php.ini.j2 │ ├── php_www.conf.j2 │ ├── proxy_phpadmin.com.conf.j2 │ └── redis.conf.j2 ├── hosts ├── host_vars ├── lb.yml ├── nginx.conf.j2 ├── nginx.yml ├── phpMyAdmin-5.0.2-all-languages.zip ├── redis.yml └── web.yml

vim redis.yml

  1. - hosts: db
  2. tasks:
  3. - name: Installed Redis Server
  4. yum:
  5. name: redis
  6. state: present
  7. - name: Configure Redis Server
  8. copy:
  9. src: ./conf/redis.conf.j2
  10. dest: /etc/redis.conf
  11. notify: Restart Redis Server
  12. - name: Started Redis Server
  13. systemd:
  14. name: redis
  15. state: started
  16. enabled: yes
  17. handlers:
  18. - name: Restart Redis Server
  19. systemd:
  20. name: redis
  21. state: restarted

vim yum.yml

  1. - name: Installed PHP7.1
  2. yum:
  3. name: "{{ packages }}"
  4. state: present
  5. vars:
  6. packages:
  7. - php71w
  8. - php71w-cli
  9. - php71w-common
  10. - php71w-devel
  11. - php71w-embedded
  12. - php71w-gd
  13. - php71w-mcrypt
  14. - php71w-mbstring
  15. - php71w-pdo
  16. - php71w-xml
  17. - php71w-fpm
  18. - php71w-mysqlnd
  19. - php71w-opcache
  20. - php71w-pecl-memcached
  21. - php71w-pecl-redis
  22. - php71w-pecl-mongodb

vim web.yml

  1. - hosts: webservers
  2. tasks:
  3. #1.安装Nginx php
  4. - name: Installed Nginx PHP7.1
  5. include: ./yum.yml
  6. #2.复制nginx,php配置文件
  7. - name: Configure Nginx Php conf
  8. copy:
  9. src: "{{ item.src }}"
  10. dest: "{{ item.dest }}"
  11. loop:
  12. - { src: ./conf/nginx.conf.j2, dest: /etc/nginx/nginx.conf }
  13. - { src: ./conf/php.ini.j2, dest: /etc/php.ini }
  14. - { src: ./conf/php_www.conf.j2, dest: /etc/php-fpm.d/www.conf }
  15. - { src: ./conf/phpadmin.com.conf.j2, dest: /etc/nginx/conf.d/phpadmin.com.conf }
  16. notify:
  17. - Restart Nginx Server
  18. - Restart PHP Server
  19. - name: Create Group WWW
  20. group:
  21. name: www
  22. gid: 666
  23. - name: Create User WWW
  24. user:
  25. name: www
  26. uid: 666
  27. group: www
  28. shell: /sbin/nologin
  29. create_home: no
  30. #3.根据配置文件初始化
  31. - name: Init Nginx Virtual Host
  32. file:
  33. path: /ansible_code
  34. state: directory
  35. owner: www
  36. group: www
  37. recurse: yes
  38. #4.拷贝代码
  39. - name: Copy Code
  40. unarchive:
  41. src: ./phpMyAdmin-5.0.2-all-languages.zip
  42. dest: /ansible_code/
  43. owner: www
  44. group: www
  45. #5.启动服务
  46. - name: Started Nginx Server
  47. systemd:
  48. name: "{{ item }}"
  49. state: started
  50. enabled: yes
  51. loop:
  52. - nginx
  53. - php-fpm
  54. #6.只要配置变更则触发重启
  55. handlers:
  56. - name: Restart Nginx PHP Server
  57. systemd:
  58. name: "{{ item }}"
  59. state: restarted
  60. loop:
  61. - nginx
  62. - php-fpm
  63. m lb.yml
  64. - hosts: lb
  65. tasks:
  66. #1.安装
  67. - name: Installed Nginx Server
  68. yum:
  69. name: nginx
  70. state: present
  71. #2.配置
  72. - name: Configure Nginx.conf
  73. copy:
  74. src: ./conf/nginx.conf.j2
  75. dest: /etc/nginx/nginx.conf
  76. notify: Restart Nginx Server
  77. - name: Init Nginx Group
  78. group:
  79. name: www
  80. gid: 666
  81. - name: Init Nginx User
  82. user:
  83. name: www
  84. uid: 666
  85. group: www
  86. shell: /sbin/nologin
  87. create_home: no
  88. #3.负载均衡的虚拟主机
  89. - name: Configure Nginx Load file
  90. copy:
  91. src: ./conf/proxy_phpadmin.com.conf.j2
  92. dest: /etc/nginx/conf.d/proxy_phpadmin.com.conf
  93. notify: Restart Nginx Server
  94. #4.启动nginx
  95. - name: Systemd Nginx Server
  96. systemd:
  97. name: nginx
  98. state: started
  99. enabled: yes
  100. handlers:
  101. - name: Restart Nginx Server
  102. systemd:
  103. name: nginx
  104. state: restarted