* MAPP: macOS – Apache – PHP – Postgres
Introduction
Since MacOS Monterey, PHP has been removed. I tried various options, including MAMP and XAMPP, but they didn’t meet my expectations. As a developer, I may need a way to install PHP on macOS on my own, not relying on a stack tool. So here are the steps I worked on to make PHP run on macOS Sonoma M1.
1. Stop Apache on macOS M1
I will disable the native Apache on macOS. In this post, we will work with packages from homebrew:
sudo apachectl stop
This command uses the Apache Control Program apachectl with sudo to stop the Apache Web Server.
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
This command uses launchctl – (Launchd, the macOS init, and service management daemon) with sudo to unload the Apache launch daemon configuration. It prevents Apache from starting automatically at system boot.
– unload: Stops and unloads the service.
– -w : Override the Disabled key and set it to false.
– /System/Library/LaunchDaemons/org.apache.httpd.plist : specifies the property list (plist) file containing the Apache service configuration.
– 2>/dev/null
: Redirects standard error output to /dev/null
, discarding any error messages produced by the command.
2. Install the homebrew tool to download and setup package reference
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
This command will download and execute the Homebrew installation script. Follow any on-screen prompts or instructions during the installation process. After the installation, you might see a message indicating Homebrew is ready. It will also provide instructions on how to add Homebrew to your shell profile. Follow those instructions to configure Homebrew.
Run this command to verify Homebrew:
brew --version
This will display a version of Homebrew
3. Use homebrew to install httpd
brew install httpd
This command will download and install the Apache HTTP Server. Follow any additional instructions or prompts that appear during the installation.
4. Start the Apache server (httpd)
brew services start httpd
This command will start the Apache server and set it to automatically start at login.
5. Check the status of httpd services
By default, httpd runs on port 8080. To verify that the Apache server – httpd is running, open a web browser and navigate to http://localhost:8080. You should see a message indicating that the Apache server – httpd is working. Alternatively, you can use the command:
brew services info httpd
If the Running is checked. The httpd has started successfully.
httpd (homebrew.mxcl.httpd)
Running: ✔
Loaded: ✔
Schedulable: ✘
User: <user_name>
PID: <PID_code>
If it fails, please redo step 1 and then retry step 4.
6. Install the php package
brew install php@8.2
In this post, I will use PHP version 8.2.
7. Verify php8.2
You can use the following commands to check the PHP version:
> php -v
PHP 8.2.12 (cli) (built: Oct 24 2023 19:22:16) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.12, Copyright (c) Zend Technologies
with Zend OPcache v8.2.12, Copyright (c), by Zend Technologie
If you can see this result, it means PHP was installed and added to the command path successfully.
8. Enable PHP in the Apache Server – httpd
To enable PHP in the Apache HTTP Server (httpd), you must configure the Apache server – httpd to recognize and process PHP files. Here are the general steps:
sudo vim /opt/homebrew/etc/httpd/httpd.conf
// Add this line below the list of httpd modules.
LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so
To verify the existence of the module, you can use the following command:
> test -f /opt/homebrew/opt/php/lib/httpd/modules/libphp.so && echo "File is existed"
File is existed # This means the module exists, and we can use this path.
9. Enable httpd to run the PHP file on the site
By default, httpd will interpret PHP files as plain text and won’t execute them with other libraries. Below the line where the PHP module is added (as mentioned in step 8), we will include the following code:
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
- <FilesMatch \.php$>: This configuration section uses a regular expression to match files with a .php extension. The \ before the dot (.) is used to escape the dot since a dot typically matches any character in regular expressions. The $ at the end ensures that the match occurs at the end of the filename.
- SetHandler application/x-httpd-php: This directive sets the handler for the matched files to application/x-httpd-php. In other words, it tells Apache – httpd that files matching the specified pattern are the PHP scripts.
10. Finally, add index.php to DirectoryIndex
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
- <IfModule dir_module>: This checks whether the dir_module is loaded. The dir_module is responsible for handling directory-related configurations.
- DirectoryIndex index.php index.html: This directive sets the order of filenames that Apache – httpd should look for when a directory is requested. In this case, it specifies that if a directory is requested, Apache – httpd should first look for index.php, and if not found, it should then look for index.html.
11. Testing whether PHP is working with httpd
# Create a file named 'info.php' in the directory '/opt/homebrew/var/www,' which contains resources for httpd.
echo "<?php phpinfo();" > /opt/homebrew/var/www/info.php
curl -I http://localhost:8080/info.php
# Result
# HTTP/1.1 200 OK
# Date: Mon, 13 Nov 2023 14:06:08 GMT
# Server: Apache/2.4.58 (Unix) PHP/8.2.12
# X-Powered-By: PHP/8.2.12
# Content-Type: text/html; charset=UTF-8
If the php and httpd setup is correct, you should see headers related to the info.php file.
12. Moodle Sites
In this post, I will use Moodle LMS framework to test Apache and PHP.
cd /opt/homebrew/var/www
git clone https://github.com/moodle/moodle.git
13. Install the PostgreSQL package to set up the database
brew install postgresql@14
14. Create a new database
# This command is used to switch to the PostgreSQL - postgres user account.
sudo -i -u postgres psql
# Set a password for postgres user.
\password postgres
# create database name: moodle.
CREATE DATABASE moodle;
15. Install the Moodle site
Type http://localhost:8080/moodle in your web browser and follow the steps to complete the Moodle setup. Once you have completed the Moodle framework setup and configuration, your MAPP setup on macOS M1 is complete.
16. Custom the port in HTTP (optional)
Somehow, we need to customize the port number for clarity. You can follow these steps:
sudo vim opt/homebrew/etc/httpd/httpd.conf
# Searching keyword "8080" then change it to 80
Listen 8080 -> Listen 80
Now, by typing http://localhost/moodle, you can access the Moodle site without specifying port 8080. Alternatively, you can use your IP address to host your application. In my case, it’s http://192.168.0.81/moodle.
Conclusion
To install Apache (httpd) on macOS M1, stop the native Apache service first. Afterward, use Homebrew to install the required packages. Here are a few links that might be helpful:
- homebrew: https://brew.sh/
- httpd: https://formulae.brew.sh/formula/httpd#default
- php@8.2: https://formulae.brew.sh/formula/php#default
- PostgreSQL: https://formulae.brew.sh/formula/postgresql@14#default
Thank you for reading, and good luck.