Install the MAMP (Mac, Apache, MySQL, PHP) stack
There are two ways that you could install (freely) the whole MAMP stack:
Also, always within /usr/local/etc/httpd/httpd.conf, make sure that the DirectoryIndex line includes index.php like this:
Start PHP, either:
Extra notes about PHP:
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.
Add the mysql binary directory to your shell path:
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.
Edit config.inc by adding a new passphrase which should be 32 chars long:
Logout from http://localhost/phpmyadmin and login again.
3.2 phpMyAdmin issue #2: The
If you login to http://localhost/phpmyadmin, and get the following error message:
- 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
- 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.3 Start the Apache server
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
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:
- Download the MAMP 4.5 download package (there is also a Windows version of MAMP).
- 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)
- 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:
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:
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
$ brew services start httpd
$ 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:
Also, always within /usr/local/etc/httpd/httpd.conf, make sure that the DirectoryIndex line includes index.php like this:
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:
- php.ini and php-fpm.ini are to be found in /usr/local/etc/php/7.2
- Get PHP version:$ php -v
- Get all the PHP modules installed:$ php -m
- If you want to install PHP extensions (e.g. php-xdebug), you must make use of the pecl command. See https://medium.com/@romaninsh/install-php-7-2-xdebug-on-macos-high-sierra-with-homebrew-july-2018-d7968fe7e8b8 for more information on how to install PHP extensions.
- To list all the services (e.g. php, httpd) launched through Homebrew:
$ brew services list
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: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:
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:
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:- Stop the MySQL server:
- 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 USER, SET PASSWORD) are disabled, and remote clients can't connect:
- Connect to the MySQL server:
- Reload the grant tables which will re-enable the account-management statements:
- Change the 'root'@'localhost' account password:
- Stop the server and restart it in normal-mode without the previous options:
- Connect to the MySQL server as root with the new password:
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):
2.2.5 Install phpMyAdmin
Download the latest release of 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:
We will setup a connection to your localhost MySQL server so you can access your MySQL databases through phpMyAdmin:
- Start Apache and MySQL
- Connect to the phpMyAdmin setup by going to http://localhost/phpmyadmin/setup
- Click on New server
- On the Authentication tab, enter your MySQL root password in the Password for config auth field
- Click on Apply to save the settings for the new server connection
- 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:
then the solution consists in creating the configuration file config.inc in
/usr/local/var/www/phpmyadmin (by default, it might not be created):
Edit config.inc by adding a new passphrase which should be 32 chars long:
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:
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:
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:
When you click on Go to start creating this user, you might get the following error:
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 theCREATE USER
statement- the keyword
AS
should be replaced with the keywordBY
in theCREATE 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
- Coursera on "Installing MAMP on Macintosh"
- Lynda course on "Install MAMP on macOS"
- Blog post on "How to Develop Locally with MAMP"
- Wikipedia article on "MAMP"
- Blog post on "Install PHP 7.2 on MacOS High Sierra (with Homebrew)"
- PHP Official manual: Installation on macOS
- Tutorial to "Get Apache, MySQL, PHP and phpMyAdmin working on macOS Sierra"
- Digital Ocean Tutorial on "How To Reset Your MySQL or MariaDB Root Password" [Linux]
- Blog post on resetting the MySQL root password with the command-line
Comments
Post a Comment