I am trying to deploy my django application.
Like many others I faced the problem of "missing" django, no module named django, though it exists.
I had to install Django of version 2, not the newest and it was installed into '/home/ivan/.local/lib/python3.5/site-packages/'.
I have my apache configured and wsgi.py looks like this:
"""
WSGI config for mysite project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
"""
import os
import sys
sys.path.append('/home/ivan/.local/lib/python3.5/site-packages/')
print(sys.path)
# import /home/ivan/.local/lib/python3.5/site-packages/django
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
application = get_wsgi_application()
When I type python3 in terminal and do this:
>>> import django
>>> django.__path__
['/home/ivan/.local/lib/python3.5/site-packages/django']
So I got my path of django module and I append it, I also tried:
sys.path.insert(0, '/home/ivan/.local/lib/python3.5/site-packages/django')
and still I got 500 error with annoying:
[Tue Apr 21 13:12:41.861348 2020] [wsgi:error] [pid 29470] [remote 185.201.90.67:1131] Traceback (most recent call last):
[Tue Apr 21 13:12:41.861379 2020] [wsgi:error] [pid 29470] [remote 185.201.90.67:1131] File "/var/www/mysite/mysite/wsgi.py", line 15, in <module>
[Tue Apr 21 13:12:41.861384 2020] [wsgi:error] [pid 29470] [remote 185.201.90.67:1131] from django.core.wsgi import get_wsgi_application
[Tue Apr 21 13:12:41.861397 2020] [wsgi:error] [pid 29470] [remote 185.201.90.67:1131] ImportError: No module named 'django'
Why wsgi cannot work properly? What am I doing wrong here?
Why interpreter cannot find a module after I forced him by sys.path.insert() ?
Huge thank in advance!
UPDATE ONE
print(sys.path) inside of wsgi returns:
['/var/www/mysite', '/usr/lib/python35.zip', '/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload',
'/usr/local/lib/python3.5/dist-packages',
'/usr/lib/python3/dist-packages',
'/home/ivan/.local/lib/python3.5/site-packages/']
UPDATE TWO
Since I am trying to deploy the app, no matter how I solve it.
Thanks to #KolaB for advice of virtualenv.
I just did it , now I have this structure:
├── mysite
├── requirements.txt
└── venvdjang
venvdjang is a dir with virtual interpreter.
I also installed django by source /venvdjang/bin/activate
pip install -r requirements.txt
Now, how can I tell my wsgi and apache2 that I want to use virtualenv?
Update Three
Thank you all for your help!
I have a VPS with flask application in production (www.example.com)
Now I have to deploy another one web application on subdomain - django. (www.sub.example.com)
This is config for django.
/etc/apache2/sites-available/mysite.conf
<VirtualHost *:80>
ServerName sub.example.com
ServerAlias www.sub.example.com
ServerAdmin sub.example.com#gmail.com
#DocumentRoot /var/www/mysite
ErrorLog ${APACHE_LOG_DIR}/mysite-error.log
CustomLog ${APACHE_LOG_DIR}/mysite-access.log combined
#DocumentRoot /var/www/mysite/index.html
WSGIDaemonProcess mysite processes=2 threads=25 python-path=/var/www/mysite
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py
Alias /robots.txt /var/www/mysite/static/robots.txt
Alias /favicon.ico /var/www/mysite/static/favicon.ico
Alias /static/ /var/www/mysite/static/
Alias /static/ /var/www/mysite/media/
<Directory /var/www/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /var/www/mysite/static>
Require all granted
</Directory>
<Directory /var/www/mysite/media>
Require all granted
</Directory>
</VirtualHost>
and this is config for flask
/etc/apache2/sites-available/FlaskApp-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.example.com
ServerAdmin example.com#mywebsite.com
WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
<Directory /var/www/FlaskApp/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/FlaskApp/FlaskApp/static
<Directory /var/www/FlaskApp/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias example.com
SSLCertificateFile /etc/letsencrypt/live/example.com
--p1ai/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com--p1ai/privkey.pem
</VirtualHost>
</IfModule>
<IfModule mod_ssl.c>
<VirtualHost *:80>
ServerName example.com
Redirect / https://www.example.com
ServerAdmin admin#mywebsite.com
WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
<Directory /var/www/FlaskApp/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/FlaskApp/FlaskApp/static
<Directory /var/www/FlaskApp/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
# Some rewrite rules in this file were disabled on your HTTPS site,
# because they have the potential to create redirection loops.
# RewriteCond %{SERVER_NAME} = example.com
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
#RewriteCond %{SERVER_PORT} !^443$
#RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
</VirtualHost>
</IfModule>
OS - Ubuntu 16.04
mod_wsgi was installed a few years ago by sudo apt-get install
Update Four
I changed WSGIDaemonProcess python-home as #Alasdair said to python-home=/var/www/venvdjang but it did not work out.
Now if I run in terminal source /var/www/venvdjang/bin/activate
(venvdjang) ivan#server:/var/www$ python
Python 3.5.2 (default, Oct 8 2019, 13:06:37)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> import sys
>>> sys.path
['', '/var/www/venvdjang/lib/python35.zip', '/var/www/venvdjang/lib/python3.5',
'/var/www/venvdjang/lib/python3.5/plat-x86_64-linux-gnu',
'/var/www/venvdjang/lib/python3.5/lib-dynload',
'/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/var/www/venvdjang/lib/python3.5/site-packages']
>>>
But my wsgi.py after removing all sys.path.append() staff still returns after print(sys.path) this:
['/var/www/venvdjang',
'/usr/lib/python35.zip',
'/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/usr/lib/python3.5/lib-dynload',
'/usr/local/lib/python3.5/dist-packages',
'/usr/lib/python3/dist-packages']
It is like he still goes to system interpreter...
Why I cannot refer to a correct virtualenv ?
I am new in django and apache. I want to publish my django website by apache and mod-wsgi.
when I start httpd.exe, I recieve 403 forbidden error in my browser.
my apache config is here
LoadFile "c:/users/zharf/appdata/local/continuum/anaconda3/envs/django/python36.dll"
LoadModule wsgi_module "c:/users/zharf/appdata/local/continuum/anaconda3/envs/django/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd"
WSGIPythonHome "c:/users/zharf/appdata/local/continuum/anaconda3/envs/django"
Alias /static/ /D:/user/JarfaSys/static/
<Directory D:/user/JarfaSys/static/>
AllowOverride none
Require all granted
</Directory>
WSGIScriptAlias / /D:/user/JarfaSys/JarfaSys/wsgi.py
WSGIPythonPath /D:/user/JarfaSys/
WSGIPassAuthorization On
<VirtualHost 127.0.0.1:443>
DocumentRoot D:/user/JarfaSys
Alias /favicon.ico D:/user/JarfaSys/JarfaSys/favicon.ico
Alias / /D:/user/JarfaSys/JarfaSys/wsgi.py
ServerName 127.0.0.1
SSLEngine on
SSLCertificateKeyFile C:/Users/zharf/TibiFiles/host.key
SSLCertificateFile C:/Users/zharf/TibiFiles/host.cert
SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
<Directory D:/user/JarfaSys/JarfaSys/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
WSGIScriptAlias / /D:/user/JarfaSys/JarfaSys/wsgi.py
WSGIPythonPath /D:/user/JarfaSys/
WSGIPassAuthorization On
<Directory D:/user/JarfaSys/JarfaSys/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
wsgi.py is :
import os
import sys
path = "D:/Ghanbari/JarfaSys/JarfaSys"
if path not in sys.path:
sys.path.append(path)
os.environ['PYTHON_EGG_CACHE'] = '/tmp/.python-eggs'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "JarfaSys.settings")
#print os.getenv("DJANGO_SETTINGS_MODULE")
#print os.getenv("PYTHON_EGG_CACHE")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
error log file is here
[Fri Aug 23 16:29:26.827875 2019] [core:error] [pid 10784:tid 1092] (20024)The given path is misformatted or contained invalid characters: [client ::1:50025] AH00036: access to / failed (filesystem path 'C:/D:')
[Fri Aug 23 16:29:26.848875 2019] [core:error] [pid 10784:tid 1092] (20024)The given path is misformatted or contained invalid characters: [client ::1:50025] AH00036: access to /favicon.ico failed (filesystem path 'C:/D:'), referer: http://localhost/
Apache service user has access to my app directory.
I study many similar questions but the problem is not solved. any suggestions would be appreciated.
Here is an example of project setup in apache. You vhosts file has a few errors
Listen 443
<VirtualHost *:443>
ServerName localhost
ErrorLog "logs/logname-error.log"
CustomLog "logs/logname-access.log" common
WSGIScriptAlias / "C:\workspace\your_project_path\wsgi.py"
Alias /static/ C:/workspace/static/
<Directory C:\your_project_path>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
Remove the / Before the D:/... use the above as pattern.
and an example of a wsgi.py
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
activate_env = r"C:\your_virtual_environment_path\Scripts\activate_this.py"
with open(activate_env) as f:
exec(f.read(), {"__file__": activate_env})
from your_main import app as application
I have a config like this :
CentOS 7 - Varnish ( cache server ) / Nginx ( reverse proxy ) / Apache
And it's my Django configuration :
WSGISocketPrefix /var/run/wsgi
<VirtualHost ip:8181>
ServerName domain.ir
ServerAlias www.domain.ir
ServerAdmin info#domain.ir
DocumentRoot /var/www/DjangoProject
UseCanonicalName On
ScriptAlias /cgi-bin/ /var/www/DjangoProject/cgi-bin/
<Directory /var/www/DjangoProject/DjangoProject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /statics/ "/var/www/DjangoProject/statics/"
<Directory "/var/www/DjangoProject/statics">
Require all granted
</Directory>
WSGIDaemonProcess DjangoProject python-path=/usr/lib/python3.4/site-packages python-home=/var/www/DjangoProject user=djangouser socket-user=djangouser
WSGIProcessGroup DjangoProject
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /var/www/DjangoProject/DjangoProject/wsgi.py
ErrorLog /var/log/apache/django-error.log
</VirtualHost>
When i want to open my website there is a permission denied error in apache log :
[wsgi:error] [pid 2131:tid 140344565954304] (13)Permission denied: [client 77.104.92.126:38982] mod_wsgi (pid=2131): Unable to connect to WSGI daemon process 'TiTar_API' on '/var/run/wsgi.2128.0.1.sock' as user with uid=99.
I know that uid=99 is nobody user. What's going on here? Why this user want to connect WSGI socket? What is the purpose of user and socket-user attributes in WSGIDaemonProcess?
Edit : I saw other questions too, but i think there is another problem ( nobody user )
Django-WSGI setup causing permission denied issues on CentOS 7
Django + Apache + mod_wsgi permission denied
https://serverfault.com/questions/357804/apache2-mod-wsgi-django-named-virtual-servers
I'm using django (2.0.1) on Raspberry Pi 3 with DietPi on it. I've upgraded RPI today and got this error
[Fri Mar 02 08:29:57.741991 2018] [wsgi:error] [pid 1564:tid 1861219376] [client 127.0.0.1:60308] Truncated or oversized response headers received from daemon process 'app': /home/user/app/app/wsgi.py, referer: http://app/
It worked well.
My config for apache
<VirtualHost *:80>
TimeOut 120
ServerAdmin admin
ServerName app
ServerAlias app
ErrorLog /home/user/app/logs/error_log
CustomLog /home/user/app/logs/access_log common
WSGIScriptAlias / /home/user/app/app/wsgi.py
WSGIDaemonProcess appprocesses=6 python-path=/home/user/app:/home/user/app_env/lib/python3.5/site-packages header-buffer-size=65536 inactivity-timeout=300
WSGIProcessGroup app
<Directory "/home/user/app">
Require all granted
</Directory>
Alias /robots.txt /home/user/app/static/robots.txt
Alias "/static/admin/" "/home/user/app/root/admin/"
<Location "/home/user/app/root/admin">
SetHandler None
</Location>
Alias "/media/" "/home/user/app/media/"
<Location "/home/user/app/media/">
SetHandler None
</Location>
Alias "/static/" "/home/user/app/static/"
<Location "/home/user/app/static/">
SetHandler None
</Location>
<LocationMatch "\.(jpg|gif|png|js|css)$">
SetHandler None
</LocationMatch>
<Directory "/home/user/app" >
WSGIProcessGroup terminal
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
my /home/user/app/app/wsgi.py is standard from django.
RPI connects trough USB0 to rfid-reader and because of this error I get a 500-error-page.
I don't know where am I wrong. I hope you can help.
I want to add a wordpress app at blog.mydomain.com but I already have a Django app at mydomain.com running with wsgi in deamon mode.
I work on a Linux server (debian) with apache2.4.10 and PHP 5.6
Here is my apache conf for mydomain.com (django.conf):
<VirtualHost 000.000.000.000:443> (my server ip)
ServerName mydomain.com
ServerAlias mydomain.com
ServerAlias www.mydomain.com
SSLEngine On
SSLCertificateFile /pathToSsl/ssl.crt
SSLCertificateKeyFile /pathToSsl/ssl.key
SSLCertificateChainFile /pathToSsl/ssl.pem
SSLVerifyClient None
WSGIDaemonProcess mydomain.com threads=4
WSGIProcessGroup mydomain.com
WSGIScriptAlias /path/to/settings/wsgi.py
# Aliases toward static data
Alias /media /pathToMedia/media/
Alias /static /pathToStatic/static/
Alias /log /var/www/html/
<Directory /path/to/settings>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access-mydomain.com.log combined
ErrorLog ${APACHE_LOG_DIR}/error-mydomain.com.log
ErrorDocument 500 "Oups something goes wrong."
</VirtualHost>
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias mydomain.com
ServerAlias www.mydomain.com
Redirect / https:// mydomain.com/
</VirtualHost>
This one is working perfectly.
And now I add a new configuration for my wordpress blog (wordpress.conf):
<VirtualHost *:80>
ServerName blog.mydomain.com
DocumentRoot "/var/www/html/wordpress"
<Directory /var/www/html/wordpress>
AllowOverride all
Options Indexes FollowSymLinks
Require all granted
</Directory>
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access-blog.fr.log combined
ErrorLog ${APACHE_LOG_DIR}/error-blog.fr.log
</VirtualHost>
The problem is when i try to access to blog.mydomain.com, I am redirected to mydomain.com after few seconds.
At the beginning, I thought that was because my conf wordpress was not found but if I put
Redirect / stackoverflow.com
in wordpress.conf and it's working. So the issue is not here.
I find this in my apache error log for the wordpress blog :
[Mon Oct 24 13:15:55.829675 2016] [wsgi:error] [pid 16958] [client 000.000.000.000:38439] mod_wsgi (pid=16958): Exception occurred processing WSGI script '/path/to/settings/wsgi.py'., referer: http://blog.mydomain.com/
[Mon Oct 24 13:15:55.829844 2016] [wsgi:error] [pid 16958] [client 000.000.000.000:38439] IOError: failed to write data, referer: http://blog.mydomain.com/
In my opinion, the problem come from wsgi, but i don't know why and how to resolve it.
I don't understand why this conf go over wsgi.
I hope you can help me.
Thanks for reading.