0

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. As such, i have given apache all the neccessary permissions and ownership over /var/www and its contents (error log , custom log). I have also created a virtual host conf file in /etc/httpd/conf.d/django.conf where the code below is present.

< VirtualHost *:80 >

    ServerAdmin root@localhost
    ServerName InternationalStudentHelp.com
    DocumentRoot /var/www/

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

    ErrorLog /etc/httpd/logs/error_log
    CustomLog /etc/httpd/logs/access_log combined

    WSGIPassAuthorization On
    WSGIDaemonProcess django_proj python-path=/var/www/django_project/src python-home=/var/www/django_project/venv
    WSGIProcessGroup django_proj
    WSGIScriptAlias / /var/www/django_project/src/mysite/wsgi.py

    <Directory /var/www/django_project/src/mysite>
            <Files wsgi.py>
                    Require all granted
            </Files>
    </Directory>

< /VirtualHost >

'''

However i am not sure how to allow HTTPD (apache) to properly accept this configuration as i just receive the errors stated below. Any help would be appreciated greatly appreciated.

EDIT: ls -al /var/www

  • drwxr-xr-x. 5 apache apache 53 Apr 30 19:02 .
  • drwxr-xr-x. 21 root root 4096 Apr 28 17:31 ..
  • drwxr-xr-x. 6 apache apache 88 Apr 30 17:34 django_project
  • drwxr-xr-x. 2 apache apache 6 Dec 3 01:19 cgi-bin
  • drwxr-xr-x. 2 apache apache 51 Apr 30 19:07 html

EDIT: errors with ErrorLog and CustomLog commented out

  • mod_wsgi (pid=36037): Failed to exec Python script file '/var/www/django_project/src/mysite/wsgi.py'.

  • mod_wsgi (pid=36037): Exception occurred processing WSGI script '/var/www/django_project/src/mysite/wsgi.py'.

  • ModuleNotFoundError: No module named 'mysite'

These errors are repeated several times

EDIT: ls -al /var/www/django_project/src/mysite/

  • drwxr-xr-x. 3 apache apache 89 Apr 30 17:33 .
  • drwxr-xr-x. 8 apache apache 182 Apr 30 17:33 ..
  • -rw-r--r--. 1 apache apache 405 Apr 30 17:33 asgi.py
  • drwxr-xr-x. 2 apache apache 4096 Apr 30 17:33 pycache
  • -rw-r--r--. 1 apache apache 3694 Apr 30 17:33 settings.py
  • -rw-r--r--. 1 apache apache 1853 Apr 30 17:33 urls.py
  • -rw-r--r--. 1 apache apache 405 Apr 30 17:33 wsgi.py

EDIT: errors produced by the config file above:

  • AH00492: caught SIGWINCH, shutting down gracefully

  • Exception ignored in: del of <_UnixSelectorEventLoop running=False closed=False debug=False>> Traceback (most recent call last): File "/usr/lib64/python3.6/asyncio/base_events.py", line 526, in del NameError: name 'ResourceWarning' is not defined

  • SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0

  • AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)

  • AH02282: No slotmem from mod_heartmonitor

  • AH02951: mod_ssl does not seem to be enabled

  • Apache/2.4.37 (Red Hat Enterprise Linux) mod_wsgi/4.6.4 Python/3.6 configured-- resuming normal operations

  • AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'

EDIT: ls -al /var/www/django_projects

  • drwxr-xr-x. 6 apache apache 88 Apr 30 17:34 .

  • drwxr-xr-x. 5 apache apache 53 Apr 30 19:02 ..

  • drwxr-xr-x. 7 apache apache 169 Apr 30 17:33 .git

  • drw-rw-rw-. 2 apache apache 29 May 1 00:29 logs

  • -rw-r--r--. 1 apache apache 404 Apr 30 17:33 pyvenv.cfg

  • -rw-r--r--. 1 apache apache 26 Apr 30 17:33 README.md

  • drwxr-xr-x. 8 apache apache 182 Apr 30 17:33 src

  • drwxr-xr-x. 5 apache apache 100 Apr 30 17:33 venv

BarTM
  • 11
  • 4
  • I would start by giving the file a name ending in ".conf", then reload or restart Apache. Also, Django has tutorials for exactly this. – Gerard H. Pille Apr 29 '20 at 07:18
  • @GerardH.Pille Oh sorry, the file does end in ".conf" i just forgot to add it. I made some edits including the error message – BarTM Apr 29 '20 at 07:29
  • remove welcome.conf (save it somewhere else, or just delete the symbolic link in sites-enabled.) "separate file "etc/httpd/conf/django_project.conf" <=> "ln -s /etc/httpd/sites-available/django_pro.conf /etc/httpd/sites-enabled/django_pro.conf". Please check which is which. Where is django_project.conf ? And what has "Include example/*.conf" got to do with this? – Gerard H. Pille Apr 29 '20 at 08:37
  • @GerardH.Pille Sorry i fixed up the "Include example/*.conf", it was meant to be " Include sites-enabled/*.conf". And as for the django_project.conf, i originally had it in "/etc/httpd/conf/" with "http.conf" as this is where i initally made it. I then just copied it into "/etc/httpd/sites-available/" to make the linking easier. And for the welcome.conf file, if i remove the link, the default "Red Hat Enterprise Linux Test Page" is just loaded. – BarTM Apr 29 '20 at 08:49
  • But without the welcome.conf, Apache starts? "The ScriptAlias directive in /etc/httpd/sites-enabled/django_project.conf at line 250" ???? I don't see 250 lines, nor do I see a ScriptAlias. – Gerard H. Pille Apr 29 '20 at 10:19
  • Concerning the visibility of your django configuration, how many "" does your configuration have? How should Apache choose between them when it receives a request? – Gerard H. Pille Apr 29 '20 at 10:24
  • The django_project.conf only has the 1 "" and its respective closing tag, the one listed above, so it shouldn't have to choose? And should the django configuration only contain the code above? Or should it be a copy of httpd.conf with the code above appended to it (i have tried both and neither work - but i believe the file should only contain the above code and nothing else). – BarTM Apr 29 '20 at 10:38
  • httpd.conf is probably the main configuration, and has an IncludeOptional for sites-enabled/*.conf, which should take your django configuration and any other config in sites-enabled. Are there other files with "VirtualHost" ? Does Apache start or do you still have syntax errors or warnings? – Gerard H. Pille Apr 29 '20 at 11:08
  • httpd.conf is in the main configuration and does have an IncludeOptional for sites-enabled/*.conf. If i am understanding the "VirtualHost" correctly, the only file in "/sites-enabled" is /django_project.conf (when there is a link between "/sites-available" and "/sites-enabled"). When there is a link, apache does not start and i get errors. – BarTM Apr 29 '20 at 11:17
  • Are the errors still exactly the same as in your original question? – Gerard H. Pille Apr 29 '20 at 11:54
  • Sorry, i deleted the welcome.conf file (moved it - but kept the link between /etc/httpd/sites-available/django_pro.conf and /etc/httpd/sites-enabled/django_pro.conf in hopes that it would load my django project) and httpd works with no errors. However, when i go to the web page it isnt my django applicaiton. Instead it is a bare page with a title **Index of /** and four smaller headers "Name", "last modified", "Size", "Description" – BarTM Apr 29 '20 at 12:40
  • Probably because of the "autoindex.conf". What is "the webpage"? – Gerard H. Pille Apr 29 '20 at 13:01
  • Oh sorry, the web page is the one apache is hosting on port 80. It is the IP address of my virtual machine on port 80. (VM_ip:80) – BarTM Apr 29 '20 at 13:28
  • Disable autoindex.conf. OK, now search your configuration for anything concerning "WSGI". Perhaps my script may help: https://stackoverflow.com/questions/27152943/how-can-i-view-the-complete-httpd-configuration/61501155#61501155 – Gerard H. Pille Apr 29 '20 at 13:37
  • When i try to disable auto index (commenting out the LoadModule in /etc/httpd/conf/conf.modules.d/00-base.conf) and restart httpd, i receive more errors: AH00526: Syntax error on line 16 of /etc/httpd/conf.d/autoindex.conf. Invalid command 'IndexOptions', perhaps misspelled or defined by a module not included in the server configuration. Also, what should i do with files concering WSGI? – BarTM Apr 29 '20 at 14:44
  • It would be better for now to disable autoindex completely, not only the loadmodule. You really should aquire some basic knowledge about Apache. I'd like you to add anything concerning WSGI to your original question. Any loadmodule, config, ... – Gerard H. Pille Apr 29 '20 at 15:12
  • I have tried disabling it further via "Option -Index" under the tag in /etc/httpd/sites-available/django_project (Or shall i just move/rename the file - from what i have seen on apache documentation, they both work?). And for the conf files concerning WSGI, there exists: /etc/httpd/conf.modules.d/10-wsgi-python3.conf and /etc/httpd/modules/mod_wsgi_python3.so . Sorry for taking up your time with this, i have just been struggling on this for a while and nothing online seems to work. – BarTM Apr 29 '20 at 15:43
  • You have chosen a very complex architecture. Why did you modify django_project.conf if the error is in the autoindex.conf? Put aside the autoindex.conf too. Please add anything referring to WSGI to your original question. – Gerard H. Pille Apr 29 '20 at 16:28
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/107385/discussion-between-bartm-and-gerard-h-pille). – BarTM Apr 30 '20 at 02:21
  • @GerardH.Pille i have tried to somewhat start again with the configuraiton following this guide https://computingforgeeks.com/deploy-python-3-django-application-on-centos-with-apache-and-mod-wsgi/. However, when i get to the end and restart apache i just receive these errors again: Starting The Apache HTTP Server..., httpd.service: Main process exited, code=exited, status=1/FAILURE, httpd.service: Failed with result 'exit-code'., Failed to start The Apache HTTP Server. – BarTM Apr 30 '20 at 08:25
  • Check which errors there are in Apache error.log. – Gerard H. Pille Apr 30 '20 at 09:06
  • I've checked the guide - been there before - and I find it strange there is no "LoadModule wsgi" or something like that. You will have to post your complete configuration somewhere or send it to me, I can't help you with the partial information you provide. – Gerard H. Pille Apr 30 '20 at 09:13
  • Yeah when i check the error.log, it is empty, which confused me. I uninstalled and reinstalled httpd to get the configuration back to default. So now the only thing i have added, is the django.conf file in the tutorial i sent. How should i send you the complete configuration? Do you recommend any guide/tutorial i should try and follow? – BarTM Apr 30 '20 at 09:15
  • Also, to test a simple server with a basic message (to see if the basics are working) i tried this https://tecadmin.net/install-apache-mod-wsgi-centos-8/. But this also did not work as i received the errors: AH00526: Syntax error on line 1 of /etc/httpd/conf.d/python-wsgi.conf: Invalid command 'WSGIScriptAlias', perhaps misspelled or defined by a module not included in the server configuration – BarTM Apr 30 '20 at 09:21
  • Edit: i have managed to get that basic server working now - to test that mod_wsgi is properly installed – BarTM Apr 30 '20 at 10:24
  • I suppose you had to add the LoadModule? – Gerard H. Pille Apr 30 '20 at 10:45
  • I just uninstalled and reinstalled it. However, my django application still does not seem to work. – BarTM Apr 30 '20 at 11:36
  • So when i try the basic https://tecadmin.net/install-apache-mod-wsgi-centos-8/ web page config, it works and i can load the desired page. However, when i try the virtual host code on https://computingforgeeks.com/deploy-python-3-django-application-on-centos-with-apache-and-mod-wsgi/, i get these errors:httpd.service: Main process exited, code=exited, status=1/FAILURE, httpd.service: Failed with result 'exit-code'. What do you think the difference is between these two code segments that produces this error? – BarTM Apr 30 '20 at 11:56
  • Again: check Apache's error log. – Gerard H. Pille Apr 30 '20 at 12:00
  • The error log under /var/www/django_proj/logs/apis_error.log is empty. Is the virtual host code for the error log correct? – BarTM Apr 30 '20 at 12:03
  • How should I know? The code you refer to says "ErrorLog /var/www/awesomedjango/logs/apis_error.log". Has Apache permission to write to the log you refer to? – Gerard H. Pille Apr 30 '20 at 12:14
  • Do you have errors under /var/log/apache2 or /var/log/httpd ? – Gerard H. Pille Apr 30 '20 at 12:25
  • Oh yeah there is. It says it does not have permission to open the log file – BarTM Apr 30 '20 at 12:31
  • I thought i had already given it permissions of the /var/www directory through "sudo chown -R apache:apache /var/www/", but this doesnt seem to let it open the file – BarTM Apr 30 '20 at 12:37
  • "it does not have permission to open the log file" Please be specific. Which log file? What does "ls -al /var/www" give ? – Gerard H. Pille Apr 30 '20 at 13:29
  • (13)Permission denied: AH00091: httpd: could not open error log file /var/www/django_project/logs/apis_error.log. AH00015: Unable to open logs. Is the error. I also added the "ls -al" output to the post above – BarTM Apr 30 '20 at 13:35
  • Perhaps you've noticed there is no "/var/www/Assignment-2/" ? – Gerard H. Pille Apr 30 '20 at 13:37
  • Sorry, that was a mistype. It is actually /var/www/django_project/logs/apis_error.log – BarTM Apr 30 '20 at 13:39
  • Sorry? I have that directory in my root, not /var/www/ – BarTM Apr 30 '20 at 13:44
  • From what i can tell, apache has owner ship over the log files so it should be able to write to it. As such, the httpd service seems to run if i comment out the "ErrorLog" and "CustomLog" lines (to see if this is where the error was). However, if i go to the webpage (ip:80) there is just an internal server error (which im guessing is due to the lack of the ErrorLog and CustomLog ?) – BarTM Apr 30 '20 at 14:51
  • Do you have errors under /var/log/apache2 or /var/log/httpd ? Watch out for mysterious mistypes. I'd like to see "ls -al /var/www/django_project". – Gerard H. Pille Apr 30 '20 at 14:56
  • Yes, i have the access_log and error_log in /var/log/httpd. And the most recent error in error_log is : (13)Permission denied: AH00091: httpd: could not open error log file /var/www/django_project/logs/apis_error.log. AH00015: Unable to open logs – BarTM Apr 30 '20 at 14:59
  • Does the timestamp of that error correspond to the time of your visit to ip:80 ? – Gerard H. Pille Apr 30 '20 at 15:01
  • Oh sorry, that error is with "ErrorLog" and "CustomLog". With those two lines commented out, i get different errors corresponding to my visit to ip:80. I will add those errors above. – BarTM Apr 30 '20 at 15:06
  • Does /var/www/django_project/src/mysite/wsgi.py exist? If so, show me "ls -al /var/www/django_project/src/mysite/". – Gerard H. Pille Apr 30 '20 at 15:18
  • Yes it does exist: -rw-r--r--. 1 apache apache 405 Apr 30 17:33 wsgi.py. I added the full output above – BarTM Apr 30 '20 at 15:22
  • Who is running the Apache webserver? It looks as if it's not the user "apache". Who owns the logs in /var/log/httpd ? – Gerard H. Pille Apr 30 '20 at 15:32
  • Apache owns the error logs in /var/log/httpd, whilst the root of the virtual machine owns the access_log. Right now the apache server is not running. But when it is running, i believe my user on the virtual machine is running it. – BarTM May 01 '20 at 00:19
  • Apache should be started as root, and running as the user set in the configuration. Usually www-data or apache. Running as your user explains your troubles. – Gerard H. Pille May 01 '20 at 00:32
  • I tried stopping apache via "systemctl stop httpd.service". I then started apache as the root user via "sudo systemctl start httpd.service" (and also tried by logging into root). Apache is the user and group set in the configuration. But i still get the same errors in /etc/httpd/logs/error_log: (13)Permission denied: AH00091: httpd: could not open error log file /var/www/django_project/logs/apis_error.log. AH00015: Unable to open logs. These errors occur when i try to run "sudo systemctl start httpd.service" – BarTM May 01 '20 at 01:33
  • Also, if i change the ErrorLog and CustomLog lines in /etc/httpd/conf.d/django.conf to /etc/httpd/logs/error_log and /etc/httpd/logs/access_log, the apache service seems to run. However, i get a "500 internal server error" when i visit ip:80. And the errors are the same ones i added to my post above. (ModuleNotFoundError: No module named 'mysite') – BarTM May 01 '20 at 02:02
  • Modulenotfounderror is proof wsgi is working. It's a python error. – Gerard H. Pille May 01 '20 at 04:12
  • Check your WSGIDaemonProcess parameters. – Gerard H. Pille May 01 '20 at 04:35
  • I checked the parameters and altered them slightly. I edited the config code above to show the parameters. This however produces different errors, which i will also add above. – BarTM May 01 '20 at 05:03
  • WSGIScriptAlias does not refer to the daemon. Selinux could be the reason Apache can't go outside of /var/www/html. – Gerard H. Pille May 01 '20 at 06:23
  • How would i make it refer to the daemon? would i have to configure SELinux Policies for Apache? – BarTM May 01 '20 at 06:26
  • Refer to the daemon with the process-group parameter, but it may not be necessary, since your alias is "/". If SELinux is active, you need to configure it. For the problem with the logs, I would need to see "ls -al /var/www/django_projects". – Gerard H. Pille May 01 '20 at 07:59
  • SELinux is enabled (sestatus command). This may sound dumb, but how would i configure it? And how would i refer to the daemon with the process-group parameter? Isnt that what these lines are doing: WSGIDaemonProcess django_proj python-path=/var/www/django_project/src python-home=/var/www/django_project/venv. WSGIProcessGroup django_proj. Ill add the "ls -al" above – BarTM May 01 '20 at 08:16
  • Have a look here: https://www.serverlab.ca/tutorials/linux/web-servers-linux/configuring-selinux-policies-for-apache-web-servers/ – Gerard H. Pille May 01 '20 at 09:24
  • And have a look at /var/www/django_projects. Don't you see a difference between the logs folder and eg. src ? – Gerard H. Pille May 01 '20 at 09:26
  • Oh ok i see the difference. Should the permissions be the same? I thought that the logs folder was different as it isn't executable/doesnt contain executables. I had previously done chmod 666 on the logs folder. but i dont believe i performed a chmod on say src. – BarTM May 01 '20 at 10:02
  • You really need to brush up your Linux knowledge. The "x" on a directory means it can be searched. With the "chmod 666" you removed search rights from o, g and even u (the owner!!). Which tutorial told you to "chmod 666 logs" ? – Gerard H. Pille May 01 '20 at 10:06
  • Oh sorry, I was previously taught/thought that the "x" means it could be executed. Im not sure which tutorial told me. I have added such permissions (x) to the owner and group now. Should i keep the ErrorLog and CustomLog location the same as i have it above? Or should i change it back to /var/www/django_project/logs/apis_error.log (and the same for access.log)? – BarTM May 01 '20 at 11:00
  • When i change both the ErrorLog and CustomLog back to apis_error.log and the same for access.log, i still get those permission errors, despite the logs folder having permissions drwxrwxrwx. 2 apache apache. – BarTM May 01 '20 at 11:07
  • Did you fix SELinux? – Gerard H. Pille May 01 '20 at 11:39
  • In the SELinux set up you sent im currently trying to set up the policies, where is the /webapps referring to? As i cant seem to find a cache folder ? – BarTM May 01 '20 at 12:02
  • /webapps is an example. Where are your applications? Under /var/www/django_project ? Where are your logs? – Gerard H. Pille May 01 '20 at 12:10
  • Yeah i have added the policies for everything except the cache folder. I cant seem to find a cache folder. And what would this be referring to "/webapps/app1/public_html/uploads(/.*)?" in relation to my project? – BarTM May 01 '20 at 15:05
  • And the upload folder seems to be different on my system. Is there any way to check what it is? – BarTM May 01 '20 at 15:11
  • Does your application allow users to upload files? With the SELinux permissions, you can now demand a "ls -lZ" from your directories, and you should see the SELinux rules. – Gerard H. Pille May 01 '20 at 15:36
  • Sorry i was trying to set the SELinux permissions but i kept running into too many problems. I have been referred to this tutorial https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-centos-7. And so far it is better. However, when i try to run "./manage.py makemigrations" i get this error: "/usr/bin/env: ‘python\r’: No such file or directory" – BarTM May 02 '20 at 06:35
  • I think i have fixed this by running "python3 manage.py makemigrations" – BarTM May 02 '20 at 06:37

0 Answers0