Skip to content

Commit 33882b1

Browse files
cgrssamdark
authored andcommitted
Adapted yii2-app-advanced Vagrant setup to basic app (yiisoft#161)
1 parent fc82ba4 commit 33882b1

File tree

9 files changed

+268
-1
lines changed

9 files changed

+268
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ phpunit.phar
2727
/phpunit.xml
2828

2929
tests/_output/*
30-
tests/_support/_generated
30+
tests/_support/_generated
31+
32+
#vagrant folder
33+
/.vagrant

Vagrantfile

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
require 'yaml'
2+
require 'fileutils'
3+
4+
required_plugins = %w( vagrant-hostmanager vagrant-vbguest )
5+
required_plugins.each do |plugin|
6+
exec "vagrant plugin install #{plugin}" unless Vagrant.has_plugin? plugin
7+
end
8+
9+
domains = {
10+
app: 'yii2basic.dev'
11+
}
12+
13+
config = {
14+
local: './vagrant/config/vagrant-local.yml',
15+
example: './vagrant/config/vagrant-local.example.yml'
16+
}
17+
18+
# copy config from example if local config not exists
19+
FileUtils.cp config[:example], config[:local] unless File.exist?(config[:local])
20+
# read config
21+
options = YAML.load_file config[:local]
22+
23+
# check github token
24+
if options['github_token'].nil? || options['github_token'].to_s.length != 40
25+
puts "You must place REAL GitHub token into configuration:\n/yii2-app-basic/vagrant/config/vagrant-local.yml"
26+
exit
27+
end
28+
29+
# vagrant configurate
30+
Vagrant.configure(2) do |config|
31+
# select the box
32+
config.vm.box = 'bento/ubuntu-16.04'
33+
34+
# should we ask about box updates?
35+
config.vm.box_check_update = options['box_check_update']
36+
37+
config.vm.provider 'virtualbox' do |vb|
38+
# machine cpus count
39+
vb.cpus = options['cpus']
40+
# machine memory size
41+
vb.memory = options['memory']
42+
# machine name (for VirtualBox UI)
43+
vb.name = options['machine_name']
44+
end
45+
46+
# machine name (for vagrant console)
47+
config.vm.define options['machine_name']
48+
49+
# machine name (for guest machine console)
50+
config.vm.hostname = options['machine_name']
51+
52+
# network settings
53+
config.vm.network 'private_network', ip: options['ip']
54+
55+
# sync: folder 'yii2-app-advanced' (host machine) -> folder '/app' (guest machine)
56+
config.vm.synced_folder './', '/app', owner: 'vagrant', group: 'vagrant'
57+
58+
# disable folder '/vagrant' (guest machine)
59+
config.vm.synced_folder '.', '/vagrant', disabled: true
60+
61+
# hosts settings (host machine)
62+
config.vm.provision :hostmanager
63+
config.hostmanager.enabled = true
64+
config.hostmanager.manage_host = true
65+
config.hostmanager.ignore_private_ip = false
66+
config.hostmanager.include_offline = true
67+
config.hostmanager.aliases = domains.values
68+
69+
# quick fix for failed guest additions installations
70+
# config.vbguest.auto_update = false
71+
72+
# provisioners
73+
config.vm.provision 'shell', path: './vagrant/provision/once-as-root.sh', args: [options['timezone']]
74+
config.vm.provision 'shell', path: './vagrant/provision/once-as-vagrant.sh', args: [options['github_token']], privileged: false
75+
config.vm.provision 'shell', path: './vagrant/provision/always-as-root.sh', run: 'always'
76+
77+
# post-install message (vagrant console)
78+
config.vm.post_up_message = "App URL: http://#{domains[:app]}"
79+
end

vagrant/config/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# local configuration
2+
vagrant-local.yml
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Your personal GitHub token
2+
github_token: <your-personal-github-token>
3+
# Read more: https://github.com/blog/1509-personal-api-tokens
4+
# You can generate it here: https://github.com/settings/tokens
5+
6+
# Guest OS timezone
7+
timezone: Europe/London
8+
9+
# Are we need check box updates for every 'vagrant up'?
10+
box_check_update: false
11+
12+
# Virtual machine name
13+
machine_name: yii2basic
14+
15+
# Virtual machine IP
16+
ip: 192.168.83.137
17+
18+
# Virtual machine CPU cores number
19+
cpus: 1
20+
21+
# Virtual machine RAM
22+
memory: 1024

vagrant/nginx/app.conf

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
server {
2+
charset utf-8;
3+
client_max_body_size 128M;
4+
sendfile off;
5+
6+
listen 80; ## listen for ipv4
7+
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6
8+
9+
server_name yii2basic.dev;
10+
root /app/web/;
11+
index index.php;
12+
13+
access_log /app/vagrant/nginx/log/yii2basic.access.log;
14+
error_log /app/vagrant/nginx/log/yii2basic.error.log;
15+
16+
location / {
17+
# Redirect everything that isn't a real file to index.php
18+
try_files $uri $uri/ /index.php$is_args$args;
19+
}
20+
21+
# uncomment to avoid processing of calls to non-existing static files by Yii
22+
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
23+
# try_files $uri =404;
24+
#}
25+
#error_page 404 /404.html;
26+
27+
location ~ \.php$ {
28+
include fastcgi_params;
29+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
30+
#fastcgi_pass 127.0.0.1:9000;
31+
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
32+
try_files $uri =404;
33+
}
34+
35+
location ~ /\.(ht|svn|git) {
36+
deny all;
37+
}
38+
}

vagrant/nginx/log/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#nginx logs
2+
yii2basic.access.log
3+
yii2basic.error.log
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
#== Bash helpers ==
4+
5+
function info {
6+
echo " "
7+
echo "--> $1"
8+
echo " "
9+
}
10+
11+
#== Provision script ==
12+
13+
info "Provision-script user: `whoami`"
14+
15+
info "Restart web-stack"
16+
service php7.0-fpm restart
17+
service nginx restart
18+
service mysql restart

vagrant/provision/once-as-root.sh

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env bash
2+
3+
#== Import script args ==
4+
5+
timezone=$(echo "$1")
6+
7+
#== Bash helpers ==
8+
9+
function info {
10+
echo " "
11+
echo "--> $1"
12+
echo " "
13+
}
14+
15+
#== Provision script ==
16+
17+
info "Provision-script user: `whoami`"
18+
19+
export DEBIAN_FRONTEND=noninteractive
20+
21+
info "Configure timezone"
22+
timedatectl set-timezone ${timezone} --no-ask-password
23+
24+
info "Prepare root password for MySQL"
25+
debconf-set-selections <<< "mariadb-server-10.0 mysql-server/root_password password \"''\""
26+
debconf-set-selections <<< "mariadb-server-10.0 mysql-server/root_password_again password \"''\""
27+
echo "Done!"
28+
29+
info "Update OS software"
30+
apt-get update
31+
apt-get upgrade -y
32+
33+
info "Install additional software"
34+
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
35+
36+
info "Configure MySQL"
37+
sed -i "s/.*bind-address.*/bind-address = 0.0.0.0/" /etc/mysql/mariadb.conf.d/50-server.cnf
38+
mysql -uroot <<< "CREATE USER 'root'@'%' IDENTIFIED BY ''"
39+
mysql -uroot <<< "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'"
40+
mysql -uroot <<< "DROP USER 'root'@'localhost'"
41+
mysql -uroot <<< "FLUSH PRIVILEGES"
42+
echo "Done!"
43+
44+
info "Configure PHP-FPM"
45+
sed -i 's/user = www-data/user = vagrant/g' /etc/php/7.0/fpm/pool.d/www.conf
46+
sed -i 's/group = www-data/group = vagrant/g' /etc/php/7.0/fpm/pool.d/www.conf
47+
sed -i 's/owner = www-data/owner = vagrant/g' /etc/php/7.0/fpm/pool.d/www.conf
48+
cat << EOF > /etc/php/7.0/mods-available/xdebug.ini
49+
zend_extension=xdebug.so
50+
xdebug.remote_enable=1
51+
xdebug.remote_connect_back=1
52+
xdebug.remote_port=9000
53+
xdebug.remote_autostart=1
54+
EOF
55+
echo "Done!"
56+
57+
info "Configure NGINX"
58+
sed -i 's/user www-data/user vagrant/g' /etc/nginx/nginx.conf
59+
echo "Done!"
60+
61+
info "Enabling site configuration"
62+
ln -s /app/vagrant/nginx/app.conf /etc/nginx/sites-enabled/app.conf
63+
echo "Done!"
64+
65+
info "Initailize databases for MySQL"
66+
mysql -uroot <<< "CREATE DATABASE yii2basic"
67+
mysql -uroot <<< "CREATE DATABASE yii2basic_test"
68+
echo "Done!"
69+
70+
info "Install composer"
71+
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
3+
#== Import script args ==
4+
5+
github_token=$(echo "$1")
6+
7+
#== Bash helpers ==
8+
9+
function info {
10+
echo " "
11+
echo "--> $1"
12+
echo " "
13+
}
14+
15+
#== Provision script ==
16+
17+
info "Provision-script user: `whoami`"
18+
19+
info "Configure composer"
20+
composer config --global github-oauth.github.com ${github_token}
21+
echo "Done!"
22+
23+
info "Install project dependencies"
24+
cd /app
25+
composer --no-progress --prefer-dist install
26+
27+
info "Create bash-alias 'app' for vagrant user"
28+
echo 'alias app="cd /app"' | tee /home/vagrant/.bash_aliases
29+
30+
info "Enabling colorized prompt for guest console"
31+
sed -i "s/#force_color_prompt=yes/force_color_prompt=yes/" /home/vagrant/.bashrc

0 commit comments

Comments
 (0)