036 Apache

22 Mar 2013

Apache is a popular open source http server that runs on many operating systems. We will go through some of the common config files and their settings on both Mac OSX and AWS EC2. Lastly, we will also go through what is the .htaccess file and some of its configurations.

Download video: mp4

Sample code: Github

Similar episodes: 014 Local web servers, 033 AWS

##Background on Apache

  1. Main website
  2. eBook for download
  3. Sublimetext Syntex highlighting for Apache conf

##Things to learn with Apache

###1. install and config files

####on a mac osx

  1. MAC OSX comes with a pre-installed Apache. Check it’s version:

    ``` httpd -v

    Server version: Apache/2.2.22 (Unix) Server built: Aug 24 2012 17:16:58 ```

  2. Explore httpd.conf file contents:
    1. open the main configuration file with a text editor. it might be usually found in /etc/httpd/conf/httpd.conf as well.

      subl /private/etc/apache2/httpd.conf

    2. search for the following lines in the config file to learn about them from the surrounding comments:

      … ServerRoot … Listen … DocumentRoot

  3. open the hosts file in a text editor. it might be also found in /etc/hosts:

    subl /private/etc/hosts

  4. Change the Document Root:
    1. go to the DocumentRoot and create a simple index.html. start the apache server and visit localhost in the browser.

      sudo apachectl start

    2. change the DocumentRoot to another location with an index.html. Then stop and start the apache server

      … DocumentRoot "/Users/username/Desktop" … <Directory "/Users/username/Desktop"> …

  5. Change the vhosts - allows you to run several websites from a single computer
    1. uncomment the line in the file httpd.conf:

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

    2. edit the httpd-vhosts.conf file

      <VirtualHost *:80> ServerName project.localhost DocumentRoot "/location/to/the/project" <Directory /location/to/the/project> AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>

    3. include the name in the hosts file project.localhost

####on a linux distro (aws ec2)

  1. create a new EC2 instance and ssh into the virtual server

    ssh -i mykey.pem [email protected][public-dns]

  2. after getting inside the virtual server, install the apache server

    yum install httpd

  3. explore the various files and folders by opening them up in a text editor, nano in this case

    ``` nano /etc/httpd/conf/httpd.conf nano /etc/hosts ls -al /var/www/html


###1. .htaccess file

  1. about .htaccess:
    • .htaccess code works in the httpd.conf file, but not all httpd.conf code works in .htaccess files
    • a directory level apache configuration file
    • used generally for speeding up website, debugging and security
  2. simple rewrite url

    # simple rewrite RewriteEngine On RewriteRule ^about$ about.html

  3. add custom error pages

    ErrorDocument 404 /404.html

  4. expires headers


    ExpiresActive on ExpiresDefault "access plus 1 month" ExpiresByType image/gif "access plus 1 month"


  5. custom header

    Header set Join-Us "Looking for a job? Email us!"

##More Resources on Apache

  1. ultimate .htaccess
  2. html5 boilerplate .htaccess
  3. intro to apache
  4. .htaccess doc
  5. protect wordpress site htaccess
  6. intro to .htaccess
  7. .htaccess for SEO
  8. creative http headers and 4 ways to recruit developers
  9. mamp - Mac, Apache, MySQL, PHP

##Build Link of this Episode

Codecademy - Learn to code interactively, for free