Last active
May 11, 2020 17:36
-
-
Save gphg/e6a717e47ca46fae610d3059ff5edb84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode: ruby -*- | |
# vi: set ft=ruby : | |
VAGRANTFILE_API_VERSION = '2' | |
# https://github.com/hashicorp/vagrant/issues/9442#issuecomment-374785457 | |
unless Vagrant::DEFAULT_SERVER_URL.frozen? | |
Vagrant::DEFAULT_SERVER_URL.replace('https://vagrantcloud.com') | |
end | |
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| | |
config.vm.box = "minimal/xenial64" | |
config.vm.box_check_update = true | |
config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" | |
config.vm.network "forwarded_port", guest: 81, host: 8081, host_ip: "127.0.0.1" | |
config.vm.network "forwarded_port", guest: 82, host: 8082, host_ip: "127.0.0.1" | |
config.vm.network "private_network", ip: "192.168.33.10" | |
config.vm.synced_folder ".", "/var/www/web", | |
type: "rsync", | |
rsync__args: ["--verbose", "--archive", "--delete", "-z"] | |
config.vm.provider "virtualbox" do |vb| | |
# Display the VirtualBox GUI when booting the machine | |
vb.gui = false | |
# Customize the amount of memory on the VM: | |
vb.memory = "1024" | |
end | |
# Provision | |
config.vm.provision "shell", inline: <<-SHELL | |
MYSQL_ROOT_PASS="password" | |
PGSQL_ROOT_PASS="password" | |
VIRTUALHOST="localhost" | |
WEBAPP_PATH="/var/www/web" | |
PHP_VERSION=7.2 | |
#PGSQL_VERSION=10 | |
grep -q "127.0.0.1 ${VIRTUALHOST}" /etc/hosts || echo "127.0.0.1 ${VIRTUALHOST}" >> /etc/hosts | |
# Fix for https://bugs.launchpad.net/ubuntu/+source/livecd-rootfs/+bug/1561250 | |
if ! grep -q "ubuntu-xenial" /etc/hosts; then | |
echo "127.0.0.1 ubuntu-xenial" >> /etc/hosts | |
fi | |
# Creates a swap file if necessary | |
RAM=`awk '/MemTotal/ {print $2}' /proc/meminfo` | |
if [ $RAM -lt 1000000 ] && [ ! -f /swap/swapfile ]; then | |
echo "================================================================================" | |
echo "Adding swap" | |
echo "================================================================================" | |
echo "This process may take a few minutes. Please wait..." | |
mkdir /swap | |
dd if=/dev/zero of=/swap/swapfile bs=1024 count=1000000 | |
chmod 600 /swap/swapfile | |
mkswap /swap/swapfile | |
swapon /swap/swapfile | |
echo "/swap/swapfile swap swap defaults 0 0" >> /etc/fstab | |
echo "Done." | |
fi | |
export DEBIAN_FRONTEND=noninteractive | |
echo "================================================================================" | |
echo "Updating and Installing Required Packages" | |
echo "================================================================================" | |
apt-get update | |
apt-get install -y software-properties-common | |
add-apt-repository ppa:ondrej/php -y | |
debconf-set-selections <<< "mysql-server mysql-server/root_password password ${MYSQL_ROOT_PASS}" | |
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password ${MYSQL_ROOT_PASS}" | |
apt-get update | |
apt-get install -y \ | |
php$PHP_VERSION apache2 composer \ | |
php$PHP_VERSION-intl php$PHP_VERSION-json php$PHP_VERSION-mbstring php$PHP_VERSION-xml php$PHP_VERSION-zip \ | |
php$PHP_VERSION-xdebug \ | |
php$PHP_VERSION-mysql php$PHP_VERSION-mysqlnd mysql-server mysql-client \ | |
php$PHP_VERSION-sqlite3 sqlite3 \ | |
#php$PHP_VERSION-memcached memcached \ | |
#php$PHP_VERSION-redis redis-server \ | |
php$PHP_VERSION-curl curl \ | |
php$PHP_VERSION-gd php$PHP_VERSION-imagick | |
if [ ! -z $PGSQL_VERSION ]; then | |
apt-get install -y php$PHP_VERSION-pgsql postgresql-$PGSQL_VERSION | |
fi | |
apt-get autoclean | |
echo "================================================================================" | |
echo "Configuring Databases" | |
echo "================================================================================" | |
sed -i "s/^bind-address/#bind-address/" /etc/mysql/mysql.conf.d/mysqld.cnf | |
mysql -e "CREATE DATABASE IF NOT EXISTS forge COLLATE 'utf8_general_ci'; | |
UPDATE mysql.user SET Host='%' WHERE user='root'; | |
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; | |
FLUSH PRIVILEGES;" -uroot -p$MYSQL_ROOT_PASS | |
systemctl restart mysql | |
if [ ! -z $PGSQL_VERSION ]; then | |
sed -i "s/^#listen_addresses = 'localhost'/listen_addresses = '*'/" /etc/postgresql/$PGSQL_VERSION/main/postgresql.conf | |
grep -q "host all root all md5" /etc/postgresql/$PGSQL_VERSION/main/pg_hba.conf \ | |
|| echo "host all root all md5" >> /etc/postgresql/$PGSQL_VERSION/main/pg_hba.conf | |
sudo -u postgres psql -tc "SELECT 1 FROM pg_roles WHERE rolname='root'" | grep -q 1 \ | |
|| sudo -u postgres psql -c "CREATE ROLE root WITH SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN" | |
sudo -u postgres psql -c "ALTER ROLE root WITH PASSWORD '${PGSQL_ROOT_PASS}'" | |
sudo -u postgres psql -tc "SELECT 1 FROM pg_database WHERE datname='forge'" | grep -q 1 \ | |
|| sudo -u postgres psql -c "CREATE DATABASE forge" | |
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE forge TO root" | |
systemctl restart postgresql | |
fi | |
echo "================================================================================" | |
echo "Configuring Virtual Hosts" | |
echo "================================================================================" | |
mkdir -p "${WEBAPP_PATH}/build/coverage-html" | |
mkdir -p "${WEBAPP_PATH}/public" | |
mkdir -p "${WEBAPP_PATH}/user_guide_src/build/html" | |
mkdir -p "${WEBAPP_PATH}/writable/apache" | |
chown -R vagrant:vagrant $WEBAPP_PATH | |
# Creates a symlink in the user home | |
if [ ! -d /home/vagrant/web ]; then | |
ln -s $WEBAPP_PATH /home/vagrant/web | |
fi | |
sed -i "s/APACHE_RUN_USER=www-data/APACHE_RUN_USER=vagrant/" /etc/apache2/envvars | |
sed -i "s/APACHE_RUN_GROUP=www-data/APACHE_RUN_GROUP=vagrant/" /etc/apache2/envvars | |
grep -q "Listen 81" /etc/apache2/ports.conf || sed -i "s/^Listen 80/Listen 80\\nListen 81\\nListen 82/" /etc/apache2/ports.conf | |
sed -i "s/^display_errors = Off/display_errors = On/" /etc/php/$PHP_VERSION/apache2/php.ini | |
sed -i "s/^display_startup_errors = Off/display_startup_errors = On/" /etc/php/$PHP_VERSION/apache2/php.ini | |
echo "ServerName ${VIRTUALHOST} | |
<Directory ${WEBAPP_PATH}> | |
DirectoryIndex index.html index.php | |
Options All | |
AllowOverride All | |
</Directory> | |
<VirtualHost *:80> | |
ServerAdmin vagrant@localhost | |
DocumentRoot ${WEBAPP_PATH}/public | |
ErrorLog ${WEBAPP_PATH}/writable/apache/error.log | |
CustomLog ${WEBAPP_PATH}/writable/apache/custom.log combined | |
</VirtualHost> | |
<VirtualHost *:81> | |
DocumentRoot ${WEBAPP_PATH}/build/coverage-html | |
</VirtualHost> | |
<VirtualHost *:82> | |
DocumentRoot ${WEBAPP_PATH}/user_guide_src/build/html | |
</VirtualHost> | |
" > /etc/apache2/sites-available/web.conf | |
a2enmod rewrite | |
a2dissite 000-default.conf | |
a2ensite web.conf | |
systemctl restart apache2 | |
echo "================================================================================" | |
echo "Services Status" | |
echo "================================================================================" | |
service --status-all | |
SHELL | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment