Make a Vagrant box with Ubuntu 18.04 LAMP stack, plus configure it for development, plus install Python, Ruby, Rails, Node.js, Codeception, Java, Selenium, headless Chrome browser.
- Host: Linux or Mac.
- Guest: Ubuntu 18.04, Apache 2.4, MariaDB 10.3, PHP 7.2, Git 2.18.
In host terminal:
mkdir -p ~/vm && cd $_
git clone https://github.com/stemar/vagrant-ubuntu-18-04-tests-rails.git ubuntu-18-04-tests-rails
cd ~/vm/ubuntu-18-04-tests-rails
vagrant up --provision
vagrant ssh
- Use a clean Ubuntu 18.04 box available from Bento with 64GB HDD virtual space.
- Leave code and version control files physically outside the VM while virtually accessing them inside the VM.
- Use any GUI tool outside the VM to access data inside the VM.
- IDEs, browsers, database administration applications, Git clients
- Use
http://localhost:8002
in a browser outside the VM to access Apache inside the VM. - Use the same SSH keys inside and outside VM.
- Use the same Git config inside and outside VM.
- Have
Vagrantfile
and its provision file be located anywhere on your host machine, independently of your projects location. - Use
~
as/home/vagrant
inside the VM for the location of synchronized directories.- Disable the default
/vagrant
synchronized toVagrantfile
's location.
- Disable the default
- Use Bash for provisioning.
- Every developer will know Bash; not every developer will know Ansible, Chef and Puppet.
- You could copy/paste the Bash commands if you configured a VirtualBox manually without Vagrant.
- Use MariaDB and Adminer without a password for username
root
. - Use Apache
.conf
files outside the VM to customize the web server configuration inside the VM. - Use
rbenv
to install Ruby. - Use
bundler
to install Rails.
Look at all VirtualBox downloads here
https://github.com/dotless-de/vagrant-vbguest
In host terminal:
vagrant plugin update
vagrant plugin install vagrant-vbguest
In host terminal:
cat ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub
And maybe:
cat ~/.ssh/authorized_keys
cat ~/.ssh/config
cat ~/.ssh/known_hosts
In host terminal:
cat ~/.gitconfig
In host terminal:
mkdir -p ~/vm && cd $_
git clone https://github.com/stemar/vagrant-ubuntu-18-04-tests-rails.git ubuntu-18-04-tests-rails
You can have more than one vagrant dirtree under the
~/vm
directory.
Ex.:git clone https://github.com/stemar/vagrant-centos-7-6.git centos-7-6
Vagrant supports the definition of multiple VMs inside one Vagrantfile
,
but if I separate my VMs by LAMP stack in a dirtree, I can run, maintain and troubleshoot them independently.
- I can have a smaller, focused
Vagrantfile
for each VM. - I can have LAMP-specific
config
files to help the provision file. .vagrant
is created independently within each VM directory.- I can open separate tabs in my terminal,
cd
into separate VM dirtrees andvagrant up
/vagrant halt
without having to write the machine name:vagrant up ubuntu-18-04-tests-rails
/vagrant halt ubuntu-18-04-tests-rails
vagrant global-status
still works as intended to see all VMs on the host machine.- I change the HTTP and MySQL ports in
Vagrantfile
to avoid collisions and Vagrant errors at provisioning.
We want root
with no password in our VM to avoid writing a password zillions of time we access MySQL inside the VM.
Of course, you would have a root
password on a server but this is a virtual machine hosted locally.
As of version 4.6.3, Adminer is blocking any user with no password.
To allow root
with no password, config/adminer.php
is created.
We don't want to edit php.ini
directly but we want to add a development-related custom set of php.ini
overrides.
PHP doesn't allow the loading of a custom
php.ini
file to override its own settings (except when PHP is installed as CGI which is not the case here).
We have to do it with .htaccess
at the /var/www
level;
see PHP configuration settings
You will see many red line warnings from
apt-get
during provisioning but let the script finish, most of them are not fatal errors.
You can prepend the vagrant up
command with these environment variables or
you can edit Vagrantfile
.
Add the environment variable SYNC_DIR
with your own path name under your home directory.
Name it the same name to reduce confusion.
Ex.: if the host machine has ~/projects
a.k.a. /Users/stemar/projects
,
the guest machine will have ~/projects
, a.k.a. /home/vagrant/projects
.
In host terminal:
cd ~/vm/ubuntu-18-04-tests-rails
SYNC_DIR=projects vagrant up --provision
Add the environment variable PORT_80
with a port number to redirect to.
Ex.: redirect port 80 to port 8080.
In host terminal:
cd ~/vm/ubuntu-18-04-tests-rails
PORT_80=8080 vagrant up --provision
Add the environment variable PORT_3306
with a port number to redirect to.
Ex.: redirect port 3306 to port 33061.
In host terminal:
cd ~/vm/ubuntu-18-04-tests-rails
PORT_80=8080 PORT_3306=33061 vagrant up --provision
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:
mount -t vboxsf -o uid=1000,gid=1000 home_vagrant_vm /home/vagrant/vm
The error output from the command was:
/sbin/mount.vboxsf: mounting failed with the error: No such device
Halt the box and redo up
vagrant halt
SYNC_DIR=projects vagrant up --provision
In host terminal:
vagrant halt -f
OR
vagrant destroy -f
AND
SYNC_DIR=projects vagrant up --provision
In host terminal:
vagrant ssh
In guest terminal:
vagrant@ubuntu-18-04:~$
In guest terminal:
ll
...
cat ~/.bashrc
In guest terminal:
mysql -u root
MariaDB [(none)]> SHOW DATABASES; quit;
In guest terminal:
cat /etc/hosts
cat /etc/apache2/apache2.conf
cat /etc/apache2/envvars
ll /etc/apache2/conf-available
ll /etc/apache2/conf-enabled
ll /etc/apache2/sites-available
ll /etc/apache2/sites-enabled
cat /etc/apache2/conf-available/localhost.conf
cat /etc/apache2/sites-available/virtualhost.conf
cat /etc/apache2/conf-available/adminer.conf
apachectl -V
apachectl configtest
curl -I localhost
Result:
HTTP/1.1 200 OK
...
In host browser:
http://localhost:8002
You see the "Apache2 Ubuntu Default Page".
Ex.: Replace example.com
with your domain and the port number with your custom redirect number.
In host browser:
http://example.com.localhost:8002
You see the home page.
In guest terminal:
curl -I localhost/adminer.php
Result:
HTTP/1.1 200 OK
...
In host browser:
http://localhost:8002/adminer.php
- Username:
root
- Password: leave empty
- Vagrant: https://www.vagrantup.com
- Vagrant troubleshooting: https://www.mediawiki.org/wiki/MediaWiki-Vagrant#Troubleshooting_startup
- Oracle VirtualBox: https://www.virtualbox.org/wiki/Downloads
- Oracle VirtualBox Guest Additions: https://www.virtualbox.org/manual/ch04.html
- Ubuntu: https://www.ubuntu.com
- Bento box: https://app.vagrantup.com/bento/boxes/ubuntu-18.04
- Bento GitHub: https://github.com/chef/bento
- https://www.howtoforge.com/tutorial/install-apache-with-php-and-mysql-on-ubuntu-18-04-lamp
- https://linuxize.com/post/how-to-install-mariadb-on-ubuntu-18-04