Starting built in Apache server in Mac OS X 10.9 (Mavericks)

Learn starting built in apache server in mac os x 10.9 (mavericks) with practical examples, diagrams, and best practices. Covers apache, webserver, osx-mavericks development techniques with visual ...

Starting the Built-in Apache Server in macOS 10.9 (Mavericks)

Hero image for Starting built in Apache server in Mac OS X 10.9 (Mavericks)

A comprehensive guide to enabling, configuring, and troubleshooting the Apache web server pre-installed on macOS 10.9 Mavericks for local development.

macOS 10.9 Mavericks, like its predecessors, comes with a pre-installed Apache HTTP Server. This built-in web server is an excellent tool for local development, allowing you to host websites and test web applications without needing a separate server environment. This article will guide you through the process of enabling, configuring, and managing Apache on your Mavericks system, ensuring you can get your local development environment up and running smoothly.

Enabling the Apache Web Server

Unlike some earlier macOS versions where Apache could be toggled via System Preferences, Mavericks requires command-line interaction to start and stop the server. The apachectl utility is your primary tool for managing the Apache service. Before you begin, it's good practice to ensure your system is up to date, though it's not strictly necessary for Apache itself.

sudo apachectl start

Command to start the Apache web server.

After running this command, Apache should be active. You can verify its status by opening your web browser and navigating to http://localhost or http://127.0.0.1. You should see a page displaying "It works!" which confirms Apache is serving content.

Understanding Apache Configuration Files

Apache's behavior is controlled by its configuration files. On macOS Mavericks, the main configuration file is httpd.conf. It's crucial to understand its location and how to modify it safely. Additionally, Apache uses a users directory for individual user websites, which requires separate configuration.

flowchart TD
    A[User Request] --> B("http://localhost/~username/")
    B --> C{Apache Server}
    C --> D["/etc/apache2/httpd.conf"]
    D --> E["/etc/apache2/users/username.conf"]
    E --> F["/Users/username/Sites/"]
    F --> G[Serve Content]

Apache request flow for user-specific websites on macOS.

The primary configuration file is located at /etc/apache2/httpd.conf. For user-specific websites, Apache looks for configuration files in /etc/apache2/users/. Each user typically has a file named username.conf within this directory, pointing to their Sites folder.

sudo nano /etc/apache2/httpd.conf

Command to open the main Apache configuration file for editing.

Enabling User Directories and PHP

To host websites in your user's Sites folder (e.g., ~/Sites), you need to enable the userdir_module and configure a user-specific .conf file. Additionally, if you plan to develop dynamic web applications, you'll likely need to enable PHP.

1. Enable Userdir Module

Open /etc/apache2/httpd.conf and uncomment the line LoadModule userdir_module libexec/apache2/mod_userdir.so. Also, uncomment Include /private/etc/apache2/extra/httpd-userdir.conf.

2. Configure User-Specific Sites

Navigate to /etc/apache2/users/ and create a file named yourusername.conf (replace yourusername with your actual macOS username). Add the following content to this file, ensuring the Directory path points to your Sites folder:

<Directory "/Users/yourusername/Sites/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

3. Enable PHP (Optional)

In /etc/apache2/httpd.conf, uncomment the line LoadModule php5_module libexec/apache2/libphp5.so. This will enable PHP processing for your Apache server.

4. Restart Apache

After making any changes to configuration files, you must restart Apache for them to take effect. Use the command sudo apachectl restart.

Once these steps are completed, you can create an index.html or index.php file in your ~/Sites directory and access it via http://localhost/~yourusername/ in your web browser. This setup provides a robust local development environment.