Skip to content

Instantly share code, notes, and snippets.

@ricardocanelas
Last active September 17, 2020 05:45
Show Gist options
  • Save ricardocanelas/3ab28ba0a33a218f927cc9e39eb0effe to your computer and use it in GitHub Desktop.
Save ricardocanelas/3ab28ba0a33a218f927cc9e39eb0effe to your computer and use it in GitHub Desktop.
Vagrant / PHP7.1 + MySQL5.6 + Apache
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.network "private_network", ip: "192.168.100.100"
config.vm.synced_folder "./www", "/var/www/", :nfs => { :mount_options => ["dmode=777","fmode=666"] }
# If you have trouble with NFS above, comment it out and use the following instead
# config.vm.synced_folder "./www", "/var/www/", :mount_options => ["dmode=777", "fmode=666"]
# config.vm.synced_folder "./www", "/var/www/", :owner=> 'www-data', :group=>'root'
config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.cpus = 1
end
config.ssh.insert_key = false
config.vm.provision :shell, keep_color: true, path: "Vagrant.provision.sh"
end
#filename: Vagrantfile.provision.sh
#!/usr/bin/env bash
###########################################
# by Ricardo Canelas #
# https://gist.github.com/ricardocanelas #
#-----------------------------------------#
# + Apache #
# + PHP 7.1 #
# + MySQL 5.6 or MariaDB 10.1 #
# + NodeJs, Git, Composer, etc... #
###########################################
# ---------------------------------------------------------------------------------------------------------------------
# Variables & Functions
# ---------------------------------------------------------------------------------------------------------------------
APP_DATABASE_NAME='app'
echoTitle () {
echo -e "\033[0;30m\033[42m -- $1 -- \033[0m"
}
# ---------------------------------------------------------------------------------------------------------------------
echoTitle 'Virtual Machine Setup'
# ---------------------------------------------------------------------------------------------------------------------
# Update packages
apt-get update -qq
apt-get -y install git curl vim
# ---------------------------------------------------------------------------------------------------------------------
echoTitle 'Installing and Setting: Apache'
# ---------------------------------------------------------------------------------------------------------------------
# Install packages
apt-get install -y apache2 libapache2-mod-fastcgi apache2-mpm-worker
# linking Vagrant directory to Apache 2.4 public directory
# rm -rf /var/www
# ln -fs /vagrant /var/www
# Add ServerName to httpd.conf
echo "ServerName localhost" > /etc/apache2/httpd.conf
# Setup hosts file
VHOST=$(cat <<EOF
<VirtualHost *:80>
DocumentRoot "/var/www/app/public"
ServerName app.dev
ServerAlias app.dev
<Directory "/var/www/app/public">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
EOF
)
echo "${VHOST}" > /etc/apache2/sites-enabled/000-default.conf
# Loading needed modules to make apache work
a2enmod actions fastcgi rewrite
sudo service apache2 restart
# ---------------------------------------------------------------------------------------------------------------------
# echoTitle 'MYSQL-Database'
# ---------------------------------------------------------------------------------------------------------------------
# Setting MySQL (username: root) ~ (password: password)
sudo debconf-set-selections <<< 'mysql-server-5.6 mysql-server/root_password password password'
sudo debconf-set-selections <<< 'mysql-server-5.6 mysql-server/root_password_again password password'
# Installing packages
apt-get install -y mysql-server-5.6 mysql-client-5.6 mysql-common-5.6
# Setup database
mysql -uroot -ppassword -e "CREATE DATABASE IF NOT EXISTS $APP_DATABASE_NAME;";
mysql -uroot -ppassword -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password';"
mysql -uroot -ppassword -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'password';"
sudo service mysql restart
# Import SQL file
# mysql -uroot -ppassword database < my_database.sql
# ---------------------------------------------------------------------------------------------------------------------
# echoTitle 'Maria-Database'
# ---------------------------------------------------------------------------------------------------------------------
# Remove MySQL if installed
# sudo service mysql stop
# apt-get remove --purge mysql-server-5.6 mysql-client-5.6 mysql-common-5.6
# apt-get autoremove
# apt-get autoclean
# rm -rf /var/lib/mysql/
# rm -rf /etc/mysql/
# Install MariaDB
# export DEBIAN_FRONTEND=noninteractive
# debconf-set-selections <<< 'mariadb-server-10.0 mysql-server/root_password password root'
# debconf-set-selections <<< 'mariadb-server-10.0 mysql-server/root_password_again password root'
# apt-get install -y mariadb-server
# Set MariaDB root user password and persmissions
# mysql -u root -proot -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; FLUSH PRIVILEGES;"
# Open MariaDB to be used with Sequel Pro
# sed -i 's|127.0.0.1|0.0.0.0|g' /etc/mysql/my.cnf
# Restart MariaDB
# sudo service mysql restart
# ---------------------------------------------------------------------------------------------------------------------
echoTitle 'Installing: PHP'
# ---------------------------------------------------------------------------------------------------------------------
# Add repository
add-apt-repository ppa:ondrej/php
apt-get update
apt-get install -y python-software-properties software-properties-common
# Remove PHP5
# apt-get purge php5-fpm -y
# apt-get --purge autoremove -y
# Install packages
apt-get install -y php7.1 php7.1-fpm
apt-get install -y php7.1-mysql
apt-get install -y mcrypt php7.1-mcrypt
apt-get install -y php7.1-cli php7.1-curl php7.1-mbstring php7.1-xml php7.1-mysql
apt-get install -y php7.1-json php7.1-cgi php7.1-gd php-imagick php7.1-bz2 php7.1-zip
# ---------------------------------------------------------------------------------------------------------------------
echoTitle 'Setting: PHP with Apache'
# ---------------------------------------------------------------------------------------------------------------------
apt-get install -y libapache2-mod-php7.1
# Enable php modules
# php71enmod mcrypt (error)
# Trigger changes in apache
a2enconf php7.1-fpm
sudo service apache2 reload
# Packages Available:
# apt-cache search php7-*
# ---------------------------------------------------------------------------------------------------------------------
# echoTitle 'Installing & Setting: X-Debug'
# ---------------------------------------------------------------------------------------------------------------------
# cat << EOF | sudo tee -a /etc/php/7.1/mods-available/xdebug.ini
# xdebug.scream=1
# xdebug.cli_color=1
# xdebug.show_local_vars=1
# EOF
# ---------------------------------------------------------------------------------------------------------------------
# Others
# ---------------------------------------------------------------------------------------------------------------------
echoTitle 'Installing: Node 6 and update NPM'
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
apt-get install -y nodejs
npm install npm@latest -g
echoTitle 'Installing: Git'
apt-get install -y git
echoTitle 'Installing: Composer'
curl -s https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
# ---------------------------------------------------------------------------------------------------------------------
# Others
# ---------------------------------------------------------------------------------------------------------------------
# Output success message
echoTitle "Your machine has been provisioned"
echo "-------------------------------------------"
echo "MySQL is available on port 3306 with username 'root' and password 'password'"
echo "(you have to use 127.0.0.1 as opposed to 'localhost')"
echo "Apache is available on port 80"
echo -e "Head over to http://192.168.100.100 to get started"
@juandelossantos
Copy link

@ricardocanelas Muchas Gracias!

Funcionó bien, pero cuando fui a la URL me dio error 404 y es debido a que en el archivo Vagrant.provision.sh en las líneas 52 y 55 apunta a la carpeta app/public, mientras que en el VagrantFile apunta a config.vm.synced_folder "./www", "/var/www", :nfs => { :mount_options => ["dmode=777", "fmode=666"] }

Modifiqué el archivo de provision y está funcionando muy bien!!

@anisur3036
Copy link

anisur3036 commented Nov 13, 2017

Thanks for this bootstrap. But one thing that "libapache2-mod-fastcgi" this command not works for me. and one thing that u missing
sudo mkdir /var/www/app/public/

@radixs
Copy link

radixs commented Dec 16, 2017

I had a problem after running apt-get install -y mysql-server-5.6 mysql-client-5.6 mysql-common-5.6 - could not connect to the database due to the error ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) which got resolved after installing just apt-get install -y mysql-server.

I am not sure why the error occured, perhaps it is better to split the command or something, or just leave the mysql-server which is all you need for LAMP development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment