Last active
June 26, 2020 10:06
-
-
Save akrabat/1d74f0b6ad04b2638178ea3f96cec3eb to your computer and use it in GitHub Desktop.
Vagrantfile starting point for PHP/MySQLn
This file contains hidden or 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
Vagrant.configure("2") do |config| | |
config.vm.box = "ubuntu/focal64" | |
# Shared ports | |
# config.vm.network "forwarded_port", host:8888, guest: 80 | |
# config.vm.network "forwarded_port", host:3307, guest: 3306 | |
# Mount shared folder using NFS | |
# config.vm.synced_folder ".", "/vagrant", | |
# id: "core", | |
# :nfs => true, | |
# :mount_options => ['nolock,vers=3,udp,noatime'] | |
# Do some network configuration | |
config.vm.network "private_network", ip: "192.168.99.250" | |
config.vm.provider :virtualbox do |vb| | |
vb.customize ["modifyvm", :id, "--memory", 1024] | |
vb.customize ["modifyvm", :id, "--cpus", 2] | |
vb.customize ["modifyvm", :id, "--uartmode1", "disconnected" ] # no log file | |
end | |
# Prevents "stdin: is not a tty" on Ubuntu (https://github.com/mitchellh/vagrant/issues/1673) | |
config.vm.provision "fix-no-tty", type: "shell" do |s| | |
s.privileged = false | |
s.inline = "sudo sed -i '/tty/!s/mesg n/tty -s \\&\\& mesg n/' /root/.profile" | |
end | |
# install and configure the system as root user | |
config.vm.provision 'shell', privileged: true, inline: <<SHELL | |
############################################################################### | |
echo "========== Update packages ==========" | |
export DEBIAN_FRONTEND=noninteractive | |
add-apt-repository -y ppa:ondrej/php | |
# add-apt-repository -y ppa:ubuntuhandbook1/apps | |
apt-get -y update | |
apt-get install -qq software-properties-common &> /dev/null || exit 1 | |
apt-get -y update | |
############################################################################### | |
echo "========== Installing baseline bits ==========" | |
apt-get -q -y install vim autojump | |
############################################################################### | |
echo "========== Prevent outbound connections to most ports ==========" | |
# allow DNS, SSH, HTTP and HTTPS outbound. This will deny any potential outbout email too | |
sudo ufw default allow incoming | |
sudo ufw default deny outgoing | |
sudo ufw allow out 53 # dns | |
sudo ufw allow out 22 # ssh | |
sudo ufw allow out 80 # http | |
sudo ufw allow out 443 # https | |
sudo ufw enable | |
############################################################################### | |
echo "========== Installing PHP & MySQL Packages ==========" | |
apt-get -q -y install mysql-server mysql-client \ | |
apache2 \ | |
php7.4 php-mbstring php-pear php7.4-dev php7.4-xml libapache2-mod-php7.4 \ | |
php7.4-opcache php7.4-cli php7.4-curl php7.4-gd php-xdebug \ | |
php7.4-mysql php7.4-pdo-mysql \ | |
re2c gcc g++ install php-ast | |
############################################################################### | |
echo "========== Configure PHP ==========" | |
sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php/7.4/apache2/php.ini | |
sed -i "s/display_errors = .*/display_errors = On/" /etc/php/7.4/apache2/php.ini | |
sed -i "s/html_errors = .*/html_errors = On/" /etc/php/7.4/apache2/php.ini | |
sed -i "s/;date.timezone =.*/date.timezone = \\\"Europe\\/London\\\"/" /etc/php/7.4/apache2/php.ini | |
sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php/7.4/cli/php.ini | |
sed -i "s/display_errors = .*/display_errors = On/" /etc/php/7.4/cli/php.ini | |
sed -i "s/html_errors = .*/html_errors = Off/" /etc/php/7.4/cli/php.ini | |
sed -i "s/;date.timezone =.*/date.timezone = \\\"Europe\\/London\\\"/" /etc/php/7.4/cli/php.ini | |
if [ $(grep -c "overload_var_dump" /etc/php/7.4/mods-available/xdebug.ini) -eq 0 ]; then | |
cat <<- 'EOF' >> /etc/php/7.4/mods-available/xdebug.ini | |
xdebug.overload_var_dump = 2 | |
xdebug.scream = 0 | |
xdebug.max_nesting_level = 1000 | |
xdebug.var_display_max_children = -1 | |
xdebug.var_display_max_data = -1 | |
xdebug.var_display_max_depth = -1 | |
xdebug.collect_params = 0 | |
xdebug.remote_enable = on | |
xdebug.remote_connect_back = off | |
xdebug.profiler_enable_trigger = off | |
xdebug.remote_host = 10.0.2.2 | |
xdebug.idekey = XDEBUG | |
EOF | |
fi | |
############################################################################### | |
echo "========== Configure Apache ==========" | |
a2enmod rewrite | |
# Need to tell Apache to use the vagrant user so that it can access /vagrant | |
sed -i "s/APACHE_RUN_USER=.*/APACHE_RUN_USER=vagrant/" /etc/apache2/envvars | |
sed -i "s/APACHE_RUN_GROUP=.*/APACHE_RUN_GROUP=vagrant/" /etc/apache2/envvars | |
echo "========== Update VirtualHost ==========" | |
sed -i "s/DocumentRoot .*/DocumentRoot \\/vagrant\\/checkout_of_website_repo/" /etc/apache2/sites-available/000-default.conf | |
sed -i 's/<\\/VirtualHost>/ <Directory "\\/vagrant\\/checkout_of_website_repo">\\ | |
Options Indexes FollowSymLinks MultiViews\\ | |
AllowOverride All\\ | |
Order allow,deny\\ | |
Allow from all\\ | |
Require all granted\\ | |
\\ | |
# RewriteEngine On\\ | |
# RewriteBase \\/\\ | |
# RewriteCond %{REQUEST_FILENAME} -s [OR]\\ | |
# RewriteCond %{REQUEST_FILENAME} -l [OR]\\ | |
# RewriteCond %{REQUEST_FILENAME} -d\\ | |
# RewriteRule ^.*$ - [NC,L]\\ | |
# RewriteRule ^.*$ index.php [NC,L]\\ | |
<\\/Directory>\\ | |
<\\/VirtualHost>/' /etc/apache2/sites-available/000-default.conf | |
############################################################################### | |
echo "========== Configure Mysql ==========" | |
# database: 19ft, username: 19ft, password: 19ft | |
DB_NAME=19ft | |
DB_USER=19ft | |
DB_PWD=19ft | |
mysql -u root -e "CREATE DATABASE $DB_NAME CHARACTER SET utf8 COLLATE utf8_general_ci"; | |
mysql -u root -e "CREATE USER '$DB_USER'@'%' IDENTIFIED BY '$DB_PWD'"; | |
mysql -u root -e "GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'%'" | |
# Allow anyone to connect to MySQL - don't do this in production! | |
sed -i "s/^bind-address.*/bind-address = 0.0.0.0/" /etc/mysql/mysql.conf.d/mysqld.cnf | |
# set max_allowed_packet to a big number if required | |
#sed -i "s/^max_allowed_packet.*/max_allowed_packet = 1073741824/" /etc/mysql/mysql.conf.d/mysqld.cnf | |
service mysql restart | |
# Restore initial database dump if we have one in db directory | |
SEED_DB=19ft_database.sql | |
if [ -e /vagrant/db/$SEED_DB ]; then | |
echo "========== Restore $SEED_DB to MySQL ==========" | |
mysql -u root $DB_NAME < /vagrant/db/$SEED_DB | |
fi | |
############################################################################### | |
echo "========== Restart Apache ==========" | |
service apache2 restart | |
SHELL | |
############################################################################### | |
# Configure "vagrant" user's environment | |
config.vm.provision 'shell', privileged: false, inline: <<SHELL | |
echo "========== Update vagrant user's /.bashrc ==========" | |
if [ $(grep -c "autojump" .bashrc) -eq 0 ]; then | |
cat <<- 'EOF' >> .bashrc | |
# Enable autojump | |
source /usr/share/autojump/autojump.bash | |
# Map up & down to history search once a command has been started. | |
bind '"\e[A":history-search-backward' | |
bind '"\e[B":history-search-forward' | |
cd /vagrant | |
EOF | |
source .bashrc | |
fi | |
############################################################################### | |
# Finished | |
echo "" | |
echo "Done" | |
SHELL | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment