# Using Django on Apache 2 with mod\_wsgi

**URL:** <https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724>\
**Category:** Deployment\
**Created:** [July 10, 2022, 9:06am UTC](https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724 "2022-07-10T09:06:15Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![MatteoLanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteolanglois/32/9220_2.png) [@MatteoLanglois](https://forum.djangoproject.com/u/MatteoLanglois)\
**Post date:** [July 10, 2022, 9:06am UTC](https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724/1 "2022-07-10T09:06:15Z")

</div>

I’m trying to host a django project on my server with apache2 and mod\_wsgi. I’ve followed the tutorial on the Django documentation and some other tutorial however, none of them worked.  
I want to have my Django Project at [myurl.com/recettes](http://myurl.com/recettes) but actually I have the 404 error of Apache.

My Server is on Ubuntu 20.04 with Apache 2.4.41, my venv is with Python 3.8 and Django 4.0.5

My VHost :

```auto
<VirtualHost *:80>
        ServerName myurl.com
        ServerAlias www.myurl.com

        Alias /static /home/myurl.com/static
        <Directory /home/myurl.com/static>
                Require all granted
        </Directory>

        WSGIScriptAlias /recettes /home/myurl.com/MyProject/wsgi.py process-group=myurl.com

        <Directory /home/myurl.com/MyProject>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>

        WSGIDaemonProcess MyProject python-home=/home/myurl.com/env python-path=/home/myurl.com
        WSGIProcessGroup MyProject
</VirtualHost>

```

My wsgi.py

```auto
import os, sys

sys.path.append("/home/myurl.com/recettes")

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'recettes.settings')
application = get_wsgi_application()

```

I can give more informations if you want. Thanks for helping me ^^

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 10, 2022, 11:57am UTC](https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724/2 "2022-07-10T11:57:37Z")

</div>

Note that your `process-group` reference in the first quote below does not match the process group definitions shown in the second and third quotes.

(Note, this may not be the only problem here, just the first one I spotted.)

Also, for all cases like this, it may be worthwhile for you to set your Apache instance to debug and check your logs carefully. Usually the logs have some hints in them when trying to diagnose problems.

> [@MatteoLanglois](#):
>
> `ScriptAlias /recettes /home/myurl.com/MyProject/wsgi.py process-group=myurl.com`

> [@MatteoLanglois](#):
>
> `WSGIDaemonProcess MyProject python-home=/home/myurl.com/env python-path=/home/myurl.com`

> [@MatteoLanglois](#):
>
> `WSGIProcessGroup MyProject`

It may be useful to review the docs at [Quick Configuration Guide — mod\_wsgi 5.0.0 documentation](https://modwsgi.readthedocs.io/en/develop/user-guides/quick-configuration-guide.html#delegation-to-daemon-process)

In addition to restarting Apache, you may also want to verify that the daemon process is running.

---

<div class="post-metadata">

**Author:** ![MatteoLanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteolanglois/32/9220_2.png) [@MatteoLanglois](https://forum.djangoproject.com/u/MatteoLanglois)\
**Post date:** [July 10, 2022, 1:10pm UTC](https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724/3 "2022-07-10T13:10:31Z")

</div>

I fixed the first problem and i was watching if the daemon is running but it seems not.  
For the logs, actually I have no error. I will read again the docs of mod\_wsgi.

---

<div class="post-metadata">

**Author:** ![MatteoLanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteolanglois/32/9220_2.png) [@MatteoLanglois](https://forum.djangoproject.com/u/MatteoLanglois)\
**Post date:** [July 11, 2022, 5:40pm UTC](https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724/4 "2022-07-11T17:40:25Z")

</div>

I try a few things but it doesn’t work, i didn’t understand why the daemon isn’t running.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 11, 2022, 6:06pm UTC](https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724/5 "2022-07-11T18:06:53Z")

</div>

I’d set the log level for Apache to debug, and I’d check both the apache logs and syslog to see if mod\_wsgi is even trying to start. (If it starts and fails, there should be syslog messages for it.)

I’d also double/triple check that your file and directory permissions are correct. Apache needs access to those files, so that entire directory tree should be owned either by the user or group that apache runs as.

---

<div class="post-metadata">

**Author:** ![MatteoLanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteolanglois/32/9220_2.png) [@MatteoLanglois](https://forum.djangoproject.com/u/MatteoLanglois)\
**Post date:** [July 12, 2022, 7:37am UTC](https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724/6 "2022-07-12T07:37:36Z")

</div>

I think think the daemon start, I have a log :

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/5/57a44cd229385e199ab532180b52968661a6b220.png)

---

<div class="post-metadata">

**Author:** ![MatteoLanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteolanglois/32/9220_2.png) [@MatteoLanglois](https://forum.djangoproject.com/u/MatteoLanglois)\
**Post date:** [July 12, 2022, 7:38am UTC](https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724/7 "2022-07-12T07:38:07Z")

</div>

But after, I have another strange log :

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/2/2e9894375b664e8bc12fc90769355380f2c2f710.png)

Furthermore, permissions seems correct.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 12, 2022, 11:56am UTC](https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724/8 "2022-07-12T11:56:12Z")

</div>

I can’t quite read these images, but if those first messages contain the PID for the daemons, then you can use the `ps` command to verify that they’re still running.

I really can’t tell what that second image is supposed to be showing.

---

<div class="post-metadata">

**Author:** ![MatteoLanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteolanglois/32/9220_2.png) [@MatteoLanglois](https://forum.djangoproject.com/u/MatteoLanglois)\
**Post date:** [July 12, 2022, 12:35pm UTC](https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724/9 "2022-07-12T12:35:45Z")

</div>

The Daemons seems good :

```auto
UID PID PPID C STIME TTY STAT TIME CMD
www-data 87227 29250 0 14:30 ? Sl 0:00 /usr/sbin/apache2 -k start

```

The second picture shows that :

```auto
||[Tue Jul 12 14:34:14.815932 2022] [ssl:debug] [pid 87229] ssl_engine_kernel.c(415): [client 81.220.149.245:58853] AH02034: Initial (No.1) HTTPS request received for child 1 (server les-roseaux.dev:443)                                  
││[Tue Jul 12 14:34:14.816098 2022] [authz_core:debug] [pid 87229] mod_authz_core.c(817): [client 81.220.149.245:58853] AH01626: authorization result of Require all granted: granted ││[Tue Jul 12 14:34:14.816114 2022] [authz_core:debug] [pid 87229] mod_authz_core.c(817): [client 81.220.149.245:58853] AH01626: authorization result of <RequireAny>: granted ││[Tue Jul 12 14:34:14.816232 2022] [core:info] [pid 87229] [client 81.220.149.245:58853] AH00128: File does not exist: /var/www/html/recettes/                                                                                              
││[Tue Jul 12 14:34:14.896071 2022] [ssl:debug] [pid 87229] ssl_engine_kernel.c(415): [client 81.220.149.245:58853] AH02034: Subsequent (No.2) HTTPS request received for child 1 (server les-roseaux.dev:443), referer: https://les-roseaux.
││[Tue Jul 12 14:34:14.896091 2022] [authz_core:debug] [pid 87229] mod_authz_core.c(817): [client 81.220.149.245:58853] AH01626: authorization result of Require all granted: granted, referer: https://les-roseaux.dev/recettes/            
││[Tue Jul 12 14:34:14.896095 2022] [authz_core:debug] [pid 87229] mod_authz_core.c(817): [client 81.220.149.245:58853] AH01626: authorization result of <RequireAny>: granted, referer: https://les-roseaux.dev/recettes/                   
││[Tue Jul 12 14:34:14.896108 2022] [core:info] [pid 87229] [client 81.220.149.245:58853] AH00128: File does not exist: /var/www/html/favicon.ico, referer: https://les-roseaux.dev/recettes/                                                
││[Tue Jul 12 14:34:14.897012 2022] [ssl:debug] [pid 87229] ssl_engine_io.c(1102): [client 81.220.149.245:58853] AH02001: Connection closed to child 1 with standard shutdown (server les-roseaux.dev:443)

```

Sorry for the precedent pictures ^^

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 12, 2022, 1:59pm UTC](https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724/10 "2022-07-12T13:59:38Z")

</div>

The pid you show here is for the apache master process itself. I would expect the mod\_wsgi daemons to show up as a list of one or more separate processes. (You should also have multiple apache worker processes as well.)

Unfortunately, I no longer have an apache instance that I can verify this with. (It’s been almost 10 years since I’ve deployed to Apache at all, and closer to 15 since I’ve used mod\_wsgi.) If you’re not getting more useful detail than this in the logs, I’m beginning to doubt that I’m going to be able to help you.

---

<div class="post-metadata">

**Author:** ![MatteoLanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteolanglois/32/9220_2.png) [@MatteoLanglois](https://forum.djangoproject.com/u/MatteoLanglois)\
**Post date:** [July 12, 2022, 2:01pm UTC](https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724/11 "2022-07-12T14:01:25Z")

</div>

Ok, I will continue to search, thanks for the help ^^

There is multiple process for Apache but 0 for wsgi.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 12, 2022, 2:08pm UTC](https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724/12 "2022-07-12T14:08:50Z")

</div>

I will toss out one more item that I know trip some people up.

From: [Virtual Environments — mod\_wsgi 5.0.0 documentation](https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html#virtual-environment-and-python-version)

> When using a Python virtual environment with mod\_wsgi, it is very important that it has been created using the same Python installation that mod\_wsgi was originally compiled for. It is not possible to use a Python virtual environment to force mod\_wsgi to use a different Python version, or even a different Python installation.

(more details follow on that page)

---

<div class="post-metadata">

**Author:** ![MatteoLanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteolanglois/32/9220_2.png) [@MatteoLanglois](https://forum.djangoproject.com/u/MatteoLanglois)\
**Post date:** [July 12, 2022, 2:21pm UTC](https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724/13 "2022-07-12T14:21:49Z")

</div>

I have already corrected this problem yesterday, so it isn’t that… I’ll continue to search 🤔

---

<div class="post-metadata">

**Author:** ![Mayanktaker](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mayanktaker/32/9619_2.png) [@Mayanktaker](https://forum.djangoproject.com/u/Mayanktaker)\
**Post date:** [August 8, 2022, 10:07pm UTC](https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724/14 "2022-08-08T22:07:10Z")

</div>

Did you find the solution? Coz I am new too and trying to deploy my first app into production and getting something same problems.

---

<div class="post-metadata">

**Author:** ![MatteoLanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteolanglois/32/9220_2.png) [@MatteoLanglois](https://forum.djangoproject.com/u/MatteoLanglois)\
**Post date:** [August 8, 2022, 10:21pm UTC](https://forum.djangoproject.com/t/using-django-on-apache-2-with-mod-wsgi/14724/15 "2022-08-08T22:21:19Z")

</div>

Finally I move entirely to nginx, it’s easier.
