Skip to content

Instantly share code, notes, and snippets.

@emotality
Last active October 20, 2022 03:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emotality/b885a96431fa14f3fd87b475fb147760 to your computer and use it in GitHub Desktop.
Save emotality/b885a96431fa14f3fd87b475fb147760 to your computer and use it in GitHub Desktop.
Install and configure Apache, MySQL and PHP on macOS

MAMP (LAMP stack on macOS)

macOS Mojave comes pre-installed with apache 2.4.34 and PHP 7.1.19. This guide is just to configure apache and install MySQL 5.7.25.

Apache

Make a Sites folder in your home folder

$ mkdir ~/Sites

Add a username.conf in /etc/apache2/users:

$ sudo nano /etc/apache2/users/$(whoami).conf
# Save and exit: Cntrl + X, Y, Enter

Insert/Replace with the following (replace your username) and save:

<Directory "/Users/username/Sites/">
    AllowOverride All
    Options Indexes MultiViews FollowSymLinks
    Require all granted
</Directory>

Set correct permission for config

$ sudo chmod 644 /etc/apache2/users/$(whoami).conf

Open the main httpd.conf config:

$ sudo nano /etc/apache2/httpd.conf

Scroll down and change listen address and port to:

Listen 127.0.0.1:80 # was: #Listen 12.34.56.78:80

Uncomment and save the following (Cntrl+W to search):

LoadModule deflate_module libexec/apache2/mod_deflate.so
LoadModule expires_module libexec/apache2/mod_expires.so
LoadModule userdir_module libexec/apache2/mod_userdir.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php7_module libexec/apache2/libphp7.so

# User home directories
Include /private/etc/apache2/extra/httpd-vhosts.conf

# Virtual hosts
Include /private/etc/apache2/extra/httpd-userdir.conf

Change main server configuration: User and group from:

User _www
Group _www

ServerAdmin you@example.com

#ServerName www.example.com:80

to:

User username
Group staff

ServerAdmin YOUR@EMAIL.com

ServerName localhost:80

Document root from:

DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
    ...
    AllowOverride none
    ...

to:

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

Save and exit: Cntrl + X, Y, Enter

Open userdir Apache config file:

$ sudo nano /etc/apache2/extra/httpd-userdir.conf

Uncomment the following then save and exit:

Include /private/etc/apache2/users/*.conf

Restart the Apache server:

$ sudo apachectl -k restart

MySQL

Download and install MySQL for OSX 10.11

https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.25-macos10.14-x86_64.dmg

Copy your temporary password

An awesome tool that keeps all your clipboards:
https://itunes.apple.com/us/app/copyclip-clipboard-history/id595191960?mt=12

Start MySQL: SystemPreference -> MySQL -> Start MySQL Server. Tick Automatically start server on startup.

Command to start and stop MySQL in Terminal: $ sudo launchctl load -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist $ sudo launchctl unload -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist

Open your shell profile

$ nano ~/.bash_profile

Add MySQL directory in a new line and save

$ export PATH="/usr/local/mysql/bin:$PATH"

Reload the shell

$ source ~/.bash_profile

Change your MySQL root password

$ /usr/local/mysql/bin/mysqladmin -u root -p'temp-password' password 'new-password'

To fix a possible 2002 MySQL Socket error

$ sudo mkdir /var/mysql
$ sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Verify your MySQL version if you want to:

$ /usr/local/mysql/bin/mysql -v -u root -p'newpassword'

Restart Apache and enjoy your new MAMP server! 😎

$ sudo apachectl -k restart

To configre virtual hosts, open vhosts config:

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

Remove dummy data and paste in your new host:

<VirtualHost *:80>
    DocumentRoot "/Users/username/Sites/example-project/public"
    ServerName example.local
</VirtualHost>

To use http://localhost, insert:

<VirtualHost *:80>
    DocumentRoot "/Users/username/Sites/example-project/public"
    ServerName localhost
</VirtualHost>

Remember to add your hosts to /etc/hosts:

$ sudo nano /etc/hosts

And add your hosts:

127.0.0.1   example.local
127.0.0.1   example2.local

Restart apache!

$ sudo apachectl -k restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment