Skip to content

Commit

Permalink
Adapted yii2-app-advanced Vagrant setup to basic app (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrs authored and samdark committed Dec 4, 2017
1 parent fc82ba4 commit 33882b1
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -27,4 +27,7 @@ phpunit.phar
/phpunit.xml

tests/_output/*
tests/_support/_generated
tests/_support/_generated

#vagrant folder
/.vagrant
79 changes: 79 additions & 0 deletions Vagrantfile
@@ -0,0 +1,79 @@
require 'yaml'
require 'fileutils'

required_plugins = %w( vagrant-hostmanager vagrant-vbguest )
required_plugins.each do |plugin|
exec "vagrant plugin install #{plugin}" unless Vagrant.has_plugin? plugin
end

domains = {
app: 'yii2basic.dev'
}

config = {
local: './vagrant/config/vagrant-local.yml',
example: './vagrant/config/vagrant-local.example.yml'
}

# copy config from example if local config not exists
FileUtils.cp config[:example], config[:local] unless File.exist?(config[:local])
# read config
options = YAML.load_file config[:local]

# check github token
if options['github_token'].nil? || options['github_token'].to_s.length != 40
puts "You must place REAL GitHub token into configuration:\n/yii2-app-basic/vagrant/config/vagrant-local.yml"
exit
end

# vagrant configurate
Vagrant.configure(2) do |config|
# select the box
config.vm.box = 'bento/ubuntu-16.04'

# should we ask about box updates?
config.vm.box_check_update = options['box_check_update']

config.vm.provider 'virtualbox' do |vb|
# machine cpus count
vb.cpus = options['cpus']
# machine memory size
vb.memory = options['memory']
# machine name (for VirtualBox UI)
vb.name = options['machine_name']
end

# machine name (for vagrant console)
config.vm.define options['machine_name']

# machine name (for guest machine console)
config.vm.hostname = options['machine_name']

# network settings
config.vm.network 'private_network', ip: options['ip']

# sync: folder 'yii2-app-advanced' (host machine) -> folder '/app' (guest machine)
config.vm.synced_folder './', '/app', owner: 'vagrant', group: 'vagrant'

# disable folder '/vagrant' (guest machine)
config.vm.synced_folder '.', '/vagrant', disabled: true

# hosts settings (host machine)
config.vm.provision :hostmanager
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = true
config.hostmanager.aliases = domains.values

# quick fix for failed guest additions installations
# config.vbguest.auto_update = false

# provisioners
config.vm.provision 'shell', path: './vagrant/provision/once-as-root.sh', args: [options['timezone']]
config.vm.provision 'shell', path: './vagrant/provision/once-as-vagrant.sh', args: [options['github_token']], privileged: false
config.vm.provision 'shell', path: './vagrant/provision/always-as-root.sh', run: 'always'

# post-install message (vagrant console)
config.vm.post_up_message = "App URL: http://#{domains[:app]}"
end
2 changes: 2 additions & 0 deletions vagrant/config/.gitignore
@@ -0,0 +1,2 @@
# local configuration
vagrant-local.yml
22 changes: 22 additions & 0 deletions vagrant/config/vagrant-local-example.yml
@@ -0,0 +1,22 @@
# Your personal GitHub token
github_token: <your-personal-github-token>
# Read more: https://github.com/blog/1509-personal-api-tokens
# You can generate it here: https://github.com/settings/tokens

# Guest OS timezone
timezone: Europe/London

# Are we need check box updates for every 'vagrant up'?
box_check_update: false

# Virtual machine name
machine_name: yii2basic

# Virtual machine IP
ip: 192.168.83.137

# Virtual machine CPU cores number
cpus: 1

# Virtual machine RAM
memory: 1024
38 changes: 38 additions & 0 deletions vagrant/nginx/app.conf
@@ -0,0 +1,38 @@
server {
charset utf-8;
client_max_body_size 128M;
sendfile off;

listen 80; ## listen for ipv4
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6

server_name yii2basic.dev;
root /app/web/;
index index.php;

access_log /app/vagrant/nginx/log/yii2basic.access.log;
error_log /app/vagrant/nginx/log/yii2basic.error.log;

location / {
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php$is_args$args;
}

# uncomment to avoid processing of calls to non-existing static files by Yii
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
# try_files $uri =404;
#}
#error_page 404 /404.html;

location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
try_files $uri =404;
}

location ~ /\.(ht|svn|git) {
deny all;
}
}
3 changes: 3 additions & 0 deletions vagrant/nginx/log/.gitignore
@@ -0,0 +1,3 @@
#nginx logs
yii2basic.access.log
yii2basic.error.log
18 changes: 18 additions & 0 deletions vagrant/provision/always-as-root.sh
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

#== Bash helpers ==

function info {
echo " "
echo "--> $1"
echo " "
}

#== Provision script ==

info "Provision-script user: `whoami`"

info "Restart web-stack"
service php7.0-fpm restart
service nginx restart
service mysql restart
71 changes: 71 additions & 0 deletions vagrant/provision/once-as-root.sh
@@ -0,0 +1,71 @@
#!/usr/bin/env bash

#== Import script args ==

timezone=$(echo "$1")

#== Bash helpers ==

function info {
echo " "
echo "--> $1"
echo " "
}

#== Provision script ==

info "Provision-script user: `whoami`"

export DEBIAN_FRONTEND=noninteractive

info "Configure timezone"
timedatectl set-timezone ${timezone} --no-ask-password

info "Prepare root password for MySQL"
debconf-set-selections <<< "mariadb-server-10.0 mysql-server/root_password password \"''\""
debconf-set-selections <<< "mariadb-server-10.0 mysql-server/root_password_again password \"''\""
echo "Done!"
info "Update OS software"
apt-get update
apt-get upgrade -y
info "Install additional software"
apt-get install -y php7.0-curl php7.0-cli php7.0-intl php7.0-mysqlnd php7.0-gd php7.0-fpm php7.0-mbstring php7.0-xml unzip nginx mariadb-server-10.0 php.xdebug
info "Configure MySQL"
sed -i "s/.*bind-address.*/bind-address = 0.0.0.0/" /etc/mysql/mariadb.conf.d/50-server.cnf
mysql -uroot <<< "CREATE USER 'root'@'%' IDENTIFIED BY ''"
mysql -uroot <<< "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'"
mysql -uroot <<< "DROP USER 'root'@'localhost'"
mysql -uroot <<< "FLUSH PRIVILEGES"
echo "Done!"
info "Configure PHP-FPM"
sed -i 's/user = www-data/user = vagrant/g' /etc/php/7.0/fpm/pool.d/www.conf
sed -i 's/group = www-data/group = vagrant/g' /etc/php/7.0/fpm/pool.d/www.conf
sed -i 's/owner = www-data/owner = vagrant/g' /etc/php/7.0/fpm/pool.d/www.conf
cat << EOF > /etc/php/7.0/mods-available/xdebug.ini
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_connect_back=1
xdebug.remote_port=9000
xdebug.remote_autostart=1
EOF
echo "Done!"
info "Configure NGINX"
sed -i 's/user www-data/user vagrant/g' /etc/nginx/nginx.conf
echo "Done!"
info "Enabling site configuration"
ln -s /app/vagrant/nginx/app.conf /etc/nginx/sites-enabled/app.conf
echo "Done!"
info "Initailize databases for MySQL"
mysql -uroot <<< "CREATE DATABASE yii2basic"
mysql -uroot <<< "CREATE DATABASE yii2basic_test"
echo "Done!"
info "Install composer"
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
31 changes: 31 additions & 0 deletions vagrant/provision/once-as-vagrant.sh
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

#== Import script args ==

github_token=$(echo "$1")

#== Bash helpers ==

function info {
echo " "
echo "--> $1"
echo " "
}

#== Provision script ==

info "Provision-script user: `whoami`"

info "Configure composer"
composer config --global github-oauth.github.com ${github_token}
echo "Done!"

info "Install project dependencies"
cd /app
composer --no-progress --prefer-dist install

info "Create bash-alias 'app' for vagrant user"
echo 'alias app="cd /app"' | tee /home/vagrant/.bash_aliases

info "Enabling colorized prompt for guest console"
sed -i "s/#force_color_prompt=yes/force_color_prompt=yes/" /home/vagrant/.bashrc

0 comments on commit 33882b1

Please sign in to comment.