Showing posts with label apache. Show all posts
Showing posts with label apache. Show all posts

Monday, August 15, 2011

How to Serve Django Static Files by Apache

Serving static files out from python is not good idea since there is more effective way to do that. Why not let apache do this? Suppose you followed one of two previous posts that let you configure apache to server django application using mod_python or mod_wsgi. The idea is simple, we define an alias for the media directory of the django application and set it handler to none. Here is a partial example of apache site configuration file:
    ...

    Alias "/static/admin/" "/usr/lib/pymodules/python2.7/django/contrib/admin/media/"
    <Location "/static/admin/">
        SetHandler None
    </Location>

    Alias "/static/" "/usr/local/lib/hello/static/"
    <Location "/static/">
        SetHandler None
    </Location>
And finally let turn on static content expiration.
    ...

    <IfModule mod_expires.c>
        <FilesMatch "\.(jpg|gif|png|css|js)$">
            ExpiresActive on
            ExpiresDefault "access plus 7 days"
        </FilesMatch>
    </IfModule>
Enable expires module and restart apache2.
a2enmod expires
/etc/init.d/apache2 restart

How to Install Django on Debian using Apache mod_wsgi

Here we are going install a basic django application using apache2 mod_wsgi module. Requirements:
  • server dns: web01.dev.local
  • django code location: /usr/local/lib
  • django project: hello
Let install few packages we need:
apt-get -y install apache2 libapache2-mod-wsgi \
    python-django 
Once you ensure apache is up and running. Let create a simple django application:
cd /usr/local/lib
django-admin startproject hello
Create a /usr/local/lib/django-hello.wsgi file:
import sys
import os
import os.path

sys.path.append(os.path.dirname(__file__))
os.environ['DJANGO_SETTINGS_MODULE'] = 'hello.settings'

from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()
Add apache site configuration that will serve the django wsgi site. Add the following to /etc/apache2/sites-available/django-hello:
<VirtualHost *:80>
        ServerName web01.dev.local
        DocumentRoot /var/www/

        ErrorLog ${APACHE_LOG_DIR}/error.log
        # Possible values include: debug, info, notice, 
        # warn, error, crit, alert, emerg.
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        WSGIScriptAlias / /usr/local/lib/django-hello.wsgi
        WSGIProcessGroup hello-site
        WSGIDaemonProcess hello-site processes=2 threads=16 maximum-requests=1000 display-name=apache-hello-wsgi
</VirtualHost>
Now we are going disable default apache site and enable django-hello site:
a2dissite default
a2ensite django-hello
/etc/init.d/apache2 restart
Once you navigate to http://web01.dev.local/ you should be able to see the default django welcome screen.

If you experience performance issue with content rendering, overhead of framework internal time, have a willing to fine control content caching consider take a look at the following post.

How to Install Django on Debian using Apache mod_python

Here we are going install a basic django application using apache2 mod_python module. Requirements:
  1. server dns: web01.dev.local
  2. django code location: /usr/local/lib
  3. django project: hello
Let install few packages we need:
apt-get -y install apache2 libapache2-mod-python \
    python-django 
Once you ensure apache is up and running. Let create a simple django application:
cd /usr/local/lib
django-admin startproject hello
Add apache site configuration that will serve the django site. Add the following to /etc/apache2/sites-available/django-hello:
<VirtualHost *:80>
        ServerName web01.dev.local
        DocumentRoot /var/www/

        ErrorLog ${APACHE_LOG_DIR}/error.log
        # Possible values include: debug, info, notice, 
        # warn, error, crit, alert, emerg.
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        SetHandler python-program
        PythonHandler django.core.handlers.modpython
        SetEnv DJANGO_SETTINGS_MODULE hello.settings
        PythonDebug On
        PythonPath "['/usr/local/lib'] + sys.path"
</VirtualHost>
Now we are going disable default apache site and enable django-hello site:
a2dissite default
a2ensite django-hello
/etc/init.d/apache2 restart
Once you navigate to http://web01.dev.local/ you should be able to see the default django welcome screen.

Troubleshooting

If you unable navigate to the apache this could be related to ipv6 support (that is turned on by default). You can verify that issuing the following command:
netstat -tunlp | grep :80
If the apache process is not listening on ipv4 address (e.g. 0.0.0.0:80) just follow the following post to disable ipv6 in apache.

Saturday, July 16, 2011

How to Install Bugzilla in Debian

Here we are going install bugzilla 3.6.3 in Debian testing (all in one host). By the time of this writing bugzilla package was not available in testing release and one from stable failed to run in testing release. The option is to use it from sid release.

Install

  1. Since we are going install everything on one box we need two essential components: a web server and database server, apache and mysql serve this purpose quite well (if you have mysql already somewhere in the network you need mysql-client package instead).
    apt-get -y install apache2 mysql-server
    
  2. Add sid repository to your apt sources list and update package list:
    echo "deb http://ftp.debian.org/debian sid main" \
        >> /etc/apt/sources.list
    apt-get update
    # You should be able to find bugzilla3 package now
    apt-cache search bugzilla3
    
  3. Install bugzilla:
    apt-get install bugzilla3
    
    Configure database for bugzilla3 with 
    dbconfig-common? Yes
    
  4. Comment out sid repository in apt sources file
  5. You can now navigate to your bugzilla home page by simply entering:
    http://bugzilla-server-name/bugzilla3
    

Serving from Web Server Root

Default installation of bugzilla install it into bugzilla3 virtual directory. You can change it to be server from root directory:
  1. Set urlbase in /etc/bugzilla3/params:
    'urlbase' => ''
    
    or even better (this url is the common initial leading part of all Bugzilla urls):
    'urlbase' => 'http://bugzilla-server-name/'
    
  2. Disable default site in apache:
    a2dissite default
    
  3. Ensure the following in /etc/apache2/conf.d/bugzilla3.conf:
    <VirtualHost *:80>
        #Alias /bugzilla3 /usr/share/bugzilla3/web
        DocumentRoot /usr/share/bugzilla3/web
        SetEnv X_BUGZILLA_WEBPATH "/" 
    
        ...
    
    </VirtualHost>
    
  4. Let apache know about the changes we made:
    /etc/init.d/apache2 reload
    

Database backup/restore

During installation of bugzilla it creates a new database with name bugzilla3.
  1. Here is a small script to backup database:
    # Backup bugzilla database
    /etc/init.d/apache2 stop
    mysqldump -p bugzilla3 > backup.sql
    /etc/init.d/apache2 start
    
  2. ... and restore.
    # Restore bugzilla database
    /etc/init.d/apache2 stop
    mysql -p bugzilla3 < backup.sql
    /etc/init.d/apache2 start
    
Read more about bugzilla here.

Friday, February 18, 2011

Apache Kerberos Authentication over SSL for SVN

Suppose you already have a web site working over SSL (see here) and you would like add security on top of that, namely use Kerberos for authentication. I assume you saw the following:
  • Serving Multiple SVN Repositories with Apache (see here)

Thursday, February 17, 2011

Apache Basic Authentication over SSL with PAM Kerberos/LDAP

Suppose you already have a web site serving multiple subversion repositories over SSL (see here) and you would like add security on top of that, namely use Kerberos for authentication and LDAP for authorization. Before we proceed please ensure your machine is capable to authenticate against Kerberos/LDAP (see here). I will assume you saw the following:
  • Serving Multiple SVN Repositories with Apache (see here)
  • Debian OpenLDAP client with Kerberos (see here)

Wednesday, February 16, 2011

Serving Multiple SVN Repositories with Apache

Here are our requirements:
  • SVN web server FQDN: scm1 ; scm1.dev.local
  • SVN is served via SSL only
  • Repositories access url: https://scm1/svn/project1, https://scm1.dev.local/svn/project2
  • Access: public
  • Policies: /var/lib/svn/conf/policies
  • Root: /var/lib/svn/repos
Before we proceed please see:
  • Apache with SSL (see here)
  • Revision control with subversion (see here). You can skip settings related to security permissions, etc since the authentication/authorization will be managed by apache.

Apache with SSL

Here we are going setup a web site with SSL support, so content can be securely served via https.
  • Web server FQDN: web1 ; web1.dev.local
  • Content served via: HTTP and HTTPS
  • Content location: /var/www/