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.

2021-10-16: Recent versions of MAMP use a helper tool to start/stop Apache (httpd) if bound to a privileged port below 1024. The script is unable to use this helper tool. Consequently, MAMP is unable to stop any httpd processes started by the script. As I’ve not found a solution to this, the script should no longer be used.

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 Mac OS X Lion the Keychain Scripting.app is no longer included. Prior to Mac 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 | sed 's/\"//g'")
	set thePassword to do shell script ("security find-generic-password -wl \"MAMP\"")

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 | sed 's/\"//g'")
	set thePassword to do shell script ("security find-generic-password -wl \"MAMP\"")
	
	# 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:

Dummy Content
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 | sed 's/\"//g'")
		set thePassword to do shell script ("security find-generic-password -wl \"MAMP\"")
	
		
		# 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.

11 responses

Leave a Reply to Olivia Cancel reply

10 Comments

  • Hey Steve,

    first of all thank you very much for you kind and helpful tutorials.

    I am a self-taught noob and am struggling with some strange behaviour using MAMP and this tutorial of yours. It’d be amazing if you could give me hint on what I might be doing wrong, if you find the time:

    I set up MAMP for testing a website on a local Web Server (Port 80) and use the first line of your above script (skipping the Mysql part, but I don’t need that, right?). I am running a Mac Mini with Catalina installed.

    tell application “Finder”
    do shell script “/Applications/MAMP/bin/startApache.sh &” password PASSWORD user name USERNAME with administrator privileges
    end tell

    to start the server. I exported the script as “App” to make it usable in the startup objects.

    It works flawlessly. I am not prompted with a password request and the server is running and reachable via 127.0.0.1 and in my local Wifi.

    But after restarting the computer roughly 10 times the server won’t be acessible anymore.

    If I am running the script from the script editor then I’ll get the prompt “httpd service already running” – but the server is not accessible.

    Only solution is to manually start Mamp (the “server button will be orange and still says “stop” even though the server doesn’t work), stop the servers and restart a couple of times. Then it will work again for a couple of times and then stop working again.

    Any idea what is happening here? This bevahviour is strangely irrational – I have no idea where to search but as I said: I am a total beginner.

    All the best and thanks a lot for your time
    Paul

    • Hi Paul,

      I believe this might be caused by the de.appsolute.mamphelper process which runs as the root user and starts when you start MAMP’s Apache on a privileged port – below 1024. This behaviour was introduced a few macOS versions ago by changes in macOS and/or MAMP. Unfortunately, I’ve never gotten around to fixing this. You could try setting the Apache port to a non-privileged port – say 8888 and see if it works.

      Regards, Steve.

      • Thank you so much for your very fast reply!

        Okay, so it could be a good idea to switch to another tool e.g. Xamp? Or even setting a server up without external tools? But that sounds complex.

        Setting Mamp to port 8888 works since Mamp wont ask for the password then and the script isn’t needed anymore. But I really would like to stay on port 80 to make my site directly reachable from our mobile phones without adding the :8888 to the anddress i am using. (We use a fritzbox and it is comfortable to reach the server just by typing the machine’s name in the browser).

        Manually starting mamp on port 80 after every startup does work too. But then there‘s the password promt again…

        I guess you tried a lot of things – but is there theoretically a way to close this de.appsolute.mamphelper
        Process manually from script before shutting down my machine? Would that help?

        Thank you very much again, i am sure that we‘ll find a solution (with another tool or approach) too – but i spent a lot of time testing this Mamp setup so i am not quite ready to let go. 🙂

        All the best
        Paul

        • Hi Paul,

          If you’re using port 80 for Apache, when you start the web server from the MAMP application it uses its helper tool to start the web server. You can see this in Activity Monitor if you choose View > All Processes and search for httpd. You’ll see several processes, one of which will be owned by root the others by the logged-in user. When you stop the web server, all of the httpd processes are killed.

          When MAMP’s web server is started by the script it doesn’t use the helper tool. Consequently, when you stop the web server in MAMP it may prompt you to start the helper tool, but it doesn’t kill any of the httpd processes, presumably because the helper tool was started after the httpd processes and it doesn’t know their process IDs. This is why MAMP shows the orange start button. By the way, to stop httpd processes started by the script, force quit each one in Activity Monitor starting with the one owned by root.

          I did a little experiment by starting the helper tool from the command line, confirming the helper tool was running in Activity Monitor, running the script to start MAMP’s servers and then stopping the servers in MAMP. Same problem. Even thought helper tool was running before the httpd processes started. Bottom line: I think this script is probably no longer useable.

          One possible solution is using a launchd daemon to start MAMP as root at start-up or log-in. If you then configured MAMP to start servers when starting you may have the servers running when you login. If I get a chance over the next week I’ll take a look at this.

          Regards, Steve.

          EDIT: This seems like a non-starter. It’s possible to start MAMP from the command-line as root, but having done so the web server will not start.

          • Hey there and thanks so much for diving in even deeper.

            After fiddling around some more I tried MAMP Pro and finally found out, that there‘s a checkbox for „start servers after start-up“ in the preferences. And that seems to work out just fine even with port 80!! I didn‘t know about that feature and obviously didn‘t do enough resrearch. 🙂
            And even though I‘d prefer the free version, this could be my solution if nothing else works. I‘ll have to try it out a bit longer though.

            So thanks again and please let me know if you have an idea for a more elegant solution – but at least I finally seem to understand what caused the problem after your explanation about the helper tool and will get some peace. 🙂 And I also found an „easy way out“…

            Best wishes
            Paul

  • 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