How to Setup Drupal on MacOS

Introduction

MacOS comes with PHP 7, Apache2 http server, and SQLite built-in which is everything you need to run a Drupal site. If you want to use MySQL, that is an additional install. In this guide, we will look at how to setup Drupal on your Mac.

PHP setup

PHP should be installed by default. You can verify it by running:

php --version

Drupal setup

To use Drupal, simply download and extract the files somewhere on your disk.

The sites/default/files/ directory will need to be writable by the web server. The default user and group that run are _www and can be configured in /etc/apache2/httpd.conf file. You can use chmod and chown to grant write permissions to the server.

mkdir sites/default/files
sudo chown -R nanodano:_www sites/default/files
chmod -R 775 sites/default/files

The sites temporary files directory will need to be writable, as well as the public and private file system dirs. These directories are configured in the Drupal web interface at /admin/config/media/file-system. Follow the same chown and chmod operations for any directory or file that needs writing by the server.

You will also need to copy the default.settings.php to settings.php and make it writable to use the web interface installer.

cp sites/default/{default.,}settings.php
sudo chown nanodano:_www sites/default/settings.php
chmod 775 sites/default/settings.php

Apache setup

The best way to set up local sites with Apache is to use virtual hosts.

In your main Apache configuration file /etc/apache2/httpd.conf:

  • Ensure the php module is turned on: LoadModule php7_module libexec/apache2/libphp7.so
  • Ensure the rewrite moodule is turned on: LoadModule rewrite_module libexec/apache2/mod_rewrite.so

Then in the same file, make sure to uncomment the lines related to virtual hosts:

  • LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
  • Include /private/etc/apache2/extra/httpd-vhosts.conf

Then update the file /private/etc/apache2/extra/httpd-vhosts.conf with the entries for your virtual hosts like this example:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/Users/nanodano/mydrupal"
    ServerName www.mydrupal.local
    ErrorLog "/private/var/log/apache2/mydrupal.local-error_log"
    CustomLog "/private/var/log/apache2/mydrupal.local-access_log" common
</VirtualHost>

<Directory /Users/nanodano/mydrupal>
  Require all granted
  AllowOverride all
</Directory>

You will also need to update your /etc/hosts file to provide a local domain name that matches the ServerName of your vhost like:

127.0.0.1 www.mydrupal.local

Control the Apache2 http server with the commands:

sudo apachectl start
sudo apachectl stop

MySQL setup

This step is optional, since you can choose to use SQLite instead.

Install MySQL using the official installer. When it asks what authentication method to use, choose the option that maintains legacy support with MySQL 5.

To start MySQL server, go to the System Preferences | MySQL and start.

sudo /usr/local/mysql/support-files/mysql.server stop

NOTE: The database configuration in your Drupal settings.php may not work with localhost and might need to be 127.0.0.1.

Run the Drupal installer

After you have Apache configured and your Drupal files and permissions setup, you can start the Apache server and visit your site in the browser. In my example, my virtual host ServerName was mydrupal.local and I updated my /etc/hosts file to add mydrupal.local so I can use my browser and visit http://mydrupal.local/ and I should see the Drupal site. If you have not configured your site in the settings.php file already, it will prompt you to provide your site information.

Conclusion

After reading this, you should understand the steps involved with setting up a Drupal site on MacOS using PHP, Apache2 and MySQL.

References