Running a PHP application on Big Sur using Apache

Running a PHP application on Big Sur using Apache

If you have a PHP application and want a quick approach for running it on a mac with the latest OS version, then this tutorial might be useful.

Testing and launching the Apache server

Big Sur already comes with Apache installed. You can check the current version by running:

~ httpd -v 
Server version: Apache/2.4.46 (Unix)
Server built:   May  8 2021 03:38:34

In order to start the server you can run:

~ sudo apachectl start

Open  localhost in any browser and you should see:

Configuring Apache to run PHP 7

Big Sur also comes with PHP installed, but the local Apache configs may not support running PHP by default.

In order to activate this setting, open /etc/apache2/httpd.conf with nano or any other text editor and uncomment the following line:

LoadModule php7_module libexec/apache2/

PHP is now enabled, all you need to do is restart the Apache web server for the changes to take effect:

~ sudo apachectl restart

Setting up mySQL

Ensure that you have mySQL correctly installed and running on your machine.

  • you can download the .dmg from here, then follow the instructions on the wizard.
  • once the wizard is done go to System Preferences > MySQL and ensure that the server is running as shown below:

In order to have mySQL binaries available via command line, you can also add /usr/local/mysql/bin/ to the PATH. In my case I did:

~ nano ~/.zshrc
export PATH=/usr/local/mysql/bin:$PATH

Finally, you need to ensure PHP and MySQL can communicate with one another, which you can achieve by running:

~ cd /var
~ sudo mkdir mysql
~ cd mysql
~ sudo ln -s /tmp/mysql.sock mysql.sock

Serving the application

Check Apache's default DocumentRoot on /etc/apache2/httpd.conf:

As you can see, in my case it's /Library/WebServer/Documents.

This means that this is the directory where you should put your PHP application.

Once the app is inside this directory you'll be able to see it running by going to  http://localhost/<path-to-your-initial-file>.