Apache, PHP, and MySQL on macOS Catalina

PREREQUISITES:


In order to follow this guide, you should have a Mac computer with the Catalina OS installed.

 

I was using for several years  MAMP, Fywheel, as well as package managers like brew, and all work pretty well, but why not using the preinstalled Apache and PHP that is shipped in almost all macOS?

In this tutorial, I will show you how to setup/install Apache, PHP, and MySQL on macOS Catalina.
 

Open the Terminal app and switch to the root user so you can run the commands in this post without any permission issues:

sudo su -

 

Enable Apache on macOS

apachectl start

Check if works by accessing:
http://localhost

Enable PHP for Apache

First, let’s make a backup of the default Apache configuration

cd /etc/apache2/

cp httpd.conf httpd.conf.Catalina

 
Edit the Apache configuration

nano httpd.conf

 
Uncomment the following line

LoadModule php7_module libexec/apache2/libphp7.so

 

Restart Apache:

apachectl restart

 
You can verify PHP is enabled by creating a phpinfo() page in your DocumentRoot.

The default DocumentRoot for macOS Catalina is /Library/WebServer/Documents. In order to verify this, we run the command below to find out our Apache configuration.

grep DocumentRoot httpd.conf

 
Create the phpinfo() page in your DocumentRoot:

echo '<?php phpinfo(); ?>' > /Users/WebServer/Documents/info.php

Verify PHP by accessing http://localhost/phpinfo.php

 

Customizing PHP settings (php.ini)

MacOS’ PHP uses a default php.ini file based on /private/etc/php.ini.default.
To customize your PHP environment, if a php.ini file doesn’t already exist at /private/etc/php.ini, copy the default template to create a main php.ini file:

sudo cp /private/etc/php.ini.default /private/etc/php.ini

Make any changes you wish to php.ini and restart apache to reload all configuration files:

sudo apachectl restart

If you were to run phpinfo() in a PHP file from the web server, you should now see that the Loaded Configuration File property now has the value /etc/php.ini.

A very common tweak to the default PHP configuration is to allow larger file upload sizes. The post_max_size and upload_max_filesize properties are only a few megs by default. These limits can be raised as you see fit.

Many developers also tweak the max_execution_time, max_input_time, and memory_limit settings depending on their project.

Always remember to restart apache after making changes to your PHP configuration.

 

Install MySQL on macOS Catalina

Download and install the latest MySQL version for your macOS.

 
Setup
Open a new terminal and run the code below this will update the mysql path on your ~/.profile

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

 
Please run mysql_secure_installation to make your MySQL secured

/usr/local/mysql/bin/mysql_secure_installation

Recommended setup:
Strong Password? Yes
Change the root password? Yes
Remove anonymous users? yes
Disallow root login remotely? Yes
Remove test database? Yes
Reload privileges tables now? Yes

We can set a command shortcut for mysql:

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:

mysql -u root -p'yourpassword'

 
Useful mysql commands

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

 

Connect PHP and MySQL

Create symlink to ensure PHP and MySQL can communicate each other.

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

 

Edited my Apache Configuration:

mkdir /var/mysql
nano /etc/apache2/httpd.conf

 
Uncommented the following lines

LoadModule deflate_module libexec/apache2/mod_deflate.so
LoadModule expires_module libexec/apache2/mod_expires.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so

 

Virtual Hosts

Edit the Apache configuration file:

nano /etc/apache2/httpd.conf

 
Add the below line after this included #Include /private/etc/apache2/extra/httpd-vhosts.conf

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

 
Create the apache2 vhosts directory.

mkdir /etc/apache2/vhosts
cd /etc/apache2/vhosts

 
Create the default virtual host configuration file.

nano _default.conf

 
Add the following configuration:

<VirtualHost *:80>
    DocumentRoot "/Users/nuno/Sites/"
</VirtualHost>

 
The default virtual host was created for when the Apache web server can’t find a virtual host, it will use this configuration. By prefixing this file with an underscore, Apache will include it first.

Now we can create our first virtual host. The example below contains the virtual host configuration for my test site.

Create the virtual host configuration file:

nano nuno-test.conf

 
Add the following configuration:

<VirtualHost *:80>
        DocumentRoot "/Users/nuno/Sites/nuno-test/"
        ServerName nuno-test.test
        ErrorLog "/private/var/log/apache2/nuno-test.test-error_log"
        CustomLog "/private/var/log/apache2/nuno-test.test-access_log" common

        <Directory "/Users/nuno/Sites/nuno-test/">
            AllowOverride All
            Require all granted
        </Directory>
</VirtualHost>

This VirtualHost configuration allows me to access my site from http://nuno-test.test for local development.

 
The final step is to restart Apache:

apachectl restart

 
Test our apache configuration/strong>

apachectl configtest

 

Start, Stop, Restart and test Apache server config


I know it is a little bit daunting but I promise that we’ll never come back to this nasty lengthy configuration file again. Four simple commands are everything that we need to remember from now on. Start, stop, restart and configuration test.

sudo apachectl start
sudo apachectl stop
sudo apachectl restart
sudo apachectl configtest

 

Mapping our vhosts on macOS hosts file

nano /etc/hosts

 
Add your virtual host to the bottom of the file. It should match the value you used for the ServerName configuration.

127.0.0.1       nuno-test.test

 
Flush macOS DNS cache:

dscacheutil -flushcache

 

Permissions

You may receive 403 Forbidden when you visit your local site. This is likely a permissions issue. Simply put, the Apache user (_www) group (_www) needs to have access to read, and sometimes write, to your web directory.

sudo chown -R _www:_www /Users/nuno/Sites/nuno-test/*

 

Conclusion

By using the macOS preinstalled Apache and PHP we can avoid installing extra software on our macOS which sometimes could lead us to conflicts/errors/miss configuration on our Mac Operating system.

ABOUT AUTHOR

Nuno

I am a Freelance Web Developer and WordPress Expert based in London with a wealth of website development and support experience. I am great at problem solving and developing quick solutions.

LEAVE A COMMENT

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • LET'S TALK!

    Fill in the form below to make an enquiry or find my contact details on my contact page.

  • This form collects your name and email address so that I can respond to the enquiry(ies) that you submit via this website. Please check my privacy policy for the full story on how I use, protect and manage your submitted data.