1
0

install_php_mysql_debian.yml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. ---
  2. - name: Install PHP and MySQL on Debian-based systems
  3. hosts: debian
  4. become: yes # Use privilege escalation to become the root user
  5. vars:
  6. db_username: wordpress
  7. db_password: wordpress
  8. db_name: wordpress
  9. tasks:
  10. - name: Update apt cache
  11. ansible.builtin.apt:
  12. update_cache: yes
  13. # These two will be needed to install mysqlclient
  14. - name: Install gcc, mariadb-dev, pkgconfig and pip3
  15. ansible.builtin.apt:
  16. name:
  17. - gcc # needed to compile
  18. - python3-dev # needed to compile
  19. - mariadb-dev
  20. - python3-pip
  21. - pkgconfig
  22. state: present
  23. - name: Install PHP
  24. ansible.builtin.apt:
  25. name:
  26. - php
  27. - php-fpm
  28. - php-iconv # for WordPress
  29. - php-mysqli # for WordPress/MySQL
  30. - php-phar # for wp-cli
  31. - php-json # for wp-cli
  32. - php-openssl # for wp-cli
  33. - php-curl # for wp-cli
  34. state: present
  35. - name: Install curl
  36. ansible.builtin.apt:
  37. name: curl
  38. state: present
  39. - name: Check if WordPress exists in /var/www/wordpress
  40. stat:
  41. path: /var/www/wordpress
  42. register: wordpress_dir
  43. - name: Check if wp-config.php exists in /var/www/wordpress
  44. stat:
  45. path: /var/www/wordpress/wp-config.php
  46. register: wp_config
  47. # Only if wordpress_dir.stat.exists is false, download WordPress
  48. - name: Install WordPress using curl
  49. command: curl -o /tmp/wordpress.tar.gz https://wordpress.org/latest.tar.gz
  50. args:
  51. creates: /tmp/wordpress.tar.gz
  52. when: wordpress_dir.stat.exists == False
  53. # Unzip WordPress only if /var/www/wordpress doesn't exist
  54. - name: Unzip WordPress
  55. # ansible.builtin.unarchive:
  56. command: tar -xzf /tmp/wordpress.tar.gz -C /var/www
  57. when: wordpress_dir.stat.exists == False
  58. - name: Install Nginx
  59. ansible.builtin.apk:
  60. name: nginx
  61. state: present
  62. - name: Install MariaDB Server
  63. ansible.builtin.apk:
  64. name: mariadb
  65. state: present
  66. - name: Install MariaDB Client Tools
  67. ansible.builtin.apk:
  68. name: mariadb-client
  69. state: present
  70. - name: Start Nginx Service
  71. ansible.builtin.service:
  72. name: nginx
  73. state: started
  74. enabled: yes # Ensure the MySQL service starts on boot
  75. # Store mariadb status in a variable
  76. # It'll be useful to conditionally run `/etc/init.d/mariadb setup`
  77. - name: Check MariaDB status
  78. command: rc-service mariadb status
  79. register: mariadb_status
  80. ignore_errors: yes
  81. # # Echo 'MARIADB STARTED' if mariadb is running
  82. # - name: Debug MariaDB status
  83. # ansible.builtin.debug:
  84. # msg: "MARIADB STARTED"
  85. # when: mariadb_status.stdout.find('started') != -1
  86. # # Echo 'MARIADB STOPPED' if mariadb is NOT running
  87. # - name: Debug MariaDB status
  88. # ansible.builtin.debug:
  89. # msg: "MARIADB STOPPED"
  90. # when: mariadb_status.stdout.find('started') == -1
  91. # Problem I had: it failed here after 1st run
  92. - name: MariaDB setup
  93. command: /etc/init.d/mariadb setup
  94. # run only if service is NOT running, that is, the output from
  95. # `rc-service mariadb status` doesn't contain `started`
  96. when: mariadb_status.stdout.find('started') == -1
  97. - name: Start MariaDB Service
  98. ansible.builtin.service:
  99. name: mariadb
  100. state: started
  101. enabled: yes # Ensure the MariaDB service starts on boot
  102. - name: Install mysqlclient
  103. ansible.builtin.pip:
  104. name: mysqlclient
  105. state: present
  106. - name: create MySQL database
  107. mysql_db:
  108. check_implicit_admin: yes
  109. login_user: "{{ db_username }}"
  110. login_password: "{{ db_password }}"
  111. name: "{{ db_name }}"
  112. encoding: utf8mb4
  113. collation: utf8mb4_unicode_ci
  114. state: present
  115. - name: create MySQL user
  116. mysql_user:
  117. name: "{{ db_username }}"
  118. password: "{{ db_password }}"
  119. priv: "{{ db_name }}.*:ALL,GRANT"
  120. state: present
  121. - name: Downlownload wp salt config
  122. command: curl https://api.wordpress.org/secret-key/1.1/salt/
  123. register: wp_salt
  124. when: wp_config.stat.exists == False
  125. # Register wp-salt content as variable
  126. # - name: Read wp-salt content
  127. # ansible.builtin.slurp:
  128. # src: /tmp/wp-salt
  129. # register: wp_salt
  130. # Use variables from vars above and from wp_salt
  131. - name: Create wp-config.php
  132. ansible.builtin.template:
  133. src: templates/wp-config-sample.php.j2
  134. dest: /var/www/wordpress/wp-config.php
  135. owner: root
  136. group: root
  137. mode: 0644
  138. when: wp_config.stat.exists == False
  139. # Check that default.conf.bak exists
  140. - name: Check if default.conf.bak exists
  141. ansible.builtin.stat:
  142. path: /etc/nginx/http.d/default.conf.bak
  143. register: default_conf_bak
  144. # Backup nginx default vhost config if not already backed up
  145. - name: Backup default Nginx vhost config
  146. command: mv /etc/nginx/http.d/default.conf /etc/nginx/http.d/default.conf.bak
  147. when: default_conf_bak.stat.exists == False
  148. # Overwrite default vhost config with our own
  149. - name: Write new vhost config
  150. ansible.builtin.template:
  151. src: templates/nginx-wordpress.conf.j2
  152. dest: /etc/nginx/http.d/nginx-wordpress.conf
  153. owner: root
  154. group: root
  155. mode: 0644
  156. - name: Restart PHP-FPM
  157. ansible.builtin.service:
  158. name: php-fpm81
  159. state: restarted
  160. enabled: yes # Ensure the PHP-FPM service starts on boot
  161. - name: Restart Nginx
  162. ansible.builtin.service:
  163. name: nginx
  164. state: restarted
  165. enabled: yes # Ensure the PHP-FPM service starts on boot
  166. # Check if wp-cli exists
  167. - name: Check if wp-cli exists
  168. ansible.builtin.stat:
  169. path: /usr/local/bin/wp
  170. register: wp_cli
  171. # Download WordPress CLI
  172. - name: Download WordPress CLI
  173. command: curl -o /usr/local/bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
  174. args:
  175. creates: /usr/local/bin/wp
  176. when: wp_cli.stat.exists == False
  177. # Make WordPress CLI executable
  178. - name: Make WordPress CLI executable
  179. command: chmod +x /usr/local/bin/wp
  180. # Check if WordPress is installed
  181. - name: Check if WordPress is installed
  182. command: wp core is-installed --path=/var/www/wordpress
  183. register: wp_installed
  184. ignore_errors: yes
  185. # WP core install (cwd: /var/www/wordpress)
  186. - name: Install WordPress
  187. command: wp core install --url=192.168.1.85 --title=My\ Blog --admin_user=admin --admin_password=admin --admin_email=benoithubert@gmail.com
  188. args:
  189. chdir: /var/www/wordpress
  190. when: wp_installed.rc != 0