For a local web design and development environment, running Apache with PHP is key. This article explains how to run Apache Webserver with PHP, WordPress and PHPMyAdmin setup on macOS.
Running an Apache web server on a Mac is very easy. In fact, older Macs were shipped with a web server. In a previous version of this article I advised to use the out-of-the-box web server that is shipped with macOS. However, it seems that the shipped Apache is not compatible with recent installations of Homebrew PHP since macOS Mojave. So I rather recommend to deactivate the out-of-the-box web server and instead install your own Apache server through Homebrew with the command:
brew install httpd
To deactivate the default Apache that is shipped with the operating system remove 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 install PHP and PHPMyAdmin. Afterwards, we will dig into the Apache configuration and how to add virtual hosts.
To install web applications like WordPress and to make use of PHPMyAdmin, we need to first install MySQL. See this Guide on how to install MySQL using Homebrew.
Likewise Apache, there is a system default PHP installed, but we will also use the Homebrew PHP installation to be more flexible with the used PHP version. The installation is fairly easy using the command:
brew install php
You can optionally install several and earlier versions of PHP with Homebrew. For example, if you want to install PHP 7.1 use:
brew install firstname.lastname@example.org
Note that you can have several PHP versions installed but activate only one version at a time. That is why we will just use the latest version and stick with the first installation command.
The tool PHPMyAdmin is a well-known web-based user interface for MySQL database administration programmed in 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.
Note that you now have basically two different instances of Apache installed on your system: The out-of-the-box web server and the web server installed by Homebrew. The path to the configuration of the Homebrew instance is
/opt/homebrew/etc/httpd/. In this directory, we need to update the file
/opt/homebrew/etc/httpd/httpd.conf and apply the following changes:
- In line 52 change
- In line 181 remove the hashtag
#in front of
#LoadModule rewrite_module lib/httpd/modules/mod_rewrite.soto activate this line and load
- Add a new line 182 with content
LoadModule php_module LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.soto activate your recent PHP you just installed through Homebrew.
- In line 193 set the username to your user
- In line 194 change the line to
Group _wwwto set the group.
- In line 224 change the line to
- Change lines 281 to 283 to
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.
- At the end of the file (should be line 535), add the following:
Adding virtual hosts
To change the directory where we will store our websites, we define some virtual hosts. Additionally, we add some nice local domains to access our local websites.
We will store the local websites in the directory
~/Localserver, that is
/Users/user/Localserver, where user refers to your respective username. First of all, create the directory:
cd ~/ mkdir Localserver
Next, we need to make the directory accessible by setting the chmod to 755 (owner can read/write/execute, group can read, others can read too)
sudo chmod -R 755 ~/Localserver
To add some local domains, we edit the hosts file using the following command:
sudo vi /etc/hosts
Note that we need superuser rights to edit the hosts file. At the end of the file, we can register new local domains. Add the following lines and save the file:
127.0.0.1 wordpress.local 127.0.0.1 phpmyadmin.local
The two lines allow us to access our local sites using the local domains
With the last change in the previous section about Apache configuration, we told Apache to include a configuration file located at
/opt/homebrew/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 "/opt/homebrew/share/phpmyadmin" <Directory /opt/homebrew/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. With the configuration we tell Apache that all our web server files will be located under the path
/Users/user/Localserver/. There, we can add a
index.html file with 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 Apache
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
This is actually the
index.html we have placed under
You can check if the PHP and mod_rewrite module are activated in Apache on the Terminal with the command:
The command should print you a list of activated modules like this:
$ 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)
Download WordPress from WordPress.org. Move the downloaded file to
mv ~/Downloads/wordpress-4.6.1.zip ~/Localserver/
Unpack the ZIP file:
cd ~/Localserver unzip wordpress-4.6.1.zip
This 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 of WordPress.