404 error when deploying django project with apache2

Please let me ask a naive question since I cannot find any systemized documents on django for beginners on the internet.
Since I got an error that I cannot put more than 2 links, I replace “/” with “|” in my post.

I tried to create a new django project on apache2, but I got 404 error. Here are what I did.

In |home|ubuntu|django directory, type

django-admin startproject proj

Edit proj|proj|wsgi.py as follows.

import os
import sys

from django.core.wsgi import get_wsgi_application

os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘proj.settings’)

sys.path.append(‘|home|ubuntu|django|proj’);
sys.path.append(‘|home|ubuntu|django|proj|proj’);

application = get_wsgi_application()

Edit ALLOWED_HOSTS in proj|proj|settings.py as follows.

ALLOWED_HOSTS = [‘example.com’]

Edit |etc|apache2|conf-available|wsgi.conf as follows.

WSGIScriptAlias |proj |home|ubuntu|django|proj|proj|wsgi.py
WSGIPythonPath |home|ubuntu|django|proj

<Directory |home|ubuntu>
Require all granted
<|Directory>

<Directory |home|ubuntu|django|proj|proj>

Require all granted
<|Files>
<|Directory>

I don’t touch urls.py. It is just a default version as follows.

from django.contrib import admin
from django.urls import path

urlpatterns = [
path(‘admin|’, admin.site.urls),
]

Restart apache2 as

|etc|init.d|apache2 restart

Access to http:||example.com|proj, and then got 404 error as follows.

Page not found (404)
Request Method: GET
Request URL: http:||example.com|proj|
Using the URLconf defined in proj.urls, Django tried these URL patterns, in this order:

admin|
The empty path didn’t match any of these.

My enviroment is as follows.

Ubuntu 18.04 LTS (AWS)
Apache|2.4.29
Python 3.6.9
Django 3.0.5

How can I solve this 404 error?

The good news is that it appears like your Django setup is effectively working. You’re getting a Django error, which means that apache is handing the request off to Django. This means that the problem is in the Django side, not the apache side.

The issue is the URL you’re trying to reference. Proj is actually not part of your url scheme. Notice in your urlpatterns, you’re only defining admin, you have no reference to a url component named ‘proj’. (Nor should you, unless you have created a view to be called for that url.)

Verify your project is working by going to /admin. All your other urls that you want need to be created as Django views and then registered in the urlpatterns.

There’s a separation between your directory structure of your project and the url structure of your application. There doesn’t need to be any correlation between the two.

If you haven’t already done so, I strongly encourage you to work through the full Django tutorial. It’s a whole lot easier to deploy a fully working Django app than it is to try to do development in the deployed environment.

Ken