How to Generate Code Sign Certificate and Install and Configure PHP on macOS with Apache
If you’re a developer working on macOS and need to set up PHP with Apache, this guide will walk you through the process step by step. We’ll use Homebrew, a popular package manager, to install PHP and configure it with Apache for local development.
Step 1: Install Homebrew
Homebrew is a package manager that simplifies software installation on macOS. To install it, open the Terminal and enter:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
After installation, verify it by running:
brew --version
Step 2: Install PHP Using Homebrew
Once Homebrew is installed, you can install PHP by running:
brew install php
This will install the latest version of PHP along with necessary dependencies. You can check the installed version with:
php -v
Step 3: Generate Certificate in Mac Keychain
For enhanced security, generate and store an authentication certificate in your Mac’s keychain. This step ensures that PHP modules can be authenticated correctly within Apache.
Step 4: Sync and Sign the PHP Module
To allow PHP to run securely with Apache, we need to sign the PHP module using an authority certificate. Run the following command:
codesign --sign "webwila" --force --keychain ~/Library/Keychains/login.keychain-db /opt/homebrew/opt/php/lib/httpd/modules/libphp.so
Make sure to replace "
with your own certificate name if different.webwila
"
Step 5: Link PHP in Apache
Next, link PHP to Apache by adding the following line in your Apache configuration file:
LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so "webwila"
This step ensures that Apache recognizes PHP as a module and can process PHP scripts.
Step 6: Restart Apache and Verify Configuration
Once everything is set up, restart Apache with the command:
sudo apachectl restart
To confirm that your configuration is correct, run:
sudo apachectl configtest
If there are no syntax errors, your PHP setup is now successfully configured with Apache.
Happy coding!