[Answered ]-System user of Django app configured with Apache httpd and mod_wsgi-3.4 being set to root

1👍

What does your WSGIDaemonProcess configuration in the rest of your Apache config look like? You can set the user there.

WSGIDaemonProcess mysite user=ctaftest group=ctaftest threads=5
👤Jordan

1👍

To start with, if you call:

os.path.expanduser("dir_path")

it should return just:

dir_path

Did you instead mean:

os.path.expanduser("~/dir_path")

Anyway, when you use embedded mode of mod_wsgi, your code runs in the Apache child worker processes. These processes can be shared with other Apache modules such as PHP and Perl modules. Because it is a shared environment neither mod_wsgi or any web application code can’t be presumptuous in thinking it can change the current working directory of the process. As a result, the current working directory is inherited from what ever Apache is started with, which would be the root of the file system.

For similar reasons, you can’t go overriding what environment variables may be set and as a result, if Apache passed through HOME as being that for the root user that Apache starts as, then when you use os.path.expanduser(‘~’), the tilde will be replaced with whatever HOME was set to.

So what you are seeing is quite normal, including one process still running as root, which is the parent Apache process, in which none of your requests are being run anyway, as it just acts as a process monitor to manage the child worker processes, handle restarts etc.

In general, in a web application it is regarded as bad practice to rely on things like the current working directory, the values of environment variables such as HOME, USERNAME, PATH etc, as they aren’t always set to sensible things depending on the hosting environment.

That all said, if when using mod_wsgi, you instead use the preferred daemon mode, then because at that point it is only running your Python web application, mod_wsgi will override HOME to be the directory for the user that the daemon process runs as. If environment variables such as USER, USERNAME and LOGNAME are set, it will also similarly override those with a value corresponding to what user the daemon process runs as. It will even change the current working directory to be the home directory for that user.

In summary. You should not be building in such dependencies into a web application, but specify such things via configuration, otherwise you limit portability. If you for some reason don’t want to do that, then use daemon mode of mod_wsgi instead.

0👍

To solve it , the accepted answer helped , however I had to add

`WSGIProcessGroup` directive also

So I configured something like this.

WSGIDaemonProcess ctaf.com user=ctaftest group=ctaftest threads=10 python-path=/home/ctaftest/virtualpython/CTAFWEB_PRODUCTION/ctafweb

WSGIProcessGroup ctaf.com

Leave a comment