Sunday 22 May 2022

Install WordPress on Ubuntu using Ansible Playbook


ansible-playbook wordpress.yml


cat wordpress.yml 

---

- hosts: all

  become: yes

  vars_files:

    - vars.yml


  tasks:

    - name: Install prerequisites

      apt: 

        name: aptitude 

        update_cache: yes 

        state: latest 


    - name: Install Apache MySQL PHP Packages

      apt: 

        name: "{{ item }}" 

        update_cache: yes 

        state: latest

      loop: [ 'apache2', 'mysql-server', 'python3-pymysql', 'php', 'php-mysql', 'libapache2-mod-php' ]


    - name: Install PHP Modules

      apt: 

        name: "{{ item }}"

        update_cache: yes 

        state: latest

      loop: [ 'php-curl', 'php-gd', 'php-mbstring', 'php-xml', 'php-xmlrpc', 'php-soap', 'php-intl', 'php-zip' ]


   # Configure Apache

    - name: Create document root

      file:

        path: "/var/www/{{ http_host }}"

        state: directory

        owner: "www-data"

        group: "www-data"

        mode: '0755'


    - name: Set up Apache VirtualHost

      template:

        src: "apacheconf.j2"

        dest: "/etc/apache2/sites-available/{{ http_conf }}"

      notify: Reload Apache


    - name: Enable rewrite module

      shell: /usr/sbin/a2enmod rewrite

      notify: Reload Apache


    - name: Enable new site

      shell: /usr/sbin/a2ensite {{ http_conf }}

      notify: Reload Apache


    - name: Disable default Apache site

      shell: /usr/sbin/a2dissite 000-default.conf

      notify: Restart Apache


  # Configure MySQL

      

    - name: Set the root password

      mysql_user:

        name: root

        password: "{{ mysql_root_password }}"

        login_unix_socket: /var/run/mysqld/mysqld.sock


    - name: Creates database for WordPress

      mysql_db:

        name: "{{ mysql_db }}"

        state: present

        login_user: root

        login_password: "{{ mysql_root_password }}"


    - name: Create MySQL user for WordPress

      mysql_user:

        name: "{{ mysql_user }}"

        password: "{{ mysql_password }}"

        priv: "{{ mysql_db }}.*:ALL"

        state: present

        login_user: root

        login_password: "{{ mysql_root_password }}"


 # Configure WordPress

    - name: Download and unpack latest WordPress

      unarchive:

        src: https://wordpress.org/latest.tar.gz

        dest: "/var/www/{{ http_host }}"

        remote_src: yes

        creates: "/var/www/{{ http_host }}/wordpress"


    - name: Change ownership

      file:

        path: "/var/www/{{ http_host }}"

        state: directory

        recurse: yes

        owner: www-data

        group: www-data


    - name: Change permissions for directories

      shell: "/usr/bin/find /var/www/{{ http_host }}/wordpress/ -type d -exec chmod 750 {} \\;"


    - name: Change permissions for files

      shell: "/usr/bin/find /var/www/{{ http_host }}/wordpress/ -type f -exec chmod 640 {} \\;"


    - name: Copy wp-config

      template:

        src: "wpconf.j2"

        dest: "/var/www/{{ http_host }}/wordpress/wp-config.php"

        owner: www-data

        group: www-data


  handlers:

         

    - name: Reload Apache

      service:

        name: apache2

        state: reloaded


    - name: Restart Apache

      service:

        name: apache2

        state: restarted


cat vars.yml 

---

#MySQL credentials

mysql_root_password: "mysql_root_password"

mysql_db: "mysql_db_name"

mysql_user: "mysql_wp_user"

mysql_password: "mysql_wp_password"


#HTTP info

http_host: "domain_name"

http_conf: "domain.conf"

http_port: "80"



cat wpconf.j2 

<?php

define( 'DB_NAME', '{{ mysql_db }}' );

define( 'DB_USER', '{{ mysql_user }}' );

define( 'DB_PASSWORD', '{{ mysql_password }}' );

define( 'DB_HOST', 'localhost' );

define( 'DB_CHARSET', 'utf8' );

define( 'DB_COLLATE', '' );

define('FS_METHOD', 'direct');

define( 'AUTH_KEY',         '{{ lookup('password', '/dev/null chars=ascii_letters length=64') }}' );

define( 'SECURE_AUTH_KEY',  '{{ lookup('password', '/dev/null chars=ascii_letters length=64') }}' );

define( 'LOGGED_IN_KEY',    '{{ lookup('password', '/dev/null chars=ascii_letters length=64') }}' );

define( 'NONCE_KEY',        '{{ lookup('password', '/dev/null chars=ascii_letters length=64') }}' );

define( 'AUTH_SALT',        '{{ lookup('password', '/dev/null chars=ascii_letters length=64') }}' );

define( 'SECURE_AUTH_SALT', '{{ lookup('password', '/dev/null chars=ascii_letters length=64') }}' );

define( 'LOGGED_IN_SALT',   '{{ lookup('password', '/dev/null chars=ascii_letters length=64') }}' );

define( 'NONCE_SALT',       '{{ lookup('password', '/dev/null chars=ascii_letters length=64') }}' );

$table_prefix = 'wp_';

define( 'WP_DEBUG', false );

if ( ! defined( 'ABSPATH' ) ) {

        define( 'ABSPATH', dirname( __FILE__ ) . '/' );

}

require_once( ABSPATH . 'wp-settings.php' );


No comments:

Post a Comment