Starting MAMP’s Apache and MySQL Servers at Login

S

Whether using XAMPP or MAMP to provide a local server environment I prefer to have the respective Apache and MySQL servers start when I login.

With XAMPP I use a launch daemon and this works perfectly. With MAMP it’s even easier as there’s an option to Start Servers when starting MAMP in MAMP’s preferences. I only need to add MAMP.app to my Login Items and MAMP’s servers will be up and running after logging in.

Well, they will be if using the default MAMP ports for Apache: 8888 and MySQL: 8889. However, I prefer using the standard ports for Apache: 80 and MySQL: 3306 and doing so requires I enter a password when MAMP starts as port 80 is a privileged or well-known port. Not ever having had any success using a launch daemon with MAMP the only alternative appeared to be AppleScript.

Saving this AppleScript as an Application and adding it to the Login Items will work but it has one major problem; the PASSWORD and USERNAME are stored in plain text.

tell application "Finder"
	# Start MAMP's Apache server
	do shell script "/Applications/MAMP/bin/startApache.sh &" password PASSWORD user name USERNAME with administrator privileges
			
	# Start MAMP's MySQL server
	do shell script "/Applications/MAMP/bin/startMysql.sh"
end tell

AppleScript to start MAMP’s Apache and MySQL servers with user name and password stored in plain text

 

 

To overcome this security issue a new item can be added to the users keychain and made available to the AppleScript using Keychain Scripting. Unfortunately, as of OS X Lion the Keychain Scripting.app is no longer included. Prior to OS X Lion it could be found in /System/Library/ScriptingAdditions/. We’ll see two alternative methods later, but first we need to create a new item in our keychain.

Open Keychain Access.app in the /Applications/Utilities folder:

Keychain Access showing keychain list with login keychain selected

Keychain Access showing keychain list with login keychain selected

 

 

Ensure the login keychain is selected and press ⌘N to create a new password item:

Adding a new keychain item in Keychain Access

Adding a new keychain item in Keychain Access

 

 

Enter the Keychain Item Name. I’ve used MAMP, but it can be anything. Enter the Account Name and Password you use to login into your computer and click Add.

The two alternatives methods I mentioned earlier for accessing the keychain using AppleScript are Red Sweater’s Usable Keychain Scripting and the security command in Terminal.

Using Usable Keychain Scripting the following AppleScript will grab the Account Name and Password for the keychain item named MAMP and assign each to a variable:

tell application "Finder"

	# get the user name and password using Usable Keychain Scripting
	tell application "Usable Keychain Scripting"
	
		set theKeyList to every generic item of current keychain whose name is "MAMP"
		
		repeat with x from 1 to (length of theKeyList)
			set theKey to item x of theKeyList
			set theUserName to the account of theKey
			set thePassword to password of theKey
		end repeat
		
	end tell
	
end tell

AppleScript to obtain user name and password using Usable Keychain Scripting

 

 

The same can be achieved using the security command in Terminal:

tell application "Finder"

	# get the user name and password using the security Terminal command 
	set theUserName to do shell script ("security  find-generic-password -l \"MAMP\" | grep \"acct\" | cut -c 19-99 | tr -d '\"'")
	set thePassword to do shell script ("security 2>&1 >/dev/null find-generic-password -gl \"MAMP\" | cut -c 11-99 | tr -d '\"'")

end tell

AppleScript to obtain user name and password using the security command in Terminal

 

 

I prefer using the security command so I’ll include this method in my AppleScript as opposed to Usable Keychain Scripting though the latter will function just as well.

Substituting the plain text PASSWORD and USERNAME from the original AppleScript for their respective variables: thePassword and theUserName, we can use the following AppleScript:

tell application "Finder"

	# get the user name and password using the security Terminal command 
	set theUserName to do shell script ("security  find-generic-password -l \"MAMP\" | grep \"acct\" | cut -c 19-99 | tr -d '\"'")
	set thePassword to do shell script ("security 2>&1 >/dev/null find-generic-password -gl \"MAMP\" | cut -c 11-99 | tr -d '\"'")
	
	# Start MAMP's Apache server
	do shell script "/Applications/MAMP/bin/startApache.sh &" password thePassword user name theUserName with administrator privileges
			
	# Start MAMP's MySQL server
	do shell script "/Applications/MAMP/bin/startMysql.sh > /dev/null 2>&1"

end tell

AppleScript to start MAMP’s Apache and MySQL servers using the security command in Terminal

 

 

Save this AppleScript as an AppleScript application and add it to the user’s Login Items and MAMP’s Apache and MySQL servers will be up and running when user login is complete.

Start MAMP Servers Applescript application added to Login Items

Start MAMP Servers Applescript application added to Login Items

 

 

Run this AppleScript now and you’ll be prompted with the following:

Authorisation for the security command to access the 'MAMP' keychain item

Authorisation for the security command to access the 'MAMP' keychain item

 

 

Clicking Always Allow will ensure this prompt won’t appear each time the user logs in. Opening the Access Control panel for the MAMP keychain item in Keychain Access shows the security command added to the list of authorised applications:

Access Control panel for the 'MAMP' keychain item

Access Control panel for the 'MAMP' keychain item

 

 

While this AppleScript will suffice I’ve included some other functionality in the final version.

This code uses a user-defined function to ensure that MAMP’s Apache and MySQL servers are only started if no other instances of Apache or MySQL are currently running:

tell application "Finder"

	# Check if the Apache or MySQL servers are already running
	set apacheRunning to my ProcessRunning("httpd")
	set mysqlRunning to my ProcessRunning("mysqld")
	
end tell

on ProcessRunning(processName)
	
	set AppleScript's text item delimiters to "\\|"
	set theExpression to processName as string
	set AppleScript's text item delimiters to ""
	
	set isRunning to every paragraph of (do shell script "ps -acwx -o command | grep -ix " & quoted form of theExpression & " || echo 'false'")
	
	if isRunning contains "false" then
		return false
	else
		return true
	end if
	
end ProcessRunning

AppleScript to determine if Apache and MySQL are currently running

 

 

This code starts the MAMP control panel and then hides it after a short delay:

tell application "MAMP"
	activate
	delay 3
end tell
		
tell application "System Events" to set visible of process "MAMP" to false

AppleScript to open and then hide MAMP.app

 

 

The final version in its entirety:

tell application "Finder"
	
	# Check if the Apache or MySQL servers are already running
	set apacheRunning to my ProcessRunning("httpd")
	set mysqlRunning to my ProcessRunning("mysqld")
	
	
	# If neither the Apache nor MySQL servers are already running...
	if (not apacheRunning and not mysqlRunning) then
		
		
		# get the user name and password using the security Terminal command 
		set theUserName to do shell script ("security  find-generic-password -l \"MAMP\" | grep \"acct\" | cut -c 19-99 | tr -d '\"'")
		set thePassword to do shell script ("security 2>&1 >/dev/null find-generic-password -gl \"MAMP\" | cut -c 11-99 | tr -d '\"'")
	
		
		# Start MAMP's Apache server
		do shell script "/Applications/MAMP/bin/startApache.sh &" password thePassword user name theUserName with administrator privileges
		
		# Start MAMP's MySQL server
		do shell script "/Applications/MAMP/bin/startMysql.sh > /dev/null 2>&1"
		
		tell application "MAMP"
			activate
			delay 3
		end tell
		
		tell application "System Events" to set visible of process "MAMP" to false
		
	else
		
		if (apacheRunning and mysqlRunning) then
			
			set theMessage to "Both Apache and MySQL servers are"
			
		else if apacheRunning then
			
			set theMessage to "The Apache server is"
			
		else if mysqlRunning then
			
			set theMessage to "The MySQL server is"
			
		end if
		
		display alert "Local Server Environment" message theMessage & " already running." & return & return buttons {"OK"} default button 1 giving up after 50 as critical
		
	end if
	
end tell

on ProcessRunning(processName)
	
	set AppleScript's text item delimiters to "\\|"
	set theExpression to processName as string
	set AppleScript's text item delimiters to ""
	
	set isRunning to every paragraph of (do shell script "ps -acwx -o command | grep -ix " & quoted form of theExpression & " || echo 'false'")
	
	if isRunning contains "false" then
		return false
	else
		return true
	end if
	
end ProcessRunning

Final version of AppleScript to start MAMP’s Apache and MySQL servers at login

 

 

Hat Tip: Start MAMP at login without password request at Mac OS X Hints and How can I find out if certain processes (multiple) are running? at MacScripter.

About the author

A native Brit exiled in Japan, Steve spends too much of his time struggling with the Japanese language, dreaming of fish & chips and writing the occasional blog post he hopes others will find helpful.

6 responses

5 Comments

  • By adding the security command to the list of authorised applications every one using the computer can now find your system password using the security command. Therefore it’s not much different to storing your password as cleartext in the applescript…

    • Hi Jonas,

      I’m not sure I understand. Are you saying that the security command allows other users to access my keychain? Could you please elaborate? An example would be helpful.

      Regards, Steve.

  • Steve,
    Thanks so much for your well-written and astute post. I’m new to the MAMP scene, but an old applescripter. I so much wanted to keep my MAMP ports “standard,” but was tiring of the constant password request. Your instructions should provide relief.
    Cheers,
    Dan

    • Thanks Dan. I hope it proves useful. If you have any tips on improving the script, let me know.

      Regards, Steve.

1 Pingback

Steve

Recent Comments

Recent Posts

Archives