Installing Apache and PHP using Homebrew on macOS and virtual hosts configuration for multiple projects.
Prerequisites
Make sure you have Homebrew installed on your macOS. If not install it by running the following command in your terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Step 1: Install Apache
To install Apache using Homebrew, open the terminal and run:
brew install httpd
brew services start httpd
brew services list
Visit http://localhost:8080
in your browser. You should see the “It works!” page, indicating that Apache is successfully installed.
Apache to listen for traffic on the port 8080. However, as HTTP traffic goes to port 80 by default, we want to listen on that port instead. We need to edit Apache configuration file.
nano /opt/homebrew/etc/httpd/httpd.conf
Listen 80
Step 2: Install PHP
Next, let’s install PHP. Run the following command:
brew install php
nano /opt/homebrew/etc/httpd/httpd.conf
ServerName localhost
LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
# PHP settings
Include /opt/homebrew/etc/httpd/extra/httpd-php.conf
/opt/homebrew/etc/httpd/extra/httpd-php.conf
:<IfModule php_module>
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
</IfModule>
Step 3: Set Up Virtual Hosts
Now, let’s set up virtual hosts to manage multiple projects easily.
First, enable the virtual hosts configuration in the main Apache configuration file by adding the following line:
nano /opt/homebrew/etc/httpd/httpd.conf
Include /opt/homebrew/etc/httpd/httpd-vhosts.conf
Next, create a new configuration file for your virtual hosts:
nano /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf
mypage.com
and the document root with desired values:<VirtualHost *:80>
ServerAdmin webmaster@mypage
.com
DocumentRoot "/path/to/your/project/mypage
"
ServerName mypage
.com
ErrorLog "/opt/homebrew/var/log/httpd/mypage
-error_log"
CustomLog "/opt/homebrew/var/log/httpd/mypage-access_log" common
</VirtualHost>
Don’t forget to create the directory for your project if it doesn’t exist:
mkdir -p /path/to/your/project/mypage
Step 4: Update Hosts File
To access your virtual host via a custom domain, we need to update your hosts file. Open the file with:
sudo nano /etc/hosts
Add the following line:
127.0.0.1 mypage.com
Step 5: Restart Apache
After making these changes, restart Apache for the configuration to take effect:
brew services restart httpd
Step 6: Test Your Setup
Now you should be able to access your virtual host by navigating to http://mypage.com:8080
in your browser. You can also create an info.php
file in your document root to test if PHP is working:
phpinfo();
http://mypage.com:8080/info.php
. You should see the PHP configuration page.Conclusion
Congratulations! You’ve successfully installed Apache and PHP on macOS using Homebrew and configured virtual hosts for your projects. This setup allows you to easily manage multiple projects without any conflicts. Happy coding!