Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_wsgi working directory and user

I'm running flask on mod_wsgi. my flask app, which is on /var/www/app receives some file from user and saves it to /var/www/app/tmp directory. However even after all chmod and chown(thought it was a permission problem), I was unable to reach that tmp directory.

After some debugging I found out that the current working directory of the flask app is /. I can change working directory by os.chdir('/var/www/'), but I'd like to avoid that for security concerns.

here is my apache configuration:

<VirtualHost *:80>
    ServerName mysite.com
    ServerAlias site.com
    ServerAdmin admin@localhost

    WSGIDaemonProcess app user=www-data group=www-data processes=1
    WSGIScriptAlias / /var/www/app.wsgi

    Alias /static /var/www/app/static

    <Directory /var/www/app>
        WSGIProcessGroup app
        WSGIApplicationGroup %{GLOBAL}
        WSGIScriptReloading On
        Order deny,allow
        Allow from all
    </Directory>

    <Location "/static">
        SetHandler None
    </Location>

</VirtualHost>

How can I change working directory of my app from / to /var/www ?

like image 486
thkang Avatar asked Feb 11 '13 08:02

thkang


People also ask

Where is Mod_wsgi so?

If installing the Apache module by hand, the file is called 'mod_wsgi.so'. The compiled Apache module can be found in the “. libs” subdirectory. The name of the file should be kept the same when copied into its appropriate location.

What are Wsgi files?

The Web Server Gateway Interface (WSGI, pronounced whiskey or WIZ-ghee) is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python programming language. The current version of WSGI, version 1.0. 1, is specified in Python Enhancement Proposal (PEP) 3333.

What is Wsgipythonhome?

Used to indicate to Python when it is initialised where its library files are installed.

What is WSGI script alias?

The WSGIScriptAlias directive behaves in the same manner as the Alias directive, except that it additionally marks the target directory as containing WSGI scripts, or marks the specific file-path as a script, that should be processed by mod_wsgi's wsgi-script handler.


1 Answers

The documentation for WSGIDaemonProcess says you can use the home=... stanza:

home=directory

Defines an absolute path of a directory which should be used as the initial current working directory of the daemon processes within the process group. If this option is not defined, in mod_wsgi 1.X the current working directory of the Apache parent process will be inherited by the daemon processes within the process group. Normally the current working directory of the Apache parent process would be the root directory. In mod_wsgi 2.0+ the initial current working directory will be set to be the home directory of the user that the daemon process runs as.

I'm curious, though -- why would using os.chdir be any more of a security risk in your opinion?

like image 70
AKX Avatar answered Sep 28 '22 11:09

AKX