Jump to content

Setting Up a PHP Development Environment Using VirtualBox


zilli
 Share

Recommended Posts

Posted (edited)

Hey there! Here’s an alternative approach to setting up a PHP development environment. It's neither better nor worse than others, just different. I've been using this method for years, and it has served me flawlessly. So, I decided to share it with you. Enjoy!

0) Overview

The idea here is to keep my system neat and tidy. Since I use one laptop for everything, I like to keep all my development tools separate so everything stays organized. The main tool I use is a virtual machine, specifically VirtualBox. Another significant reason I use a VM is that it lets me mirror my development setup exactly as it will be on the production server. This way, I know I’m developing with the same PHP/APACHE/MYSQL versions that I'll use in production. Of course, this only works when I have full control over the production server.

1) Installing VirtualBox

Let's start by installing VirtualBox. You’ll also need to install the Guest Additions ISO, but we can do that later.

2) Choosing the Guest OS

Since all my production servers run Linux, I install the same OS on the guest VM. Here are some tips for installing your VM guest:

  • To replicate the production server, I choose a minimal installation, avoiding window managers, desktop systems, or anything that wouldn’t be installed on production.
  • You can configure your LAMP stack later, but enable your SSH server now as you will need it later.

3) Configuring the VM

After the installation is done, let's configure your VM. These configurations are specific to my hardware and the fact that I am using Linux as the guest OS. With my machine off, this is what I have:

01.png.a472d67db1504ff586f10ff552a9dd65.png

02.png.1622aae1e35efa105f1e3aba45b53472.png

03.png.6c07b3fdb1650619d51f68e7afbe5891.png

04.png.6cd916b3c227f4aa08bc68795f07bfeb.png

05.png.4e2e98ca2b122b2c7148a57ce49f9416.png

Select the Port Forwarding button to configure rules for accessing services from your guest OS on your host OS.

06.png.19b5f88a3514958c04e9e6532bc0ac72.png
07.png.7852a5bab99cf550cab478c5cef4e2c9.png

08.png.0b0f7778f16b3a80cacc69950ac1922e.png

Note: '/home/daniel/Public/WWW' is the host OS path containing the PHP files. 'Folder Name' is a label used for mounting this directory.

4) Running the VM in Headless Mode

To minimize resource usage and keep the VM running in the background, I use headless mode, meaning no GUI (although I can start it if needed). This is all about command line! Here’s how to set it up for Linux, Windows, and macOS.

Linux
I achieved this with two bash aliases on my Linux host machine. Add the following alias to your `~/.bash_profile` or `~/.bashrc` file:

alias headless='VBoxManage startvm LAMP --type headless'
alias poweroff='VBoxManage controlvm LAMP poweroff'

After adding the aliases, run `source ~/.bash_profile` or `source ~/.bashrc` to apply the changes.
 

Windows
Create two batch files, one for starting the VM in headless mode and another for powering it off.

headless.bat

@echo off
VBoxManage startvm LAMP --type headless

poweroff.bat

@echo off
VBoxManage controlvm LAMP poweroff


macOS
Add the following aliases to your `~/.bash_profile` or `~/.zshrc` file:

alias headless='VBoxManage startvm LAMP --type headless'
alias poweroff='VBoxManage controlvm LAMP poweroff'

After adding the aliases, run `source ~/.bash_profile` or `source ~/.zshrc` to apply the changes.

5) Accessing the VM via SSH and Installing Guest Additions

Without a GUI, the way to access the VM is via SSH. Ensure you have SSH installed on your host OS. Here’s how to set it up for Linux, Windows, and macOS.

Linux
I have another alias for this:

alias go='ssh -p2222 YOURHOST@127.0.0.1'

Windows
Create a batch file for SSH access.

go.bat

@echo off
ssh -p2222 YOURHOST@127.0.0.1

macOS
Add the following alias to your `~/.bash_profile` or `~/.zshrc` file:

alias go='ssh -p2222 YOURHOST@127.0.0.1'

After adding the alias, run `source ~/.bash_profile` or `source ~/.zshrc` to apply the changes.

Install your Guest Additions

The installation process may differ depending on your host OS, so it's best to do a quick Google search and follow the documentation. It's not hard to do, it just involves a few steps. You need to have the Guest Additions installed before continuing to the next step.
 

6) Setting Up the Guest Machine

Once you’re in your guest machine via SSH, it's time to set it up. The actual setup will depend on your needs, so I’ll just show you how to get your files served from your host system. The following setup happens on your guest OS:

6.1) Mounting Directories

From your `/etc/fstab`, add something like:

# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
www  /var/www/ vboxsf  auto,rw,uid=33,gid=33 0 0
  • `www` is the same name as in the 'Folder Name' field shown in the picture above.
  • `/var/www` is where I want `www` to be mounted. It could be `/srv`, `/mnt`, or any other path you prefer.
  • This is the type of file system. vboxsf stands for VirtualBox Shared Folder, a type used to mount folders shared between the host and guest operating systems.
  • These are the options for `auto,rw,uid=33,gid=33`:
    • auto: Automatically mount the file system at boot.
    • rw: Mount the file system with read and write permissions.
    • uid=33: Set the user ID of the mounted files to 33 (usually the www-data user in many Linux distributions). Change this to your Apache's user.
    • gid=33: Set the group ID of the mounted files to 33 (usually the www-data group in many Linux distributions). Change this to your Apache's group.

6.2) Configuring Apache

From your Apache configuration (`/etc/apache2/sites-available/000-default.conf`):

<VirtualHost *:80>
    ServerName pw.test
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/pw.test
    LogLevel debug
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Here, the `DocumentRoot` points to what you have in your fstab.

6.3) Configuring MySQL/MariaDB

From your MySQL/MariaDB configuration (`/etc/mysql/mariadb.conf.d/50-server.cnf`):

[mysqld]
bind-address = 0.0.0.0
#skip-external-locking


7) Connecting to the Database

This is how I connect to my database via a database manager. The important information here is the Host and Port.

2024-07-06_17-14.png.09677e8b6c86f34ff57314f0fd0ebb81.png

? Accessing the Web Server

In your browser, type `http://localhost:8080`. That's it!

==========

Let me know your thoughts and suggestions. Cheers!

 

Edited by zilli
  • Like 7
  • Thanks 2
Link to comment
Share on other sites

 Share

×
×
  • Create New...