Enabling Apache and Virtual Hosts on macOS

Enabling Apache and Virtual Hosts on macOS

Directory Navigation

When setting up a development environment on macOS, it’s essential to understand how to configure Apache and virtual hosts. In this article, we’ll guide you through the process of enabling Apache, turning on PHP, setting up virtual hosts, and adding DNS resolution.

Step 1: Start Apache

macOS comes with Apache pre-installed, and you can start it using the following command:

sudo apachectl start

Navigate to http://localhost in your browser to see the “It works” description, indicating a successful start of the Apache server. The default virtual host (site root) directory is located at /Library/WebServer/Documents.

Step 2: Turn on PHP in the Apache

To enable PHP support in Apache, you need to modify the httpd.conf file. Before making any changes, back up the original file:

sudo cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.bak

Then, open the file in a text editor:

sudo vim /etc/apache2/httpd.conf

Locate and remove the comment symbol (#) from the following lines:

#LoadModule php5_module libexec/apache2/libphp5.so

Step 3: Check phpinfo()

Create a new file called info.php in the /Library/WebServer/Documents directory:

cp /Library/WebServer/Documents/index.html.en info.php

Add the following code to the file:

<?php
phpinfo();
?>

Open a browser and navigate to http://localhost/info.php to see the PHP information page.

Step 4: Set the Virtual Hosts and Virtual Directories

To set up virtual hosts, you need to configure the main Apache configuration file (httpd.conf) and create a virtual hosts configuration file (vhosts.conf).

Configure the main Apache configuration file (httpd.conf):

Locate and remove the comment symbol (#) from the following lines:

#LoadModule authn_core_module libexec/apache2/mod_authn_core.so
#LoadModule authz_host_module libexec/apache2/mod_authz_host.so
#LoadModule authz_core_module libexec/apache2/mod_authz_core.so
#LoadModule dir_module libexec/apache2/mod_dir.so
#LoadModule userdir_module libexec/apache2/mod_userdir.so
#LoadModule alias_module libexec/apache2/mod_alias.so

Modify the DocumentRoot and <Directory> settings to point to the desired directory:

DocumentRoot "/Users/sunshine/Workspace"
<Directory "/Users/sunshine/Workspace">

Create a virtual hosts configuration file (vhosts.conf):

Create a new file called vhosts.conf in the /etc/apache2/extra directory:

sudo vim /etc/apache2/extra/vhosts.conf

Add the following code to the file:

<VirtualHost *:80>
    DocumentRoot "/Users/sunshine/Workspace"
    ServerName phpworkspace
    ErrorLog "/private/var/log/apache2/phpworkspace-error_log"
    CustomLog "/private/var/log/apache2/phpworkspace-access_log" common
    <Directory />
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

Step 5: Add the DNS Resolution

To add the DNS resolution, modify the /etc/hosts file:

sudo vi /etc/hosts

Add the following line to the file:

127.0.0.1 phpworkspace

Step 6: Troubleshooting

If you encounter any issues, such as a 403 error, try the following:

  • Check if the file exists at the root of the site.
  • Set the default page to display.
  • Modify the directory permissions using the following command:
sudo chmod -R 775 /Users/sunshine/Workspace

By following these steps, you should be able to enable Apache and virtual hosts on your macOS system.