im currently trying to deploy my django project to a web server hosted by a RHEL-8 VM. Im trying to set up the VM to utilize HTTPD (apache). However, i can’t seem to get the Virtual Host configuration files right. I am pretty new to django and web servers as this is my first time deploying a web server. As such, i have been trying to do this for quite some time now and nothing online seems to work. As of recently i have tried more or less following this tutorial https://computingforgeeks.com/deploy-python-3-django-application-on-centos-with-apache-and-mod-wsgi/ on getting it deployed. As such, here is the code i have added into /etc/httpd/conf.d/django.conf:

<VirtualHost *:80>
        ServerAdmin info@awesomedjango.com
        ServerName awesomedjango.com
        DocumentRoot /var/www/

        Alias /static /var/www/awesomedjango/static
        <Directory "/var/www/awesomedjango/static">
                Options FollowSymLinks
                Order allow,deny
                Allow from all
                Require all granted
        </Directory>

        ErrorLog /var/www/awesomedjango/logs/apis_error.log
        CustomLog /var/www/awesomedjango/logs/apis_access.log combined

        WSGIPassAuthorization On
        WSGIDaemonProcess awesomedjango python-path=/var/www/awesomedjango:/var/www/awesomedjango/venv/lib/python3.6/site-packages
        WSGIProcessGroup awesomedjango
        WSGIScriptAlias / /var/www/awesomedjango/v/wsgi.py

        <Directory /var/www/awesomedjango/awesomedjango>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>
</VirtualHost>

But when i try to restart apache to apply this via “’ apachectl restart ”’, i just receive these errors:

  • httpd.service: Main process exited, code=exited, status=1/FAILURE
  • httpd.service: Failed with result ‘exit-code’.
  • Failed to start The Apache HTTP Server.

Any help would be greatly appreciated as i cant seem to find a solution to my problem online. Thanks

1 comment
  • EDIT: new config file under /etc/httpd/conf.d/django.conf:

    Alias /static /home/rh/django_apache/src/static_root
    <Directory /home/rh/django_apache/src/static_root>
        Require all granted
    </Directory>
    
    <Directory /home/rh/django_apache/src/mysite>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    
    WSGIDaemonProcess django_apache python-path=/home/rh/django_apache/src:/home/rh/django_apache/src/venv/lib/python3.6/site-packages
    WSGIProcessGroup django_apache
    WSGIScriptAlias / /home/rh/django_apache/src/mysite/wsgi.py
    

    Output of “apachectl -t”

    
    

    Output of “apachectl configtest”

    Syntax OK
    

    Error logs under /etc/httpd/logs/error_log:

    [Mon May 04 17:21:13.114508 2020] [mpm_event:notice] [pid 83087:tid 139745409411392] AH00492: caught SIGWINCH, shutting down gracefully
    [Mon May 04 17:21:14.238225 2020] [core:notice] [pid 83673:tid 140701031287104] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0
    [Mon May 04 17:21:14.239135 2020] [suexec:notice] [pid 83673:tid 140701031287104] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
    [Mon May 04 17:21:14.257061 2020] [lbmethod_heartbeat:notice] [pid 83673:tid 140701031287104] AH02282: No slotmem from mod_heartmonitor
    [Mon May 04 17:21:14.262183 2020] [mpm_event:notice] [pid 83673:tid 140701031287104] AH00489: Apache/2.4.37 (Red Hat Enterprise Linux) OpenSSL/1.1.1c mod_wsgi/4.6.4 Python/3.6 configured -- resuming normal operations[Mon May 04 17:21:14.262213 2020] [core:notice] [pid 83673:tid 140701031287104] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
    

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

SOLVED: Errors lied within SELinux. I had to give fix up selinux policies for my project. Have a look at this thread for more detail: https://unix.stackexchange.com/questions/50639/httpd-cant-write-to-folder-file-because-of-selinux

Hi there @abartlett1206,

Usually, after making any changes to your Apache config, I would recommend first running Apache config test before restarting the service:

  • apachectl -t

And then only if you get Syntax OK proceed with the Apache restart.

I think that the problem might be that the mod_wsgi enabled is not enabled.

I would recommend following the steps from this tutorial here on how to configure your Apache service:

https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-centos-7#configure-apache

If this still does not work, feel free to share the output of the config test command and the last few lines from your Apache error log.

Hope that this helps!
Regards,
Bobby

by Justin Ellingwood
Django is a powerful web framework that can help you get your Python application or website off the ground quickly. Django includes a simplified development server for testing your code locally, but for anything even slightly production related, a more secure and powerful web...
  • Hi @bobbyiliev,
    I have tried to follow that tutorial also. However, i am not sure how to do it with a django project that is already made? It manages to work when i create a new django project, so would i just have to copy all of my files over to this new project?
    Thanks

    • As such, i have also tried following this guide, still with no luck, as it also creates a new django project. https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-centos-7

      by Justin Ellingwood
      Django is a powerful web framework that can help you get your Python application or website off the ground. Django includes a simplified development server for testing your code locally, but for anything even slightly production related, a more secure and powerful web server...
      • Hi there @abartlett1206,

        What is the exact error that you get at the end?

        Can you share the output of the Apache config test command and the output of your Apache error log?

        Regards,
        Bobby

        • Hi Bobby,
          So at the beginning of the tutorial i try to run “python3 manage.py runserver 0.0.0.0:8080” as a simple test if it is working. As such, that command works if i create a new django project as per the tutorial but not if i try to use my pre-made django project via “git clone”. I change the corresponding “settings.py” as per the tutorial too. However, when i go to the page after performing “python3 manage.py runserver 0.0.0.0:8080”. i get this page “TemplateDoesNotExist at /
          base.html”

          Environment:
          
          
          Request Method: GET
          Request URL: http://10.86.161.79:8080/
          
          Django Version: 3.0.6
          Python Version: 3.6.8
          Installed Applications:
          ['personal',
           'account',
           'django.contrib.admin',
           'django.contrib.auth',
           'django.contrib.contenttypes',
           'django.contrib.sessions',
           'django.contrib.messages',
           'django.contrib.staticfiles']
          Installed Middleware:
          ['django.middleware.security.SecurityMiddleware',
           'django.contrib.sessions.middleware.SessionMiddleware',
           'django.middleware.common.CommonMiddleware',
           'django.middleware.csrf.CsrfViewMiddleware',
           'django.contrib.auth.middleware.AuthenticationMiddleware',
           'django.contrib.messages.middleware.MessageMiddleware',
           'django.middleware.clickjacking.XFrameOptionsMiddleware']
          
          Template loader postmortem
          Django tried loading these templates, in this order:
          
          Using engine django:
              * django.template.loaders.filesystem.Loader: /home/rh/django_apache/src/TEMPLATES/base.html (Source does not exist)
              * django.template.loaders.app_directories.Loader: /home/rh/django_apache/src/personal/templates/base.html (Source does not exist)
              * django.template.loaders.app_directories.Loader: /home/rh/django_apache/src/account/templates/base.html (Source does not exist)
              * django.template.loaders.app_directories.Loader: /home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/contrib/admin/templates/base.html (Source does not exist)
              * django.template.loaders.app_directories.Loader: /home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/contrib/auth/templates/base.html (Source does not exist)
          
          
          Template error:
          In template /home/rh/django_apache/src/personal/templates/personal/home.html, error at line 1
             base.html
             1 :  {% extends 'base.html' %} 
             2 : 
             3 : 
             4 : {% block content %}
             5 : 
             6 : 
             7 : 
             8 : 
             9 : 
             10 :  <div class="container marketing">
             11 : 
          
          
          Traceback (most recent call last):
            File "/home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
              return self.template.render(context)
            File "/home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/template/base.py", line 171, in render
              return self._render(context)
            File "/home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/template/base.py", line 163, in _render
              return self.nodelist.render(context)
            File "/home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/template/base.py", line 936, in render
              bit = node.render_annotated(context)
            File "/home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/template/base.py", line 903, in render_annotated
              return self.render(context)
            File "/home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/template/loader_tags.py", line 127, in render
              compiled_parent = self.get_parent(context)
            File "/home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/template/loader_tags.py", line 124, in get_parent
              return self.find_template(parent, context)
            File "/home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/template/loader_tags.py", line 104, in find_template
              template_name, skip=history,
            File "/home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/template/engine.py", line 129, in find_template
              raise TemplateDoesNotExist(name, tried=tried)
          
          The above exception (base.html) was the direct cause of the following exception:
            File "/home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
              response = get_response(request)
            File "/home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
              response = self.process_exception_by_middleware(e, request)
            File "/home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
              response = wrapped_callback(request, *callback_args, **callback_kwargs)
            File "/home/rh/django_apache/src/personal/views.py", line 13, in basic_screen
              return render(request, "personal/home.html", context)
            File "/home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/shortcuts.py", line 19, in render
              content = loader.render_to_string(template_name, context, request, using=using)
            File "/home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/template/loader.py", line 62, in render_to_string
              return template.render(context, request)
            File "/home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/template/backends/django.py", line 63, in render
              reraise(exc, self.backend)
            File "/home/rh/django_apache/src/venv/lib/python3.6/site-packages/django/template/backends/django.py", line 84, in reraise
              raise new from exc
          
          Exception Type: TemplateDoesNotExist at /
          Exception Value: base.html  
          

          Thanks

          • Hi there @abartlett1206,

            Actually the Apache configuration looks all correct according to the logs and the config test.

            The error that you’ve shared indicates that there is something wrong with the application itself, it looks like that it is failing to find the base.html template.

            Have you set permissions of your files correctly as described in the tutorial so that the apache service could read them?

            Regards,
            Bobby

        • Hi Bobby,
          I have managed to fix my settings.py file such that i can get the server running via “sudo python3 manage.py runserver 0.0.0.0:8080 –insecure”. However, the apache server still does not seem to work (500 Internal server error) with similar errors to as before. I have given permissions as per the tutorial. Would the problem perhaps be this line: <Directory /home/rh/error_msg/Project/src/mysite> in the below code. (I restarted, following the tutorial again, hence the directory name changes)

          Alias /static /home/rh/error_msg/Project/src/static_cdn
          <Directory /home/rh/error_msg/Project/src/static_cdn>
              Require all granted
          </Directory>
          
          <Directory /home/rh/error_msg/Project/src/mysite>
              <Files wsgi.py>
                  Require all granted
              </Files>
          </Directory>
          
          WSGIDaemonProcess django_apache python-path=/home/rh/error_msg/Project/src/:/home/rh/error_msg/Project/src/venv/lib/python3.6/site-packages
          WSGIProcessGroup django_apache
          WSGIScriptAlias / /home/rh/error_msg/Project/src/mysite/wsgi.py
          

          These are the errors it is now displaying:

          [Wed May 06 13:13:50.085793 2020] [mpm_event:notice] [pid 112907:tid 139937910372672] AH00492: caught SIGWINCH, shutting down gracefully
          [Wed May 06 13:13:51.197167 2020] [core:notice] [pid 113281:tid 140677730421056] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0
          [Wed May 06 13:13:51.198168 2020] [suexec:notice] [pid 113281:tid 140677730421056] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
          [Wed May 06 13:13:51.215848 2020] [lbmethod_heartbeat:notice] [pid 113281:tid 140677730421056] AH02282: No slotmem from mod_heartmonitor
          [Wed May 06 13:13:51.220927 2020] [mpm_event:notice] [pid 113281:tid 140677730421056] AH00489: Apache/2.4.37 (Red Hat Enterprise Linux) OpenSSL/1.1.1c mod_wsgi/4.6.4 Python/3.6 configured -- resuming normal operatio$
          [Wed May 06 13:13:51.220959 2020] [core:notice] [pid 113281:tid 140677730421056] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND
          
          • EDIT: When i check the error logs located in “/var/log/httpd/error_log”, this is what it displays:

            [Wed May 06 18:00:32.003249 2020] [mime_magic:error] [pid 118752:tid 140670406264576] [client 10.48.70.51:53258] AH01512: mod_mime_magic: can't read `/home/rh/django_apache/src/mysite/wsgi.py'
            [Wed May 06 18:00:32.003369 2020] [mime_magic:error] [pid 118752:tid 140670406264576] [client 10.48.70.51:53258] AH01512: mod_mime_magic: can't read `/home/rh/django_apache/src/mysite/wsgi.py'
            [Wed May 06 18:00:32.037340 2020] [wsgi:error] [pid 118750:tid 140670708401920] (13)Permission denied: [remote 10.48.70.51:53258] mod_wsgi (pid=118750, process='django_apache', application='10.86.161.79|'): Call to fopen() failed for '/home/rh/django_apache/src/mysite/wsgi.py'.
            [Wed May 06 18:00:32.307664 2020] [mime_magic:error] [pid 118752:tid 140670389479168] [client 10.48.70.51:53257] AH01512: mod_mime_magic: can't read `/home/rh/django_apache/src/mysite/wsgi.py', referer: http://10.86$
            [Wed May 06 18:00:32.307753 2020] [mime_magic:error] [pid 118752:tid 140670389479168] [client 10.48.70.51:53257] AH01512: mod_mime_magic: can't read `/home/rh/django_apache/src/mysite/wsgi.py', referer: http://10.86$
            [Wed May 06 18:00:32.308069 2020] [wsgi:error] [pid 118750:tid 140670599296768] (13)Permission denied: [remote 10.48.70.51:53257] mod_wsgi (pid=118750, process='django_apache', application='10.86.161.79|'): Call to fopen() failed for '/home/rh/django_apache/src/mysite/wsgi.py'.
            

            But i have permissions set up according to the tutorial?

            EDIT: I have changed the permissions around again, and its back to the errors above.

            EDIT: Again, the “Permission denied” errors are arose with me just restarting httpd.

    • New code in /etc/httpd/conf.d/django.conf :

      Alias /static /home/rh/django_apache/src/static_root
      <Directory /home/rh/django_apache/src/static_root>
          Require all granted
      </Directory>
      
      <Directory /home/rh/django_apache/src/mysite>
          <Files wsgi.py>
              Require all granted
          </Files>
      </Directory>
      
      WSGIDaemonProcess django_apache python-path=/home/rh/django_apache/src:/home/rh/django_apache/src/venv/lib/python3.6/site-packages
      WSGIProcessGroup django_apache
      WSGIScriptAlias / /home/rh/django_apache/src/mysite/wsgi.py
      

      output of “apachectl -t”

      
      

      Output of “apachectl configtest”

      Syntax OK
      

      Error log at /etc/httpd/logs/error_log

      [Mon May 04 20:06:57.511601 2020] [mpm_event:notice] [pid 83673:tid 140701031287104] AH00492: caught SIGWINCH, shutting down gracefully
      [Mon May 04 20:06:58.622365 2020] [core:notice] [pid 85231:tid 139717329324352] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0
      [Mon May 04 20:06:58.623443 2020] [suexec:notice] [pid 85231:tid 139717329324352] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
      [Mon May 04 20:06:58.641009 2020] [lbmethod_heartbeat:notice] [pid 85231:tid 139717329324352] AH02282: No slotmem from mod_heartmonitor
      [Mon May 04 20:06:58.646476 2020] [mpm_event:notice] [pid 85231:tid 139717329324352] AH00489: Apache/2.4.37 (Red Hat Enterprise Linux) OpenSSL/1.1.1c mod_wsgi/4.6.4 Python/3.6 configured -- resuming normal operations[Mon May 04 20:06:58.646507 2020] [core:notice] [pid 85231:tid 139717329324352] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
      

      Output of “ls -al django_apache/src/mysite”

      drwxrwxr-x. 3 rh rh   89 May  4 16:33 .
      drwxrwxr-x. 9 rh rh  194 May  4 16:38 ..
      -rw-rw-r--. 1 rh rh  405 May  4 16:33 asgi.py
      drwxrwxr-x. 2 rh rh 4096 May  4 16:39 __pycache__
      -rw-rw-r--. 1 rh rh 3495 May  4 16:39 settings.py
      -rw-rw-r--. 1 rh rh 1853 May  4 16:33 urls.py
      -rwxrwxr-x. 1 rh rh  405 May  4 16:33 wsgi.py
      
Submit an Answer