Skip to content

Instantly share code, notes, and snippets.

@karlhillx
Last active August 1, 2024 16:18
Show Gist options
  • Save karlhillx/b2ccb9ea1f30ff59505311e1908c3dc4 to your computer and use it in GitHub Desktop.
Save karlhillx/b2ccb9ea1f30ff59505311e1908c3dc4 to your computer and use it in GitHub Desktop.
macOS High Sierra Setup: Homebrew + Apache + PHP + MariaDB + SSL

macOS High Sierra Setup: Homebrew + Apache + PHP + MariaDB + SSL

Homebrew Installation

First let's install Homebrew.

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

Homebrew can self-diagnose. Let's see if everything works the way it should.

$ brew doctor

Add the Homebrew taps we need.

$ brew tap homebrew/core

Apache Installation

macOS comes with Apache pre-installed, stop it and prevent it from starting on boot.

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

We don't want to use the pre-installed Apache. Instead we'll brew and configure it to run on standard ports (80/443).

$ brew install httpd

Check installation.

$ which apachectl
/usr/local/bin/apachectl

Start Apache, open browser with http://127.0.0.1 and you should see a message saying “It works!”

$ sudo apachectl -k start

Set Apache to launch on startup.

$ sudo brew services start httpd

You can watch the Apache error log in a new Terminal tab/window during a restart to see if anything is invalid or causing a problem:

$ tail -f /usr/local/var/log/httpd/error_log

Remember useful commands.

$ sudo apachectl start
$ sudo apachectl stop
$ sudo apachectl -k restart
$ sudo apachectl configtest

PHP Installation

$ brew install [email protected]
$ brew unlink [email protected]
$ brew install [email protected]

The php.ini file can be found in: /usr/local/etc/php/7.2/php.ini.

Apache PHP Setup

You have successfully installed your PHP versions, but we need to tell Apache to use them. You will again need to edit the /usr/local/etc/httpd/httpd.conf. Modify the paths as follows, comment out all but one entry:

#LoadModule php5_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp5.so
LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so

Edit /usr/local/etc/httpd/httpd.conf and uncomment these lines...

LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
LoadModule socache_shmcb_module lib/httpd/modules/mod_socache_shmcb.so
LoadModule ssl_module lib/httpd/modules/mod_ssl.so
LoadModule userdir_module lib/httpd/modules/mod_userdir.so
LoadModule vhost_alias_module lib/httpd/modules/mod_vhost_alias.so

Include /usr/local/etc/httpd/extra/httpd-userdir.conf
Include /usr/local/etc/httpd/extra/httpd-vhosts.conf
Include /usr/local/etc/httpd/extra/httpd-ssl.conf

Modify httpd.conf more...

Check DirectoryIndex includes index.php.

DirectoryIndex index.php index.html

User username
Group staff

DocumentRoot "/Users/username/Sites"
<Directory "/Users/username/Sites">
    AllowOverride All

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

Find Listen 8080 and change it:

Listen 80

Servername is disabled by default, set it to localhost:

#ServerName www.example.com:8080
ServerName localhost

Restart apache

$ sudo apachectl -k restart

PHP Switcher Script

We hard-coded Apache to use PHP 5.6, but we really want to be able to switch between versions. Luckily, some industrious individuals have already done the hard work for us and written a very handy little PHP switcher script.

We will install the sphp script into brew's standard /usr/local/bin:

$ curl -L https://gist.github.com/w00fz/142b6b19750ea6979137b963df959d11/raw > /usr/local/bin/sphp
$ chmod +x /usr/local/bin/sphp

Testing the PHP Switching

After you have completed these steps, you should be able to switch your PHP version by using the command sphp followed by a two digit value for the PHP version:

$ sphp 72

MariaDB Installation

Install MariaDB with Homebrew.

brew install mariadb

After installation, start the MariaDB server with...

brew services start mariadb

After MariaDB is started, you can connect.

mysql -uroot

$ brew install mariadb
$ brew services start mariadb
$ mysql_secure_installation

SSL/Virtual Hosts

Change default 8443 ports to 443 in the SSL configuration file.

$ vi /usr/local/etc/httpd/extra/httpd-ssl.conf

Replace 'Listen 8443' with 'Listen 443'.

Also update it here:

<VirtualHost _default_:8443>

General setup for the virtual host

DocumentRoot "/usr/local/var/www"
ServerName www.example.com:443

<VirtualHost _default_:443>

Save the file, open up /usr/local/etc/httpd/extra/httpd-vhosts.conf and add your own SSL based virtual hosts.

$ vi /usr/local/etc/httpd/extra/httpd-vhosts.conf

Create your virtual host entries that you want to use SSL with.

<VirtualHost *:443>
    DocumentRoot "/Users/your_user/Sites"
	ServerName yourdomain.com
	SSLEngine on
	SSLCertificateFile "/usr/local/etc/httpd/server.crt"
	SSLCertificateKeyFile "/usr/local/etc/httpd/server.key"
</VirtualHost>

Certificates

Generate a key and certificate.

$ cd /usr/local/etc/httpd
$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt
@sebastian684
Copy link

This is a great setup but I still get You don't have permission to access this resource. when I request https:localhost
image

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