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.
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.
Now add this line to the bottom of this file, replacing “nameserver” with whatever you wish to call yours.
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.
# 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
# Use name-based virtual hosting.
# 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.
CustomLog "logs/dummy-host.example.com-access_log" common
CustomLog "logs/dummy-host2.example.com-access_log" 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:
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.
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:
As opposed to:
Onto the final leg, now we just need to reboot apache, so, in terminal you can do:
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
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 google.access.me.com 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.
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.
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`.
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.