Configure Apache for Web Development - rajivkanaujia/alphaworks GitHub Wiki

Background

MacOS is shipped with Apache HTTP Server. This wiki provides instructions on how to configure the Apache shipped with MacOS for web development.

Goal

  • Get http://localhost working
  • Create a "Sites" folder on your Mac where you can deploy and test sites build in PHP etc.
  • Get User Root where "Sites" will resolve to. Hence http://localhost/~<whoami>/ URL will resolve successfully. Do replace with actual value.
  • $ whoami on my Mac (Terminal app) resolves into rajiv. So the actual URL on my machine will be http://localhost/~rajiv/
    $ whoami
    rajiv

In the subsequent wikis, I will provide instructions on how to install and use Apache HTTP server that was not shipped with MacOS, including how to use multiple versions of PHP.

Prerequisite

You have successfully configured JAVA_HOME. See Installing and configuring Java on Mac

Configure Apache

Get http://localhost working

Check if the Apache webserver is up and running

    $ httpd -v

Basic commands to operate the webserver

    $ apachectl /?

Start the webserver

    $ sudo apachectl start

Test if apache is working by testing on browser http://localhost. There are some errors I encountered, so I will list them -

Handling errors

Configuration blocking the localhost resolution

Test the configuration using the command mentioned below. Fix errors. There are documented ways to fix various errors if you do a simple web search. At the end, http://localhost must work.

    $ apachectl configtest 
    AH00526: Syntax error on line 20 of /private/etc/apache2/extra/httpd-mpm.conf:
    Invalid command 'LockFile', perhaps misspelled or defined by a module not included in the server configuration

For the error above following section in the configuration was commented and webserver restarted by using command $ sudo apachectl restart

    #
    # The accept serialization lock file MUST BE STORED ON A LOCAL DISK.
    #
    # Commenting this section
    # <IfModule !mpm_winnt_module>
    #     <IfModule !mpm_netware_module>
    #         LockFile "/private/var/log/apache2/accept.lock"
    #     </IfModule>
    # </IfModule>
    #

Restart apache and test configuration again

    $ sudo apachectl restart
    $ apachectl configtest 

ServerName missing in httpd.conf resolution

    $ apachectl configtest 
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using RK-MacBook-Pro-15.local. Set the 'ServerName' directive globally to suppress this message

To fix that problem, you need to edit the httpd.conf file. You can use nano to edit the file. First create a backup using

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

Since I started using Visual Studio Code, which now supports sudo, I will open the file in Visual Studio Code by using the command

    open -a /Applications/Visual\ Studio\ Code.app/ /etc/apache2/httpd.conf

Search for the word ServerName and you will find something like

    #
    # ServerName gives the name and port that the server uses to identify itself.
    # This can often be determined automatically, but we recommend you specify
    # it explicitly to prevent problems during startup.
    #
    # If your host doesn't have a registered DNS name, enter its IP address here.
    #
    #ServerName www.example.com:80

Add the following line to the file.

    #
    # ServerName gives the name and port that the server uses to identify itself.
    # This can often be determined automatically, but we recommend you specify
    # it explicitly to prevent problems during startup.
    #
    # If your host doesn't have a registered DNS name, enter its IP address here.
    #
    #ServerName www.example.com:80
    ServerName localhost

Save the file using sudo once you get the challange

Restart apache and test configuration again

    $ sudo apachectl restart
    $ apachectl configtest 

Retest the configuration and ensure there are no more errors

    $ apachectl configtest
    Syntax OK

Restart the apache if no errors are found (as a caution)

    $ sudo apachectl restart

Check http://localhost and ensure you get a successful message

Creating and testing "Sites" under User Root

Create a directory under User Root called Sites where all web development artifacts will be deployed. This step will create a User Root and Sites under it.

Create Document Root

Goal is to create a folder /Users/<login name>/Sites If the login name is "rajiv" then the folder will be /Users/rajiv/Sites Note that System Root is at /Library/WebServer/Documents/

Create a folder "Sites" under ~/ (or user root). Alternatively, use Mac's Finder to create the folder /Users/rajiv/Sites

    $ cd ~/
    $ mkdir Sites

Creating username.conf

Go to /etc/apache2/users and create the file

    $ cd /etc/apache2/users/
    $ sudo nano username.conf

Edit the file to add following lines and save the file. If the file did not exist, it will be created in the process.

    $ sudo nano username.conf
    <Directory "/Users/rajiv/Sites">
        AllowOverride All
        Options Indexes MultiViews FollowSymLinks
        Require all granted
    </Directory>

Check the permission of the file

    $ ls -lh username.conf 
    -rw-r--r--  1 root  wheel   138B Oct  4 14:47 username.conf

Needed permission for the file is "644" --> This means I can change it, everyone else can read it. Fix the above permission by chmod if needed

    $ sudo chmod 644 username.conf

Enabling apache modules that ensures PHP is working by editing httpd.conf and httpd-userdir.conf Create backups of httpd.conf and uncomment following lines using steps mentioned below:

    $ cd /etc/apache2
    $ sudo  cp httpd.conf httpd.conf.backup
    $ ls -lh httpd.conf*
    -rw-r--r--  1 root  wheel    20K Sep 28 19:16 httpd.conf
    -rw-r--r--  1 root  wheel    20K Oct  4 18:34 httpd.conf.backup

Edit the httpd.conf Note in nano, you can use ^w for find opt-w to find again

    $ sudo nano /etc/apache2/httpd.conf

Out-of the-box, the lines below should already be uncommented. If they are not, then uncomment them

    LoadModule authz_core_module libexec/apache2/mod_authz_core.so
    LoadModule authz_host_module libexec/apache2/mod_authz_host.so

Uncomment following lines

    LoadModule userdir_module libexec/apache2/mod_userdir.so
    LoadModule php7_module libexec/apache2/libphp7.so
    Include /private/etc/apache2/extra/httpd-userdir.conf

Create backups of httpd-userdir.conf and uncomment following lines using steps mentioned below:

    $ cd /etc/apache2/extra
    $ sudo cp httpd-userdir.conf httpd-userdir.conf.backup
    $ sudo nano httpd-userdir.conf

Uncomment following and save the file

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

RESTART Apache

    $ sudo apachectl restart

TEST using inprivate mode browsing

Assuming your login user name as rajiv Visit http://localhost/~rajiv/

You should see

TEST if PHP is working via browser

Create a file name it “phpinfo.php” and save it in your document root ~/Sites/phpinfo.php with the contents below, then view it in a browser.

    <?php phpinfo(); ?>

Use terminal to verify the version too

    $ php -version
    PHP 7.3.11 (cli) (built: Jun  5 2020 23:50:40) ( NTS )
    Copyright (c) 1997-2018 The PHP Group
    Zend Engine v3.3.11, Copyright (c) 1998-2018 Zend Technologies

Note: If you like the instructions here, please refer it on your posts/documentation. Contact me if there are corrections needed.

⚠️ **GitHub.com Fallback** ⚠️