소스 검색

Install WP sur Debian 12 OK

Benoît Hubert 1 년 전
부모
커밋
fab603be0c

+ 23 - 4
2024-php-mysql/README.md

@@ -1,12 +1,16 @@
-## Erreurs rencontrées
+# Install WP sur Alpine, Debian
+
+## Alpine
+
+### Erreurs rencontrées
 
 Run playbook:
 
 ```
-ansible-playbook -i inventory/hosts playbooks/install_php_mysql.yml -K
+ansible-playbook -i inventory/hosts playbooks/install_php_mysql_alpine.yml -K
 ```
 
-### MariaDB déjà démarré
+#### MariaDB déjà démarré
 
 ```
 TASK [MariaDB setup] **********************************************************************************************************************
@@ -16,7 +20,7 @@ fatal: [192.168.1.85]: FAILED! => {"changed": true, "cmd": ["/etc/init.d/mariadb
 Résolu en vérifiant si MariaDB est déjà up & running.
 
 
-### Module manquant pour mysql_db
+#### Module manquant pour mysql_db
 
 ```
 ASK [create MySQL database] **************************************************************************************************************
@@ -24,3 +28,18 @@ fatal: [192.168.1.85]: FAILED! => {"changed": false, "msg": "A MySQL module is r
 ```
 
 Pas mal de modules à installer
+
+## Debian
+
+```
+ansible-playbook -i ../ansible-initial-debian-setup/inventory.ini playbooks/install_php_mysql_debian.yml -K --limit debian-111
+```
+
+Changements par rapport à Alpine
+
+- **ATTENTION** Nginx doit être installé **avant** d'unzip WordPress vers `/var/www` (BUG dans mon playbook Alpine).
+- `pkgconfig` et `mariadb-dev` n'existent pas sous Debian.
+- `php8.2-json` et `php8.2-openssl` non plus
+- `mariadb` non plus => `mariadb-server` et `mariadb-client`
+- install de `mysqlclient` se fait via apt (`python3-mysqldb`)
+- du coup plus besoin d'installer `gcc`, `python3-dev`, `python3-pip`

+ 2 - 2
2024-php-mysql/playbooks/install_php_mysql_alpine.yml

@@ -1,5 +1,5 @@
 ---
-- name: Install PHP and MySQL on Debian-based systems
+- name: Install PHP and MySQL on Alpine-based systems
   hosts: all
   become: yes # Use privilege escalation to become the root user
   vars:
@@ -7,7 +7,7 @@
     db_password: wordpress
     db_name: wordpress
   tasks:
-    - name: Update apt cache
+    - name: Update apk cache
       ansible.builtin.apk:
         update_cache: yes
 

+ 74 - 63
2024-php-mysql/playbooks/install_php_mysql_debian.yml

@@ -1,38 +1,41 @@
 ---
 - name: Install PHP and MySQL on Debian-based systems
   hosts: debian
-  become: yes # Use privilege escalation to become the root user
+  become: yes
   vars:
     db_username: wordpress
     db_password: wordpress
     db_name: wordpress
+    site_url: debian-111
   tasks:
     - name: Update apt cache
       ansible.builtin.apt:
         update_cache: yes
 
-    # These two will be needed to install mysqlclient
-    - name: Install gcc, mariadb-dev, pkgconfig and pip3
-      ansible.builtin.apt:
-        name:
-          - gcc # needed to compile
-          - python3-dev # needed to compile
-          - mariadb-dev
-          - python3-pip
-          - pkgconfig
-        state: present
-
+    # Utilisés pour installer/compiler le package python mysqlclient
+    # - name: Install gcc, mariadb-dev, pkgconfig and pip3
+    #   ansible.builtin.apt:
+    #     name:
+    #       - gcc # needed to compile
+    #       - python3-dev # needed to compile
+    #       # - mariadb-dev
+    #       - python3-pip
+    #       # - pkgconfig
+    #     state: present
+
+    # Remarque : php 8.2 pour Debian Bookworm
     - name: Install PHP
       ansible.builtin.apt:
         name:
-          - php
-          - php-fpm
-          - php-iconv # for WordPress
-          - php-mysqli # for WordPress/MySQL
-          - php-phar # for wp-cli
-          - php-json # for wp-cli
-          - php-openssl # for wp-cli
-          - php-curl # for wp-cli
+          - php8.2
+          - php8.2-fpm
+          - php8.2-iconv # for WordPress
+          - php8.2-mysqli # for WordPress/MySQL
+          - php8.2-phar # for wp-cli
+          # - php8.2-json # for wp-cli
+          # pas de package php8.2-openssl pour Debian Bookworm
+          # - php8.2-openssl # for wp-cli
+          - php8.2-curl # for wp-cli
         state: present
 
     - name: Install curl
@@ -40,6 +43,11 @@
         name: curl
         state: present
 
+    - name: Install Nginx
+      ansible.builtin.apt:
+        name: nginx
+        state: present
+
     - name: Check if WordPress exists in /var/www/wordpress
       stat:
         path: /var/www/wordpress
@@ -63,31 +71,23 @@
       command: tar -xzf /tmp/wordpress.tar.gz -C /var/www
       when: wordpress_dir.stat.exists == False
 
-    - name: Install Nginx
-      ansible.builtin.apk:
-        name: nginx
-        state: present
-
-    - name: Install MariaDB Server
-      ansible.builtin.apk:
-        name: mariadb
-        state: present
-
-    - name: Install MariaDB Client Tools
-      ansible.builtin.apk:
-        name: mariadb-client
+    - name: Install MariaDB Server & Client
+      ansible.builtin.apt:
+        name:
+          - mariadb-server
+          - mariadb-client
         state: present
 
-    - name: Start Nginx Service
-      ansible.builtin.service:
-        name: nginx
-        state: started
-        enabled: yes # Ensure the MySQL service starts on boot
+    # - name: Start Nginx Service
+    #   ansible.builtin.service:
+    #     name: nginx
+    #     state: started
+    #     enabled: yes # Ensure the MySQL service starts on boot
 
     # Store mariadb status in a variable
     # It'll be useful to conditionally run `/etc/init.d/mariadb setup`
     - name: Check MariaDB status
-      command: rc-service mariadb status
+      command: service mariadb status
       register: mariadb_status
       ignore_errors: yes
 
@@ -104,11 +104,11 @@
     #   when: mariadb_status.stdout.find('started') == -1
 
     # Problem I had: it failed here after 1st run
-    - name: MariaDB setup
-      command: /etc/init.d/mariadb setup
-      # run only if service is NOT running, that is, the output from
-      # `rc-service mariadb status` doesn't contain `started`
-      when: mariadb_status.stdout.find('started') == -1
+    # - name: MariaDB setup
+    #   command: /etc/init.d/mariadb setup
+    #   # run only if service is NOT running, that is, the output from
+    #   # `rc-service mariadb status` doesn't contain `started`
+    #   when: mariadb_status.stdout.find('started') == -1
 
     - name: Start MariaDB Service
       ansible.builtin.service:
@@ -116,10 +116,17 @@
         state: started
         enabled: yes # Ensure the MariaDB service starts on boot
 
-    - name: Install mysqlclient
-      ansible.builtin.pip:
-        name: mysqlclient
+    # On ne va pas le faire avec pip mais avec apt
+    # - name: Install mysqlclient
+    #   ansible.builtin.pip:
+    #     name: mysqlclient
+    #     state: present
+
+    - name: Installer le client MySQL pour Python
+      ansible.builtin.apt:
+        name: python3-mysqldb
         state: present
+        update_cache: yes
 
     - name: create MySQL database
       mysql_db:
@@ -159,29 +166,30 @@
         mode: 0644
       when: wp_config.stat.exists == False
 
-    # Check that default.conf.bak exists
-    - name: Check if default.conf.bak exists
-      ansible.builtin.stat:
-        path: /etc/nginx/http.d/default.conf.bak
-      register: default_conf_bak
-
-    # Backup nginx default vhost config if not already backed up
-    - name: Backup default Nginx vhost config
-      command: mv /etc/nginx/http.d/default.conf /etc/nginx/http.d/default.conf.bak
-      when: default_conf_bak.stat.exists == False
-
     # Overwrite default vhost config with our own
-    - name: Write new vhost config
+    - name: Write WordPress vhost config
       ansible.builtin.template:
         src: templates/nginx-wordpress.conf.j2
-        dest: /etc/nginx/http.d/nginx-wordpress.conf
+        dest: /etc/nginx/sites-available/nginx-wordpress.conf
         owner: root
         group: root
         mode: 0644
 
+    - name: Create symbolic link to enable the vhost
+      ansible.builtin.file:
+        src: /etc/nginx/sites-available/nginx-wordpress.conf
+        dest: /etc/nginx/sites-enabled/nginx-wordpress.conf
+        state: link
+
+    # remove `default` symlink from sites-enabled
+    - name: Remove default symlink from sites-enabled
+      ansible.builtin.file:
+        path: /etc/nginx/sites-enabled/default
+        state: absent
+
     - name: Restart PHP-FPM
       ansible.builtin.service:
-        name: php-fpm81
+        name: php8.2-fpm
         state: restarted
         enabled: yes # Ensure the PHP-FPM service starts on boot
 
@@ -210,13 +218,16 @@
 
     # Check if WordPress is installed
     - name: Check if WordPress is installed
-      command: wp core is-installed --path=/var/www/wordpress
+      command: sudo -u debian wp core is-installed --path=/var/www/wordpress
       register: wp_installed
       ignore_errors: yes
 
     # WP core install (cwd: /var/www/wordpress)
     - name: Install WordPress
-      command: wp core install --url=192.168.1.85 --title=My\ Blog --admin_user=admin --admin_password=admin --admin_email=benoithubert@gmail.com
+      command: sudo -u debian wp core install --url={{ site_url }} --title=My\ Blog --admin_user=admin --admin_password=admin --admin_email=benoithubert@gmail.com
+      # WP-CLI complains if we run it as root, so we become a regular user
+      become: no
+      # Shou
       args:
         chdir: /var/www/wordpress
       when: wp_installed.rc != 0

+ 4 - 1
2024-php-mysql/playbooks/templates/nginx-wordpress.conf.j2

@@ -12,7 +12,10 @@ server {
 
     location ~ \.php$ {
         include fastcgi_params;
-        fastcgi_pass 127.0.0.1:9000;
+        # this works for alpine
+        # fastcgi_pass 127.0.0.1:9000;
+        # this works for debian
+        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     }

+ 6 - 1
ansible-initial-debian-setup/README.md

@@ -258,4 +258,9 @@ core/2.15/reference_appendices/interpreter_discovery.html for more information.
 
 ```
 hostnamectl hostname fedora-131
-```
+```
+
+## PLAYBOOKS
+
+Limiter le run du playbook à tel host/groupe : <https://docs.ansible.com/ansible/latest/inventory_guide/intro_patterns.html#patterns-and-ansible-playbook-flags>
+

+ 5 - 5
ansible-initial-debian-setup/inventory.ini

@@ -1,12 +1,12 @@
 ; inventory.ini - ansible inventory file
 [debian]
-debian-111 192.168.1.18 ansible_user=debian ansible_become=true ansible_become_method=sudo ansible_become_user=root ansible_python_interpreter=/usr/bin/python3 
-debian-112 192.168.1.85 ansible_user=debian ansible_become=true ansible_become_method=sudo ansible_become_user=root ansible_python_interpreter=/usr/bin/python3 
-debian-113 192.168.1.109 ansible_user=debian ansible_become=true ansible_become_method=sudo ansible_become_user=root ansible_python_interpreter=/usr/bin/python3 
+debian-111 ansible_host=192.168.1.18 ansible_user=debian ansible_become=true ansible_become_method=sudo ansible_become_user=root ansible_python_interpreter=/usr/bin/python3 
+debian-112 ansible_host=192.168.1.85 ansible_user=debian ansible_become=true ansible_become_method=sudo ansible_become_user=root ansible_python_interpreter=/usr/bin/python3 
+debian-113 ansible_host=192.168.1.109 ansible_user=debian ansible_become=true ansible_become_method=sudo ansible_become_user=root ansible_python_interpreter=/usr/bin/python3 
 
 [alpine]
-alpine-121 192.168.1.72 ansible_user=alpine
-alpine-122 192.168.1.48 ansible_user=alpine
+alpine-121 ansible_host=192.168.1.72 ansible_user=alpine
+alpine-122 ansible_host=192.168.1.48 ansible_user=alpine
 
 ; [fedora]
 ; fedora-131 192.168.1.x