How to deploy applications

In this article you will learn how to deploy a application under Linux / UNIX environments.

You can refer to our article titled, “How to install” if you don’t know what is or if is currently not installed on your machine / server.


The "main" application file in is typically called "".


Note: We will be deploying a simple "Hello World" application to test our installation of You will learn how to follow this procedure to deploy applications in the future.


First, you need to create all child and parent directories by using the command below:


sudo mkdir -p /var/www/


Save a file called “” under directory /var/www/ Please note that this directory should also be same in the Apache virtual host configuration which we will cover later in this article.


sudo vim /var/www/



import web


urls = (

    '(.*)', 'hello'


app = web.application(urls, globals())


class hello:

    def GET(self, name):

        if not name:

            name = 'World'

        return 'Hello, ' + name + '!'


if __name__ == "__main__":


We all know there are numerous ways to deploy a application. There are a number of web servers already in the market like Apache, Lighttpd, NGINX, and Cherokee and more to come.


We will be using the most famous Apache web server along with the module mod_wsgi, which is used to parse and serve python applications.


WSGI is an evolution of the CGI standard and has performance comparable to FastCGI and embedded interpreter application deployments.


We assume at this stage that you already have installed Apache, python, mod_wsgi configured and it is ready to be used.


If you don’t know how to install Apache, python, mod_wsgi, please refer to one of our pervious articles on “How to setup python with mod_wsgi and apache”.


Enable Apache web server’s mod_rewrite which is used for the URL rewriting by using the command below.

sudo a2enmod rewrite


WSGI requires a very minor change to be made to your application. Add the content below to end of the file we created earlier called “”.


app = web.application(urls, globals(), autoreload=False)

application = app.wsgifunc()


Please refer to your previous created virtual host, which we did in our previous article called “”.

The virtual host file for “” looks like below after the modification.






       DocumentRoot /var/www/

       ErrorLog /var/www/

       CustomLog /var/www/ combined


    WSGIScriptAlias / /var/www/

    Alias /static /var/www/


    <Directory /var/www/>

      SetHandler wsgi-script

      Options ExecCGI



    AddType text/html .py


    <Location />

      RewriteEngine on

      RewriteBase /

      RewriteCond %{REQUEST_URI} !^/static

      RewriteCond %{REQUEST_URI} !^(/.*)

      RewriteRule ^(.*)$$1 [PT]




If your Apache virtual host is not yet enabled, you may enable it using the command below. If it is already enabled there is no need to do it now and you can kindly skip this step.


sudo a2ensite


Finally, restart your Apache web server by using the command below:


sudo /etc/init.d/apache2 restart


sudo service apache2 restart


A small overview of what we have done above, all the requests for the domain will be handled by WSGI, with the application files located in "/var/www/".


All static files can be stored in /var/www/ and served directly by Apache. Furthermore, the rewrite rules convert requests so that paths beneath are handled by the application without including in the URL.


For example, the request for would be processed as but requests for would not be rewritten by the mod_rewrite of Apache and content would be served from the directory "/var/www/".


By opening the web browser, you should be able to see our nice “Hello world” application based on


If yes, congratulations! You have now deployed a web application based on

Posted in
Last update:
2015-12-03 20:39
Average rating:0 (0 Votes)