Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atelierbram/9106021e798cac1f8f20bf9cc9ce4c4d to your computer and use it in GitHub Desktop.
Save atelierbram/9106021e798cac1f8f20bf9cc9ce4c4d to your computer and use it in GitHub Desktop.
How to setup Homebrew, Apache, PHP and MariaDB on macOS

How to setup Homebrew, Apache, PHP and MariaDB on macOS

This guide is intended to assist you in maintaining an up-to-date development environment for macOS using the latest versions of Homebrew, Apache, PHP, and MariaDB.

Make a backup of your computer, but also think about exporting databases of your projects in .sql files from PhpMyAdmin.

XCode Command Line Tools

If you don't already have XCode installed, it's best to first install the command line tools as these will be used by homebrew:

xcode-select --install

To set up Apache, PHP, and MariaDB, you will need to follow these steps: If Homebrew is already present, because you upgraded your OS to the latest version, consider first uninstalling the existing Homebrew installation and all of it’s packages, to start clean.

brew remove --force $(brew list --formula) --ignore-dependencies
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

Install Homebrew:

Homebrew is a package manager specifically designed for macOS that simplifies installing and managing software packages. To install Homebrew, open a terminal window and enter the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

This command will download and execute the Homebrew installation script. Once the installation is finished, you can confirm that Homebrew is functioning properly by running the following command:

brew --version
brew doctor

This should print the version number of Homebrew. If it does, then Homebrew is installed and working correctly.

If you already have the built-in Apache running, it will need to be shutdown first, and any auto-loading scripts removed. It really doesn't hurt to just run all these commands in order - even if it's a fresh installation:

sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null

Now we need to install the new version provided by Brew:

Install Apache:

To install Apache, run the following command:

brew install httpd

This will download and install Apache and any dependencies it requires. Once the installation is complete, you can start, stop or restart the Apache server by running the following command:

brew services start httpd
brew services stop httpd
brew services restart httpd

To verify that the Apache server is running, open a web browser and visit the URL http://localhost. You should see the Apache "It works!" page.

Note: If you are using macOS Catalina or later, you may need to adjust the Apache configuration to allow it to bind to the default HTTP and HTTPS ports. You can do this by editing the file /opt/local/etc/apache2/httpd.conf and adding the following lines:

Listen 80
Listen 443

Apache Configuration

Now that we have a working web server, we will want to do is make some configuration changes so it works better as a local development server.

In the latest version of Brew, you have to manually set the listen port from the default of 8080 to 80, so we will need to edit Apache's configuration file /opt/homebrew/etc/httpd/httpd.conf.

sudo vim /opt/homebrew/opt/httpd/httpd.conf

Search for the term DocumentRoot, and you should see the following line:

DocumentRoot "/opt/homebrew/var/www"

Change this to point to your user directory where your_user is the name of your user account:

DocumentRoot "/Users/your_user/Sites"

You also need to change the <Directory> tag reference right below the DocumentRoot line. This should also be changed to point to your new document root also:

<Directory "/Users/your_user/Sites">

In that same block you will find an AllowOverride setting, this should be changed as follows:

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All

Also we should now enable mod_rewrite which is commented out by default. Search for mod_rewrite.so and uncomment the line by removing the leading #.

LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

User & Group

Now we have the Apache configuration pointing to a Sites folder in our home directory. One problem still exists, however. By default, apache runs as the user _www and group _www. This will cause permission problems when trying to access files in our home directory. About a third of the way down the httpd.conf file there are two settings to set the User and Group Apache will run under. Change these to match your user account (replace your_user with your real username), with a group of staff:

User your_user
Group staff

Servername

Apache likes to have a server name in the configuration, but this is disabled by default, so search for:

#ServerName www.example.com:8080
ServerName localhost

Install PHP:

To install PHP, run the following command:

brew install php

This can take a long to time, so take your time for all of this. It will install PHP and create a default configuration file at /usr/local/etc/php/8.2/php.ini. You can edit this file to configure PHP as needed.

Once Apache and PHP are installed, you need to configure Apache to use PHP. To do this, you will need to edit the Apache configuration file, which is located at /usr/local/etc/httpd/httpd.conf. Open the Apache configuration file in a text editor and look for the following lines:

# for MacOs on intel processors:
# LoadModule php8_module libexec/apache2/libphp8.so
# for the latests MacOs on silicon processors, specify the exact location of the installed homebrew PHP version of `libphp.so`:
LoadModule php_module /usr/local/Cellar/php/8.2.10/lib/httpd/modules/libphp.so
AddType application/x-httpd-php .php

If these lines are not present, add them to the end of the file.

Save the configuration file and restart Apache. You can do this by running the following command:

brew services restart httpd

Test the installation:

To verify that PHP is working correctly with Apache, create a file called phpinfo.php in the Apache document root (this used to be /usr/local/var/www/htdocs but should by now be the Sites folder within your home directory) with the following contents:

<?php phpinfo(); ?>

Then, open a web browser and visit the URL http://localhost/phpinfo.php. This should display a page with information about your PHP installation. If you see the PHP information page, then PHP is working correctly with Apache on your system.

Install MariaDB:

To install MariaDB, run the following command:

brew install mariadb

This will install MariaDB and create a default configuration file at /usr/local/etc/my.cnf. You can edit this file to configure MariaDB as needed.

Start the Apache and MariaDB services:

To start the Apache and MariaDB services, run the following commands:

brew services start httpd
brew services start mariadb

To test the MariaDB installation, run the following command:

sudo mysql -u root

This will open the MariaDB prompt. You can run SQL commands here to interact with the database. Setup a password for 'root': On many OS distributions, MySQL and MariaDB are initialized with an unset root password, or a password that is logged into the MySQL/MariaDB error log. Use the following procedure to set a root password.

In the case where a root password has been set, you may find a temporary password in the MySQL/MariaDB error log. To check, attempt to log in as root using the command:

sudo mysql -u root

If this does not work, search the MySQL/MariaDB error log for the word ‘temporary’, like the example below. Your error log may be in a different location based upon the OS version:

grep 'temporary' /var/log/mariadb/mariadb.log

Based upon that response, you should be able to log in. For example, if the password you found was ‘abcdefghijk’, you should be able to login with the following:

mysql -uroot -pabcdefghikj

else use:

sudo mysql -u root

To change the root password, type the following at the MySQL/MariaDB command prompt:

MariaDB [(none)]> FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyN3wP4ssw0rd';
flush privileges;
exit;

NOTE: If the ALTER USER command doesn’t work, it’s usually indicative of a bigger problem. However, you can try UPDATE ... SET to reset the root password instead.

UPDATE mysql.user SET authentication_string = PASSWORD('new_password') WHERE User = 'root' AND Host = 'localhost';

Remember to reload the grant tables after this.

flush privileges;

Store the new password in a secure location.

That’s it! You should now have Apache, PHP, and MariaDB set up on your macOS system.

Download and setup PhpMyAdmin

Go to phpmyadmin.net and download the current version of PhpMyAdmin. Unzip and place in Sites folder. Go to localhost/phpmyadmin/setup/ for the set up of the root password.

Create a database and import an sql file of one of your projects

Go to WordPress - how to install for instructions of how to use PhpMyAdmin with WordPress.

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment