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 172.16.202.95
ServerName localhost

Listen 8180

<VirtualHost *:8180>
    DocumentRoot /var/www/virtual/www.sam1.com/html/
    ServerName myproject.local
    ErrorLog logs/dummy-www.sam1.com-error_log
    CustomLog logs/dummy-www.sam1.com-access_log common

    Alias /static   /var/www/virtual/www.sam1.com/html/static/

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

</VirtualHost>

The main line that needs attention is
Alias /static   /var/www/virtual/www.sam1.com/html/static/

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