1
0

install_php_mysql_alpine.yml 6.7 KB

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