1
0

install_php_mysql_debian.yml 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # Run this with:
  2. # ansible-playbook -i <inventory> playbooks/install_php_mysql_debian.yml -bkK --limit debian-xyz -e @vars-debian-xyz.yml
  3. ---
  4. - name: Install PHP and MySQL on Debian-based systems
  5. hosts: debian
  6. become: yes
  7. tasks:
  8. - name: Update apt cache
  9. ansible.builtin.apt:
  10. update_cache: yes
  11. # Remarque : php 8.2 pour Debian Bookworm
  12. - name: Install PHP
  13. ansible.builtin.apt:
  14. name:
  15. - php8.2
  16. - php8.2-fpm
  17. - php8.2-phar # for wp-cli
  18. - php8.2-curl # for wp-cli
  19. - php8.2-mysqli # for WordPress/MySQL
  20. - php8.2-iconv # for WordPress
  21. - php8.2-gd # for WordPress
  22. - php8.2-imagick
  23. - php8.2-intl
  24. - php8.2-mbstring
  25. - php8.2-zip
  26. - php8.2-dom
  27. state: present
  28. - name: Install curl
  29. ansible.builtin.apt:
  30. name: curl
  31. state: present
  32. - name: Install Nginx
  33. ansible.builtin.apt:
  34. name: nginx
  35. state: present
  36. - name: Check if WordPress exists in /var/www/wordpress
  37. stat:
  38. path: /var/www/wordpress
  39. register: wordpress_dir
  40. - name: Check if wp-config.php exists in /var/www/wordpress
  41. stat:
  42. path: /var/www/wordpress/wp-config.php
  43. register: wp_config
  44. # Only if wordpress_dir.stat.exists is false, download WordPress
  45. - name: Install WordPress using curl
  46. command: curl -o /tmp/wordpress.tar.gz https://wordpress.org/latest.tar.gz
  47. args:
  48. creates: /tmp/wordpress.tar.gz
  49. when: wordpress_dir.stat.exists == False
  50. # Unzip WordPress only if /var/www/wordpress doesn't exist
  51. - name: Installer WordPress
  52. ansible.builtin.unarchive:
  53. src: /tmp/wordpress.tar.gz
  54. dest: /var/www
  55. remote_src: yes
  56. owner: www-data
  57. group: www-data
  58. when: wordpress_dir.stat.exists == False
  59. - name: Install MariaDB Server & Client
  60. ansible.builtin.apt:
  61. name:
  62. - mariadb-server
  63. - mariadb-client
  64. state: present
  65. - name: Start Nginx Service
  66. ansible.builtin.service:
  67. name: nginx
  68. state: started
  69. enabled: yes # Ensure the MySQL service starts on boot
  70. - name: Start MariaDB Service
  71. ansible.builtin.service:
  72. name: mariadb
  73. state: started
  74. enabled: yes # Ensure the MariaDB service starts on boot
  75. - name: Installer le client MySQL pour Python
  76. ansible.builtin.apt:
  77. name: python3-mysqldb
  78. state: present
  79. update_cache: yes
  80. - name: create MySQL database
  81. mysql_db:
  82. check_implicit_admin: yes
  83. login_user: "{{ db_username }}"
  84. login_password: "{{ db_password }}"
  85. name: "{{ db_name }}"
  86. encoding: utf8mb4
  87. collation: utf8mb4_unicode_ci
  88. state: present
  89. - name: create MySQL user
  90. mysql_user:
  91. name: "{{ db_username }}"
  92. password: "{{ db_password }}"
  93. priv: "{{ db_name }}.*:ALL,GRANT"
  94. state: present
  95. - name: Downlownload wp salt config
  96. command: curl https://api.wordpress.org/secret-key/1.1/salt/
  97. register: wp_salt
  98. when: wp_config.stat.exists == False
  99. # Register wp-salt content as variable
  100. # - name: Read wp-salt content
  101. # ansible.builtin.slurp:
  102. # src: /tmp/wp-salt
  103. # register: wp_salt
  104. # Use variables from vars above and from wp_salt
  105. - name: Create wp-config.php
  106. ansible.builtin.template:
  107. src: templates/wp-config-sample.php.j2
  108. dest: /var/www/wordpress/wp-config.php
  109. owner: www-data
  110. group: www-data
  111. mode: '0440'
  112. when: wp_config.stat.exists == False
  113. # Overwrite default vhost config with our own
  114. - name: Write WordPress vhost config
  115. ansible.builtin.template:
  116. src: templates/nginx-wordpress.conf.j2
  117. dest: /etc/nginx/sites-available/nginx-wordpress.conf
  118. owner: root
  119. group: root
  120. mode: 0644
  121. - name: Create symbolic link to enable the vhost
  122. ansible.builtin.file:
  123. src: /etc/nginx/sites-available/nginx-wordpress.conf
  124. dest: /etc/nginx/sites-enabled/nginx-wordpress.conf
  125. state: link
  126. # remove `default` symlink from sites-enabled
  127. - name: Remove default symlink from sites-enabled
  128. ansible.builtin.file:
  129. path: /etc/nginx/sites-enabled/default
  130. state: absent
  131. - name: Restart PHP-FPM
  132. ansible.builtin.service:
  133. name: php8.2-fpm
  134. state: restarted
  135. enabled: yes # Ensure the PHP-FPM service starts on boot
  136. - name: Restart Nginx
  137. ansible.builtin.service:
  138. name: nginx
  139. state: restarted
  140. enabled: yes # Ensure the PHP-FPM service starts on boot
  141. # Check if wp-cli exists
  142. - name: Check if wp-cli exists
  143. ansible.builtin.stat:
  144. path: /usr/local/bin/wp
  145. register: wp_cli
  146. # Download WordPress CLI
  147. - name: Download WordPress CLI
  148. command: curl -o /usr/local/bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
  149. args:
  150. creates: /usr/local/bin/wp
  151. when: wp_cli.stat.exists == False
  152. # Make WordPress CLI executable
  153. - name: Make WordPress CLI executable
  154. command: chmod +x /usr/local/bin/wp
  155. # Check if WordPress is installed
  156. - name: Check if WordPress is installed
  157. command: sudo -u www-data wp core is-installed --path=/var/www/wordpress
  158. register: wp_installed
  159. ignore_errors: yes
  160. # WP core install (cwd: /var/www/wordpress)
  161. - name: Install WordPress
  162. command: sudo -u www-data wp core install --url={{ site_url }} --title=My\ Blog --admin_user=admin --admin_password=admin --admin_email=benoithubert@gmail.com
  163. # WP-CLI complains if we run it as root, so we become a regular user
  164. become: no
  165. # Shou
  166. args:
  167. chdir: /var/www/wordpress
  168. when: wp_installed.rc != 0