MAMP v2.0+ and Apache Virtual Host Set Up

A colleague of mine was having some trouble setting up virtual host’s with his new installation of MAMP recently. So I decided to sit down and have a little look into it, and here follows a small guide to getting vhosts working locally on your mac using MAMP v2.0 and it’s bundled apache.

I’ve chosen to use “mate” which is the terminal shorthand for textmate, which you can download here Macromates Textmate, but you can swap out my instance’s of this with a more familiar editor like “nano” or “vim”.

Firstly run the following command or open /Applications/MAMP/conf/apache/httpd.conf in your preferred editor.

dave$ mate /Applications/MAMP/conf/apache/httpd.conf

First thing I noticed was that MAMP now quietly includes a bunch of file’s rather than just having it all appended at the bottom of httpd.conf, however all the usual files

Somewhere in amongst all of these files you will find the following:

# Virtual hosts
# Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

Simply uncomment it like so;

# Virtual hosts
Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

I used a command from terminal like so:

dave$ mate /Applications/MAMP/conf/apache/httpd.conf

Next we need to set up a record in our /etc/hosts/ file, you will need to call on the power of “sudo” for this. Use the following command:

dave$ sudo mate /private/etc/hosts

You will of course be prompted for your password, which is your system password. Once open you should see something like this:

# Host Database
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##	localhost	broadcasthost
::1             localhost
fe80::1%lo0	localhost

Now add this line to the bottom of this file, replacing “nameserver” with whatever you wish to call yours. testrun

Save and Quit.

Now you need to, create your virtual host in the vhosts file. So remember that line we uncommented in the httpd.conf file? The file that points to is now where we’re heading so.

dave$ mate /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

Once open you should see something like this:

# Virtual Hosts
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
# Please see the documentation at
# for further details before you try to setup virtual hosts.
# You may use the command line option '-S' to verify your virtual host
# configuration
# Use name-based virtual hosting.
NameVirtualHost *:80
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any  block.
DocumentRoot "/Applications/MAMP/htdocs/playme/http/"
ErrorLog "logs/"
CustomLog "logs/" common
DocumentRoot "/Applications/MAMP/htdocs/playmedeux/http/"
ErrorLog "logs/"
CustomLog "logs/" common

You’ll notice that there are two uncommented dummy servers set up, if you want to reference them at a later date just simply comment them out using the “#” infront of text on every line, or you can simply delete them. If your commenting it should end up like this:

#   ServerAdmin
#   DocumentRoot "/Applications/MAMP/htdocs/playme/http/"
#   ServerName
#   ServerAlias
#   ErrorLog "logs/"
#   CustomLog "logs/" common

Right, now to adding our new one, called testrun, let’s copy the format from above and insert this code into the bottom of our file. Note that at this point you now need to decide where your document route is going to be, in most cases I would just create a folder inside htdocs inside my mamp install something like below:

dave$ mkdir /Applications/MAMP/htdocs/testrun/

Our document root in our VirtualHost setup should now reflect that when you add the following.

<virtualHost *>
ServerName testrun
DocumentRoot /Applications/MAMP/htdocs/testrun/

Notice how i’ve decided to make this available on a specific port but have just opted for any port, normal port numbers for apache are :8888 or :80 so you could specify either one of those if you so wished like so:

VirtualHost *:8888

As opposed to:

VirtualHost *

Onto the final leg, now we just need to reboot apache, so, in terminal you can do:

/Applications/MAMP/bin/apache2/bin/apachectl restart

Or simply if you prefer the mamp interface a stop and start of server’s should suffice.

One last thing, let’s quickly add a index.html file to our Document Root for testing purposes.

mate /Applications/MAMP/htdocs/nameserver/index.html

And enter “Welcome to the testrun vhost – Served by MAMP” to the file. Save and quit.

Finally type http://testrun:8888 into your favourite browser (of course with MAMP started) and you should see your index.html file served up nicely.

Terminal Mv command including hidden files

I often forget how to do this, so this time im going to blog it so that I can always look back at it and hopefully it will also be helpful for everyone else.

Often when im moving files around on my development environment or remote servers I need to move files and folders back and forth, including important files such as .htaccess files.

A simple command like:

mv Folder/* .

Actually does not move hidden files, all be it moves all the rest.

I found two solutions.

Most linux machines will allow the following to set hidden files to be included. Note your regular move commands will now always include hidden files, if you do not want this then move on to the second solution.

shopt -s dotglob

And if not this works pretty nicely too

ls -A <source> | while read i; do mv <source>/"$i" <destination>; done

Hope this helps.

Connecting To Server With Putty Using SSH Keys & Port Forwarding

Quick and dirty tutorial to connecting to a remote server with ssh keys and putty.

Firstly you need to download putty and puttygen which can be found:-

Here – Putty Client
Here – PuTTYgen

Yes they are both the same page, you will find the download links for both here.

Once you have downloaded both the clients you will need to first generate yourself a key using the puttygen and then save it out to a file in a location you will easily be able to remember. Remember you need to generate a public and private key, but the private is the one of most use to you.

Im going to largely breeze over adding your ssh key to the server, as you should really know how to do this already before attempting to use putty with ssh keys and port forwarding.

Open up putty, enter the host as whatever it needs to be either an ip address or a host name like and specify the port as 22 if it isn’t already.

Then head down on the left hand menu to the SSH list which will probably be shut, click the plus to expand the list and head to the AUTH tab. Here enter the user you want to auto login with and specify the path to your key using the browse file function.

If all is correct head back to the “session” tab and move to the right hand side, and save your settings under any name you like, this is to ensure that you can pick up where you left off when returning to the same connection again.

Now you have done that hit open and you should with any luck either be presented with a enter passphrase for key authorisation, enter that and youll be in.

Port Forwarding Mysql From Remote To Local

Should you like us sometimes want to dev locally to a remote database, Putty can be used to tunnel a mysql connection from a remote server to your local mysql port. To do this in most cases simply forward the following or well at least for me.

Navigate to the following.
Connection -> SSH -> Tunnels -> Options Controlling Port Forwarding

L 3307 port destination to
the remote database.

Add that in, head back to session save it and then open it and wollah you should now be able to dev locally to the remote database, by forwarding mysql to your local ports.

This is just a quick and dirty tutorial and not meant to be a fully comprehensive guide, however if theres something you want to know I may be able to help, so leave a comment.

Simple Directory Protection with .htaccess

To deny access to a folder or directory, simply create a new .htaccess file inside that directory.

Then depending on the authorisation type you want to use you can make rules to allow or deny access.
View some examples below.

Allow by Ip

order allow,deny
allow from
deny from all

This denys anyone not on ip access to your folder.

Link MAMP & mysql to mac os x terminal

Last night I trawled the internet trying to find the answer to how to hook up my mac os x terminal to my mamp mysql using the > mysql command in the terminal.

For mac os x 10.5.6 Leapoard users this command worked just fine.

sudo ln -s /Applications/MAMP/Library/bin/mysql /usr/bin/mysql

I also noticed attempting to use the mysqldump command resulted in this error: “-bash: mysqldump: command not found”

To solve it cd into “/Applications/MAMP/Library/bin/” and run the same command and you’ll have much better luck icon smile Link MAMP & mysql to mac os x terminal

Mysql Dump Ignore Tables And Rsync To Remote Server

Not having had huge experience working with mysql dumping and rsync. This afternoon I was faced with the task of taking a backup of one our busy, very database intensive games using mysqldump command, but I also needed to exclude some rather over sized tables from the dump. After which I wanted to pipe over the mysqldumps to our remote secure testing server using rsync which id also never used before.

So the first things first I let our users know the site is going down in 5 mins I then sabotage our db_connect functions so that users a directed to a page with no connection attempts or db calls on it.

I then write the command to dump data and tables ignoring a few tables which id already flagged as potentially large and time consuming to dump.

My command looked a little like this:-

mysqldump -u username -p --ignore-table=database.tableA, database.tableB -B database -K > /path/to/outputfile.sql

With quite a few more ignores hehe but hopefully you’ll get the gist.

I then just wanted to dump the structure of the troublesome tables.

So I did something like the below.

mysqldump -u username -p database table1 table2 -d > /path/to/outputfile.sql

Notice the -d, this is very important as it signifies no data just structure.

Now I have my two files I want to sync them accross the our test area located on another server, this time using a rsync

I used ssh with rysnc and my final command looked like this.

rsync -a -e ssh backups/ username@server:/home/google/backups

Which simply took my backups directory on the server i was connected to already and synced it with the credentials entered here. Seemlessly and with all permissions etc carried over.

Now thus I had achieved what I needed to and all that was left was to load the files into the database for the test area. By simply:

mysql -u username -p database < /path/to/inputfile.sql

Mysql Dumping / Uploading a file or files via terminal and SSH

Just something I found myself looking up today so that I can do everything from the comfort of my little terminal window.

I wanted to take a backup from a table in one database and up it to another db.

Firstly I dumped the tables on my remote server to my sites webroot.

mysqldump -u root -p database_name table_1 table_2 table_3 > /path/to/outputfile/outputfile.sql

This will prompt you for your password, enter it and it should, providing everything else is correct dump to ‘outputfile.sql’.

Now you have your backup residing at your web servers webroot or wherever it is you may have chosen to store it.

This file can now be retrieved either by a FTP client or if your like me then using terminal by either using curl or scp to local.

Curl example:

curl -O

Scp (preferred via ssh):
scp username@server:path_on_server/file path_to_download_to

Once on local you will probably want to move this to your other server via uploading which we will again use Scp for this.

scp path_to_file/file username@server:path_to_upload_to

Once youve got the file on the server which the database you want to import data to resides.
Connect to the server in this case via ssh.
Then restore your file to the database.
mysql -u root -p databasename < /path/toupload/directory/outputfile.sql

This is the general format to remember

mysql -u [username] -p [password] [database_to_restore] < [backupfile] If its a zipped backup you want to restore simply use the following command gunzip < outputfile.sql | mysql -u root -p DatabaseName Also remember you dont have to have the password prompt you each time you can include it in the original command by changing `-p` to `-p password`. Very basic but will suit most peoples needs.

How to add a new vhost with mamp on mac osx

Adding a new vhost is something I have to do on a regular basis when working on new projects locally.
Its a fairly straight forward task as well.

Download and install:- MAMP

Once you have done this navigate either with a finder window or via terminal to /Applications/MAMP/htdocs/

Once here create a folder again either with finder or terminal e.g mkdir myfirstsite

cd myfirstsite – to enter that folder and create another folder: mkdir http

Lets also add a test landing page so add a file called index.html into your http directory either in finder window or just by the following command in terminal.

Cd /Applications/MAMP/htdocs/myfirstsite/http/
vim index.html
hit ‘i’ to insert
type “Hello this is my first site”
hit escape-key + type ‘:wq’

Now we want to do 2 things, firstly add the virtual host to the httpd.conf file:

Navigate to: cd /Applications/MAMP/conf/apache/httpd.conf

Once here either open the text file in a text editor or simply enter it with: vim httpd.conf in terminal if your familiar with this method.

Scroll the very bottom and add the following:

DocumentRoot /Applications/MAMP/htdocs/myfirstsite/http
ServerName myfirstsite

Then secondly you need to add the site to the your local OS X machines host file with the following command via terminal window only.

printf “” | sudo tee -a /etc/hosts

After that restart MAMP by ‘stop servers’ and then start servers option.

Then open up your browser pop into the address bar:- http://myfirstsite:8888/ and with any luck you’ll be on your way.

A useful command for mac os x terminal

When working on your server with ssh access, you tend to get very used to doing everything you need to do via the terminal window. Sometimes you may come accross problems which unless you have the know how you will be puzzled as to why somethings not working.

Today I came across a small problem in that a few commands which would normally work on my local os x machine were not working on my server machine.
Im going to use the ‘zip’ function as my guinea pig. If this function is not installed or enabled in your servers configuration/installation you will see a message like this:

-bash: zip: command not found

This is easy to fix simply by running the following command:

yum install zip

Now if you try the zip command again you will notice its worked or at least should work. You can also if you wish check to see if its installed first by viewing the install list on your server with the following command.

rpm -qa