Apache, MySQL, and PHP

How to configure a native AMP stack on macOS

Steps and configurations to quickly setup macOS with a native AMP sever stack for use with Wordpress and other PHP based frameworks.

Apache WebSharing

  1. Use the Terminal.app found at:

    /Applications/Utilities/Terminal
  2. Type sudo apachectl start to Start the Apache Webserver.</li>

  3. Navigate to http://localhost/ with your browser.

    The text It works! will be shown. A local Apache webserver on the Mac is now running.

  4. </ol>
    1. Start the Apache Webserver with sudo apachectl start

    2. Stop the Apache Webserver with sudo apachectl stop

    3. Restart the Apache Webserver with sudo apachectl restart.

    4. Check which version of Apache is installed with httpd -v. On any computer with macOS Yosemite or higher, the version should be Apache-2.4.9 or higher.

    Note:

    To turn on/off the Apache web-server from the macOS System Preferences pane, navigate to Sharing, and check the box next to Internet Sharing or Web Sharing in older versions of macOS. Toggling the checkbox turns Apache on/off as well as typing the Terminal.app commands.

    Document Root

    The document root is the location from which website files are 'served' from the computer's file system. Apple initially configures the document root of your computer to:

    /Library/WebServer/Documents/

    Via the Finder.app, navigate to this directory and view the contents. Open index.html with Atom.app, and within the <code class="pa1 br2 lh-solid red bg-washed-red" body  </code> tags the will be the text 'It works!'. This is the file the browser is reading when you visit http://localhost/.

    Previous versions of macOS also provided an additional document root located at:

    /Sites

    Apple has stopped pre-configuring this in it's latest version of macOS. To allow Apache to use this directory to serve documents, make a directory at:

    /Users/\[username\]/Sites

    Navigate into the directory using Terminal.app by typing cd /etc/apache2/users.

    Next type sudo nano username.conf, and then enter the admin password when prompted.

    Inside the newly created username.conf type the following:

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

    Replace username with the computer's login username. If you are ever unsure what your username is, you can type whoami in the Terminal.app.

    At times, the system user permissions on the username.conf file will need to be changed for the Apache to work correctly.

    To verify the permissions type ls -sla.

    They need to be -rw-r--r-- 1 root wheel 298 May 24 12:00 username.conf.

    To change the permissions type sudo chmod 644 your-username.conf.

    Next configure Apache to use the newly created file. Type cd /etc/apache2 to navigate to the Apache main directory.

    Open the httpd.conf file with Atom.app and uncomment the following Apache modules:

    LoadModule authz_core_module libexec/apache2/mod_authz_core.so
    LoadModule authz_host_module libexec/apache2/mod_authz_host.so
    LoadModule userdir_module libexec/apache2/mod_userdir.so
    LoadModule rewrite_module libexec/apache2/mod_rewrite.so
    

    Uncomment the following include in httpd.conf:

    Include /private/etc/apache2/extra/httpd-userdir.conf

    Open the httpd-usedir.conf file and uncomment the following Apache include:

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

    Restart Apache by typing sudo apachectl restart.

    The /Sites document root will now be viewable at: http://localhost/\~username/

    PHP</h2>

    If want to be able to use templates, perform server-side scripts, or use a framework like Wordpress, you'll want to enable PHP. First, open the same httpd.conf file from the previous step and uncomment the following Apache module:

    LoadModule php5_module libexec/apache2/libphp5.so
    

    Restart Apache again sudo apachectl restart

    Next add a new phpinfo.php file.

    In Termianl.app, type cd /Users/username/Sites/.

    Next type sudo nano phpinfo.php, and add the following snippet into the file to the file:

    This will show you all the information about the version of PHP you have installed on your computer.

    MySQL</h2>

    To use MySQL with macOS, you need to download MySQL. Make sure to select the macOS ver. 10.9 (x86, 64-bit), DMG Archive version (works on 10.10). The latest version available is MySQL 5.6.21.

    Once the MySQL download is complete, open the .dmg to run the installer by dlouble clicking the file.

    To start the MySQL server from the System Preferences pane or via the command line or type the following to start MySQL. sudo /usr/local/mysql/support-files/mysql.server start.

    Note:

    Start and Stop MysQL from the System Preference.This is much easier if you tend to not use MySQL from Terminal.app, and mostly use phpMyAdmin (covered in the next step) to manage MySQL databases.

    To find the MySQL version type /usr/local/mysql/bin/mysql -v.

    Set up a root user password for MySQL. This allows for things like Wordpress and phpMyAdmin to access the database.

    Type /usr/local/mysql/bin/mysqladmin -u root password '\[your-password\]' into Terminal.app, replacing \[your-password\] with your password. Be sure to use single ‘quotes’ surrounding the password.

    Note:

    This is not the same as the macOS admin password – this is a unique password exclusively for MySQL.

    Fix the 2002 socket error – which links where MySQL places the socket and where macOS configures it by default. MySQL puts it in /tmp and macOS looks for it to be confifured at /var/mysql.

    To do do so, type sudo mkdir /var/mysql. Enter the macOS password if prompted.

    Next type sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock into Terminal.app, and again enter the macOS password if prompted.

    phpMyAdmin</h2>

    phpMyAdmin is a PHP interface used to interact with a MySQL databases. Download phpMyAdmin.

    Unzip the compress file and move the entire directory into the document root level and rename the directory to phpMyAdmin. Next create a directory named config by typing mkdir \~/Sites/phpmyadmin/config, followed by chmod o\+w \~/Sites/phpmyadmin/config to change the directory permissions. From the browser navigate to http://localhost/\~username/phpmyadmin/setup/ or http://localhost/phpmyadmin/setup/. Create a new localhost mysql server connection by clicking the New server button. Next, navigate to the Authentication tab and set the local mysql root user and the password. Add the username 'root', and the MySQL password.


    Note: This is not the macOS Admin or root password - it is the MySQL root user.

    Make sure to click the Save button, and you will be returned to the previous setup page. You need to click the </cod>Save</code> button on this page as well. Now navigate to myPhpAdmin directory, and check to make sure there is now a config.inc.php by typing ls \~/Sites/phpMyAdmin. You need to move this file to the root directory of phpMyAdmin, and then remove the /config directory. Navigating to http://localhost/\~username/phpmyadmin in the browser will now allow you to interact with your MySQL databases. If you ever need to upgrade phpMyAdmin just download the latest version and copy the config.inc.php from the existing directory into your new new installation.

    Apache Virtual Hosts

    You are now setup to serve websites from your local machine, but if you are developing multiple sites from your \~/Sites directory it is a good idea to configure a virtual host for each site. This allows each of your sites to have a unique domain. Usually this would be some flavor of dev.\[project-name\].com or dev-\[project-name\].com (replacing \[project-name\] with the name of your site).

    Navigate to /etc/apache2/ with Terminal.app by typing cd /etc/apache2/ and open the httpd.conf file by typing sudo nano httpd.conf. Uncomment the following line:

    Include /private/etc/apache2/extra/httpd-vhosts.conf

    You need to also allow another module so uncomment the following as well:

    LoadModule vhost_alias_module
    libexec/apache2/mod_vhost_alias.so

    Next you need to modify the http-vhosts.conf file. With Terminal.app type sudo nano /etc/apache2/extra/http-vhosts.conf. By default Apache provides an example virtual host.

    <VirtualHost \*:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/usr/docs/dummy-host2.example.com"
    ServerName dummy-host2.example.com
    ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
    CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
    </VirtualHost>

    Using Atom.app, copy and paste the example virtual host provide below, and extend it to suit you project's needs. Your code should look similar to the code below (replace "project-name" with the name of your site as well as "your-username" with your username).

    <VirtualHost \*:80>
    ServerName project-name.com
    ServeAlias www.project-name.com
    DocumentRoot "/Users/\[your-username\]/Sites/project-name"
    ErrorLog "/private/var/log/apache2/project-name.com-error_log"
    CustomLog "/private/var/log/apache2/project-name.com-access_log" common
    ServerAdmin your-username@project-name.com
    </VirtualHost>

    The last step is to add www.\[project-name\].com to your Mac's host file. This will resolve the localhost to your local IP.

    Start by opening your Mac's hosts file by navigating to /etc, and opening hosts with Atom.app. Add the following:</p>
    127\.0.0.1 project-name.com www.project-name.com

    Use Terminal.app to restart Apache by typing sudo apachectl restart, and voila! Visit http://\[project-title\].com in your browser to see your website. You'll notice now if you try to visit localhost in your browser, your'll receive a 403 error. To be able to still use the you Mac default document root, re-open your httpd-vhosts.conf file, and add the following virtual host:

    <VirtualHost \*:80>
    ServerName localhost
    DocumentRoot /Library/WebServer/Documents/
    </VirtualHost>

    Restart Apache again with sudo apachectl restart.Your Mac is now setup to serve websites from both \~/Sites and /Library/WebServer/Documents/. Each time you want to create a new project, simply add it to httpd-vhosts.conf, with Atom.app the add the domain to hosts file, and use sudo apachectl restart. This is a great way to setup local development environments for Wordpress, and other php based website.