Skip to content

Instantly share code, notes, and snippets.

@kraeml
Last active April 14, 2017 14:14
Wordpress with ansible
---
# https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-lemp-on-ubuntu-16-04
- hosts: wp
become: yes
vars:
WP_DOMAIN: "wordpress.rdf.loc"
WP_PATH: "/var/www/html/wordpress"
tasks:
- name: Install software
apt:
name: "{{item}}"
update_cache: yes
with_items:
- nginx
- php
- php-mysql
- php-curl
- php-gd
- mysql-client
- php-mbstring
- php-xml
- php-xmlrpc
- name: Configure /etc/hosts
lineinfile:
dest: /etc/hosts
line: '127.0.0.1 {{WP_DOMAIN}}'
- name: Nginx directories
file:
state: directory
path: "{{WP_PATH}}/{{item}}"
with_items:
- public
- logs
- name: Configure nginx
template:
src: /vagrant/wordpress.rdf.loc.j2
dest: "/etc/nginx/sites-available/{{WP_DOMAIN}}"
notify: nginx-restart
- name: Configure wordpress in nginx
file:
src: "/etc/nginx/sites-available/{{WP_DOMAIN}}"
dest: "/etc/nginx/sites-enabled/{{WP_DOMAIN}}"
state: link
notify: nginx-restart
- name: Skript ausführen
script: /vagrant/install-wp.sh
args:
creates: /home/vagrant/.wp
#- name: Rechte auf wordpress
# file:
# path: "{{WP_PATH}}"
# owner: www-data
# group: www-data
handlers:
- name: nginx-restart
service:
name: nginx
state: restarted
---
# defaults file for mysql
MY_DOMAIN: "mysql.rdf.loc"
WP_DB_NAME: "wordpress"
WP_DB_USERNAME: "wordpress"
WP_DB_PASSWORD: "wordpress"
WP_IP: 192.168.68.21
MYSQL_ROOT_PASSWORD: "root"
DB_IP: 192.168.68.22
PACKAGES:
- mysql-server
- python-mysqldb
---
# https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-lemp-on-ubuntu-16-04
- hosts: wp
become: yes
vars:
WP_DOMAIN: "wordpress.rdf.loc"
WP_PATH: "/var/www/html/wordpress"
WP_DB_NAME: "wordpress"
WP_DB_USERNAME: "wordpress"
WP_DB_PASSWORD: "wordpress"
DB_IP: 192.168.68.22
MY_DOMAIN: "mysql.rdf.loc"
WP_ADMIN_USERNAME: "admin"
WP_ADMIN_PASSWORD: "admin"
WP_ADMIN_EMAIL: "no@spam.org"
MYSQL_ROOT_PASSWORD: "root"
WP_IP: 192.168.68.21
tasks:
- pause: seconds=20
- name: Install software
apt:
name: "{{item}}"
update_cache: yes
with_items:
- nginx
- php
- php-mysql
- php-curl
- php-gd
- mysql-client
- php-mbstring
- php-xml
- php-xmlrpc
- name: Configure /etc/hosts
lineinfile:
dest: /etc/hosts
line: '127.0.0.1 {{WP_DOMAIN}}'
- name: Nginx directories
file:
state: directory
path: "{{WP_PATH}}/{{item}}"
owner: www-data
group: www-data
recurse: yes
with_items:
- public
- logs
- name: Configure nginx
template:
src: /vagrant/wordpress.rdf.loc.j2
dest: "/etc/nginx/sites-available/{{WP_DOMAIN}}"
notify: nginx-restart
- name: PHP info
copy:
src: /vagrant/php-info.php
dest: /var/www/html/php-info.php
owner: www-data
group: www-data
- name: Configure wordpress in nginx
file:
src: "/etc/nginx/sites-available/{{WP_DOMAIN}}"
dest: "/etc/nginx/sites-enabled/{{WP_DOMAIN}}"
state: link
notify: nginx-restart
- meta: flush_handlers
- name: Download wordpress
unarchive:
src: https://wordpress.org/latest.tar.gz
dest: "{{WP_PATH}}/public"
remote_src: yes
group: www-data
owner: www-data
extra_opts:
- "--strip-components=1"
- name: Config kopieren
copy:
src: "{{WP_PATH}}/public/wp-config-sample.php"
dest: "{{WP_PATH}}/public/wp-config.php"
owner: www-data
group: www-data
mode: 0660
- name: Config anpassen
replace:
dest: "{{WP_PATH}}/public/wp-config.php"
regexp: "{{item.regexp}}"
replace: "{{item.replace}}"
with_items:
- {"regexp": "database_name_here", "replace": "{{WP_DB_NAME}}"}
- {"regexp": "username_here", "replace": "{{WP_DB_USERNAME}}"}
- {"regexp": "password_here", "replace": "{{WP_DB_PASSWORD}}"}
- {"regexp": "localhost", "replace": "{{DB_IP}}"}
- name: Config anpassen
lineinfile:
dest: "{{WP_PATH}}/public/wp-config.php"
line: "{{item}}"
with_items:
- "define('FS_METHOD', 'direct');"
- name: Rechte auf wordpress
file:
path: "{{WP_PATH}}"
owner: www-data
group: www-data
recurse: yes
- name: Setup wp-user and wp-password
command: curl --silent "http://{{WP_DOMAIN}}/wp-admin/install.php?step=2" --data-urlencode "weblog_title={{WP_DOMAIN}}" --data-urlencode "user_name={{WP_ADMIN_USERNAME}}" --data-urlencode "admin_email={{WP_ADMIN_EMAIL}}" --data-urlencode "admin_password={{WP_ADMIN_PASSWORD}}" --data-urlencode "admin_password2={{WP_ADMIN_PASSWORD}}" --data-urlencode "pw_weak=1" --data-urlencode "blog_public=0"
handlers:
- name: nginx-restart
service:
name: nginx
state: restarted
#!/bin/bash
# Based on https://peteris.rocks/blog/unattended-installation-of-wordpress-on-ubuntu-server/
# Variables
source /vagrant/variables
# Install software
# We are going to install nginx, PHP and MySQL.
# By default, mysql-server is going to ask for the root password and we automate that with debconf-set-selections.
echo "Install software"
sleep 45
echo "mysql-server-5.7 mysql-server/root_password password $MYSQL_ROOT_PASSWORD" | sudo debconf-set-selections
echo "mysql-server-5.7 mysql-server/root_password_again password $MYSQL_ROOT_PASSWORD" | sudo debconf-set-selections
sudo apt-get update
sudo apt install -y mysql-server
sudo sed -i 's/bind-address[[:space:]]*=[[:space:]]*127.0.0.1/bind-address = 0.0.0.0/g' /etc/mysql/mysql.conf.d/mysqld.cnf
sudo service mysql restart
#sudo systemctl restart mysql.service
# Configure MySQL
# We are going to create a user and a database for WordPress. This database user will have full access to that database.
echo "Configure MySQL"
mysql -u root -p$MYSQL_ROOT_PASSWORD <<EOF
CREATE USER '$WP_DB_USERNAME'@'localhost' IDENTIFIED BY '$WP_DB_PASSWORD';
CREATE USER '$WP_DB_USERNAME'@'$WP_IP' IDENTIFIED BY '$WP_DB_PASSWORD';
CREATE DATABASE $WP_DB_NAME;
GRANT ALL ON $WP_DB_NAME.* TO '$WP_DB_USERNAME'@'localhost';
GRANT ALL ON $WP_DB_NAME.* TO '$WP_DB_USERNAME'@'$WP_IP';
CREATE USER 'root'@'$WP_IP' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'$WP_IP';
FLUSH PRIVILEGES;
EOF
---
# tasks file for mysql
- name: Set MySQL root password before installing
debconf:
name: "mysql-server"
question: "mysql-server/root_password"
# To add quotes for shell usage
# http://docs.ansible.com/ansible/playbooks_filters.html#other-useful-filters
value: "{{MYSQL_ROOT_PASSWORD | quote}}"
vtype: "password"
- name: Confirm MySQL root password before installing
debconf:
name: "mysql-server"
question: "mysql-server/root_password_again"
value: "{{MYSQL_ROOT_PASSWORD | quote}}"
vtype: "password"
- name: Install Packages
apt:
name: "{{item}}"
update_cache: yes
cache_valid_time: 3600
with_items: "{{PACKAGES}}"
- name: New MySQL config
template:
src: mysqld.cnf
dest: /etc/mysql/mysql.conf.d/mysqld.cnf
notify: restart_mysql
- name: Deletes anonymous MySQL server user for localhost
mysql_user:
user: ""
state: "absent"
login_password: "{{ MYSQL_ROOT_PASSWORD }}"
login_user: root
- name: Secures the MySQL root user
mysql_user:
user: "root"
password: "{{ MYSQL_ROOT_PASSWORD }}"
host: "{{ item }}"
login_password: "{{MYSQL_ROOT_PASSWORD}}"
login_user: root
priv: "*.*:ALL,GRANT"
with_items:
- 127.0.0.1
- localhost
- ::1
- "{{ ansible_fqdn }}"
- name: Removes the MySQL test database
mysql_db:
db: test
state: absent
login_password: "{{ MYSQL_ROOT_PASSWORD }}"
login_user: root
- name: Create the WP database
mysql_db:
login_password: "{{MYSQL_ROOT_PASSWORD}}"
login_user: root
db: "{{WP_DB_NAME}}"
state: present
- name: Create the WP user
mysql_user:
user: "{{WP_DB_USERNAME}}"
password: "{{WP_DB_PASSWORD}}"
host: "{{ item }}"
priv: "{{WP_DB_NAME}}.*:ALL,GRANT"
login_password: "{{MYSQL_ROOT_PASSWORD}}"
login_user: root
with_items:
- localhost
- "{{WP_IP}}"
---
# https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-lemp-on-ubuntu-16-04
- hosts: wp
become: yes
vars:
WP_DOMAIN: "wordpress.rdf.loc"
WP_PATH: "/var/www/html/wordpress"
WP_DB_NAME: "wordpress"
WP_DB_USERNAME: "wordpress"
WP_DB_PASSWORD: "wordpress"
DB_IP: 192.168.68.22
MY_DOMAIN: "mysql.rdf.loc"
WP_ADMIN_USERNAME: "admin"
WP_ADMIN_PASSWORD: "admin"
WP_ADMIN_EMAIL: "no@spam.org"
MYSQL_ROOT_PASSWORD: "root"
WP_IP: 192.168.68.21
tasks:
- pause: seconds=20
- name: Install software
apt:
name: "{{item}}"
update_cache: yes
with_items:
- nginx
- php
- php-mysql
- php-curl
- php-gd
- mysql-client
- php-mbstring
- php-xml
- php-xmlrpc
- curl
- name: Configure /etc/hosts
lineinfile:
dest: /etc/hosts
line: '127.0.0.1 {{WP_DOMAIN}}'
- name: Nginx directories
file:
state: directory
path: "{{WP_PATH}}/{{item}}"
owner: www-data
group: www-data
recurse: yes
with_items:
- public
- logs
- name: Configure nginx
template:
src: /vagrant/wordpress.rdf.loc.j2
dest: "/etc/nginx/sites-available/{{WP_DOMAIN}}"
notify: nginx-restart
- name: PHP info
copy:
src: /vagrant/php-info.php
dest: /var/www/html/php-info.php
owner: www-data
group: www-data
- name: Configure wordpress in nginx
file:
src: "/etc/nginx/sites-available/{{WP_DOMAIN}}"
dest: "/etc/nginx/sites-enabled/{{WP_DOMAIN}}"
state: link
notify: nginx-restart
- meta: flush_handlers
- name: Download wordpress
unarchive:
src: https://wordpress.org/latest.tar.gz
dest: "{{WP_PATH}}/public"
remote_src: yes
group: www-data
owner: www-data
extra_opts:
- "--strip-components=1"
- name: Config kopieren
copy:
src: "{{WP_PATH}}/public/wp-config-sample.php"
dest: "{{WP_PATH}}/public/wp-config.php"
owner: www-data
group: www-data
mode: 0660
- name: Config anpassen
replace:
dest: "{{WP_PATH}}/public/wp-config.php"
regexp: "{{item.regexp}}"
replace: "{{item.replace}}"
with_items:
- {"regexp": "database_name_here", "replace": "{{WP_DB_NAME}}"}
- {"regexp": "username_here", "replace": "{{WP_DB_USERNAME}}"}
- {"regexp": "password_here", "replace": "{{WP_DB_PASSWORD}}"}
- {"regexp": "localhost", "replace": "{{DB_IP}}"}
- name: Config anpassen
lineinfile:
dest: "{{WP_PATH}}/public/wp-config.php"
line: "{{item}}"
with_items:
- "define('FS_METHOD', 'direct');"
- name: Rechte auf wordpress
file:
path: "{{WP_PATH}}"
owner: www-data
group: www-data
recurse: yes
- name: Setup wp-user and wp-password
command: curl --silent "http://{{WP_DOMAIN}}/wp-admin/install.php?step=2" --data-urlencode "weblog_title={{WP_DOMAIN}}" --data-urlencode "user_name={{WP_ADMIN_USERNAME}}" --data-urlencode "admin_email={{WP_ADMIN_EMAIL}}" --data-urlencode "admin_password={{WP_ADMIN_PASSWORD}}" --data-urlencode "admin_password2={{WP_ADMIN_PASSWORD}}" --data-urlencode "pw_weak=1" --data-urlencode "blog_public=0"
handlers:
- name: nginx-restart
service:
name: nginx
state: restarted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment