Last Updated: September 09, 2019
·
4.268K
· raddevon

Start your MAMP dev server with Grunt

I just started a new project that requires a PHP server, but the built-in PHP server used by grunt-php is not beefy enough. Instead, I need to run the MAMP server in order to test it.

To get around this, I installed the excellent grunt-exec plugin which allows Grunt to run arbitrary commands. Add these tasks to your Gruntfile to allow you to start and stop the MAMP server:

exec: {
      serverup: {
        command: '/Applications/MAMP/bin/start.sh'
      },
      serverdown: {
        command: '/Applications/MAMP/bin/stop.sh'
      }
    }

Of course, you can name the tasks however you like. serverup and serverdown just made the most sense to me.

Now, edit your Grunt task containing a watch task. Add exec:serverup before the watch and exec:serverdown after the watch. Here's an example:

grunt.registerTask('default', ['jshint', 'concat', 'compass:dev', 'exec:serverup', 'watch', 'exec:serverdown']);

UPDATE: Although this works for bringing the server up, it does not work for taking it down. Once you end the task in your shell, none of the subsequent Grunt tasks fire, so the serverdown task never runs.

var exec = require('child_process').exec;
process.on('SIGINT', function () {
    exec('/Applications/MAMP/bin/stop.sh', function () {
        process.exit();
    });
});

Include this outside the module.exports for Grunt. This will watch for the SIGINT event — which fires when you use Ctrl-C to end your task — and will bring the server down when that event fires.

You can remove the serverdown task as it never fires anyway.

4 Responses
Add your response

This is a great article (actually the only one online which talks about using Grunt with MAMP). Just wondering if you had to do any configurations on the MAMP side to get this to work? I tried running this...I don't get any errors, and php is in fact running but when Grunt opens up the browser (at localhost:9000), I still can't run php files

over 1 year ago ·

I like this, unfortunately, when I use this workflow, the server doesn't stop. The only way I know how to get out of the watch task is "Control+C" this quits the task before it gets to the shutdown command. Is there a way you use to make this happen differently?

over 1 year ago ·

Yes, Scott. I did find a solution for this, and I was sure I had updated this post. Sorry about that. I have now updated it with the solution to stop the server. I hope this helps!

over 1 year ago ·

I'd love to get this to work but I'm getting an error

(13)Permission denied: make_sock: could not bind to address [::]:80

(13)Permission denied: make_sock: could not bind to address 0.0.0.0:80

no listening sockets available, shutting down

Unable to open logs

Exited with code: 1.

Any ideas why this is or how to fix it.

over 1 year ago ·