
Setting up my development environment with Vagrant
I’ve changed jobs quite often due to various reasons. I’ve never seen or been to a company where a developer joins, and the on-boarding experience is, here’s your laptop, we’ve installed everything that you need to do your work. Get cracking. If I ever joined a company like that, I’d probably get a minor stroke.
HOW DARE THEY FORCE ME TO USE VISUAL STUDIO!
BLASPHEMY!
So the most important thing to do before getting funky with the codes is setting up the development environment. Here’s the list of stuffs that I do to setup the development environment on a newly assigned PC/Laptop (excluding IDE).
Stuffs that I need initially is, Chocolatey, VirtualBox, Vagrant & MinGW.
1. Installing Chocolatey & Other Packages.
Windows do not have a package manager. This is the next best thing (unless there’s something newer that I’ve never heard about). There are several ways to install chocolatey but the one that I often use is the first one.
Open up a CMD prompt and run it as an administrator.
Then you’ll end up something like this.
After having a CMD prompt with administrator privilege, install Chocolatey.
After installing chocolatey, refresh the environmental variables. There is no easy way or a single line command that can be done (for now) that I know of. So, close the command prompt and reopen it again (run as administrator, refer above). After running the cmd prompt, you can type in choco to view the installed version of chocolatey.
Now chocolatey is installed, I need to install all the other stuffs to continue.
When all three of the above software is installed, refresh the environmental variables (close and reopen cmd prompt, this time we dont need the administrator privileges).
2. Setting up the VM with Vaprobash
Clone (or as an svn user would like to call it, checkout) Vaprobash. Its the simplest provisioning script for vagrant that I have used.
After cloning it, there’s a file in the folder called Vagrantfile, open it up with a text editor. The softwares that I usually use is PHP (already uncomment), Vim (line 209), Apache (line 219), MySQL (line 230), NodeJS (line 318) and Composer (line 332). So from the Vagrantfile, uncomment those lines.
Now, I need to define a folder that I want to be synced with the guest VM. Put it below line 127.
1 |
config.vm.synced_folder "D:/work", "/var/www" |
Next, define a port to be forwarded for the virtualhost to use, should be below line 117.
1 |
config.vm.network :forwarded_port, guest: 7001, host: 8001 |
That should be it for the Vagrantfile modifications. Start up the vm with vagrant up.
To test if the sync folder is working, if the files in D:/work is listed in /var/www, its working. Or you can create a file in the shell,
1 2 |
cd /var/www touch testfile |
Going to D:/work using Windows Explorer, if the sync is working, there would be a file called testfile in it.
3. Setting up Apache Virtualhost
Now we have vagrant all ready. Lets get into it with vagrant ssh.
First we need to create the virtualhost by creating another conf file. This new conf file will be based from 000-default.conf. Copy 000-default.conf
1 |
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mynewproject.conf |
Open up the file and edit it (never used vim? LMGTFY)
1 |
sudo vim /etc/apache2/sites-available/mynewproject.conf |
There will be quite a lot of comments in it but the gist of the file should be
1 2 3 4 5 6 7 |
Listen 7001 <VirtualHost *:7001> ServerAdmin webmaster@localhost DocumentRoot /var/www/mynewproject ErrorLog ${APACHE_LOG_DIR}/mynewproject-error.log CustomLog ${APACHE_LOG_DIR}/mynewproject-access.log combined </VirtualHost> |
Save the file!
Now is the time to enable the configuration and restart apache2.
1 2 |
sudo a2ensite mynewproject.conf service apache2 restart |
Apache should be listening to that port now. To see if it really is listening to it, use netstat.
1 |
sudo netstat -anp | grep 7001 |
e.g. in my VM it displays like this (I use port 7004 with Nginx).
So now you have your guest OS listening to 7001, your host OS listening to 8001. Everything is ready. Put your project in D:/work/mynewproject and open up http://localhost:8001 in your browser.
References
+ There are no comments
Add yours