Skip to content

Instantly share code, notes, and snippets.

@wesruv
Created January 21, 2023 18:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesruv/e0e90b4cb5d270dbfff05076baf58cb4 to your computer and use it in GitHub Desktop.
Save wesruv/e0e90b4cb5d270dbfff05076baf58cb4 to your computer and use it in GitHub Desktop.
Creating a LAMP local dev environment with Virtualbox

Creating a Virtualbox Linux VM for local LAMP dev

Download and Install Virtualbox

https://www.virtualbox.org/wiki/Downloads Choose the version for the host machine, so if you’re on Windows, choose that, if Mac, choose that.

Download Ubuntu Server

Or whatever flavor you like, I recommend this one. https://ubuntu.com/download/server

Creating the Machine

Click the “New” Icon

  1. Give the machine a name
  2. Type: Linux
  3. Version: Ubuntu 64

Next

Memory Size If you have 16GB or more RAM, I recommend giving it 4000MB of RAM. If you have less, 2000MB should be fine

Hard Disk Default option is fine

Hard disk file type Default option is fine

Storage on physical disk Default option is fine

File location and size Location is up to you, optimally the virtual disk is stored on an SSD The size depends on how much free space you have, running out of space is rough, it usually means you’ll have to create a new machine, unless you can get rid of a bunch of stuff. Expanding the disk storage of an existing VM is painful, I’ve looked it up a few times and each time starting from scratch was more appealing.

Minimum 50GB, I feel more comfortable with 100 or 150GB

Setting up the network interface(s)

Foreword:

There’s a choice here. There’s a “Host Only” feature (what I recommend), or “Bridged”.

“Host-only” means only your computer can see it, the network router and other computers on the local area network your on can’t see it. Since it’s locked off it’s a bit more secure.

“Bridged” means the Linux VM will show up like it’s another physical machine on the network. This is less secure, but can be useful if you want to test something from your phone, the phone will have a chance of seeing the the Linux VM, but there’ll may need to be host hacking on the router level to get that to work.

You could have both and keep the “Bridged” adapter off unless you’re using it.

Instructions

Select the VM you’ve made Click Settings (alternatively Machine > Settings works)

On the left side select Network

Leave Adapter 1 alone

Select Adapter 2

  1. Select: Enable Network Adapter
  2. Select “Host-only” (or “Bridged” if you prefer, see preface)
  3. Leave the default “Name:” that comes up

Press OK

Start the machine and Install Linux

Select your machine and press the “Start” icon

It will prompt you for a startup disk Press the folder icon and find the *.iso Ubuntu server install file you downloaded

Install Linux as normal Fill all options you’re prompted to, everything can be left on the default I typically don’t install any packages recommended from the end of the install process

Setting up fresh Linux for Local Dev

This is written for CLI (not that this couldn’t be done on a Linux install with a GUI) and written for Ubuntu.

Add packages necessary for build tools (useful for Node/npm)

sudo apt-get install build-essential

Add ssh package (if not already installed)

sudo apt-get install openssh-server

Add apache package (if not already installed)

sudo apt-get install apache2

Installing Samba on Ubuntu

Samba is a very fast file share over the network, great for working on files

Install from apt

sudo apt-get install samba

Edit it’s configuration

sudo nano /etc/samba/smb.conf

Under [global] settings add:

follow symlinks = yes
wide links = yes
unix extensions = no

At the end of the file add (but replace USERNAME with your linux username):

[USERNAME]
path = /home/USERNAME
available = yes
valid users = USERNAME
read only = no
browseable = yes
public = yes
writable = yes
wide links = yes
wide symlinks = yes
unix extensions = no

Restart samba

sudo service smbd restart

Enter desired password

sudo smbpasswd -a USERNAME

Connecting to Samba share

Find out the IP address on the Linux machine

ifconfig

Check for an IPV4 address (e.g. ###.#.#.##, 192.168.1.101, etc.)

Connecting to Samba share… on Windows

Press Windows Button + E to open a new explorer window In the title bar type two backslashes and an IP that you think will be the address to connect, e.g.

Right click on the folder with your username and select “Map Network Drive

Choose a letter you like, check both checkboxes (Sign in using different credentials and Reconnect at Login)

Now if you go to “This PC” in the side bar your Samba share will show up as a network drive with the letter you chose.

Connecting to Samba share… on Mac

In finder, under the menu Go > Connect to Server…

paste something like

smb://192.168.1.18/USERNAME/

replacing the ip address and username. Click the plus to save it as a favorite.

Add or create your SSH key

Locally, on a mac, if you have not generated an ssh key (look in ~/.ssh to see if there are keys there already), make a key

ssh-keygen

From a terminal on mac, to all in one go: copy your public keys, add them to authorized_keys, and set up the .ssh folder on the linux box

ssh-copy-id USERNAME@192.168.1.18

Or manually, to add an existing one, grab id_rsa and id_rsa.pub and copy it into a folder called .ssh in the root of the samba share (in linux it’s your home folder /home/USERNAME

Set the correct permissions on id_rsa

sudo chmod 200 ~/.ssh/id_rsa

Copying id_rsa.pub as authorized_keys will allow password-less login

Install Lando

Follow the appropriate instructions https://docs.lando.dev/basics/installation.html

Recommend installing NVM to install Node/NPM

Allows you to easily switch between versions of Node/NPM and easily update/upgrade and rollback: https://github.com/nvm-sh/nvm

After install run:

nvm ls-remote

Find the version you want to run (I look for the latest LTS version), e.g. 10.6.3 in this output:

       v10.15.3   (LTS: Dubnium)
       v10.16.0   (LTS: Dubnium)
       v10.16.1   (LTS: Dubnium)
       v10.16.2   (LTS: Dubnium)
       v10.16.3   (Latest LTS: Dubnium)
        v11.0.0
        v11.1.0

Install the version of choice:

nvm install v10.16.3

Then set it as default:

nvm alias default v10.16.3

If there’s a specific project you need a different version for, install whatever version is needed (e.g. v6.17.1), then run:

nvm use v6.17.1

nvm use will make it so any node or npm commands in the active session will use the version specified. In any other terminal window (currently open, or opened in the future) it’ll use the default.

This means you could be running two projects with different npm versions in different windows, if that’s what you needed.

Practical/Personal

Currently, linux server is set up “from scratch”.

ssh’d in using -A to pass through authorization from ssh keys on mac laptop. This allows git cloning, pushes, etc. using pre-existing keys.

global ~/.gitconfig was copied from laptop to the linux server.

Consider adding config to not change file permissions, this would be per project. git config core.fileMode false

(Initially, copying my devel/sites directory from the mac laptop, with all the project files, git repos, db backups, etc, to the linux server, a bunch of cruft like .__ files came with it, and some permissions issues. The new setup on the linux server seems to be cleaner. So, copying from laptop to linux server is not recommended.)

Commands are done via terminal ssh’d to the linux server.

(Initially, tried local mac terminal cd’d into the samba mount, but commands like git status took a very long time. Running commands on laptop cd’d into samba share is not recommended.)

Additional apt-get packages installed: vim curl fish traceroute.

Drupal Local VM

Setup

Once the VM files have been put on a machine you'll need to go to it's settings and update it's Network adapters (these are computer specific).

If the machine isn't listed in Virtualbox yet, double click the .vbox file. This should get it added to the list.

  1. Go to Settings on drupbuntu
  2. Go to Network along the left
  3. Go to Adapter 2
  4. Make sure "Name:" dropdown has a valid value (e.g. Virtualbox Host-only adapter)
  5. Press OK

If there wasn't a valid option in the dropdown, see "Creating a new Host Network"

Find the machine's IP

Start the machine in Virtualbox.

Login when it's done (if you need a login user you'll have to talk to someone that already has one on the machine).

Once you've logged in run:

ifconfig | grep 192.

This should give you the local IP for the machine. Only the host machine can access the VM (assuming you've left it on a Host Only adapter), this has two advantages:

  • if the host machine is on the VPN, the VM is also on the VPN network
  • the machine's content is less vulnerable to hacking, the host machine would have to be exposed

Host hack the machine's address

To get the local site running with less setup, it's easiest to "host hack" your host machine. If you prefer not to for some reason, you'll have to modify the sites.php file, talk to Wes for details.

To host hack your machine you'll need to find your hosts file.

In Mac/Linux it should be: /etc/hosts On Windows it should be: C:\Windows\System32\drivers\etc\hosts

You'll need to be admin to edit the file, in Mac/Linux I prefer:

sudo nano /etc/hosts

If you use nano, to close the file press Ctrl + X and then follow the prompts on the bottom for saving or not saving.

On Windows:

  • Press the Windows button
  • Type "notepad"
  • Right click on Notepad and "Run as Administrator"
  • Open the hosts file

Once you have the file open, at the bottom add the VM's IP, then add vbdrupal.com, e.g.

192.168.157.3      vbdrupal.com

Save and close

Connecting to Samba share… on Windows

Press Windows Button + E to open a new explorer window In the title bar type two backslashes and an IP that you think will be the address to connect, e.g.

Right click on the folder with your username and select “Map Network Drive

Choose a letter you like, check both checkboxes (Sign in using different credentials and Reconnect at Login)

Now if you go to “This PC” in the side bar your Samba share will show up as a network drive with the letter you chose.

Connecting to Samba share… on Mac

In finder, under the menu Go > Connect to Server…

You'll connect to something like:

smb://192.168.1.18/USERNAME/

Replacing the ip address and username. If you have the machine IP host hacked (e.g. vbdrupal.com) you can use smb://vbdrupal.com/USERNAME

Click the plus in the bottom left to save it as a favorite.

Add or create your SSH key

Locally, on Mac/Linux, if you have not generated an ssh key (look in ~/.ssh to see if there are keys there already), make a key

ssh-keygen

From a terminal on Mac/Linux, to all in one go: copy your public keys, add them to authorized_keys, and set up the .ssh folder on the linux box

ssh-copy-id USERNAME@192.168.1.18

Or manually, to add an existing one, grab id_rsa and id_rsa.pub and copy it into a folder called .ssh in the root of the samba share (in linux it’s your home folder /home/USERNAME

Set the correct permissions on id_rsa

sudo chmod 200 ~/.ssh/id_rsa

Copying id_rsa.pub as authorized_keys will allow password-less login

Starting Up and Connecting to the Machine

To use the VM, start it in Virtualbox.

After ~ 1 minute the machine should be completely started and the SSH agent should be ready.

Connecting to the machine via SSH for Mac/Linux

If you have an SSH key in ~/.ssh/authorized_keys, and that key is in your session you can run:

ssh -A vbdrupal.com

The -A will pass the ssh key into the session so it doesn't need to be added again inside of the VM.

Connecting to the machine via SSH for Windows

... ask Wes (currently no one else is doing this)

Running and Working on Drupal

Once in the machine navigate to the customer-portal-kbase

To start docker and the drupal site:

docc start

Connect to the samba share and you should be able to open customer-portal-kbase folder in the editor of your choice, and edit/save any files. If there are any permissions issues, feel free to deal with them liberally, the git repo is set to ignore local file perms and not share them upstream.

On the host computer, navigate to http://vbdrupal.com, this should contact your local version of the Drupal site. If that doesn't work, check:

  • The hosts file to make sure vbdrupal.com has the right IP
  • Make sure docker is running, type docc start in the customer-portal-kbase folder

For code changes it's quickest if command line git is used from the SSH command line session. It can be done using whatever you prefer on the host machine, but it will be much slower.

Logging into Drupal

Visit one of these URL's depending on what role you'd like:

  • /adminlogin/jnanaadmin to login as a power user
  • /adminlogin/asp-judges for a customer

This will take you to the homepage, which may not show your role.

To make sure you're logged in you can visit http://vbdrupal.com/admin, this should show the Drupal backend and have your username and login status shown./

Turning off the machine

Make sure docker is shutdown first, in customer-portal-kb run:

docc stop

Then you can shutdown the VM with:

sudo shutdown -h now

Troubleshooting

Can't SSH into the machine

Check to see if you have the right IP

From inside the machine run:

ifconfig | grep 192.

If you an IP shows, and you have the right one when you try to start an SSH session... ask Wes or someone for help.

If an IP does NOT show that starts with 192 it's likely your host adapter failed. Follow the instructions under "Creating a new Host Network", feel free to disable or completely remove your old adapter to avoid confusion. Once you make a new host adapter you will need to find out the new IP to connect to for your SSH sessions.

Creating a new Host Network

  1. Go to File > Host Network Manager
  2. Create (top left)
  3. Follow prompts
  4. In the last column of the Host Network Manager, make sure your adapter is enabled
  5. Go to Settings for your VM
  6. Go to Network along the left
  7. Select your new Host network adapter in the Adapter tab that has the "Host Only" (probably adapter 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment