Skip to content

Apache with PHP, WordPress and PHPMyAdmin

Running an apache web server on a Mac is very easy. In a previous guide of this guide I advised to use the Apache server that is shipped with macOS. However, it seems that the shipped Apache is not running with the Homebrew PHP 7.3 installation under macOS Mojave.

Instead, we install an Apache server through Homebrew using

brew install httpd

The default Apache that is shipped with the operating system will be deactivated by removing it from the system autostart

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

Before we configure the new Apache server, we first will install PHP and PHPMyAdmin.

Install PHP

Likewise Apache, there is a system default PHP installed, but we will use the homebrew installation to be more flexible with the used PHP version.

The installation is fairly easy using homebrew

brew install php

You can optionally install several and earlier versions of PHP using homebrew. E.g. if you want to install PHP 7.1 use:

brew install php@7.1

But note that you can activate only one PHP version at a time. That’s why we will just use the latest version and stick with the first installation command.

Install PHPMyAdmin

The tool PHPMyAdmin is a well-known GUI for MySQL database administration relying on Apache web server and PHP. Again, the installation is easy using homebrew:

brew install phpmyadmin

The installation routine will tell you to edit your httpd.conf to use PHPMyAdmin. We will do this in the next section when we edit the Apache configuration.

Apache configuration

Note that you now have basically two different instances of Apache installed on your system. The path to the configuration of the Homebrew instance is

/usr/local/etc/httpd

There, we need to update the file /usr/local/etc/httpd/httpd.conf and apply the following changes:

  1. In line 52 change Listen 8080 to Listen 80
  2. In line 181 remove the hashtag # in front of #LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so to activate this line and load mod_rewrite.
  3. Add a new line 182 with content LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so to activate PHP 7.
  4. In line 193 set the username to your user User user.
  5. In line 194 change the line to Group staff to set the group.
  6. In line 224 change the line to ServerName localhost.
  7. Change lines 281 to 283 to

    <IfModule dir_module>
    DirectoryIndex index.html index.php home.pl index.cgi
    </IfModule>

    Here we tell httpd to look for HTML index files first, and if none is found to look for a PHP index file. The latter two are needed for phpmyadmin.
  8. At the end of the file (should be line 535), add the following

    Include /usr/local/etc/httpd/vhosts.conf

Adding virtual hosts

To change the directory where the webpages are stored, we will define some virtual hosts. Additionally, we will add some nice local domains to access our local webpages.

First of all, create the directory where we want to store our webpages:cd ~/
mkdir Localserver

We need to make the directory accessible by setting the chmod to 755 (owner can read/write/execute, group can read, others can read):sudo chmod -R 755 ~/Localserver

We will store the local webpages in the directory ~/Localserver, that is /Users/user/Localserver, where user refers to your username.

Edit the hosts file usingsudo vi /etc/hosts

and add the following lines to the end of the file127.0.0.1 wordpress.local
127.0.0.1 phpmyadmin.local

This will allow us to access our local sites using the local domains http://wordpress.local and http://phpmyadmin.local.

With the last change in the previous section, we told Apache to include a configuration located at /usr/local/etc/httpd/vhosts.conf. So we need to create this file and add the following content:

<FilesMatch .php$>
  SetHandler application/x-httpd-php
</FilesMatch>

<VirtualHost *:80>
  ServerName localhost
  DocumentRoot "/Users/user/Localserver"
  <Directory "/Users/user/Localserver">
    Options FollowSymLinks Multiviews Indexes
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  ServerName phpmyadmin.local
  DocumentRoot "/usr/local/share/phpmyadmin/"
  <Directory /usr/local/share/phpmyadmin/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    <IfModule mod_authz_core.c>
      Require all granted
    </IfModule>
    <IfModule !mod_authz_core.c>
      Order allow,deny
      Allow from all
    </IfModule>
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  ServerName wordpress.local
  DocumentRoot "/Users/user/Localserver/wordpress/"
  <Directory "/Users/user/Localserver/wordpress/">
    Options FollowSymLinks Multiviews Indexes
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

In the latter replace user with your username.

We just told Apache that all our web server files will be located under path /Users/user/Localserver/. There, we can add a index.html file and add the content It works!. Also, we registered a local domain called http://wordpress.local/ that routes to the path /Users/user/Localserver/wordpress/ where we will put our WordPress installation, and we registered the domain http://phpmyadmin.local/ that routes to the PHPMyAdmin we previously installed using Homebrew.

Starting and stopping

Start your new Apache web server so that the changes become effective and we can test the server using the command

sudo brew services start httpd

Then you can open the website http://localhost/ and you should see a page saying

It works!

This is actually the index.html we have placed under /Users/user/Localserver/.

You can check if the PHP and mod_rewrite module are activated in Apache with

httpd -M

which will give you a list of activated modules like

$ httpd -M

Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 mpm_prefork_module (static)
 authn_file_module (shared)
 authn_core_module (shared)
 authz_host_module (shared)
 authz_groupfile_module (shared)
 authz_user_module (shared)
 authz_core_module (shared)
 access_compat_module (shared)
 auth_basic_module (shared)
 reqtimeout_module (shared)
 filter_module (shared)
 mime_module (shared)
 log_config_module (shared)
 env_module (shared)
 headers_module (shared)
 setenvif_module (shared)
 version_module (shared)
 slotmem_shm_module (shared)
 unixd_module (shared)
 status_module (shared)
 autoindex_module (shared)
 negotiation_module (shared)
 dir_module (shared)
 alias_module (shared)
 php7_module (shared)
 hfs_apple_module (shared)

Install WordPress

Download WordPress from WordPress.org. Move the downloaded file to ~/Localserver:mv ~/Downloads/wordpress-4.6.1.zip ~/Localserver/

Unpack the ZIP file:cd ~/Localserver
unzip wordpress-4.6.1.zip

The creates the directory ~/Localserver/wordpress containing all WordPress files. As a next step you can open WordPress in your browser at http://wordpress.local and follow the standard installation procedure.