Tuesday, June 30, 2015

Serving Static Content in Apache

Static content (not regularly Changed) is one of the important elements in a Web application. As a part of serving static content we use Apache server by placing it as a Front-end Server. Apache server performs better when serving static content when compared with other servers.

In this article we will see how we can configure Apache server for serving static content.

1) Create a directory called static that we use to place the static files we wish to serve

For example, the URL http://localhost/static/logo.png will send the image ./static/logo.png to the client.

2) Configure the Apache server configuration file httpd.conf with the below details

ServerName localhost

Listen 8180

<VirtualHost *:8180>
    DocumentRoot /var/www/virtual/
    ServerName myproject.local
    ErrorLog logs/
    CustomLog logs/ common

    Alias /static   /var/www/virtual/

    <Directory "/var/www/virtual/">
        Options None
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted  
     Allow from all


The main line that needs attention is
Alias /static   /var/www/virtual/

The alias directive element allows documents to be stored in the local file system other than under the DocumentRoot. URLs with a path beginning with URL-path will be mapped to local files beginning with the directory-path.

Access the URL using myproject.local:8180/static/<Static File>

No comments :

Post a Comment