Last active
July 3, 2023 12:01
-
-
Save ChristianOellers/80f00761334437d34affc61108a85ba8 to your computer and use it in GitHub Desktop.
Symfony + Vagrant settings - VM configuration, aliases and snippets for speedy workflows.
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
#!/bin/bash | |
# Utility aliases to call from within running Vagrant box. Speed up fixing common, recurring issues during development. | |
# @todo - Adjust paths and commands as needed. | |
# TOC | |
# - Symfony | |
# - Logs | |
# - SYSTEM | |
# ---------------------------------------------------------------------------------------------------------------------------------- Symfony | |
## | |
# Run from server root | |
## | |
# Caches | |
alias sfcache="php /vagrant/bin/console cache:pool:clear cache.global_clearer;" | |
alias sfcache="php /vagrant/bin/console cache:clear --env=prod; sudo chown -R --preserve-root www-data:www-data /vagrant/var/cache/;" | |
# Logs | |
# 1. Log all errors, infos, deprecations, debug | |
# 2. Log important only (e.g. ignore deprecations) | |
alias sflogall="tail -f /vagrant/var/log/dev.log;" | |
alias sflogmin="tail -f /vagrant/var/log/dev.log | grep -Ev '_wdt|assets|Deprecated|doctrine.DEBUG';" | |
# Permissions | |
# - Useful after running rsync of files from server to Vagrant box, | |
# issues appear in certain occasions only | |
alias sfuser="sudo chown -R www-data:www-data /vagrant;" | |
alias sflog="sudo chmod -R 777 /vagrant/var/log/;" | |
alias sfperm="sfuser; sflog;" | |
# Fix - SQL mode | |
# - Might be relevant in rare issue cases, but can be fixed in `Vagrantfile`. | |
# - Use: Append arbitrary query, e.g.: "SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));" | |
#alias sfsql="sudo mysql -u root -e" | |
# ------------------------------------------------------------------------------------------------------------------------------------- Logs | |
# nginx | |
alias logng="tail -f /var/log/nginx/vagrant.dev.error.log;" | |
# Xdebug | |
alias logxd="tail -f /var/log/xdebug/xdebug.log;" | |
# ----------------------------------------------------------------------------------------------------------------------------------- SYSTEM | |
# Examples | |
alias cls="clear" |
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
#!/bin/bash | |
## | |
# How to use: | |
# - Run from Vagrantfile or inside the box, if the first option gives issues (sudo sh init.sh). | |
# - Adjust as needed, given example and stack might differ for other applications. | |
## | |
apt update -y | |
apt upgrade -y | |
# Server | |
apt install -y nginx | |
if ! [ -L /var/www/html ]; then | |
rm -rf /var/www/html | |
ln -fs /vagrant /var/www/html | |
fi | |
systemctl enable nginx.service | |
systemctl start nginx.service | |
# MySQL | |
apt install -y mysql-server | |
systemctl enable mysql.service | |
systemctl start mysql.service | |
# PHP packages | |
# Anything needed ... | |
#apt -y install php~ xdebug~ ... | |
# XDebug - Setup + Config. | |
# - https://gist.github.com/jamesstout/fd6eae2b2ba34e7f32ff3e1a179d55fe | |
# - https://github.com/Varying-Vagrant-Vagrants/VVV/issues/621 | |
# | |
# Checks | |
# - Gateway (remote_host) : route -nee | awk '{ print $2 }' | sed -n 3p | |
# - Added to PHP : php -i | grep xdebug.remote_host | |
# - Module loaded : php -m | grep -i xdebug | |
XDEBUG_CONFIG=" | |
zend_extension=xdebug.so | |
xdebug.remote_enable = 1 | |
xdebug.remote_connect_back = 1 | |
xdebug.remote_port = 9000 | |
xdebug.max_nesting_level = 512 | |
xdebug.remote_autostart = true | |
xdebug.remote_host = 10.0.2.2 | |
xdebug.remote_log = /var/log/xdebug/xdebug.log | |
" | |
sudo mkdir /var/log/xdebug | |
sudo mv /var/log/xdebug.log xdebug/ | |
sudo chmod 666 /var/log/xdebug/xdebug.log | |
# Adjust to own PHP version | |
#sudo bash -c "printf \"$XDEBUG_CONFIG\" > /etc/php/~/mods-available/xdebug.ini" | |
# PHP + nginx | |
#systemctl enable php~-fpm.service | |
#systemctl start php~-fpm.service | |
#systemctl restart nginx.service | |
# Composer | |
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" | |
php composer-setup.php --install-dir=/usr/bin --filename=composer | |
php -r "unlink('composer-setup.php');" | |
# Redis | |
apt install -y redis-server | |
systemctl enable redis-server.service | |
systemctl start redis-server.service |
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
#!/bin/bash | |
# Filter (Symfony) log messages | |
# - Include only 'example' messages (arbitrary example) | |
# - Exclude any deprecation, debug messages | |
tail -f var/log/dev.log | grep -E 'example' | grep -Eiv 'Deprecated|Debug'; | |
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 : | |
$ip = "192.168.1.1" | |
$memory = 2048 | |
$cpus = 4 | |
Vagrant.configure("2") do |config| | |
config.vm.box = "ubuntu/bionic64" | |
config.vm.network "private_network", ip: $ip | |
# Alternative type: "nfs" | |
config.vm.synced_folder ".", "/vagrant", type: "rsync", | |
rsync__chown: false, | |
rsync__rsync_ownership: false, | |
rsync__exclude: [".git/", ".idea/", ".vagrant/", ".vscode/", "node_modules/", "var/"] | |
config.vm.provider "virtualbox" do |vb| | |
vb.memory = $memory | |
vb.cpus = $cpus | |
end | |
# Optional: Custom library setup and OS config | |
# config.vm.provision "shell", path: "vagrant/init.sh" | |
# Optional: Copy custom config files into Vagrant (demo for aliases) | |
# - Adjust paths as needed (just an example) | |
#config.vm.provision "shell", run: "always", inline: "cp /vagrant/vagrant/vagrant.conf /etc/nginx/sites-enabled/" | |
config.vm.provision "shell", run: "always", inline: "cp /vagrant/vagrant/aliases ~/.bash_aliases" | |
config.vm.provision "shell", run: "always", inline: "source ~/.bashrc" | |
config.vm.provision "shell", run: "always", inline: "service nginx restart" | |
config.vm.provision "shell", run: "always", inline: "chown -R www-data:www-data /vagrant/" | |
# Fix SQL mode issue (can happen, rarely). | |
# Would need different escaping with Ruby! | |
$fixSqlMode = <<-SCRIPT | |
sudo mysql -u root -e "SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));" | |
SCRIPT | |
config.vm.provision "shell", run: "always", inline: $fixSqlMode | |
# For convenience | |
config.vm.provision "shell", run: "always", inline: "printf 'VAGRANT STARTED, view web app: http://" + $ip + "'" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment