Skip to content

Commit

Permalink
Update to Ubuntu 18.04 and add supports for PHP 7.1, 7.2, and 7.3 (#32)
Browse files Browse the repository at this point in the history
* Update system to Ubuntu 18.04 and allow to choose between PHP 5.6, 7.0, 7.1, 7.2, or 7.3

Instead of using the Vagrant box "digitalquery/wpvagrant", we use the official "ubuntu/bionic64" box and then install PHP, etc. during the first boot with the provision script.

* Hide curl output when installing wp-cli

* Use a config file to store mysql password to suppress the warning

Ref: https://stackoverflow.com/a/22933056/2603230

* Update README.markdown
  • Loading branch information
hesyifei authored and anugupta committed Jul 3, 2019
1 parent cf6a177 commit 6faf351
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 172 deletions.
26 changes: 14 additions & 12 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Vagrant Box with Ubuntu 14.04, Nginx, MySQL and PHP-FPM
# Vagrant Box with Ubuntu 18.04, Nginx, MySQL and PHP-FPM

A Vagrant setup to create an Ubuntu 14.04 (Trusty) virtual machine with Nginx, MySQL and PHP for WordPress development.
A Vagrant setup to create an Ubuntu 18.04 (Bionic) virtual machine with Nginx, MySQL and PHP for WordPress development.

Intended to be as simple as possible:

Expand All @@ -9,7 +9,7 @@ Intended to be as simple as possible:

* Quickly create a fresh WP instance served up via the Vagrant server.
* Optionally specify which version of WP to install
* Choose PHP 5.5, 5.6 or 7.0
* Choose PHP 5.6, 7.0, 7.1, 7.2, or 7.3
* Can be dropped into an existing WP directory to provide a LEMP testing and development server with minimal config changes
* Can load database dump as part of provisioning
* Renames all domain references using WP-CLI
Expand All @@ -22,10 +22,10 @@ This Vagrant box and provisioning is based on [this box from Paul Herron](https:

## What's included in the VM?

* Ubuntu 14.04 LTS (Trusty Tahir)
* Ubuntu 18.04 LTS (Bionic Beaver) (https://app.vagrantup.com/ubuntu/boxes/bionic64)
* Nginx, listening on port 80
* MySQL, listening for remote connections
* Choose between PHP 5.5, 5.6 or 7.0, with OpCache and XDebug
* Choose between PHP 5.6, 7.0, 7.1, 7.2, or 7.3, with OpCache and XDebug
* PHP-FPM
* [Byobu](http://byobu.co/)
* [WP-CLI](wp-cli.org)
Expand Down Expand Up @@ -82,8 +82,10 @@ Use these intructions if you have an existing WP install
1. Clone the wp-vagrant repo
* `git clone [email protected]:digitalquery/wp-vagrant.git`
* This creates a subfolder called `wp-vagrant`
1. Copy the VagrantFile into the site root directory
1. Copy the `Vagrantfile` into the site root directory
* `cp wp-vagrant/Vagrantfile .`
* Or alternatively, create a symbolic link:
* `ln -s wp-vagrant/Vagrantfile Vagrantfile`
1. If you want to import a database during the provisioning, then put a SQL dump file into the `wp-vagrant` folder. **This file must have a .sql extension**
1. Using your editor of choice, edit `wp-vagrant/settings.sh`
* `install_wordpress=false `
Expand All @@ -104,17 +106,17 @@ Use these intructions if you have an existing WP install

### PHP version

To change the PHP version, edit `settings.sh` and set the `php_version` setting to 5.5, 5.6 or 7.0. Run `vagrant provision` if you're changing an already created vagrant instance
To change the PHP version, edit `settings.sh` and set the `php_version` setting to 5.6, 7.0, 7.1, 7.2, or 7.3. Run `vagrant provision` if you're changing an already created vagrant instance

### Settings.sh

The full list - more comments and explanation are in the `settings.sh`file itself.
The full list - more comments and explanation are in the `settings.sh`file itself.

##### Basic Settings
* `hostname='wpvagrant.dev' # change this in the Vagrantfile as well`
* `php_version='7.0' # valid values: '5.5', '5.6' or '7.0'`
* `hostname='wpvagrant.test' # change this in the Vagrantfile as well`
* `php_version='7.2' # valid values: '5.6', '7.0', '7.1', '7.2', or '7.3'`
* `install_wordpress=true # change to false if this is being dropped in to an existing WP folder `
* `wp_version="" # eg wp_version="3.5.2 or 4.0-beta1". Leave blank for latest stable release`
* `wp_version="" # eg wp_version="3.5.2 or 4.0-beta1". Leave blank for latest stable release`
* `wp_path='/vagrant'`
* `mysql_root_password='root'`

Expand All @@ -126,7 +128,7 @@ The full list - more comments and explanation are in the `settings.sh`file itsel
##### WP admin and site setup details
* `wp_admin_user='admin'`
* `wp_admin_password='123'`
* `wp_admin_email='root@example.com'`
* `wp_admin_email='root@wpvagrant.test'`
* `wp_site_title='Vagrant'`

##### Database import
Expand Down
19 changes: 7 additions & 12 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# please see the online documentation at vagrantup.com.

# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "digitalquery/wpvagrant"

# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
# config.vm.box_url = "https://vagrantcloud.com/ubuntu/trusty64"
config.vm.box = "ubuntu/bionic64"

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
Expand Down Expand Up @@ -55,21 +51,20 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
end

config.vm.network "private_network", ip: "192.168.50.2"
config.vm.hostname = "wpvagrant.dev"
config.vm.hostname = "wpvagrant.test"
config.hostsupdater.remove_on_suspend = true

# Vagrant triggers
config.trigger.before :destroy, :stdout => true do
info "Dumping the database before destroying the VM..."
run "vagrant ssh -c 'sh /vagrant/wp-vagrant/mysql/db_dump.sh'"
config.trigger.before :destroy do |trigger|
trigger.info = "Dumping the database before destroying the VM..."
trigger.run_remote = {inline: "sh /vagrant/wp-vagrant/mysql/db_dump.sh"}
end

# provisioning script
provisioning_file = 'wp-vagrant/bootstrap.sh'
if ( File.file?(provisioning_file) )
config.vm.provision "shell" do |s|
s.path = provisioning_file
end
config.vm.provision "shell",
inline: ". /vagrant/wp-vagrant/bootstrap.sh"
end

end
20 changes: 8 additions & 12 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
#
. /vagrant/wp-vagrant/settings.sh

debconf-set-selections <<< "mysql-server-5.5 mysql-server/root_password password $mysql_root_password"
debconf-set-selections <<< "mysql-server-5.5 mysql-server/root_password_again password $mysql_root_password"
# https://serversforhackers.com/c/installing-mysql-with-debconf
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"

# default packages (php, mysql, nginx, etc), are preinstalled in the base box
# update anyway, and also make sure php-mbstring is installed
# we'll move this into the base box next update

add-apt-repository -y ppa:ondrej/php
add-apt-repository -y ppa:ondrej/nginx-mainline
apt-get update
apt-get upgrade
apt-get install php-mbstring php7.0-mbstring php5.5-mbstring php5.6-mbstring -y

apt-get install -y nginx php${php_version} php${php_version}-fpm php${php_version}-gd php${php_version}-mysql php${php_version}-cgi php${php_version}-cli php${php_version}-curl php${php_version}-mbstring ffmpeg vim git-core mysql-server mysql-client curl tmux

echo "**** add byobu config"
. /vagrant/wp-vagrant/configs/byobu.sh
Expand All @@ -23,19 +24,14 @@ echo "**** Moving nginx config files into place…"
. /vagrant/wp-vagrant/nginx/nginx.sh

echo "**** mysql config…"
mv /etc/mysql/my.cnf /etc/mysql/my.cnf.default
cp /vagrant/wp-vagrant/mysql/my.cnf /etc/mysql/my.cnf
. /vagrant/wp-vagrant/mysql/mysql.sh

echo "**** Set PHP to ${php_version} and copy config files"
. /vagrant/wp-vagrant/php/php.sh


echo "Starting services…"
service nginx restart
service php5.5-fpm stop
service php5.6-fpm stop
service php7.0-fpm stop

service php${php_version}-fpm restart
service mysql restart

Expand Down
4 changes: 2 additions & 2 deletions mysql/create_database.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
if [ ! -z $wp_db_name ] ; then

echo "**** creating database"
mysql -u root -p$mysql_root_password -e "CREATE DATABASE IF NOT EXISTS $wp_db_name;"
mysql --defaults-extra-file=~/.mysql_root.cnf -e "CREATE DATABASE IF NOT EXISTS $wp_db_name;"

if [ ! -z "$wp_db_user" ]; then
echo "**** adding custom user"
mysql -u root -p$mysql_root_password -e "GRANT ALL ON $wp_db_name.* TO '$wp_db_user'@'localhost' IDENTIFIED BY '$wp_db_password'"
mysql --defaults-extra-file=~/.mysql_root.cnf -e "GRANT ALL ON $wp_db_name.* TO '$wp_db_user'@'localhost' IDENTIFIED BY '$wp_db_password'"
fi

else
Expand Down
2 changes: 1 addition & 1 deletion mysql/db_dump.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if [ ! -z $wp_db_name ]; then
mkdir /vagrant/wp-vagrant/db_dumps
fi

mysqldump -u root --password=$mysql_root_password $wp_db_name > /vagrant/wp-vagrant/db_dumps/$db_dump_file
mysqldump --defaults-extra-file=~/.mysql_root.cnf $wp_db_name > /vagrant/wp-vagrant/db_dumps/$db_dump_file

if [ ! "$?" -eq 0 ]; then
echo "***** DATABASE DUMP FAILED - YOU MAY WISH TO ABORT VAGRANT DESTROY."
Expand Down
2 changes: 1 addition & 1 deletion mysql/import_database.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if [ ! -z $wp_db_name ] && $import_database; then
echo "**** SQL file found - proceeding with import"
wp_db_dump_file=$(find /vagrant/wp-vagrant -maxdepth 1 -name '*.sql')
echo "**** import filename is: $wp_db_dump_file"
mysql -u root -p$mysql_root_password $wp_db_name < $wp_db_dump_file
mysql --defaults-extra-file=~/.mysql_root.cnf $wp_db_name < $wp_db_dump_file

if [ ! -z $import_site_domain ]; then
echo "**** wp-cli search and replace"
Expand Down
127 changes: 0 additions & 127 deletions mysql/my.cnf

This file was deleted.

13 changes: 13 additions & 0 deletions mysql/mysql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

cp /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf.default

# Allow access from host machine (digitalquery#21)
sed -i 's/bind-address[[:space:]]\+= 127\.0\.0\.1/bind-address = 0.0.0.0/' /etc/mysql/mysql.conf.d/mysqld.cnf

# https://stackoverflow.com/a/22933056/2603230
echo "[client]
user = root
password = $mysql_root_password
host = localhost
" > ~/.mysql_root.cnf
3 changes: 2 additions & 1 deletion nginx/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ server {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php%%php_version%%-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
# https://stackoverflow.com/a/16887296/2603230
include fastcgi.conf;
}
}
3 changes: 3 additions & 0 deletions nginx/nginx.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/env bash

# Start after boot
sudo update-rc.d nginx enable

# copy nginx config
cp /vagrant/wp-vagrant/nginx/nginx.conf /etc/nginx/

Expand Down
10 changes: 6 additions & 4 deletions settings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@

# this is hostname for the vagrant box
# make sure to change it in the Vagrantfile as well
hostname='wpvagrant.dev'
hostname='wpvagrant.test'

# PHP version to install
# valid values:
# 5.5
# 5.6
# 7.0
php_version='7.0'
# 7.1
# 7.2
# 7.3
php_version='7.2'

# specify path (inside the Vagrant VM) to WordPress to allow wp-cli to do search and replace
# shouldn't need to change this from /vagrant unless you've made other config changes
Expand Down Expand Up @@ -65,7 +67,7 @@ wp_db_password=''

wp_admin_user='admin'
wp_admin_password='123'
wp_admin_email='root@example.com'
wp_admin_email='root@wpvagrant.test'
wp_site_title='WP Vagrant'

#
Expand Down
6 changes: 6 additions & 0 deletions wp/wp-cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
if [ -f /usr/local/bin/wp ]; then
echo "**** updating wp-cli"
sudo wp cli update --allow-root --yes
else
echo "**** installing wp-cli"
cd ~/
curl -Os https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
fi

wpcli_defaults_folder='/home/vagrant/.wp-cli'
Expand Down

0 comments on commit 6faf351

Please sign in to comment.