5

Versions list:

Centos 7---django 2.1.7---Apache 2.4.6---Python 3.6

Check EDIT 5 for most recent error log

followed this tutorial here: https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-centos-7

I am able to run sudo systemctl start httpd and when i go to the url i get a Internal sever error (logs at the bottom)

My file structure

home
└── user
    └── projects
        └── myapp
            ├── app
            │   ├── <All Code for Webapp including static dir>  
            ├── env (virtualenv)
            ├── manage.py
            ├── new
            │   ├── settings.py
            │   ├── urls.py
            │   └── wsgi.py
            └── requirements.txt

And i hoping someone can see a mistake in my django.conf located in my further down in my httpd folder. And hoping these are the endpoints i'm looking for

EDIT: This file is located here: /etc/httpd/conf.d/django.conf

django.conf

Alias /static /home/user/projects/myapp/app/static
<Directory /home/user/projects/myapp/app/static>
    Require all granted
</Directory>

<Directory /home/user/projects/myapp/new>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

WSGIApplicationGroup %{GLOBAL}
WSGIDaemonProcess myapp python-path=/home/user/projects/myapp python-home=/home/user/projects/myapp/env 
WSGIProcessGroup myapp
WSGIScriptAlias / /home/user/projects/myapp/new/wsgi.py

I'm not sure if these are pointing to all the right places and was hoping someone could give me a second look.

And i havent touched wsgi.py and was wondering if i am missing any logic there.

my wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'new.settings')

application = get_wsgi_application()

EDIT2: i am able to run the server but get a 500 internal server error and when i check the error_logs i get this

Updated Logs

[Mon Mar 11 10:40:39.865611 2019] [core:notice] [pid 9371] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0
[Mon Mar 11 10:40:39.868149 2019] [suexec:notice] [pid 9371] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Mon Mar 11 10:40:39.918015 2019] [auth_digest:notice] [pid 9371] AH01757: generating secret for digest authentication ...
[Mon Mar 11 10:40:39.919681 2019] [lbmethod_heartbeat:notice] [pid 9371] AH02282: No slotmem from mod_heartmonitor
[Mon Mar 11 10:40:39.948303 2019] [mpm_prefork:notice] [pid 9371] AH00163: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 configured -- resuming normal operations
[Mon Mar 11 10:40:39.948370 2019] [core:notice] [pid 9371] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Mon Mar 11 10:40:42.878806 2019] [mime_magic:error] [pid 9376] [client ip] AH01512: mod_mime_magic: can't read `/home/user/projects/myapp/new/wsgi.py'
[Mon Mar 11 10:40:42.879459 2019] [mime_magic:error] [pid 9376] [client ip] AH01512: mod_mime_magic: can't read `/home/user/projects/myapp/new/wsgi.py'
[Mon Mar 11 10:40:42.905048 2019] [:error] [pid 9372] (13)Permission denied: [remote ip] mod_wsgi (pid=9372, process='myapp', application='<url>|'): Call to fopen() failed for '/home/user/projects/myapp/new/wsgi.py'

EDIT3

In this log file it says this Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 configured when i am running python3.6 not 2.7.5, Maybe changing this would help?

EDIT 4

So i changed my WSGIDaemonProcess to

WSGIDaemonProcess myapp python-path=/home/user/projects/myapp python-home=/home/user/projects/app/env/lib/python3.6/site-packages user=<user>

Now im getting these errors

[Tue Mar 12 10:38:09.111397 2019] [mime_magic:error] [pid 18804] [client ip] AH01512: mod_mime_magic: can't read `/home/user/project/myapp/new/wsgi.py'
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site

so it looks like i need to uninstall mod_wsgi and reinstall to compile with python3.6 and not 2.7

How can i succesfully do this? i uninstall mod_wsgi and try mreinstall using sudo pip3.6 install mod_wsgi Now it doesnt recognize the new install of mod_wsgi and wont start the server because of an invalid command 'WSGIDaemonProcess' this is bc the new download isnt configured with httpd? how would i do this?

EDIT 5

Successfully installed mod_wsgi compiled with correct python version and able to run server, set SELinux to permissive to get rid of cant find '/route/to/wsgi/py. and now I am now getting no module name 'encoding' errors that repeats.

[Wed Mar 13 15:20:46.133597 2019] [core:notice] [pid 4403] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
[Tue Mar 12 14:11:57.520271 2019] [core:notice] [pid 866] AH00052: child pid 891 exit signal Aborted (6)
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

currently my permissions are all under user apache and group apache, and seems they are all under the right permissions, and i tried changing the root user/group to apache as well, and ran chmod -R 777 at the Myapp directory to make sure it had proper permissions.

Permissions under projects:

home/user/projects# ls -l

total 0
drwxrw-r-x. 7 apache apache 109 Mar 12 12:48 myapp

home/user/projects/myapp# ls -l

Permissions under myapp:

total 12
drwxrwxr-x. 7 apache apache 4096 Mar  7 13:18 app
drwxr-xr-x. 5 apache apache   56 Mar 12 12:48 env
-rwxrwxr-x. 1 apache apache  535 Mar  5 13:33 manage.py
drwxrwxr-x. 3 apache apache  110 Mar  7 14:27 new
-rw-rw-r--. 1 apache apache  869 Mar  7 14:42 requirements.txt
Mfreeman
  • 3,548
  • 7
  • 23
  • 37
  • What is "django.conf"? How is it linked to the rest of your Apache configuration? – Daniel Roseman Mar 08 '19 at 16:08
  • its located here /etc/httpd/conf.d/django.conf , i honestly cant say much about how this all works i just know its using that file when i start the apache server because i was getting errors on the file before..... i followed this tutorial here: https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-centos-7 – Mfreeman Mar 08 '19 at 16:19
  • added logs check edit – Mfreeman Mar 08 '19 at 20:15

4 Answers4

4

According to mod_wsgi's documentation:

user=name | user=#uid Defines the UNIX user name or numeric user uid of the user that the daemon processes should be run as. If this option is not supplied the daemon processes will be run as the same user that Apache would run child processes, as defined by the User directive, and it is not necessary to set this to the Apache user yourself.

I suppose that the apache user cannot access your home directory. Either try to add the user=myUserdirective to your WSGIDaemonProcess or chown your directory to the apache user. Another possible way would be to put your application inside a directory owned by apache user.

EDIT

I really should learn to read. According to the docs, it looks like you should recompile mod_wsgi using a python3.X interpreter in order to run python3.X applications (use the same versions for both compiling and application's environment)

If you want to use a different version of Python, you will need to reinstall mod_wsgi, compiling it for the version you want. It is not possible for the one mod_wsgi instance to run applications for both Python 2 and 3 at the same time.

EDIT 2

To answer one of your comments: It's normal that yum won't remove the version you installed through pip. It has no way to guess that you installed packages through other package managers AFAIK. I think that you should copy some the .so (I think it's mod_wsgi[version].so) files that lies in the directory where pip installed mod_wsgi (inside your venv/global site_packages I think) into the folder where Apache/Httpd loads its modules files (it's something like /usr/lib/apache2/modules on alpine/ubuntu so I guess it is /usr/lib/httpd/modules for CentOS, but can be different, sorry I can't give you more specific help) after moving out of this folder the old mod_wsgi.so. Don't forget to shutdown httpd before doing this, and restart it after.

Source: https://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html (section python-home)

Yoiro
  • 352
  • 3
  • 15
  • i ran these commands already which i think satifies this requirement.. i ran `sudo usermod -a -G apache` and `chmod 710 /home/` – Mfreeman Mar 11 '19 at 14:27
  • 1
    Have you tried specifying the `user=` directive in your WSGIDaemonProcess directive inside your `django.conf`? Because by just adding your `` to the `apache` group won't tell your `httpd` process to run with this specific user. Also I would suggest to give at least read permission to the members of the `apache` group (`chmod 740`) – Yoiro Mar 11 '19 at 14:36
  • 1
    I added user= at the end of the WSGIDaemonProcess(check edit) and that still gives gives me the same permission denied error – Mfreeman Mar 11 '19 at 14:44
  • 1
    I just saw your last edit. Try adding `python-home=` to your WSGIDaemonProcess directive. Sorry about not giving you exact answers, it's been a long time I haven't worked on deploying Python webapps. – Yoiro Mar 11 '19 at 15:11
  • It's there look at WSGIDaemonProcess – Mfreeman Mar 11 '19 at 15:13
  • 1
    It's not, you setup the `python-path` not `python-home` var, but nevermind. I just re-read the docs and it's not related. The python version you found in the logs refer to the python version used to build mod_wsgi. I don't think it's related. Maybe try with `chmod -R 777` your directory, and then if it works, try removing permissions one at a time. It those aren't working yet you can still try to recompile your own mod_wsgi (it's not as tedious as it seems, trust me I've already done it) with a python3.X interpreter. – Yoiro Mar 11 '19 at 15:16
  • So when i ran `sudo yum install mod_wsgi` it downloaded the 2.7 version right? so when i uninstall this module and run `sudo pip3.6 install mod_wsgi` it says that im missing apache httpd server packages, im incredibly confused – Mfreeman Mar 11 '19 at 17:21
  • 1
    I think running `yum install httpd-devel` before `pip3.6 install mod_wsgi` might do the trick – Yoiro Mar 12 '19 at 08:06
  • I already had that installed when i ran `pip3.6 install mod_wsgi` is there any configuration or movement of files i need to do for httpd to recoginze it? I run `sudo yum remove mod_wsgi` to remove the wrong mod_wsgi btw.. this doesnt remove the instance of mod_wsgi downloaded using pip – Mfreeman Mar 12 '19 at 14:35
  • 1
    Seeing your new edit and after googling a bit, try removing the python-home directive inside your django.conf file. I know I told you to put it, but now it is unrelevant to set it up. Sorry about that. – Yoiro Mar 13 '19 at 08:00
  • 1
    Can you give to output to `ls -la` of your `/home/user/projects` dir please? And same but for the `myapp` – Yoiro Mar 13 '19 at 14:15
  • Added to question, user and group were originally named after my user name with the same permissions but changed them to see it that would work – Mfreeman Mar 13 '19 at 14:20
  • and i believe you meant to say to remove python-path not python-home? – Mfreeman Mar 13 '19 at 14:27
  • 1
    I'm starting to be as lost as you are mate, but maybe is it related to the fact that your `env` directory is owned by the root user and thus the apache user wouldn't be able to use your Python dependencies, thus the 'encodings' module? – Yoiro Mar 13 '19 at 14:35
  • Yeah that doesn't seem to work either, also is there anything i should add to wsgi.py? i believe that there is something i should add so it is able to read this file – Mfreeman Mar 13 '19 at 14:38
  • 1
    The file looks OK to me. It just serves as an entrypoint to the rest of your application so Apache can serve it. See this as a script that will give a "MyApp" object that Httpd will be able to use. And most of the time you won't have to modify it. Do you still have your 'encodings' error? Have you tried to run `chmod -R 777` on your root app directory (/home/user/projects/myapp) temporarily? – Yoiro Mar 13 '19 at 14:43
  • 1
    ... One solution would be to simply put your application inside another directory owned by Apache like `/var/www/` (if not existing create it and chown the entire directory structure to apache:apache) – Yoiro Mar 13 '19 at 14:47
  • Ok should i be keeping the 777 permissions on the app dir when i move it? – Mfreeman Mar 13 '19 at 14:59
  • Yeah moving the myapp directory to /var/www/ doesn't work either, getting same errors – Mfreeman Mar 13 '19 at 15:07
  • 1
    Yes keep the 777. It's weird because it looks like a permission issue. I'm sorry but right now I'm out of tracks. Maybe Sir Dumpleton would come across your thread, I've seen he's (was?) quite active on mod_wsgi related issues. – Yoiro Mar 13 '19 at 15:12
  • Ok Thanks for your help – Mfreeman Mar 13 '19 at 15:14
2

Try permission on dir level rather on file.

<Directory /home/user/projects/myapp/new>
     Require all granted
</Directory>
webbyfox
  • 1,049
  • 10
  • 22
  • its the same error as above, [Mon Mar 11 14:51:00.850439 2019] `[mime_magic:error] [pid 18001] [client ip] AH01512: mod_mime_magic: can't read /home/user/projects/myapp/new/wsgi.py'` – Mfreeman Mar 11 '19 at 18:52
1

You can check user and group for apache by

egrep -i '^user|^group' /etc/httpd/conf/httpd.conf

So make sure you have permissions on /home/user/projects/myapp/new/wsgi.py

recheck by ls -l

Try after for example if User is apache and group is apache

sudo chown apache:apache /home/user/projects/myapp/new/wsgi.py

Yugandhar Chaudhari
  • 3,831
  • 3
  • 24
  • 40
  • I ran those commands and used the user and group specified by `ls -l` restarted the server and still get the same errors – Mfreeman Mar 12 '19 at 14:28
  • If you look at my latest edit (4) i am seeing if this is happening because my mod_wsgi isnt compiled with the same python version in my virtual environment, and cannot run the server unless i run a `sudo yum install mod_wsgi` ....using `sudo pip3.6 install mod_wsgi` does work and install but the httpd server wont start – Mfreeman Mar 12 '19 at 14:29
  • 1
    @Mfreeman you should not assign the user and group specified by `ls -l` you should do `egrep -i '^user|^group' /etc/httpd/conf/httpd.conf` and I dont think you should do different version of project and different version of package `mod_wsgi` – Yugandhar Chaudhari Mar 12 '19 at 16:06
  • The whole project and virtual environment is run off of python3.6 , so shouldnt mod_wsgi? – Mfreeman Mar 12 '19 at 16:19
  • `egrep -i '^user|^group' /etc/httpd/conf/httpd.conf` command shows me apache for user and group, so i should be using that for WSGIDaemonProccess in my .conf file? – Mfreeman Mar 12 '19 at 16:21
  • 1
    you should modify the ownership of file to user and group **apache** by `sudo chown apache:apache /home/user/projects/myapp/new/wsgi.py` – Yugandhar Chaudhari Mar 12 '19 at 16:22
  • I have done that and still will give me errors, should my whole project directory have apache ownership as well? – Mfreeman Mar 12 '19 at 16:29
  • how am i able to change ownership of all files under a directory? using the above command only works on individual directory or file? – Mfreeman Mar 12 '19 at 16:34
  • 1
    `sudo chown -R apache:apache /home/user/projects/myapp/` to recursively change all files and folders inside – Yugandhar Chaudhari Mar 12 '19 at 16:36
  • i am getting `ModuleNotFoundError: No module named 'encodings'` errors, as well as `can't read /home/user/projects/myapp/new/wsgi.py` errors – Mfreeman Mar 12 '19 at 16:39
  • 1
    seems like file permission was the issue.. is your virtualenvironment activated? refer this [question](https://stackoverflow.com/questions/38132755/importerror-no-module-named-encodings) but beware dont delete your project make a backup of project inside virtualenv – Yugandhar Chaudhari Mar 12 '19 at 16:40
  • My virtualenvironment is activated, but from that question i removed and reinstalled it, still the same problem – Mfreeman Mar 12 '19 at 16:50
  • am i using the virtualenv right? look at my file structure, it is inside myapp directory, and when i uninstalled it and reinstalled it (with same name) and then downloaded my requirements.txt it said that i already had everything installed – Mfreeman Mar 12 '19 at 16:55
  • 1
    are you activating first? by `source bin/activate` in virtualenv project and you can create for your python version by `virtualenv envname -p python3.6` @Mfreeman – Yugandhar Chaudhari Mar 12 '19 at 17:05
  • yes it has been activated while installing requirements.txt, and it has been created with my corrected python version(3.6)... i may have globally installed the requirements.txt and thats why it is already installed when i try to download it within the virtualenv? – Mfreeman Mar 12 '19 at 17:07
0

You have to pass library location for env variable.

WSGIDaemonProcess myapp python-path=/home/user/projects/myapp python-home=/home/user/projects/myapp/env/lib/<PYTHON VERSION>/site-packages/
Hardik Gajjar
  • 1,024
  • 12
  • 27