Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wordpress Development Machine in Vagrant
This Vagrantfile spins up a simple Vagrant instance of Ubuntu 18.04 LTS, installs PHP/Apache/MariaDB and puts Wordpress on there. Enjoy :)
# Some of this is based on https://peteris.rocks/blog/unattended-installation-of-wordpress-on-ubuntu-server/
# and https://www.hongkiat.com/blog/install-wordpress-locally-vagrant/
Vagrant.configure("2") do |config|
config.vm.define "wordpress" do |wordpress|
wordpress.vm.hostname = "wordpress"
wordpress.vm.provider "virtualbox" do |vb|
vb.name = "wordpress"
end
wordpress.vm.network "private_network", ip: "192.0.2.10"
wordpress.vm.boot_timeout = 1200
if Vagrant.has_plugin?("vagrant-cachier")
wordpress.cache.scope = :box
end
end
config.vm.box = "ubuntu/bionic64"
config.vm.box_check_update = false
config.vm.provision "shell", inline: <<-SHELL
if not ip addr | grep 192.0.2.10 > /dev/null ; then
# Configure NIC
NIC=$(ip link | grep -v "link/" | grep -v "LOOPBACK" | grep -v "state UP" | head -n 1 | cut -f 2 -d: | cut -f 2 -d\ )
dhclient $NIC
ip addr add 192.0.2.10/24 dev $NIC
fi
# Define variables
WP_DOMAIN="192.0.2.10"
WP_ADMIN_USERNAME="admin"
WP_ADMIN_PASSWORD="admin"
WP_ADMIN_EMAIL="no@spam.org"
WP_DB_NAME="wordpress"
WP_DB_USERNAME="wordpress"
WP_DB_PASSWORD="wordpress"
MYSQL_ROOT_PASSWORD="root"
# Update packages and install PHP
apt-get update
debconf-set-selections <<< "mysql-server mysql-server/root_password password $MYSQL_ROOT_PASSWORD"
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $MYSQL_ROOT_PASSWORD"
apt install -y php php-cli php-curl php-gd php-readline php-bcmath php-imagick libapache2-mod-php php-mysql mariadb-server git-core
# Get PHP Version
PHP_VERSION=$(dpkg -l | grep " php[0-9].[0-9] " | cut -f 3 -d\ | cut -f 3 -dp)
# Configure PHP logging
sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php/$PHP_VERSION/apache2/php.ini
sed -i "s/display_errors = .*/display_errors = On/" /etc/php/$PHP_VERSION/apache2/php.ini
sed -i "s/disable_functions = .*/disable_functions = /" /etc/php/$PHP_VERSION/cli/php.ini
# Reconfigure Apache2
a2enmod rewrite
systemctl restart apache2
# Install wp-cli
curl -sS -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
# Set up the database
echo "CREATE USER $WP_DB_USERNAME@localhost IDENTIFIED BY '$WP_DB_PASSWORD';" | mysql -uroot -p$MYSQL_ROOT_PASSWORD
echo "CREATE DATABASE $WP_DB_NAME;" | mysql -uroot -p$MYSQL_ROOT_PASSWORD
echo "GRANT ALL ON $WP_DB_NAME.* TO $WP_DB_USERNAME@localhost;" | mysql -uroot -p$MYSQL_ROOT_PASSWORD
# Download Wordpress
cd /var/www/html
curl -sS https://wordpress.org/latest.tar.gz | tar xfz -
mv wordpress/* .
rm index.html
rm wordpress.tar.gz
rmdir wordpress
# Configure Wordpress
cp -f wp-config-sample.php wp-config.php
sed -i s/database_name_here/$WP_DB_NAME/ wp-config.php
sed -i s/username_here/$WP_DB_USERNAME/ wp-config.php
sed -i s/password_here/$WP_DB_PASSWORD/ wp-config.php
echo "define('FS_METHOD', 'direct');" >> wp-config.php
chown -R www-data:www-data /var/www/html
# Run first-install wizard
curl -sS "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"
# Force rewriting permalinks to ""
wp option update permalink_structure "" --allow-root
SHELL
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.