# [SOLVED] hosting Mercurial using hgwebdir and mod_wsgi

## c00l.wave

I would like to host Mercurial repositories on my web server. I emerged mod_wsgi (2.5) and mercurial, copied /usr/share/doc/mercurial-1.2.1/hgwebdir.cgi into my vhosts cgi-bin and created a hgweb.config in the same directory:

```

[collections]

/mnt/data/hg/repositories/ = /mnt/data/hg/repositories/

[web]

style = gitweb

```

I created a repository using hg init /mnt/data/hg/repositories/test, added apache to a user group "mercurial", added a mercurial user and set all rights accordingly:

```

chown -R mercurial:mercurial /mnt/data/hg/repositories/

chmod -R g+rwx /mnt/data/hg/repositories/

chmod -R o-rwx /mnt/data/hg/repositories/

```

I added -D WSGI to my conf.d/apache2 and this to my vhost-config:

```

<VirtualHost *:80>

    ServerAdmin me@my.domain

    DocumentRoot /var/www/hg.my.domain/htdocs/

    ServerName hg.my.domain

    ErrorLog /var/www/hg.my.domain/logs/error_log

    CustomLog /var/www/hg.my.domain/logs/access_log combined

WSGIScriptAlias / /var/www/hg.my.domain/cgi-bin/hgwebdir.cgi

<Directory /var/www/hg.my.domain/cgi-bin/>

    Order deny,allow

    Allow from all

</Directory>

</VirtualHost>

```

Having also added WSGIRestrictStdout Off to my /etc/apache2/modules.d/70_mod_wsgi.conf I now get a nice empty listing on my browser. Neither the selected style (gitweb to check if the config is being parsed) nor my collections or path-settings have any effect. I always get an empty repository list.  (I expected the test-repository to show up. I only get "Mercurial repository not found" if I try to call it directly.)

I tried to hardcode the absolute path to my hgweb.config into hgwebdir.cgi and now I get a config error and this in my error_log:

```

[Sun Aug 02 13:48:17 2009] [error] [client a.b.c.d] mod_wsgi (pid=3054): Target WSGI script '/var/www/hg.my.domain/cgi-bin/hgwebdir.cgi' cannot be loaded as Python module.

[Sun Aug 02 13:48:17 2009] [error] [client a.b.c.d] mod_wsgi (pid=3054): Exception occurred processing WSGI script '/var/www/hg.my.domain/cgi-bin/hgwebdir.cgi'.

[Sun Aug 02 13:48:17 2009] [error] [client a.b.c.d] Traceback (most recent call last):

[Sun Aug 02 13:48:17 2009] [error] [client a.b.c.d]   File "/var/www/hg.my.domain/cgi-bin/hgwebdir.cgi", line 67, in <module>

[Sun Aug 02 13:48:17 2009] [error] [client a.b.c.d]     wsgicgi.launch(application)

...

```

Any idea what's going wrong?

----------

## c00l.wave

I took a short break and when I came back found out that I read the Traceback wrong...   :Embarassed: 

The issue was a KeyError exception due to a wrong check in /usr/lib64/python2.6/site-packages/mercurial/hgweb/hgwebdir_mod.py on line 226. I changed

```

if req.env['SCRIPT_NAME']:

```

to

```

if 'SCRIPT_NAME' in req.env:

```

and it works now. I already filed an upstream bug report.

----------

## c00l.wave

In case anyone else traps into this:

 *c00l.wave wrote:*   

> I would like to host Mercurial repositories on my web server. I emerged mod_wsgi (2.5) and mercurial, copied /usr/share/doc/mercurial-1.2.1/hgwebdir.cgi into my vhosts cgi-bin and created a hgweb.config in the same directory:

 

The real problem was using .cgi instead of .wsgi. You find the correct file in /usr/share/doc/mercurial-1.2.1/contrib/

----------

