Apache in Mac

 

macOS comes with Apache Server by default. To start the built-in Apache server, open the Terminal app

Type sudo apachectl start and press enter

It is recommended to create a Sites directory under username folder (username is your mac login name) This directory will be your document root for any web-related stuff.

  1. Go to Mac HDD > Users > [your account folder]
  2. Create a folder with the name Sites. When the folder is created, it will generate a folder with a compass image on the folder

To be able to recognize the files putting into Sites directory, <username>.conf needed to be setup.

 

1.     Go to cd /etc/apache2/users

2.     Create  <username>.conf and enter  the following content

 

<Directory "/Users/<username>/Sites/">

AllowOverride All

Options Indexes MultiViews FollowSymLinks

Require all granted

</Directory>

 

Configure httpd.conf  file

 

1.     Go to cd /etc/apache2.

2.     Type sudo cp httpd.conf httpd.conf.bak

3.     Open httpd.conf

 

4.     Uncomment the following modules

LoadModule authn_core_module libexec/apache2/mod_authn_core.so

LoadModule authz_host_module libexec/apache2/mod_authz_host.so

LoadModule userdir_module libexec/apache2/mod_userdir.so

LoadModule include_module libexec/apache2/mod_include.so

LoadModule rewrite_module libexec/apache2/mod_rewrite.so

 

5.     Uncomment the following line for the User home directories.

Include /private/etc/apache2/extra/httpd-userdir.conf

 

6.     Replace the below two lines with your username document root. (You can comment on those two lines by putting # in front of them.

DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">

7.     Replace with the following:

Note: USERNAME needs be replaced with your username (e.g. developer)

DocumentRoot "/Users/<USERNAME>/Sites/"
<Directory "/Users/<USERNAME>/Sites/">

 

8.     Replace AllowOverride None to AllowOverride All

Your DocumentRoot configuration in httpd.conf will look like below:

DocumentRoot "/Users/<username>/Sites/"
<Directory "/Users/<username>/Sites/">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options FollowSymLinks Multiviews
    MultiviewsMatch Any
 
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride All
 
    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

 

Configure the httpd-userdir.conf file

 

1.     Go to cd /etc/apache2/extra

2.     Type sudo cp httpd-userdir.conf httpd-userdir.conf.bak and press enter. (this step is optional if you want to keep the copy of original file.)

3.     Open httpd-userdir.conf

4.     Uncomment the following line.

Include /private/etc/apache2/users/*.conf

 

There are new security defaults in macOS 13 “Ventura”. By default, other users have no access to another user's home directory. This includes the special “_www” user that is running the Apache web server. Run the following command to give the Apache web server access to the Sites folder in your home directory.

 

chmod +a "_www allow execute" ~

 

This will add an ACL permission to your home directory that will allow the Apache web server access to all subdirectories inside your home directory. The permissions on those subdirectories may allow, or deny, access to other users such as the _www user. If you wanted to be extra paranoid, you could change the permissions on all subdirectories (except for the “Sites” folder, obviously) to disallow any access from other users. But this User Tip is a minimal setup guide, so we will just do the minimum here.

 

Turn on the Apache httpd service by running the following command in the Terminal:

sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

 

This command could fail with an error: “Load failed: 37: Operation already in progress”. If so, that means your web server is already running from a previous operating system version. However, you will still need to bump Apache to reload with the configuration changes you’ve just made. Use the following command:

 

sudo apachectl graceful

 

or Type sudo apachectl restart (this step will restart the Apache server to take effect of the changes made in the config file)

 

 

Enable the PHP

macOS has built-in PHP (at least in Big Sur and prior versions) You just need to enable the PHP from the Apache’s config file. Follow the below steps to do so.

1.     Go to cd /etc/apache2 and open /etc/apache2/httpd.conf.

2.     Uncomment the LoadModule php7_module libexec/apache2/libphp7.so by removing the # in the front of that line of code.

3.     Save and restart apache. Type sudo apachectl restart

Create phpinfo() page at ~/Sites/phpinfo.php with the following content

 

<?php phpinfo(); ?>

 

Open a browser and type the following in the address bar.

http://localhost/phpinfo.php

You should be seeing a page with php information. This means, the PHP is working on your local mac server.

 

 

 

Setting up MySQL Server

  1. Go to https://dev.mysql.com/downloads/mysql/
  2. Download the installer with DMG file.
  3. Double click the MySQL server installer and Follow the instructions on the Installer.
  4. Choose ‘Use Legacy Password Encryption’
  5. Once the installation is complete, you can move the installer to the trash.
  6. Now, if you go to System Preferences, you should be seeing MySQL.
  7. If you check, it should have the green indicator showing it is up and running.

Fix the looming 2002 socket error - which is linking where MySQL places the socket and where macOS thinks it should be, MySQL puts it in /tmp and macOS looks for it in /var/mysql the socket is a type of file that allows MySQL client/server communication.

sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Download phpMyAdmin, the zip English package will suit a lot of users, then unzip it and move the folder with its contents into the document root level renaming folder to 'phpmyadmin'.

Make the config folder

mkdir ~/Sites/phpmyadmin/config

Change the permisions

chmod o+w ~/Sites/phpmyadmin/config

Run the setup in the browser

 

http://localhost/phpmyadmin/setup

You need to create a new localhost mysql server connection, click new server.

Switch to the Authentication tab and set the local MySQL root user and the password.

Now going to http://localhost /phpmyadmin/ will now allow you to interact with your MySQL databases.

 

Configuring Virtual Hosts

Edit the Apache configuration file:  /etc/apache2/httpd.conf

Find the following line:

#Include /private/etc/apache2/extra/httpd-vhosts.conf

Below it, add the following line:

Include /private/etc/apache2/vhosts/*.conf

This configures Apache to include all files ending in .conf in the /private/etc/apache2/vhosts/ directory. Now we need to create this directory

mkdir /etc/apache2/vhosts

cd /etc/apache2/vhosts

Create the default virtual host configuration file. _default.conf

Add the following configuration:

<VirtualHost *:80>

    DocumentRoot "/Library/WebServer/Documents"

</VirtualHost>

Create this file to serve as the default virtual host. When Apache cannot find a matching virtual host, it will use the first configuration. By prefixing this file with an underscore, Apache will include it first. Technically this file is not needed as it simply repeats the configuration already in httpd.conf. However, it provides a place to add custom configuration for the default virtual host (i.e. localhost).

Now you can create your first virtual host. The example below contains the virtual host configuration for my site. Of course, you will want to substitute jasonmccreary.me with your domain name.

Create the virtual host configuration file: demandnest.conf

Add the following configuration:

          <VirtualHost *:80>

         DocumentRoot "/Users/kavi/Sites/DemandNest"

         ServerName demandnest.local

         ErrorLog "/private/var/log/apache2/demandnest.local-error_log"

         CustomLog "/private/var/log/apache2/demandnest.local-access_log" common

 

         <Directory "/Users/kavi/Sites/DemandNest">

             AllowOverride All

             Require all granted

        </Directory>

</VirtualHost>

 

This VirtualHost configuration allows me to access my site from http://demandnest.local for local development.

Note: I use the extension local. This avoids conflicts with any real extensions and serves as a reminder I am developing in my local environment.

The final step is to restart Apache:  apachectl restart

If you run into any problems, run: apachectl configtest

This will test your Apache configuration and display any error messages.

Mapping the .local extension

In order to access sites locally you need to edit your hosts file.  vi /etc/hosts

Add a line to the bottom of this file for your virtual host. It should match the value you used for the ServerName configuration. For example, my site:

127.0.0.1       demandnest.local

Run the following to clear the local DNS cache:  dscacheutil -flushcache

Now you can access your site using the .local extension. For example, http://demandnest.local.

You may receive 403 Forbidden when you visit your local site. This is likely a permissions issue. Simply put, the Apache user (_www) needs to have access to read, and sometimes write, to your web directory.

If you are not familiar with permissions, read more. For now though, the easiest thing to do is ensure your web directory has permissions of 755. You can change permissions with the command:

chmod 755 some/web/directory/

 

Any time you want to add a site to Apache on your Mac, simply create a virtual host configuration file for that site and map it in your hosts file.