install_php_mysql_debian.yml 5.6 KB

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