|
| 1 | +# -*- mode: ruby -*- |
| 2 | +# vi: set ft=ruby : |
| 3 | + |
| 4 | +# https://github.com/hashicorp/vagrant/issues/9442#issuecomment-374785457 |
| 5 | +unless Vagrant::DEFAULT_SERVER_URL.frozen? |
| 6 | + Vagrant::DEFAULT_SERVER_URL.replace('https://vagrantcloud.com') |
| 7 | +end |
| 8 | + |
| 9 | +Vagrant.configure("2") do |config| |
| 10 | + # VM Box |
| 11 | + config.vm.box = "ubuntu/bionic64" |
| 12 | + # Automatic box update checking |
| 13 | + config.vm.box_check_update = true |
| 14 | + |
| 15 | + # CodeIgniter virtual host |
| 16 | + config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" |
| 17 | + # Code Coverage virtual host |
| 18 | + config.vm.network "forwarded_port", guest: 81, host: 8081, host_ip: "127.0.0.1" |
| 19 | + # User Guide virtual host |
| 20 | + config.vm.network "forwarded_port", guest: 82, host: 8082, host_ip: "127.0.0.1" |
| 21 | + # MySQL server |
| 22 | + #config.vm.network "forwarded_port", guest: 3306, host: 3307, host_ip: "127.0.0.1" |
| 23 | + # PostgreSQL server |
| 24 | + #config.vm.network "forwarded_port", guest: 5432, host: 5433, host_ip: "127.0.0.1" |
| 25 | + # Memcached server |
| 26 | + #config.vm.network "forwarded_port", guest: 11211, host: 11212, host_ip: "127.0.0.1" |
| 27 | + # Redis server |
| 28 | + #config.vm.network "forwarded_port", guest: 6379, host: 6380, host_ip: "127.0.0.1" |
| 29 | + |
| 30 | + # Add "192.168.10.10 ${VIRTUALHOST}" in your host file to access by domain |
| 31 | + #config.vm.network "private_network", ip: "192.168.10.10" |
| 32 | + |
| 33 | + # Same path set in the $CODEIGNITER_PATH Provision |
| 34 | + # "virtualbox" type allow auto-sync host to guest and guest to host |
| 35 | + # but chmod does not work... tests will fail. |
| 36 | + # Default rsync__args except "--copy-links", to allow phpunit correctly works by symlink |
| 37 | + config.vm.synced_folder ".", "/var/www/codeigniter", type: "rsync", rsync__args: ["--verbose", "--archive", "--delete", "-z"] |
| 38 | + |
| 39 | + # Provider-specific configuration |
| 40 | + config.vm.provider "virtualbox" do |vb| |
| 41 | + # Display the VirtualBox GUI when booting the machine |
| 42 | + vb.gui = false |
| 43 | + # Customize the amount of memory on the VM: |
| 44 | + vb.memory = "1024" |
| 45 | + end |
| 46 | + |
| 47 | + # Provision |
| 48 | + config.vm.provision "shell", inline: <<-SHELL |
| 49 | + MYSQL_ROOT_PASS="password" |
| 50 | + PGSQL_ROOT_PASS="password" |
| 51 | + VIRTUALHOST="localhost" |
| 52 | + CODEIGNITER_PATH="/var/www/codeigniter" |
| 53 | + PHP_VERSION=7.2 |
| 54 | + PGSQL_VERSION=10 |
| 55 | + #APT_PROXY="192.168.10.1:3142" |
| 56 | +
|
| 57 | + grep -q "127.0.0.1 ${VIRTUALHOST}" /etc/hosts || echo "127.0.0.1 ${VIRTUALHOST}" >> /etc/hosts |
| 58 | +
|
| 59 | + # Creates a swap file if necessary |
| 60 | + RAM=`awk '/MemTotal/ {print $2}' /proc/meminfo` |
| 61 | + if [ $RAM -lt 1000000 ] && [ ! -f /swap/swapfile ]; then |
| 62 | + echo "================================================================================" |
| 63 | + echo "Adding swap" |
| 64 | + echo "================================================================================" |
| 65 | + echo "This process may take a few minutes. Please wait..." |
| 66 | + mkdir /swap |
| 67 | + dd if=/dev/zero of=/swap/swapfile bs=1024 count=1000000 |
| 68 | + chmod 600 /swap/swapfile |
| 69 | + mkswap /swap/swapfile |
| 70 | + swapon /swap/swapfile |
| 71 | + echo "/swap/swapfile swap swap defaults 0 0" >> /etc/fstab |
| 72 | + echo "Done." |
| 73 | + fi |
| 74 | +
|
| 75 | + # Prepare to use APT Proxy |
| 76 | + if [ ! -z $APT_PROXY ]; then |
| 77 | + if [ ! -f /etc/apt/sources.list-origin ]; then |
| 78 | + cp /etc/apt/sources.list /etc/apt/sources.list-origin |
| 79 | + fi |
| 80 | + sed -i "s/archive.ubuntu.com/${APT_PROXY}/" /etc/apt/sources.list |
| 81 | + sed -i "s/security.ubuntu.com/${APT_PROXY}/" /etc/apt/sources.list |
| 82 | + fi |
| 83 | +
|
| 84 | + export DEBIAN_FRONTEND=noninteractive |
| 85 | +
|
| 86 | + echo "================================================================================" |
| 87 | + echo "Updating and Installing Required Packages" |
| 88 | + echo "================================================================================" |
| 89 | +
|
| 90 | + apt-get update |
| 91 | +
|
| 92 | + debconf-set-selections <<< "mysql-server mysql-server/root_password password ${MYSQL_ROOT_PASS}" |
| 93 | + debconf-set-selections <<< "mysql-server mysql-server/root_password_again password ${MYSQL_ROOT_PASS}" |
| 94 | +
|
| 95 | + apt-get install -y \ |
| 96 | + php$PHP_VERSION apache2 composer \ |
| 97 | + php-intl php-mbstring php-xml php-zip php-xdebug \ |
| 98 | + php-mysql mysql-server mysql-client \ |
| 99 | + php-pgsql postgresql-$PGSQL_VERSION \ |
| 100 | + php-sqlite3 sqlite3 \ |
| 101 | + php-memcached memcached \ |
| 102 | + php-redis redis-server \ |
| 103 | + php-curl curl \ |
| 104 | + php-gd php-imagick \ |
| 105 | + python-pip |
| 106 | +
|
| 107 | + pip install sphinx sphinxcontrib-phpdomain |
| 108 | +
|
| 109 | + apt-get autoclean |
| 110 | +
|
| 111 | + echo "================================================================================" |
| 112 | + echo "Preparing User Guide" |
| 113 | + echo "================================================================================" |
| 114 | +
|
| 115 | + cd "${CODEIGNITER_PATH}/user_guide_src/cilexer" |
| 116 | + python setup.py install |
| 117 | + cd .. |
| 118 | + make html |
| 119 | +
|
| 120 | + echo "================================================================================" |
| 121 | + echo "Configuring Databases" |
| 122 | + echo "================================================================================" |
| 123 | +
|
| 124 | + sed -i "s/^bind-address/#bind-address/" /etc/mysql/mysql.conf.d/mysqld.cnf |
| 125 | + mysql -e "CREATE DATABASE IF NOT EXISTS codeigniter COLLATE 'utf8_general_ci'; |
| 126 | + UPDATE mysql.user SET Host='%' WHERE user='root'; |
| 127 | + GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; |
| 128 | + FLUSH PRIVILEGES;" -uroot -p$MYSQL_ROOT_PASS |
| 129 | + systemctl restart mysql |
| 130 | +
|
| 131 | + sed -i "s/^#listen_addresses = 'localhost'/listen_addresses = '*'/" /etc/postgresql/$PGSQL_VERSION/main/postgresql.conf |
| 132 | + 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 |
| 133 | + 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" |
| 134 | + sudo -u postgres psql -c "ALTER ROLE root WITH PASSWORD '${PGSQL_ROOT_PASS}'" |
| 135 | + sudo -u postgres psql -tc "SELECT 1 FROM pg_database WHERE datname='codeigniter'" | grep -q 1 ||sudo -u postgres psql -c "CREATE DATABASE codeigniter" |
| 136 | + sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE codeigniter TO root" |
| 137 | + systemctl restart postgresql |
| 138 | +
|
| 139 | + echo "================================================================================" |
| 140 | + echo "Configuring Memcached and Redis" |
| 141 | + echo "================================================================================" |
| 142 | +
|
| 143 | + sed -i "s/^bind 127.0.0.1/#bind 127.0.0.1/" /etc/redis/redis.conf |
| 144 | + sed -i "s/^protected-mode yes/protected-mode no/" /etc/redis/redis.conf |
| 145 | + sed -i "s/^-l 127.0.0.1/#-l 127.0.0.1/" /etc/memcached.conf |
| 146 | + systemctl restart redis |
| 147 | + systemctl restart memcached |
| 148 | +
|
| 149 | + echo "================================================================================" |
| 150 | + echo "Configuring Virtual Hosts" |
| 151 | + echo "================================================================================" |
| 152 | +
|
| 153 | + mkdir -p "${CODEIGNITER_PATH}/build/coverage-html" |
| 154 | + mkdir -p "${CODEIGNITER_PATH}/public" |
| 155 | + mkdir -p "${CODEIGNITER_PATH}/user_guide_src/build/html" |
| 156 | + mkdir -p "${CODEIGNITER_PATH}/writable/apache" |
| 157 | + chown -R vagrant:vagrant $CODEIGNITER_PATH |
| 158 | +
|
| 159 | + # Creates a symlink in the user home |
| 160 | + if [ ! -d /home/vagrant/codeigniter ]; then |
| 161 | + ln -s $CODEIGNITER_PATH /home/vagrant/codeigniter |
| 162 | + fi |
| 163 | +
|
| 164 | + sed -i "s/APACHE_RUN_USER=www-data/APACHE_RUN_USER=vagrant/" /etc/apache2/envvars |
| 165 | + sed -i "s/APACHE_RUN_GROUP=www-data/APACHE_RUN_GROUP=vagrant/" /etc/apache2/envvars |
| 166 | + grep -q "Listen 81" /etc/apache2/ports.conf || sed -i "s/^Listen 80/Listen 80\\nListen 81\\nListen 82/" /etc/apache2/ports.conf |
| 167 | + sed -i "s/^display_errors = Off/display_errors = On/" /etc/php/7.2/apache2/php.ini |
| 168 | + sed -i "s/^display_startup_errors = Off/display_startup_errors = On/" /etc/php/7.2/apache2/php.ini |
| 169 | +
|
| 170 | + echo "ServerName ${VIRTUALHOST} |
| 171 | +<Directory ${CODEIGNITER_PATH}> |
| 172 | + DirectoryIndex index.html index.php |
| 173 | + Options All |
| 174 | + AllowOverride All |
| 175 | +</Directory> |
| 176 | +<VirtualHost *:80> |
| 177 | + ServerAdmin vagrant@localhost |
| 178 | + DocumentRoot ${CODEIGNITER_PATH}/public |
| 179 | + ErrorLog ${CODEIGNITER_PATH}/writable/apache/error.log |
| 180 | + CustomLog ${CODEIGNITER_PATH}/writable/apache/custom.log combined |
| 181 | +</VirtualHost> |
| 182 | +<VirtualHost *:81> |
| 183 | + DocumentRoot ${CODEIGNITER_PATH}/build/coverage-html |
| 184 | +</VirtualHost> |
| 185 | +<VirtualHost *:82> |
| 186 | + DocumentRoot ${CODEIGNITER_PATH}/user_guide_src/build/html |
| 187 | +</VirtualHost> |
| 188 | +" > /etc/apache2/sites-available/codeigniter.conf |
| 189 | +
|
| 190 | + a2enmod rewrite |
| 191 | + a2dissite 000-default.conf |
| 192 | + a2ensite codeigniter.conf |
| 193 | + systemctl restart apache2 |
| 194 | +
|
| 195 | + echo "================================================================================" |
| 196 | + echo "Services Status" |
| 197 | + echo "================================================================================" |
| 198 | + service --status-all |
| 199 | +
|
| 200 | + SHELL |
| 201 | +end |
0 commit comments