How to set up a wordpress vagrant

In this tutorial, I’m set up a WordPress Vagrant, using vagrant box ubuntu with Nginx + MariaDB + WordPress. Vagrant the essential for DevOps Roles.

The structures files and folders wordpress vagrant as below:

[huupv@localhost project]$ pwd
/home/huupv/project
[huupv@localhost project]$ ls -F
keys/ nginx/ Vagrantfile var/
mysql/ php/ VAGRANT_ENV/ wp/

To configure Vagrantfile

vim Vagrantfile

The content as below:

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.hostname = "wp"
config.ssh.insert_key = false
#config.vm.boot_timeout = 800
#config.ssh.username = vagrant
#config.ssh.password = vagrant
#config.ssh.private_key_path = ["keys/.ssh/vagrant_rsa", "~/.vagrant.d/insecure_private_key"]
#config.vm.provision "file", source: "keys/.ssh/vagrant_rsa.pub", destination: "~/.ssh/authorized_keys"
config.vm.provision :shell, path: "VAGRANT_ENV/bootstrap.sh"
config.vm.network "forwarded_port", guest: 80, host: 8888
config.vm.network :public_network, :bridge => "eth1", :auto_config => false
config.vm.provider :virtualbox do |vb|
# Set VM memory size
vb.customize ["modifyvm", :id, "--memory", "512"]
# these 2 commands massively speed up DNS resolution, which means outbound
# connections don't take forever (eg the WP admin dashboard and update page)
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end
end

To configure file vagrant bootstrap.sh

vim VAGRANT_ENV/bootstrap.sh

The content as below:

#!/bin/bash
echo "Vagrant box (Ubuntu 16.04 + nginx + php7.0 + MariaDB + WordPress."
 echo "Updating apt-get"
 sudo apt-get -y update
# Nginx
 echo "Installing Nginx"
 sudo apt-get install -y nginx
# MySQL
 echo "Preparing for MySQL Installation"
 sudo apt-get install -y debconf-utils
 sudo debconf-set-selections << "mysql-server mysql-server/root_password password root"
 sudo debconf-set-selections << "mysql-server mysql-server/root_password_again password root" echo "Installing MySQL" #sudo apt-get install -y mysql-server-5.7
 sudo apt-get install -y mariadb-server mariadb-client php7.0-mysql
 sudo rm -f /etc/mysql/my.cnf
 sudo rm -f /etc/alternatives/my.cnf
 sudo cp /vagrant/mysql/my.cnf /etc/alternatives/my.cnf
 sudo ln -s /etc/alternatives/my.cnf /etc/mysql/my.cnf
 ls -ll /etc/mysql/my.cnf
 echo "Installing PHP and MySQL module"
 sudo apt-get install -y php-fpm php-mysql
# Nginx Config
 echo "Overwriting default Nginx config to work with PHP"
 sudo rm -rf /etc/nginx/sites-available/default
 cp /vagrant/nginx/default.conf /etc/nginx/sites-available/default
# php cli
 sudo sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php/7.0/fpm/php.ini
 sudo sed -i "s/display_errors = .*/display_errors = On/" /etc/php/7.0/cli/php.ini
 sudo sed -i "s/memory_limit = .*/memory_limit = 30M/" /etc/php/7.0/cli/php.ini
 sudo sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php/7.0/cli/php.ini
# php fpm
 sudo sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php/7.0/fpm/php.ini
 sudo sed -i "s/display_errors = .*/display_errors = On/" /etc/php/7.0/fpm/php.ini
 sudo sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /etc/php/7.0/fpm/php.ini
 sudo sed -i "s/memory_limit = .*/memory_limit = 30M/" /etc/php/7.0/fpm/php.ini
 sudo sed -i "s/upload_max_filesize = .*/upload_max_filesize = 100M/" /etc/php/7.0/fpm/php.ini
 sudo sed -i "s/post_max_size = .*/post_max_size = 100M/" /etc/php/7.0/fpm/php.ini
 sudo sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php/7.0/fpm/php.ini
# Restarting Nginx for config to take effect
 echo "Restarting Nginx for changes to take effect"
 sudo service nginx restart

echo "Setting Ubuntu (user) password to \"vagrant\""
echo "ubuntu:vagrant" | chpasswd
# Services restart
 sudo systemctl restart mysql.service
 sudo systemctl restart nginx.service
 sudo systemctl restart php7.0-fpm.service
sudo mysql -u root -p -e 'show databases'
 #Create wordpress databases
 bash /vagrant/mysql/create_database.sh
 #To install and configure wordpress
 cd /tmp
 curl -O https://wordpress.org/latest.tar.gz
 tar xzvf latest.tar.gz
 sudo cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
 mkdir /tmp/wordpress/wp-content/upgrade
 sudo cp -a /tmp/wordpress/* /var/www/html
 sudo find /var/www/html -type d -exec chmod g+s {} \;
 sudo chmod g+w /var/www/html/wp-content
 sudo chmod -R g+w /var/www/html/wp-content/themes
 sudo chmod -R g+w /var/www/html/wp-content/plugins
 sudo rm -f /var/www/html/wp-config.php
 sudo cp /vagrant/wp/wp-config.php /var/www/html/wp-config.php
 sudo usermod -a -G www-data ubuntu
 sudo chown -R ubuntu:www-data /var/www/html
 sudo systemctl restart nginx.service
 echo "Cleaning up additional setup files and logs"
 #sudo rm -r /var/www/html
 #sudo rm /var/www/ubuntu-xenial-16.04-cloudimg-console.log

To configure MySQL my.cf file

vim mysql/my.cnf

The content as below:

[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0

[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
bind-address = 0.0.0.0
key_buffer_size = 16M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 8
myisam-recover = BACKUP
query_cache_limit = 1M
query_cache_size = 16M
log_error = /var/log/mysql/error.log
expire_logs_days = 10
max_binlog_size = 100M
[mysqldump]
quick
quote-names
max_allowed_packet = 16M

[mysql]
[isamchk]
key_buffer_size = 16M
!includedir /etc/mysql/conf.d/

To create a database for WordPress

vim mysql/create_database.sh

The content as below:

#!/usr/bin/env bash
if [ ! -z "wordpress" ] ; then
echo "creating database"
 sudo mysql -u root -p'root' -e "CREATE DATABASE IF NOT EXISTS wordpress;"
if [ ! -z "wp_db_user" ]; then
 echo " adding custom user"
 sudo mysql -u root -p'root' -e "GRANT ALL ON wordpress.* TO 'wp_db_user'@'localhost' IDENTIFIED BY '123456789'"
 sudo mysql -u root -p'root' -e "FLUSH PRIVILEGES;"
 sudo mysql -u root -p'root' -e 'show databases;'
 fi
else
 echo "No database name specified - skipping db creation"
fi

To configure file wp-config.php for WordPress vagrant

vim wp/wp-config.php

The content as below:

<?php
define('DB_NAME', 'wordpress');
/** MySQL database username */
 define('DB_USER', 'wp_db_user');
/** MySQL database password */
 define('DB_PASSWORD', '123456789');
/** MySQL hostname */
 define('DB_HOST', 'localhost');
/** Database Charset to use in creating database tables. */
 define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
 define('DB_COLLATE', '');
 define('AUTH_KEY', 'put your unique phrase here');
 define('SECURE_AUTH_KEY', 'put your unique phrase here');
 define('LOGGED_IN_KEY', 'put your unique phrase here');
 define('NONCE_KEY', 'put your unique phrase here');
 define('AUTH_SALT', 'put your unique phrase here');
 define('SECURE_AUTH_SALT', 'put your unique phrase here');
 define('LOGGED_IN_SALT', 'put your unique phrase here');
 define('NONCE_SALT', 'put your unique phrase here');
 $table_prefix = 'wp_';
 define('WP_DEBUG', false);
 /** Absolute path to the WordPress directory. */
 if ( !defined('ABSPATH') )
 define('ABSPATH', dirname(__FILE__) . '/');
/** Sets up WordPress vars and included files. */
 require_once(ABSPATH . 'wp-settings.php');
 define('FS_METHOD', 'direct');

To configure file nginx.conf

vim nginx/default.conf

The content as below:

server {
 listen 80 default_server;
 listen [::]:80 default_server ipv6only=on;
 root /var/www/html/;
 index index.php index.html index.htm;
 client_max_body_size 20M;
 server_name devopsroles.com;
 location / {
 try_files $uri $uri/ /index.php?$args;
 }
 error_page 404 /404.html;
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root /usr/share/nginx/html;
 }
 location ~ \.php$ {
 try_files $uri =404;
 fastcgi_split_path_info ^(.+\.php)(/.+)$;
 fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
 fastcgi_index index.php;
 include fastcgi_params;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 }
 }

Vagrant up running for WordPress

[huupv@localhost ~]$ cd /home/huupv/project/
[huupv@localhost project]$ vagrant up

Conclusion

You have to set up a Vagrant WordPress Nginx + MariaDB. I hope will this your helpful. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.