Guide to Install Apache, MySQL, PHP on macOS Catalina 10.15

Guide to Install Apache, MySQL, PHP on macOS Catalina 10.15
0

I used to get my Local Web Development Environment Up & Running on macOS High Sierra, and now it’s quite simply to do with the new MacOS Catalina.

The following instruction will help you to handle AMP stack (Apache, MySQL, and PHP) and make it work smoothly on your MacOS Catalina 10.15.

Actually, my tutorial sets up the AMP stack in more of a traditional way using the loaded Apache and PHP and downloading MySQL and phpMyAdmin. Let’s see how quick to do this.

Enable Apache/WebSharing

It’s quick to start, stop and restart Apache using the some common command line through Terminal (Found under Applications/utilities)
Actually, Apache is built with macOS Catalina 10.15 already. To check Apache, use the following command:

httpd -v
49%20AM

  • To start Apache web sharing

sudo apachectl start
After Apache is started, localhost will works by enter the following url in to the web-browser: http://localhost

  • To stop Apache web sharing

sudo apachectl stop

  • To restart Apache web sharing

sudo apachectl restart

Document Root

Just make it simply by assuming that Document root is the folder that all data and single files are shared and published like “public_html” or “htdocs” folder on a real web-host environment.

macOS Catalina has web roots: system level web-root and user level web-root. What are differences between above web roots? Clearly, the user level one allows multiple accounts to have their own web root whilst the system one is global for all users. We actually are able to set the use both or just one.
Why should you setup the user level one with some extra tweaks in configuration files? By use of user level web-root, we don’t need to keep on authenticating as an admin user.

System Level Web Root

Not only Catalina, the default system document root on all macOS Version is located at: http://localhost/, and all data and files are shared and published from /Library/WebServer/Documents/

User Level Root

In each user account, there will be another web root folder missing by default is “~/Sites”. It does not take too long to set it up. What you need to do is making a “ Sites ” folder under root level of the account, and then it will work for sure. Once you make the Sites folder you will need to be noticed of that it has a unique icon which is a throwback from a few versions older. Make that folder before you set up the user configuration file described next.

We need to to make a few additional tweaks to get the ~/Sites folder back up and running.

In the following steps, “username” must be replaced by your own user name

* Firstly, You need to create “username.conf” and under the following directory: /etc/apache2/users/, and make some configurations:

sudo nano /etc/apache2/users/username.conf

Add the following codes into the username.conf

<Directory “/Users/username/Sites/”>
AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require host localhost

You need to change username to your actual user name. To save username.conf, use Ctrl + X, then Enter Y.

  • After that, set the 644 Permissions to username.conf:

sudo chmod 644 /etc/apache2/users/username.conf

  • Next, we need to open the main httpd.conf to make some modules allowed:

sudo nano /etc/apache2/httpd.conf

uncomment the following 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 include_module libexec/apache2/mod_include.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so

To php running, uncomment the below:

LoadModule php7_module libexec/apache2/libphp7.so

To allows user home directories, uncomment the following line:

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

To save above changes, use Ctrl + X, then Enter Y (or use Control + O in nano)

  • Modify the httpd-userdir.conf file

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

Uncomment the following line:

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

use Control + O in nano to save the change.

  • Now we need to restart the apache

sudo apachectl restart

Then this user level document root will be viewable at: http://localhost/~username/

Override .htaccess and allow URL Rewrites

It’s some dynamic to add rewrite rules into the .htaccess file under the web-root folder. However, we need to modify the httpd.conf file at line 217 and setting the AllowOverride to All.

sudo nano /etc/apache2/httpd.conf
htaccess-override

Also while here allow URL rewrites so your permalinks look clean, not ugly.

Modify the httpd.conf file just like the above step, uncomment the following line:

LoadModule rewrite_module libexec/apache2/mod_rewrite.so

PHP

PHP 7.3.8 is actually built in with macOS Catalina 10.15.0. However, we need to turn it on by just uncomment the following LoadModule php7_module libexec/apache2/libphp7.so from the httpd.conf (use sudo nano /etc/apache2/httpd.conf to edit just like the above step).

We need to restart apache to take the effects:

sudo apachectl restart

How to check if PHP works well?
Create an php file named phpinfo.php from the web-root folder.

sudo nano /Library/WebServer/Documents/phpinfo.php

Then add the following code <?php phpinfo(); ?>


To save above changes, use Ctrl + X, then Enter Y (or use Control + O in nano)

Now, access the localhost/phpinfo.php, you will see all php information:

MYSQL

MySQL is not built in macOS Catalina. By this time, the latest version of MYSQL, MySQL 8.0.17 does work well with macOS Catalina 15.0.0.

Download Latest MYSQL: https://dev.mysql.com/downloads/mysql/
Once you download completely the MYSQL, just run the .dmg file to install it.

Password for the MySQL root user need to be added.

Add Mysql to your path

After installation, in order to use MySQL commands without typing the full path to the commands you need to add the mysql directory to your shell path, (optional step) this is done in your Zsh shell profile “.zsh” file in your home directory (previous shells were bash ), if you don’t have that file just create it using vi or nano:

cd ; nano .zsh

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

The first command brings you to your home directory and opens the .zsh file or creates a new one if it doesn’t exist, then add in the line above which adds the MySQL binary path to commands that you can run. Exit the file with type “control + x” and when prompted to save the change by typing “y”. The last thing to do here is to reload the shell for the above to work straight away.

source ~/.zsh

How to change the MySQL root password

(This section is left in for reference – in previous macOS MySQL packages the password set during the installation process would fail – hence the info below. This newer version, however, seems to work).

Note that this is not the same as the root or admin password of macOS – this is a unique password for the MySQL root user.

Stop MySQL

sudo /usr/local/mysql/support-files/mysql.server stop

Start it in safe mode:

sudo mysqld_safe --skip-grant-tables

This will be an ongoing command until the process is finished so open another shell/terminal window, and log in without a password as root:

mysql -u root

FLUSH PRIVILEGES;

ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘MyNewPass’;
Change the lowercase ‘MyNewPass’ to what you want – and keep the single quotes.

\q

Start MySQL

sudo /usr/local/mysql/support-files/mysql.server start

Starting MySQL

You can actually start the MySQL server from the System Preferences.

Otherwise, use the following command line:

sudo /usr/local/mysql/support-files/mysql.server start

To check the MYSQL version, use the following command line:

/usr/local/mysql/bin/mysql -v -uroot -p

This also puts you into a shell interactive dialogue with MySQL, type \q to exit.

Fix the 2002 MySQL Socket error

Fix the looming 2002 socket error – which is linking where MySQL places the socket and where macOS thinks it should be, MySQL puts it in /tmp and macOS looks for it in /var/mysql the socket is a type of file that allows MySQL client/server communication.

sudo mkdir /var/mysql

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

phpMyAdmin

  • Firstly, we need to fix the 2002 socket error if you haven’t done so from the MySQL section.
  • Download the latest version of phpMyAdmin, then unzip it and move the folder with its contents into the document root level renaming folder to ‘phpmyadmin’.
  • Make the config folder

mkdir ~/Sites/phpmyadmin/config

  • Change the permissions

chmod o+w ~/Sites/phpmyadmin/config

  • Run the set up in the browser
    http://localhost/~username/phpmyadmin/setup/ or http://localhost/phpmyadmin/setup/
    phpMyAdmin

  • You need to create a new localhost mysql server connection, click new server .

Switch to the Authentication tab and set the local MySQL root user and the password.
Add in the username “root” (maybe already populated, add in the password that you set up earlier for the MySQL root user set up, click on save and you are returned to the previous screen.
(This is not the macOS Admin or root password – it is the MySQL root user)

Now going to http://localhost/~username/phpmyadmin/ will now allow you to interact with your MySQL databases.
MySQL%20databases

Permissions

To avoid permission issues during running the website, you have to set the web root and its contents to be writeable by all since it’s a local development it shouldn’t be a security issue.

Let’s say that you have a site in the User Sites folder at the following location ~/Sites/testsite you would set it to be writeable like so:

sudo chmod -R a+w ~/Sites/testsite

If you are concerned about security then instead of making it world writeable you can set the owner to be Apache _www but when working on files you would have to authenticate more as admin you are “not” the owner, you would do this like so:

sudo chown -R _www ~/Sites/testsite

This will set the contents recursively to be owned by the Apache user.

If you had the website stored at the System level Document root at say /Library/WebServer/Documents/testsite then it would have to be the latter:

sudo chown -R _www /Library/WebServer/Documents/testsite

Setting up Virtual Hosts in Apache on on macOS Catalina is straight forward after you have your local Web Development environment up and running: