Apache

 
 
Apache is a powerful and flexible HTTP/1.1 compliant web server. Originally designed as a replacement for the NCSA HTTP Server, it has grown to be the most popular web server on the Internet. As a project of the Apache Software Foundation, the developers aim to collaboratively develop and maintain a robust, commercial-grade, standards-based server with freely available source code.

The Apache HTTP Server is distributed at no charge for commercial or non-commercial use. For more information read the LICENSE.txt file.

Apache can be used with Microsoft Windows. The Apache HTTP Server Version 2.0 runs under Windows NT, Windows 2000 and Windows XP. You should download the version of Apache for Windows with the .msi extension. This is a single Microsoft Installer file containing Apache, ready to install and run.

The latest Apache HTTP Server version can be downloaded from: http://httpd.apache.org/
The latest Apache 1.3 documentation can be found at: http://httpd.apache.org/docs/1.3/
The latest Apache 2.0 documentation can be found at: http://httpd.apache.org/docs/2.0/
The latest Apache 2.2 documentation can be found at: http://httpd.apache.org/docs/2.2/






Setup Apache in macOS Sierra with PHP, Server Side Includes and name-based virtual hosting



Information
This tutorial setup two apache web projects (mobilefish_web and mypark_web) on a macOS.
It will host two domains (sand.mobilefish.com and sand.mypark.nl) using name-based virtual hosting.
Apache will be configured for PHP and server side includes.

Operating system used
macOS Sierra

Software prerequisites
none


Procedure
  1. Open a terminal and goto your homedirectory.

  2. Type: mkdir Sites

  3. Create two web projects called "mobilefish_web" and "mypark_web".

    Type: cd Sites
    Type: mkdir mobilefish_web
    Type: mkdir mypark_web

  4. Create file ~/Sites/mobilefish_web/index.html with the following content:

    <html>
    <head><title>mobilefish_web</title></head>
    <body>mobilefish_web</body>
    </html>


  5. Create file ~/Sites/mypark_web/index.html with the following content:

    <html>
    <head><title>mypark_web</title></head>
    <body>mypark_web</body>
    </html>


  6. Verify your username.
    Type: whoami

    You should see for example:
    robertlie

  7. Create an username.conf file, for example: robertlie.conf
    Type: cd /etc/apache2/users
    Type: sudo nano robertlie.conf

    This file has the following content:

    <Directory "/Users/robertlie/Sites/">
       Options Indexes MultiViews FollowSymLinks Includes
       AllowOverride All
       Order allow,deny
       Allow from all
       Require all granted
       AddType text/html .htmll .html .html
       AddOutputFilter INCLUDES .htmll .html .html
       AddType application/x-httpd-php .html
    </Directory>


  8. Set the file permission.
    Type: sudo chmod 644 robertlie.conf

  9. Modify the /etc/apache2/httpd.conf file (Make a backup of this file first!)
    Make the following changes, uncomment the following lines:

    LoadModule include_module libexec/apache2/mod_include.so
    LoadModule deflate_module libexec/apache2/mod_deflate.so
    LoadModule expires_module libexec/apache2/mod_expires.so
    LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
    LoadModule userdir_module libexec/apache2/mod_userdir.so
    LoadModule rewrite_module libexec/apache2/mod_rewrite.so
    LoadModule php5_module libexec/apache2/libphp5.so

    # User home directories
    Include /private/etc/apache2/extra/httpd-userdir.conf

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


    Note:
    Check if the httpd.conf does not contain errors, type: apachectl configtest

  10. Modify the /etc/apache2/extra/httpd-userdir.conf file (Make a backup of this file first!)
    Make the following changes, uncomment the following line:

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

  11. Modify the /etc/apache2/extra/httpd-vhosts.conf file (Make a backup of this file first!)
    Add the following lines:

    <VirtualHost *:80>
       DocumentRoot "/Users/robertlie/Sites/mobilefish_web"
       ServerName sand.mobilefish.com
       ErrorLog "/private/var/log/apache2/mobilefish-error.log"
       CustomLog "/private/var/log/apache2/mobilefish-access.log" common
    </VirtualHost>

    <VirtualHost *:80>
       DocumentRoot "/Users/robertlie/Sites/mypark_web"
       ServerName sand.mypark.nl
       ErrorLog "/private/var/log/apache2/mypark-error.log"
       CustomLog "/private/var/log/apache2/mypark-access.log" common
    </VirtualHost>


  12. Edit /etc/hosts file. Add at the bottom of the file the following lines:

    #Local sites
    127.0.0.1      sand.mobilefish.com
    127.0.0.1      sand.mypark.nl


  13. Restart apache
    Type: sudo apachectl restart

    Note:
    Stop Apache, type: sudo apachectl stop
    Start Apache, type: sudo apachectl start
    Show Apache version, type: httpd -v

    You will see, for example:
    Server version: Apache/2.4.23 (Unix)
    Server built: Aug 8 2016 18:10:45


  14. Open a browser.
    Type: http://www.mobilefish.com
    Type: http://sand.mypark.nl

  15. If your website allows you to upload files, you need to make the follwing changes:
    • Make sure your /etc/php.ini allows you to upload files.
      Checkout the following settings:
      • file_uploads = On
        Allow HTTP file uploads.
      • ;upload_tmp_dir =
        Temporary directory for HTTP uploaded files.
        Will use system default /private/var/tmp/ if not specified.
      • upload_max_filesize = 2M
        Maximum allowed size for uploaded files.
      • max_file_uploads = 20
        Maximum number of files that can be uploaded via a single request.
    • By default the uploaded files are stored in folder /private/var/tmp/
      You need to move these files to another folder (for example docroot/tmp) to access these files from a webpage. Use the php command move_uploaded_file() to move the files from /private/var/tmp/ to docroot/tmp

      Note:
      The file is removed immediately from /private/var/tmp/ after the the upload is done.
      Which mean you will not see this file in /private/var/tmp/.
    • Make sure the following groups and permissions are set:

      • chmod go-rwx docroot (only owner has access to docroot)
      • chmod go+x docroot (groups and others can access the docroot)
      • sudo chgrp -R _www docroot (all content belongs to group _www)
      • chmod -R go-rwx docroot (only owner has access to docroot)
      • chmod -R g+rx docroot (only owner and _www has access to docroot)
      • chmod -R g+rwx docroot/tmp (_www has write access to the tmp)

      Note:
      When a file is uploaded to /private/var/tmp/ and moved to docroot/tmp folder, the file is created by user _www (and group wheel).
      This user and group is set in the /etc/apache2/httpd.conf file.