Skip to content

Instantly share code, notes, and snippets.

@m1m1s1ku
Last active September 8, 2021 11:48
Show Gist options
  • Save m1m1s1ku/ad7f759eeed213f2a39cd91c8393d949 to your computer and use it in GitHub Desktop.
Save m1m1s1ku/ad7f759eeed213f2a39cd91c8393d949 to your computer and use it in GitHub Desktop.
Install Apache (with SSL) + PHP 7.1 + MySQL (macOS Sierra)

Install Apache (with SSL) + PHP 7.1 + MySQL

Made for macOS Sierra 10.12.5 (tested on a fresh mac)

Install brew

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Some tapping is needed :

  • $ brew tap homebrew/dupes
  • $ brew tap homebrew/php
  • $ brew tap homebrew/versions
  • $ brew tap homebrew/apache

Apache 2.4

  • Stop pre-installed apache (command can fail) :

sudo apachectl stop

  • Remove it totally :

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

  • Install Apache 2.4 with Homebrew

$ brew install httpd24 --with-privileged-ports --with-http2

$ sudo cp -v /usr/local/Cellar/httpd24/2.4.26/homebrew.mxcl.httpd24.plist /Library/LaunchDaemons

$ sudo chown -v root:wheel /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist

$ sudo chmod -v 644 /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist

$ sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist

The default documentRoot is : /usr/local/var/www/htdocs

Configuration :

  • Edit this file with root rights

$ sudo nano /usr/local/etc/apache2/2.4/httpd.conf

Change DocumentRoot if needed / User, group used by Apache

In the same block, you will find "AllowOverride none", change it by "AllowOverride all" (and remove line after if present)

Load rewrite module (uncomment in httpd.conf) : LoadModule rewrite_module libexec/mod_rewrite.so

To restart apache : $ sudo apachectl -k restart

PHP 7.1

Install with Homebrew : $ brew install php71 --with-httpd24 --with-gmp --with-xdebug

If brew tells that --with-gmp or with-xdebug doesn't exist, let him finish and run :

Xdebug : $ brew install php71-xdebug

GMP : $ brew install php71-gmp

Yaml : $ brew install php71-yaml

The PHP.ini file is located at : /usr/local/etc/php/7.1/php.ini

Linking PHP and Apache

  • Edit this file
/usr/local/etc/apache2/2.4/httpd.conf
  • Search for php5_module or php7_module, and replace the entire line with :
LoadModule php7_module    /usr/local/opt/php71/libexec/apache2/libphp7.so
  • Next you need to add after "index.html" "index.php", like that :
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

And add after :

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

Save and restart the server :

$ sudo apachectl -k restart

Checking :

Create a file named "index.php" into your DocumentRoot, example : $ nano /usr/local/var/www/htdocs/index.php and write into : <?php phpinfo();

Navigate to your Localhost to check if PhpInfo is here.

You should now have a working PHP 7.1 installation with XDebug and GMP !

Install MariaDB

Install with Brew, as always..

$ brew install mariadb

Install database for MariaDB :

$ mysql_install_db

Start the server :

$ mysql.server start

Secure installation :

$ /usr/local/bin/mysql_secure_installation

Configure VHosts

Virtual hosts could be used for "local domains" like "pma.dev" who can redirect to PhpMyAdmin

To activate uncomment in your httpd.conf :

$ sudo nano /usr/local/etc/apache2/2.4/httpd.conf

Uncomment this line :

LoadModule vhost_alias_module libexec/mod_vhost_alias.so

And :

Include /usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf

Next you will have to update vhost configuration, especially for generic localhost :

$ sudo nano /usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf

Example :

<VirtualHost *:80>
    DocumentRoot "/usr/local/var/www/htdocs"
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/usr/local/var/www/htdocs/pma"
    ServerName pma.dev
</VirtualHost>

DNSMasq (for .dev -> 127.0.0.1)

$ brew install dnsmasq

$ echo 'address=/.dev/127.0.0.1' > /usr/local/etc/dnsmasq.conf

$ sudo brew services start dnsmasq

$ sudo mkdir -v /etc/resolver

$ sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/dev'

Everything in *.dev will redirect to your 127.0.0.1 ;)

Configure SSL on Apache (Self signed)

Edit your httpd.conf

$ sudo nano /usr/local/etc/apache2/2.4/httpd.conf

Activate these modules (and uncomment last line):

LoadModule socache_shmcb_module libexec/mod_socache_shmcb.so

LoadModule ssl_module libexec/mod_ssl.so

Include /usr/local/etc/apache2/2.4/extra/httpd-ssl.conf

Add *:443 Virtual hosts in :

$ sudo nano /usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf

Example :

<VirtualHost *:443>
    DocumentRoot "/usr/local/var/www/htdocs"
    ServerName localhost
    SSLEngine on
    SSLCertificateFile "/usr/local/etc/apache2/2.4/server.crt"
    SSLCertificateKeyFile "/usr/local/etc/apache2/2.4/server.key"
</VirtualHost>

Generate SSL certificate :

$ cd /usr/local/etc/apache2/2.4
$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt

Check if everything is ok in the config :

$ sudo apachectl configtest

Restart Apache :

$ sudo apachectl -k restart

You should now have Apache with Php 7.1 / SSL (without MAMP) !

That's all folks.

@vaske
Copy link

vaske commented Oct 5, 2017

brew install httpd24 --with-privileged-ports --with-http2 --> I have problem with this line it said Warning: httpd 2.4.27_4 is already installed, and it's located on /usr/local/Cellar/httpd/2.4.27_4 how can I remove this completely?

@m1m1s1ku
Copy link
Author

brew uninstall httpd24
Should do the trick

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