3

I need assistance putting together a Vagrantfile.

I am trying to setup a virtual machine on my Windows desktop for working on an existing PHP/MySQL application. I've been instructed to use Vagrant and VirtualBox. I've been going through the documentation for Vagrant and found this to be over my head with a lot of information out of date. I have some background in general Linux usage, but none in setting up LAMP servers on them.

What I have:

  • Vagrant 2.1.2
  • VirtualBox 5.2.18

Things I need in the VM:

  • Centos 6
  • Apache
  • MySQL 5.5
  • PHP 5.6
  • MySQLi/Mysqlnd (PDO optional)
  • Curl
  • DOM/SimpleXML
  • Any PHP extensions needed for a typical PHP application
Tanoro
  • 871
  • 2
  • 10
  • 30
  • I can help you but what are your problem exactly? – Mohamed El Mrabet Sep 14 '18 at 14:53
  • My problem is a lack of qualifications to do this. I've tried to go through the Vagrant docs and read through some guides. This is simply above my head. If I had a reasonably functional Vagrantfile with the above specifications to get me going, I would probably figure out the intricate settings I need. It's just too large a task for me. – Tanoro Sep 17 '18 at 21:03

1 Answers1

0

It has taken a few weeks and a lot of wading through out of date tutorials, but I got it done. I am sharing so others on the LAMP service stack have a place to start in crafting their vagrantfile. Note that this was not done for elegance. It is a quick starter that is easy-ish to understand and adjust to one's needs.

A few small deviations of note were made from the above original post:

  • I went with Ubuntu/bionic64 instead of Centos 6, mostly because of the abundance of tutorial material I was able to find. Modifying this file for CentOS 6 shouldn't be too hard. CentOS uses Yum instead of Apt-get for package management. I'm not entirely certain what else is different.
  • I went with PHP 7.2 instead of 5.6.
  • I found PHP 7.2 comes with MySQLi and the native driver already installed out of the box.

Working vagrantfile and shell bootstrapper included, heavily commented for comprehension: https://pastebin.com/Eqvhq8KZ

# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = '2'

#########################################################################
# VM Setup for LAMP stack application
#
# - Install PHP and packages
# - Install Apache
# - Configure /vagrant as document root
# - Config PHP for development and logging
# - Install MySQL
# - Setup database and permissions (username and password are "vagrant")
# - Install Composer
#########################################################################

@script = <<SCRIPT

#################### PHP ####################
apt-get install -y apt-utils php7.2 php7.2-bcmath php7.2-bz2 php7.2-cli php7.2-curl php7.2-intl php7.2-json php7.2-mbstring php7.2-opcache php7.2-soap php7.2-sqlite3 php7.2-xml php7.2-xsl php7.2-zip unzip

#################### APACHE2 ####################
apt-get install -y apache2 libapache2-mod-php7.2

# Remove /var/www default
rm -rf /var/www

# Symlink /vagrant to /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 "/vagrant"
    ServerName localhost
    <Directory "/vagrant">
        Options Indexes FollowSymLinks MultiViews Includes
        AllowOverride All
        Require all granted
        Order allow,deny
        Allow from all
        AddType text/html .shtm .shtml
        AddOutputFilter INCLUDES .htm .html .shtm .shtml
    </Directory>
</VirtualHost>
EOF
)
echo "${VHOST}" > /etc/apache2/sites-enabled/000-default.conf

# Enable mod_rewrite
a2enmod rewrite

# Put PHP into development configuration
mv /etc/php/7.2/apache2/php.ini /etc/php/7.2/apache2/php.ini.back
cp /usr/lib/php/7.2/php.ini-development /etc/php/7.2/apache2/php.ini

# Enable PHP extensions in php.ini
#sed -i 's/;extension=mysqli/extension=mysqli/' /etc/php/7.2/apache2/php.ini

# PHP will log its errors in a /log/error_log file
sed -i 's:;error_log = php_errors.log:error_log = /vagrant/log/error_log:' /etc/php/7.2/apache2/php.ini

# Restart apache
systemctl restart apache2.service

#################### MYSQL ####################
apt-get install -y mysql-server mysql-client-core-5.7 php7.2-mysql

systemctl start mysql.service

# Reset root password
#/usr/bin/mysqladmin -u root password 'root'
mysqladmin -u root password 'root'

# Setup database from root user and setup the application user
mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS app"
mysql -uroot -proot app < /vagrant/db/schema.sql
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON *.* to 'vagrant'@'localhost' IDENTIFIED BY 'vagrant'"
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON *.* to 'vagrant'@'%' IDENTIFIED BY 'vagrant'"
mysql -uroot -proot -e "FLUSH PRIVILEGES"

# Allow remote connections for MySQL Workbench
MYSQLCONF=$(cat <<EOF

[mysqld]
bind-address        = 0.0.0.0
EOF
)
echo "${MYSQLCONF}" >> /etc/mysql/my.cnf

# Restart mysql
/etc/init.d/mysql restart

#################### COMPOSER ####################
curl -sS http://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

# Run composer install
cd /vagrant && composer install

#################### FINISHED! ####################
echo "** [PHP] Visit http://localhost:8080 in your browser for to view the application **"
SCRIPT


Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.vm.box = 'ubuntu/bionic64'

    config.ssh.insert_key = false

    config.vm.network "forwarded_port", guest: 80, host: 8080
    config.vm.network "forwarded_port", guest: 443, host: 8081
    config.vm.network "forwarded_port", guest: 3306, host: 3307

    #config.vm.synced_folder '.', '/var/www/html'

    # Provision runs only on the first "Vangrant up" command
    config.vm.provision 'shell', privileged: true, inline: @script

    config.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--memory", "2048"]
        vb.customize ['modifyvm', :id, "--natdnshostresolver1", "on"]
    end
end
Tanoro
  • 871
  • 2
  • 10
  • 30