Apache installation for django project - django

Iam trying to install and configure apache for python 2.7 to develop a django application.
wsgi module is configured.
and I had did the following settings
edit httpd config file
Alias /media/ 'D:/myProjects/mysite/site_media/'
Order deny,allow
Allow from all
WSGIScriptAlias / 'D:\myProjects\mysite\apache\django.wsgi'
Order deny,allow
Allow from all
Create a folder apache in my site,say D:\myProjects\mysite\apache
Create wsgi script named 'django.wsgi'
import os
import sys
sys.path.append('D:\myProjects')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Create a new folder in my site folder named 'site_media'
Copy all images,css,javascript here
Edit template with path to css & images like this
href="/media/style.css"
But when Enter 'http://127.0.0.1' in browser the browser is loading.....but not connecting to my site
when checked in error log
the error is
[Tue May 08 16:59:41 2012] [notice] Parent: child process exited with status 1 -- Restarting.
[Tue May 08 16:59:41 2012] [warn] mod_wsgi: Compiled for Python/2.7.
[Tue May 08 16:59:41 2012] [warn] mod_wsgi: Runtime using Python/2.7.3.
[Tue May 08 16:59:41 2012] [notice] Apache/2.2.21 (Win32) mod_wsgi/3.3 Python/2.7.3
configured -- resuming normal operations
[Tue May 08 16:59:41 2012] [notice] Server built: Sep 9 2011 10:26:10
[Tue May 08 16:59:41 2012] [notice] Parent: Created child process 2408
[Tue May 08 16:59:41 2012] [warn] mod_wsgi: Compiled for Python/2.7.
[Tue May 08 16:59:41 2012] [warn] mod_wsgi: Runtime using Python/2.7.3.
[Tue May 08 16:59:41 2012] [notice] Child 2408: Child process is running
ImportError: No module named site
Please help me ..

if you are using apache2, you have to configure /etc/apache2/sites-enabled/000-default because it doesnt use httpd.config you have mentioned...

Related

Django Deployment: Error 403 Forbidden You don't have permission to access / on this server

I'm currently deploying my Django app into a CentOS 7 Server (CentOS Linux release 7.8.2003) based on Django documentation here. But I encounter this problem (Error 403) stated in the error log below.
Things to take note:
Yes, I was able to run the server through a virtual environment port 8000.
The database I'm using is mysql (guide).
Does this have any relation with the access permission set for apache? As for now, I have set the permission for apache as below:
sudo chown :apache colus_cafe/
sudo chown -R :apache colus_cafe/colus_cafe/media
Python version 3.6.8 & WSGI python36-mod_wsgi.x86_64 (guide).
What have I tried: Will be updated based on given answer
remove and reinstall virtual environment.
/etc/httpd/conf.d/django.conf:
Alias /static /home/colus/colus_cafe/colus_cafe/static
<Directory /home/colus/colus_cafe/colus_cafe/static>
Require all granted
</Directory>
Alias /media /home/colus/colus_cafe/colus_cafe/media
<Directory /home/colus/colus_cafe/colus_cafe/media>
Require all granted
</Directory>
<Directory /home/colus/colus_cafe/colus_cafe>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/colus/colus_cafe/colus_cafe/wsgi.py
WSGIDaemonProcess colus_cafe_app python-home=/home/colus/colus_cafe/env python-path=/home/colus/colus_cafe
WSGIProcessGroup colus_cafe_app
/etc/httpd/logs/error_log
Current thread 0x00007fee066d6880 (most recent call first):
[Wed Jul 08 07:11:09.691137 2020] [mpm_prefork:notice] [pid 10044] AH00170: caught SIGWINCH, shutting down gracefully
[Wed Jul 08 07:11:10.768060 2020] [core:notice] [pid 10231] SELinux policy enabled; httpd running as context system_$
[Wed Jul 08 07:11:10.769024 2020] [suexec:notice] [pid 10231] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/$[Wed Jul 08 07:11:10.789925 2020] [so:warn] [pid 10231] AH01574: module wsgi_module is already loaded, skipping
[Wed Jul 08 07:11:10.793580 2020] [lbmethod_heartbeat:notice] [pid 10231] AH02282: No slotmem from mod_heartmonitor
[Wed Jul 08 07:11:10.796988 2020] [mpm_prefork:notice] [pid 10231] AH00163: Apache/2.4.6 (CentOS) mod_wsgi/4.6.2 Pyt$[Wed Jul 08 07:11:10.797021 2020] [core:notice] [pid 10231] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Wed Jul 08 07:11:10.798024 2020] [wsgi:warn] [pid 10232] (13)Permission denied: mod_wsgi (pid=10232):
Unable to stat Python home /home/colus/colus_cafe/env.
Python interpreter may not be able to be initialized correctly.
Verify the supplied path and access permissions for whole of the path.
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
Based on Maarten's comment, I have found the answer for this problem.
I need to change the access permissions of apache to read and execute the django project folder by using chmod. However, this later shows another problem below.
/etc/httpd/logs/error_log
failed to map segment from shared object permission denied mysql.
Then I found out the error shows that Python (in the virtual environment) is unable to execute the packages (mysqlclient). Hence, the solution can be found here, which to change the security context of “httpd_sys_script_exec_t” which allows Apache to execute.
I hope this helps anyone who encounters this problem. And if there are any bad practices or mistakes that I have made, please do leave a comment.
Thank you and have a nice day.

WSGI Segmentation Fault After Upgrade from Apache 2.2 to 2.4 on Debian Linux with Django

After running a dist-upgrade from debian 7.1 to 8.10, WSI seg faults with the following errors in the Apache logs:
[Tue Mar 27 21:06:34.843952 2018] [core:notice] [pid 117022] AH00052: child pid 128687 exit signal Segmentation fault (11)
[Tue Mar 27 21:06:34.844011 2018] [wsgi:info] [pid 117022] mod_wsgi (pid=128687): Process 'icmlcc' has died, deregister and restart it.
[Tue Mar 27 21:06:34.844021 2018] [wsgi:info] [pid 117022] mod_wsgi (pid=128687): Process 'icmlcc' terminated by signal 11
[Tue Mar 27 21:06:34.844030 2018] [wsgi:info] [pid 117022] mod_wsgi (pid=128687): Process 'icmlcc' has been deregistered and will no longer be monitored.
[Tue Mar 27 21:06:53.989780 2018] [ssl:debug] [pid 128282] ssl_engine_io.c(1213): (70014)End of file found: [client 183.11.70.193:19265] AH02007: SSL handshake interrupted by system [Hint: Stop button pressed in browser?!]
I'm looking for any help I can get troubleshooting this. Thanks in advance. More info...
When I view the website in my browser, I'm presented with Django's Internal Server Error page, but the SSL certificate is correctly presented and trusted.
I occasionally receive, "Truncated or oversized response headers received from daemon process" in my site error.log
I'm using packages supplied by Debian for everything:
libapache2-mod-wsgi/oldstable,now **4.3.0-1** amd64 [installed]
apache2-bin/oldstable,now 2.4.10-10+deb8u11 amd64 [installed,automatic]
python2.7/oldstable,now 2.7.9-2+deb8u1 amd64 [installed]
The code works correctly with the development server. manage.py runserver works.
The relevant parts of my .conf file:
SSLCertificateFile /etc/ssl/certs/icml.cc.crt
SSLCertificateKeyFile /etc/ssl/private/icml.cc.key
#SSLCertificateChainFile /etc/ssl/certs/gd_bundle-g2-g1.crt
SSLCACertificateFile /etc/ssl/certs/gd_bundle-g2-g1.crt
<Directory /www/icml.cc/media>
#Order deny,allow
#Allow from all
Require all granted
</Directory>
<Directory /www/icml.cc/admin>
#Order deny,allow
#Allow from all
Require all granted
</Directory>
WSGIScriptAlias / /www/icml.cc/djnipscc/wsgi.py
WSGIDaemonProcess icmlcc home=/var/www python-path=/home.local/lee/.virtualenvs/nips/lib/python2.7/site-packages:/www/icml.cc processes=25 threads=10 maximum-requests=0
WSGIProcessGroup icmlcc
I tried using a python that was compiled from source. I haven't tried building mod_wsgi from source.
Any help would be much appreciated. Thanks.

Video Straming on raspberrypi using flask apche2and wsgi server

I have used flask app for straming video via raspberrypi camera. The code i used for flask app is here:
https://blog.miguelgrinberg.com/post/video-streaming-with-flask
In local server it is doing video stream but not on my website.
I am using apache2 server having wsgi file below:
flaskapp2.wsgi
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp2/FlaskApp2")
from ashish import app as application
application.secret_key ='1233883'
The structure of file system is :
/var/www/FlaskApp2/
flskapp2.wsgi
FlaskApp2/
ashish.py
camera_pi.py
templates/index.html
The ashish.py is the flask app which is doing stream.
The error log file in apache is:
[Wed Nov 23 15:17:17.458803 2016] [mpm_prefork:notice] [pid 783] AH00163: Apache/2.4.10 (Raspbian) mod_wsgi/4.3.0 Python/2.7.9 configured -- resuming normal operations
[Wed Nov 23 15:17:17.459043 2016] [core:notice] [pid 783] AH00094: Command line: '/usr/sbin/apache2'
[Wed Nov 23 15:17:14.278353 2016] [wsgi:warn] [pid 662] mod_wsgi: Compiled for Python/2.7.8.
[Wed Nov 23 15:17:14.279359 2016] [wsgi:warn] [pid 662] mod_wsgi: Runtime using Python/2.7.9.
[Wed Nov 23 15:17:14.305871 2016] [mpm_prefork:notice] [pid 662] AH00163: Apache/2.4.10 (Raspbian) mod_wsgi/4.3.0 Python/2.7.9 configured -- resuming normal operations
[Wed Nov 23 15:17:14.306169 2016] [core:notice] [pid 662] AH00094: Command line: '/usr/sbin/apache2'
[Wed Nov 23 15:17:16.502484 2016] [wsgi:warn] [pid 670] mod_wsgi: Compiled for Python/2.7.8.
[Wed Nov 23 15:17:16.504897 2016] [wsgi:warn] [pid 670] mod_wsgi: Runtime using Python/2.7.9.
[Wed Nov 23 15:17:16.531217 2016] [mpm_prefork:notice] [pid 670] AH00163: Apache/2.4.10 (Raspbian) mod_wsgi/4.3.0 Python/2.7.9 configured -- resuming normal operations
[Wed Nov 23 15:17:16.531502 2016] [core:notice] [pid 670] AH00094: Command line: '/usr/sbin/apache2'
On localhost it is working but not on my website is there any mistake in wsgi file or i need to add something.
You have a few possible issues when getting a 404 Not Found.
If that VirtualHost is not the only one in the Apache configuration file, then it will never be used. This is because you have set ServerName incorrectly. The ServerName directory should be set to a host name. Not a URL, not an IP address. Thus using:
ServerName https://7a78657b7a.dataplicity.io
is incorrect. It should be:
ServerName 7a78657b7a.dataplicity.io
It has to be a host name as Apache relies on it for name base virtual host matching against the Host header in the request. If it is wrong, then Apache will not know which is the correct VirtualHost to use. When this occurs Apache will fall back to sending the requests to whatever was the first VirtualHost definition it found when reading the Apache configuration files. Thus if this is not the first VirtualHost definition, it will never be used. If there is no similar URL handler set up in the first VirtualHost, you will get a 404.
The second is your WSGI application entry point in the WSGI script file pointed at by the WSGIScriptAlias directive, is not called application you will get a 404. You do appear to have it being called application, so you should be fine on this point and it shouldn't be the issue. There would have been a distinctive error message in the Apache error logs of this was the issue anyway.
The third is that the URL path you are using doesn't map to a route in the Flask application. There are actually two part to this. Because you are mounting at a sub URL in Apache, the URL path must at least start with /flask2. With that value it means you need to have a route in your Flask application which matches the root of the site. Normally in Flask that would mean you have a route for #app.route('/') but am not sure whether that still works when you have mounted your Flask application at a sub URL in Apache. You don't show your route code, so can't see what you have. You might at least try instead using /flask2/ in the URL. If your video feed isn't at the root of the Flask application but a sub URL such as set up by #app.route('/video_feed') as given in the post you link, then you should be using /flask2/video_feed as URL path. If you don't use the correct URL you will get a 404.
My whole configuration of files were right but as i was running flask applications under apache server ,apache was not able to acess pi camera because it was not having root access to pi.so,on local server it was working but not on my website when it was running on apache.

Installing Django with mod_wsgi in centos, ImportError: No module named os

I have, this httpd.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.hello2.ch
DocumentRoot /home/django_www/hello
WSGIScriptAlias / /var/www/django_www/hardi/django.wsgi
ErrorLog /var/www/django_www/hardi/error_log
CustomLog /var/www/django_www/hardi/access_log common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html
</VirtualHost>
vi /etc/hosts
127.0.0.1 localhost.localdomain localhost
128.178.131.89 www.hello2.ch
128.178.131.89 ohtersite.ch
python 2.5, mod_wsgi-2.5, /root/epd-5.1.0/lib/libpython2.5.so.1.0 : installed!
ldd /root/epd-5.1.0/bin/python
libpython2.5.so.1.0 => /root/epd-5.1.0/lib/libpython2.5.so.1.0 (0x00002b1bb7333000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003da0600000)
libdl.so.2 => /lib64/libdl.so.2 (0x0000003d9fe00000)
libutil.so.1 => /lib64/libutil.so.1 (0x0000003dadc00000)
libm.so.6 => /lib64/libm.so.6 (0x0000003da0200000)
libc.so.6 => /lib64/libc.so.6 (0x0000003d9fa00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003d9f600000)
echo $LD_LIBRARY_PATH
/root/epd-5.1.0/lib/
But when i try to load the webpage, i get the following error_log:
'import site' failed; use -v for traceback
[Wed Aug 29 11:52:57 2012] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Wed Aug 29 11:52:57 2012] [notice] Digest: generating secret for digest authentication ...
[Wed Aug 29 11:52:57 2012] [notice] Digest: done
[Wed Aug 29 11:52:57 2012] [notice] mod_python: Creating 4 session mutexes based on 256 max processes and 0 max threads.
[Wed Aug 29 11:52:57 2012] [notice] Apache/2.2.3 (Red Hat) configured -- resuming normal operations
[Wed Aug 29 11:55:00 2012] [error] [client 66.249.72.187] Symbolic link not allowed or link target not accessible: /var/www/html/app/webroot/w3
[Wed Aug 29 11:58:30 2012] [error] [client 66.249.72.187] Symbolic link not allowed or link target not accessible: /var/www/html/app/webroot/w3
[Wed Aug 29 12:01:58 2012] [error] [client 66.249.72.187] Symbolic link not allowed or link target not accessible: /var/www/html/app/webroot/w3
[Wed Aug 29 12:05:30 2012] [error] [client 66.249.72.187] Symbolic link not allowed or link target not accessible: /var/www/html/app/webroot/w3
[Wed Aug 29 12:09:00 2012] [error] [client 66.249.72.187] Symbolic link not allowed or link target not accessible: /var/www/html/app/webroot/w3
[Wed Aug 29 12:12:29 2012] [error] [client 66.249.72.187] Symbolic link not allowed or link target not accessible: /var/www/html/app/webroot/w3
[Wed Aug 29 12:14:06 2012] [error] [client 178.33.137.225] Symbolic link not allowed or link target not accessible: /var/www/html/app/webroot/w3
[Wed Aug 29 12:15:59 2012] [error] [client 66.249.72.187] Symbolic link not allowed or link target not accessible: /var/www/html/app/webroot/w3
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
'import site' failed; use -v for traceback
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
....
'import site' failed; use -v for traceback
[Wed Aug 29 12:17:47 2012] [notice] caught SIGTERM, shutting down
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
'import site' failed; use -v for traceback
[Wed Aug 29 12:17:47 2012] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Wed Aug 29 12:17:47 2012] [notice] Digest: generating secret for digest authentication ...
[Wed Aug 29 12:17:47 2012] [notice] Digest: done
[Wed Aug 29 12:17:47 2012] [notice] mod_python: Creating 4 session mutexes based on 256 max processes and 0 max threads.
[Wed Aug 29 12:17:47 2012] [notice] Apache/2.2.3 (Red Hat) configured -- resuming normal operations
[Wed Aug 29 12:18:01 2012] [error] Traceback (most recent call last):
[Wed Aug 29 12:18:01 2012] [error] File "/var/www/django_www/hardi/django.wsgi", line 1, in <module>
[Wed Aug 29 12:18:01 2012] [error] import os
[Wed Aug 29 12:18:01 2012] [error] ImportError: No module named os
Any idea, in how to make Django work with mod_wsgi, and how can i make it possible to match the first VirtualHost entry with www.hello2.ch webpage and all the others at the second VirtualHost entry?
Run 'ldd' on the mod_wsgi.so.
The problem is likely that it is finding a different Python version. It can also be that since your Python is not in a standard location, that it doesn't know where to find the installation.
In the latter case, add at global scope, outside of VirtualHost, in your Apache configuration:
WSGIPythonHome /root/epd-5.1.0
The value should correspond to the value of sys.prefix printed from your Python when run.
import sys
print sys.prefix
Based on the errors you've got, it seems like a couple of things could be going on. Rather than throwing all this into comments, however, I'll go ahead and post as an answer.
In my django.wsgi file I have the following two lines that change the PATH environment variable to let python import packages from my django project:
import sys
sys.path.append('/path/to/mysite.com/app/mydjangoproject')
You'll also want to make sure you add the following in your httpd.conf inside the VirtualHost that will you will be using django with:
<Directory /path/to/mysite.com/app/mydjangoproject>
Order allow,deny
Allow from all
</Directory>
EDIT:
With regard to the symbolic link that you mention in your comments that appears in the error messages, I'm not quite sure what to make of it. If you are just trying to use it to allow apache to access resources that are outside of the DocumentRoot, you'll want to add the Options FollowSymLinks directive for Directory that contains the symbolic link

Where are my 500s coming from in my DJango app?

I've got Apache setup on windows, (yes, not a very good idea, but not my fault). I open a popup window with about 32 images in it and all but 3 of those image links work. The images are coming from a TIF file and therefore have to be converted, so it is running thru my Django app to do all this and that part is working. The 3 urls that fail, when retried work.
In the apache log, I get 29 200s followed by 3 500s, but the actual images that failed to load are scattered randomly among the 29 good images. When I watch the log with tail -f, the 500s come along many seconds after the 200s but timestamped BEFORE them, like so:
192.168.20.45 - - [08/Mar/2012:01:24:28 -0600] "GET /viewer/... 200 44277
192.168.20.45 - - [08/Mar/2012:01:24:28 -0600] "GET /viewer/... 200 52283
192.168.20.45 - - [08/Mar/2012:01:24:28 -0600] "GET /viewer/... 200 44991
192.168.20.45 - - [08/Mar/2012:01:24:29 -0600] "GET /viewer/... 200 33077
192.168.20.45 - - [08/Mar/2012:01:24:22 -0600] "GET /viewer/... 500 16
192.168.20.45 - - [08/Mar/2012:01:24:22 -0600] "GET /viewer/... 500 16
192.168.20.45 - - [08/Mar/2012:01:24:22 -0600] "GET /viewer/... 500 16
DEBUG=False, and admins are setup, so I should get an email for every 500 that happens in Django, I've tested the email and it works as expected. The code I'm going thru has logging statements for error conditions and none of those are triggering.
It's almost like Apache or Mod_wsgi knows these connections come in, but never passes them to the Django code and they just end up dying from what seems to be a timeout. Chrome DevTools ends up showing me:
**Response Headers**
Connection:close
Content-Type:text/html; charset=utf-8
Date:Thu, 08 Mar 2012 07:24:22 GMT
Server:Apache/2.2.21 (Win32) mod_wsgi/3.3 Python/2.7.2
Set-Cookie:sessionid=d4616w0f850u1eb33q7a6fzf37f840b5; Path=/
Transfer-Encoding:chunked
Vary:Cookie
I'm on windows, and Apache says this on startup:
[Thu Mar 08 01:38:09 2012] [warn] mod_wsgi: Compiled for Python/2.7.
[Thu Mar 08 01:38:09 2012] [warn] mod_wsgi: Runtime using Python/2.7.2.
[Thu Mar 08 01:38:09 2012] [notice] Apache/2.2.21 (Win32) mod_wsgi/3.3 Python/2.7.2 configured -- resuming normal operations
[Thu Mar 08 01:38:09 2012] [notice] Server built: Sep 9 2011 10:26:10
[Thu Mar 08 01:38:09 2012] [notice] Parent: Created child process 3260
[Thu Mar 08 01:38:09 2012] [warn] mod_wsgi: Compiled for Python/2.7.
[Thu Mar 08 01:38:09 2012] [warn] mod_wsgi: Runtime using Python/2.7.2.
[Thu Mar 08 01:38:09 2012] [notice] Child 3260: Child process is running
[Thu Mar 08 01:38:09 2012] [notice] Child 3260: Acquired the start mutex.
[Thu Mar 08 01:38:09 2012] [notice] Child 3260: Starting 64 worker threads.
[Thu Mar 08 01:38:09 2012] [notice] Child 3260: Starting thread to listen on port 80.
There are less than 64 requests so there should be threads left over to handle them.
Any ideas on WHAT is happening? or HOW to figure it out?
EDIT----
There is no traceback, that is the problem. There is no indication at all that Python ever saw this request.
Yes, you shouldn't use python to serve static files, but you can't put a single image TIFF from a multi image TIFF into an you have to pull it from the multi-image file, put it in a format the browser can display, and no you don't want to convert the 100+ million TIFF images into single image PNGs or JPEG. AND these are not images that can be viewed by the general public, but are restricted to their "owners"
Loaded this on a second machine and the problem does not happen.....
You can write a small middleware to handle the exception. You can do some logging in the process_exception method, to find the root of the problem:
https://docs.djangoproject.com/en/dev/topics/http/middleware/#process-exception