Install the MAMP (Mac, Apache, MySQL, PHP) stack

There are two ways that you could install (freely) the whole MAMP stack:
  1. installing the MAMP (version 4.5) software bundle is the easiest way since it is a one-click-solution for setting up a personal webserver
  2. install each components (Apache, MySQL, and PHP) separately. This second method consists in installing Apache and PHP with Homebrew, and MySQL with the package Installer. It is the hardest method but it enables you more control over the installation process, e.g you can install specific versions of each components, or install extra PHP extensions.
NOTE: MAMP 4.5, Apache 2.4.33, MySQL 8.0.11, PHP 7.2.7, and phpMyAdmin 4.8.2 are installed in the following tutorial

Contents

     2.1 Apache+PHP
          2.1.1 Install Apache+PHP
          2.1.2 Change Apache's default ports
          2.1.3 Start the Apache server
          2.1.4 Enable PHP in Apache and start PHP
     2.2 MySQL
          2.2.1 Install MySQL
          2.2.2 Start MySQL
          2.2.3 Reset the MySQL root password
          2.2.4 Fix the 2002 MySQL Socket error
          2.2.5 Install phpMyAdmin

1. Method 1: install the MAMP software bundle

Follow these instructions to install the MAMP software:
  1. Download the MAMP 4.5 download package (there is also a Windows version of MAMP).
  2. Open the downloaded installer and follow the easy steps (basically you just have to click a couple of times to select the installation destination and type)
  3. That's it! Really, it is that simple for installing the MAMP stack with this method
There are two MAMP versions: a free version (MAMP) and a paid version (MAMP Pro). MAMP Pro has many interesting features such as
  • a built-in editor
  • the possibility of using many PHP versions
  • the possibility of using other MySQL administration tools other than phpMyAdmin: Sequel Pro and MySQLWorkbench
  • additional servers and services: Dynamic DNS, Memcached, and Postfix
  • extra components available for installation: e.g. WordPress, Joomla, Drupal
This simple approach of installing the AMP stack is very appealing for various reasons such as:
  • simple installation of all the necessary components (Apache, MySQL, PHP, and many other items)
  • easy to start the Database and Web servers by just clicking on a button
  • it doesn't conflict with your other Apache, MySQL, or PHP installations
  • right out of the box, it has support for two web servers: Apache and Nginx
  • easy update of all the components
However, you might want to go for Method 2 if you want total control over each of the required components (Apache, MySQL, PHP). Maybe you are using a software that needs a particular version of a component to be able to work, or there are many components that come with MAMP that you simply don't care (e.g. Nginx or Cloud support). Also, the second method will involve a lot more steps but at the end you will get to know more about how your computer works since you will be working a lot with the command line (might not be a very appealing reason to go for method 2 but I am trying to find good reasons for going for method 2 😃).

2. Method 2: Install each components (Apache, MySQL, and PHP) separately

NOTE 1: this second method was tried on macOS Sierra 10.12.6
NOTE 2: Apache 2.4.33, MySQL 8.0.11, PHP 7.2.7, and phpMyAdmin 4.8.2 were installed

The second method consists in installing Apache and PHP with Homebrew, and MySQL with the package Installer.

2.1 Apache+PHP

2.1.1 Install Apache+PHP

Install Apache+PHP by running the following commands:
$ brew update
$ brew upgrade
$ brew install httpd php72
NOTE 1: if you don't mind not having the latest PHP version, then macOS Sierra already comes with PHP 5.6.30 and macOS High Sierra has PHP 7.1 built-in.
NOTE 2: the options --with-httpd and --with-thread-safety are not supported anymore by Homebrew. You now get the warnings:
Warning: php: this formula has no --with-httpd option so it will be ignored!
Warning: php: this formula has no --with-thread-safety option so it will be ignored!

2.1.2 Change Apache's default ports

If necessary, change Apache's default ports by editing:
  • /usr/local/etc/httpd/httpd.conf
  • /usr/local/etc/httpd/extra/httpd-ssl.conf

2.1.3 Start the Apache server

Start the Apache server, either:
  • by having launchd start httpd now and restart at login:
    $ brew services start httpd
  • or if you don't want a background service, run:
    $ apachectl start

2.1.4 Enable PHP in Apache and start PHP

Enable PHP by adding the following to /usr/local/etc/httpd/httpd.conf:
LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so

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

Also, always within /usr/local/etc/httpd/httpd.conf, make sure that the DirectoryIndex line includes index.php like this:
DirectoryIndex index.php index.html

Start PHP, either:
  • by having launchd start php now and restart at login:
    $ brew services start php
  • or if you don't want a background service, run:
    $ php-fpm

Extra notes about PHP:

2.1.5 Add your website to the DocumentRoot

By default, the DocumentRoot is mapped to /usr/local/var/www. This is where you can add all your webpages (HTML, CSS, JavaScript, PHP, etc...) for your websites. For example, you could add the following index.php file to the DocumentRoot to test that your Apache server is working fine:
<html>
  <body>
    <?php phpinfo();?>
    <h1>It works!</h1>
  </body>
</html>

Check the test webpage index.php by opening your browser @ http://localhost and you should see all the information about your PHP installation environment (e.g. PHP version, modules installed) which means that your Apache web server is working fine.

2.2 MySQL

2.2.1 Install MySQL

Download and install the MySQL package installer for macOS.

Add the mysql binary directory to your shell path:
$ vim ~/.bash_profile
export PATH="/usr/local/mysql/bin:$PATH"
$ source ~/.bash_profile
$ mysql -v

NOTE: when running MySQL shell commands with sudo, and they ask you for your password, you must enter your macOS root password, not the MySQL root password.

2.2.2 Start MySQL

If you are using macOS, you can start MySQL, either:
  • through System Preferences->MySQL
  • or via the command line:
$ sudo /usr/local/mysql/support-files/mysql.server start

2.2.3 Reset the MySQL root password

After installing MySQL, one of the first things to do is change the default MySQL root password by following these instructions:
  1. Stop the MySQL server:
    $ sudo /usr/local/mysql/support-files/mysql.server stop
  2. Re-start the MySQL server in safe-mode with the options --skip-grant-tables and --skip-networking i.e. anyone can connect without a password and with all privileges, account-management statements (e.g. ALTER USERSET PASSWORD) are disabled, and remote clients can't connect:
    $ sudo /usr/local/mysql/support-files/mysql.server start --skip-grant-tables --skip-networking
  3. Connect to the MySQL server:
    $ mysql
  4. Reload the grant tables which will re-enable the account-management statements:
    mysql> FLUSH PRIVILEGES;
  5. Change the 'root'@'localhost' account password:
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
  6. Stop the server and restart it in normal-mode without the previous options:
    $ sudo /usr/local/mysql/support-files/mysql.server stop
    $ sudo /usr/local/mysql/support-files/mysql.server start
    
  7. Connect to the MySQL server as root with the new password:
    $ mysql -u root -p
NOTE: the previous root password reset method is considered as insecure (see section B.5.3.2.3), instead see sections B.5.3.2.1(Windows) and B.5.3.2.2 (Unix and Unix-like systems) for the recommended procedure for resetting the root password which makes use of an init-file.

2.2.4 Fix the 2002 MySQL Socket error

Fix the 2002 MySQL Socket error (macOS searches for the MySQL socket in the wrong place at /var/mysql; the MySQL socket is actually in /tmp):
$ sudo mkdir /var/mysql
$ sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

2.2.5 Install phpMyAdmin


Unzip the downloaded file, and move the extracted folder phpMyAdmin-4.8.2-english to the DocumentRoot (which by default is located in /usr/local/var/www). Rename the folder to phpmyadmin so that it will be easier later when you have to type the URL to access the software.

We will setup a connection to your localhost MySQL server so you can access your MySQL databases through phpMyAdmin:
  1. Start Apache and MySQL
  2. Connect to the phpMyAdmin setup by going to http://localhost/phpmyadmin/setup
  3. Click on New server
  4. On the Authentication tab, enter your MySQL root password in the Password for config auth field
  5. Click on Apply to save the settings for the new server connection
  6. You can now go to http://localhost/phpmyadmin to interact with your MySQL server and its databases
When logging to http://localhost/phpmyadmin, you might have error messages shown in the bottom of the page (e.g. "The configuration file now needs a secret passphrase"). Fix them by following the instructions in the next section "3. Issues and fixes".

3. Issues and fixes

3.1 phpMyAdmin issue #1: The configuration file now needs a secret passphrase

If you login to http://localhost/phpmyadmin, and get the following error message:
The configuration file now needs a secret passphrase (blowfish_secret)
then the solution consists in creating the configuration file config.inc in 
/usr/local/var/www/phpmyadmin (by default, it might not be created):
$ cp config.sample.inc.php config.inc.php

Edit config.inc by adding a new passphrase which should be 32 chars long:
$cfg['blowfish_secret'] = ''; /*YOU MUST FILL IN THIS FOR COOKIE AUTH!*/

Logout from http://localhost/phpmyadmin and login again.

3.2 phpMyAdmin issue #2: The $cfg['TempDir'] (./tmp/) is not accessible. phpMyAdmin is not able to cache templates and will be slow because of this.

If you login to http://localhost/phpmyadmin, and get the following error message:
$cfg['TempDir'] (./tmp/) is not accessible. phpMyAdmin is not able to cache templates and will be slow because of this.
then the solution consists in 
  • creating a temporary folder /usr/local/var/www/phpmyadmin/tmp
  • and changing the tmp folder owner and group so that the Apache server (associated with _www) can have access to it:
$ sudo chown _www:_www tmp

3.3 phpMyAdmin issue #3: SQL error #1064

Let's say you add a user account with phpMyAdmin (version 4.8.2):

and you want to give this new user all the privileges:

When you click on Go to start creating this user, you might get the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'REQUIRE NONE WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0' at line 1

This is the faulty SQL query that phpMyAdmin constructed and executed:

These are the errors with the SQL query:
  • REQUIRE NONE and all the resource limits (e.g. MAX_QUERIES 0) must be part of the CREATE USER statement
  • the keyword AS should be replaced with the keyword BY in the CREATE USER statement
Thus, the correct SQL query for creating a new user with all the privileges is:
NOTE: the resource limits can be removed in the CREATE USER statement since by default they are set to 0.

Go to the SQL tab, and copy & paste the previous SQL query and execute it on the localhost server by clicking on Go.

Go to the User accounts tab, and check that your newly created user appears in the User accounts table

You can also logout and login with the new user credentials to make sure that your new user account is connecting fine to the MySQL server with all the privileges.

4. References

Comments

Popular posts from this blog

Deactivate conda's base environment on startup

Product review: SMONET wireless security camera system