How to Install Apache, PHP, MySQL & phpMyAdmin on macOS

In this step-by-step guide we will show you how to install and configure Apache, PHP, MySQL & phpMyAdmin on macOS .

Apache & PHP are pre-installed in macOS. This guide illustrates Apache & PHP related configuration and MySQL & phpMyAdmin installation steps.

Configuring Apache

Path
Document Root /Library/WebServer/Documents/
Configuration File /etc/apache2/httpd.conf
Access Log /var/log/apache2/access_log
Error Log /var/log/apache2/error_log
vhost file (uncomment in httpd.conf) /private/etc/apache2/extra/httpd-vhosts.conf

Apache is pre-installed in macOS. To check if apache is running, open web browser and access http://localhost

If you receive below error, it means apache is currently not running.

1
2
3
This site can’t be reached 
Localhost refused to connect
ERR_CONNECTION_REFUSED

To start apache, run below command:

1
sudo apachectl start

Now access http://localhost again and it should show a webpage with message “It works!”

In macOS, apache document root is located at: /Library/WebServer/Documents/

Below screenshot displays the location:

1
Config: /etc/apache2/httpd.conf

To check apache version, run below command in terminal:

1
2
3
httpd -v
Server version: Apache/2.4.25 (Unix)
Server built:   Feb  6 2017 20:02:10

To start/stop server, run below commands in terminal:

1
2
3
sudo apachectl start
sudo apachectl stop
sudo apachectl restart

In default configuration, apache runs on port 80. You can change the port by modifying httpd.conf file.

For changing default port, open httpd.conf

1
sudo vim /etc/apache2/httpd.conf

Modify Listen 80 to Listen 8888 (or any other desired port) and save the changes.

1
# modify Listen 80 to Listen 8888

Now restart apache for configuration changes to take effect.

1
2
# restart apache
sudo apachectl restart

Access http://localhost:8888/ in your browser and apache default page should be displayed.

Below is the recored terminal session showing setup and configuration steps for apache:

Configuring PHP

PHP is installed by default in macOS. We will update configuration files to integrate php support in apache. Without this configuration, apache will not be able to process and execute php files.

To test the configuration, create a new test.php in apache document root location.

1
sudo vim /Library/WebServer/Documents/test.php

Add below code in test.php and save the changes:

1
<?php phpinfo(); ?>

Now, access http://localhost/test.php and you will php code only. It means apache is not able to execute php code yet.

To configure apache with PHP, open httpd.conf file and uncomment as mentioned below and save the changes:

1
2
3
sudo vim /etc/apache2/httpd.conf
# uncomment below line
LoadModule php7_module libexec/apache2/libphp7.so

Now, restart apache for the configuration changes to take effect.

1
sudo apachectl restart

Access http://localhost/test.php in browser again and it should now display php information.

Installing MySQL

We are installing MySQL Community Server 5.7.23. If you want to install the latest version, you can still follow below tutorial. The installation process should remain the same.

Download mysql-5.7.23-macos10.13-x86_64.dmg (macOS 10.13 (x86, 64-bit), DMG Archive). Double click on downloaded dmg file to start the installation.

mysql-5.7.23-macos10.13-x86_64.dmg

Installation steps are simple and does’t require any selection (unless you want to install MySQL to a non standard location)

 

 

Note down the temporary password. We will use this later to create a new password for MySQL server.

Once installation is finished, open System Preferences. MySQL should be visible in last row in System Preferences.

Click on MySQL icon and a new dialog should open. You should be able to view the current status of MySQL server.

Click “Start MySQL Server” button to start the server. Select checkbox “Automatically Start MySQL Server on Startup”  if you want to start MySQL server automatically whenever your computer starts up.

“The MySQL Server instance is running” message will be displayed when the server has started and is ready for client connections.

Run below command in terminal to change MySQL password:

1
/usr/local/mysql/bin/mysqladmin -u root -p‘temppassword’ password ‘newpassword’

To check the new password:

1
/usr/local/mysql/bin/mysql -uroot -p'newpassword'

We can set a command shortcut for mysql:

1
2
3
sudo ln -sfn /usr/local/mysql/bin/mysql /usr/local/bin/mysql

sudo ln -sfn /usr/local/mysql/bin/mysqldump /usr/local/bin/mysqldump

Now, to access mysql console, we can run below short command:

1
mysql -uroot -p'newpassword'

Below configuration is need if you have applications like Django & Rails connecting MySQL database.

1
2
sudo ln -sfn /usr/local/mysql/bin/mysql_config /usr/local/bin/mysql_config
sudo ln -sfn /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib

Log file location (use below command to find error log location):

1
2
3
4
5
6
7
ps auxww|grep [m]ysqld

_mysql           24604   0.0  0.2  4922944  13072   ??  Ss   11:22AM   0:00.72 /usr/local/mysql/bin/mysqld --user=_mysql 
--basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 
--plugin-dir=/usr/local/mysql/lib/plugin 
--log-error=/usr/local/mysql/data/mysqld.local.err 
--pid-file=/usr/local/mysql/data/mysqld.local.pid

Installing phpMyAdmin

Download the latest version from https://www.phpmyadmin.net/downloads/ . We have downloaded phpMyAdmin 4.8.3 for this tutorial.

Once downloaded, unzip the package:

Rename phpMyAdmin-4.8.3-all-languages to phpmyadmin:

Now, copy phpmyadmin folder to /Library/WebServer/Documents/. Just drag and drop this folder to the new location. You will have to enter admin password for copying the files.

 

Open http://localhost/phpmyadmin and phpMyAdmin login page is displayed:

Enter username as root and MySQL root password and try to login. You will get below error:

1
2
Cannot log in to the MySQL server
mysqli_real_connect(): (HY000/2002): No such file or directory

When the MySQL client connects to localhost, it attempts to use a socket file (a special type of file which allows mysql client/server communication.). MySQL places the socket in /tmp  and macOS looks for it in /var/mysql.

To solve this issue, we create a symlink at the location where macOS expects this sock file to be at.

1
2
sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Access http://localhost/phpmyadmin again and enter username root and MySQL root password. Once login, phpMyAdmin dashboard should be visible.

If you get below error instead, it means you are using MySQL temporary password and haven't created a new password yet.

1
2
3
Mysqli_real_connect(): (HY000/1862): 
Your password has expired. To log in you must change it 
using a client that supports expired passwords.

To fix this, use command line to create a new password:

1
/usr/local/mysql/bin/mysqladmin -u root -p‘temppassword’ password ‘newpassword’

Now, access http://localhost/phpmyadmin again and enter MySQL root username and password.