conclusion of this question
1.what I wanted to do in the first place.
i wanted to allow only some user in Django can upload file. I could do write codes to do that in Django web framework. But if you set upload folder as r__ permssions, you can't upload file in the first place. I just set my upload folder as drwxrwxrwx 777 permssions to let my users in Django can upload files. leaving your folder as 777 permssion seems bad idea for security reason clearly. I wanted to set my media folder as 774 or 764.
My searching apache authentication and mod wsgi started from my wrong understanding. I thought I can allow users in django have owner or group permssion in ubuntu. for example, if you a user in Django are in a group named 'tester', you can access to a folder has 'drwxrwxr__ tester tester'.
and I thought only embedded mode is possible to use apache authentication. But that was my misunderstanding.
in conclusion, I can solve my problem by setting owner and group permission following apache default user or (more secure) user/group you set with options of WSGIDaemonProcess. Then you manage which user can upload files in your media file in Django(I would use PermissionMixin class)
additionally, This is what and how I understood in here.
1) WSGIDaemon mode is recommended in most cases. check Graham Dumpleton's article in here. It seems Embedded mode could be more efficient if you use just simple codes.
2) 'you can expect to see slowdowns once you get above a few hundred entries, and may wish to consider a different authentication method at that time'. This kind of slowdown is caused when using htpasswd file with Apache AuthUserFile. But if you let Django do authentication, it bypass the scanning of a flat file. So It is not a case when you hook Django authentication in mod wsgi.
Down below, I put link how solve when you have an error which is caused by version of mod_wsgi and version of python and explains how I fixed my codes to set embedded mode thanks to #Graham Dumpleton.
Procedure of how I solve my errors when I set embedded mode
First, I didn't have problem to work with daemon mode of mod_wsgi.
I wanted to use embedded mode of mod_wsgi with virtualenv. The reason why I wanted to use embedded mode is to use apache basic or digest authentication with mod_wsgi. according to this, 'By default the auth providers are executed in context of first interpreter created b y Python. ie., '%{GLOBAL}' and always in the Apache child processes, never in a daemon process'. I understood I can use apache basic or digest authentication with only embedded mode of mod_wsgi.
1.error because low version of mod_wsgi
If you install libapache2-mod-wsgi-py3 by apt-get in ubuntu 14 or 16, version of it is 3.4
according to this link, mod_wsgi version 4.2+ is needed for python 3.4. I installed mod_wsgi 4.5.7 following instructions in that link. no more error.
2.error with WSGIPythonPath
I followed instructions in django doc. I kept making an error and I figured out that it doesnt work with virtualenv. I found this article. I thought this is a way to solve my problem but it still doesn't work. I can't understand why it doesn't work and can't find better resource than links I mentioned.
<VirtualHost *:80>
WSGIScriptAlias / /home/cango/myproject/myproject/wsgi.py
WSGIPythonPath home/cango/myvenv/lib/python3.5/site-packages
# I also tried /home/~ I put / at very beginning. It didn't work too.
<Directory /home/cango/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
but if I change little bit to use daemon mode like below, it works!
<VirtualHost *:80>
WSGIDaemonProcess cango python-path=/home/cango/myproject:home/cango/myvenv/lib/Python3.5/site-packages
WSGIProcessGroup cango
WSGIScriptAlias / /home/cango/myproject/myproject/wsgi.py
#WSGIPythonPath home/cango/myvenv/lib/python3.5/site-packages
<Directory /home/cango/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
exactly same except a few lines for Daemon mode.
------------------- when I applied according to comments-------------------
1.Both WSGIPythonPath and WSGIPythonHome shouldn't be inside of a VirtualHost.
I added WSGIPythonPath in wsgi.conf under /etc/apache2/mods-available
wsgi.conf
<IfModule mod_wsgi.c>
WSGIPythonHome /usr/local/pythonenv
#WSGIPythonPath /usr/local/pythonenv/lib/python3.5/site-packages
#From comment, I don't need to add WSGIPythonPath, WSGIPythonHome is enough to set
I remained wsgi.load under /etc/apache2/mode-available same like below
wsgi.load
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi-py35.cpython-35m-x86_64-linux-gnu.so
I removed WSGIPythonPath in VirtualHost setting.
000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
WSGIScriptAlias / /home/cango/myproject/myproject/wsgi.py
<Directory /home/cango/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
now I don't get a error which I had before 'WSGIPythonPath something error'. but when I hit my IP address, it shows 500 internal Server Error. If I test with python manage.py runserver 0.0.0.0:8000 and enter my IP adress:8000/, I can see Django page.
apache2ctl -M I can see wsig_module
Loaded Modules:
core_module (static)
so_module (static)
watchdog_module (static)
http_module (static)
log_config_module (static)
logio_module (static)
version_module (static)
unixd_module (static)
access_compat_module (shared)
alias_module (shared)
auth_basic_module (shared)
authn_core_module (shared)
authn_file_module (shared)
authz_core_module (shared)
authz_host_module (shared)
authz_user_module (shared)
autoindex_module (shared)
deflate_module (shared)
dir_module (shared)
env_module (shared)
filter_module (shared)
mime_module (shared)
mpm_event_module (shared)
negotiation_module (shared)
setenvif_module (shared)
status_module (shared)
wsgi_module (shared)
service apache2 status
apache2.service - LSB: Apache2 web server
Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since Sat 2016-11-19 03:19:36 UTC; 2min 25s ago
Docs: man:systemd-sysv-generator(8)
Process: 19444 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
Process: 18407 ExecReload=/etc/init.d/apache2 reload (code=exited, status=0/SUCCESS)
Process: 19471 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)
Tasks: 55
Memory: 43.2M
CPU: 768ms
CGroup: /system.slice/apache2.service
├─19488 /usr/sbin/apache2 -k start
├─19491 /usr/sbin/apache2 -k start
└─19492 /usr/sbin/apache2 -k start
Nov 19 03:19:36 server4.cango.com systemd[1]: Stopped LSB: Apache2 web server.
Nov 19 03:19:36 server4.cango.com systemd[1]: Starting LSB: Apache2 web server...
Nov 19 03:19:36 server4.cango.com apache2[19471]: * Starting Apache httpd web server apache2
Nov 19 03:19:36 server4.cango.com apache2[19471]: *
Nov 19 03:19:36 server4.cango.com systemd[1]: Started LSB: Apache2 web server.
If I command a2dismod wsgi and then service apache2 restart, it causes an error. It seems I can't restart with disabling wsgi_mod because I set WSGIScriptAlias
Nov 19 03:22:59 server4.cango.com apache2[19746]: * Starting Apache httpd web server apache2
Nov 19 03:22:59 server4.cango.com apache2[19746]: *
Nov 19 03:22:59 server4.cango.com apache2[19746]: * The apache2 configtest failed.
Nov 19 03:22:59 server4.cango.com apache2[19746]: Output of config test was:
Nov 19 03:22:59 server4.cango.com apache2[19746]: AH00526: Syntax error on line 33 of /etc/apache2/sites-enabled/000-def
Nov 19 03:22:59 server4.cango.com apache2[19746]: Invalid command 'WSGIScriptAlias', perhaps misspelled or defined by a
Nov 19 03:22:59 server4.cango.com apache2[19746]: Action 'configtest' failed.
Nov 19 03:22:59 server4.cango.com apache2[19746]: The Apache error log may have more information.
Nov 19 03:22:59 server4.cango.com systemd[1]: apache2.service: Control process exited, code=exited status=1
Nov 19 03:22:59 server4.cango.com systemd[1]: Failed to start LSB: Apache2 web server.
-- Subject: Unit apache2.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit apache2.service has failed.
--
-- The result is failed.
Nov 19 03:22:59 server4.cango.com systemd[1]: apache2.service: Unit entered failed state.
Nov 19 03:22:59 server4.cango.com systemd[1]: apache2.service: Failed with result 'exit-code'.
Nov 19 03:23:50 server4.cango.com sudo[19822]: root : TTY=pts/1 ; PWD=/home/cango/jaemyun ; USER=root ; COMMAND=/bin
Nov 19 03:23:50 server4.cango.com sudo[19822]: pam_unix(sudo:session): session opened for user root by root(uid=0)
Well, I wonder what I made a mistake. Still 500 Internal Server Error.
2.Nothing prevents you from using daemon mode for your WSGI application at the same time as using the authnz handlers.
From the link which I mentioned above, it says "if the authentication check is making use of the internals of some Python web framework, it is recommended that the application using that web framework also be run in embedded mode and the same application group"
So I thought I need to use embedded mode with Django framework.
-------------------------- fixed by another comment ----------------------------
I checked error log in apache. There was an error and it seems it has problem to load wsgi script in my django project. Well, above, I changed folder name 'jaemyun' to 'myproject'. there is a wsgi.py file which was automatically made by Django under /home/cango/jaemyun/jaemyun/ path. Why does it say that it can't load it???
No module named 'jaemyun', referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.525510 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] mod_wsgi (pid=11755): Target WSGI script '/home/cango/jaemyun/jaemyun/wsgi.py' cannot be loaded as Python module.
[Sun Nov 20 10:37:24.525600 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] mod_wsgi (pid=11755): Exception occurred processing WSGI script '/home/cango/jaemyun/jaemyun/wsgi.py'.
[Sun Nov 20 10:37:24.525801 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] Traceback (most recent call last):
[Sun Nov 20 10:37:24.525866 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] File "/home/cango/jaemyun/jaemyun/wsgi.py", line 16, in <module>
[Sun Nov 20 10:37:24.525872 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] application = get_wsgi_application()
[Sun Nov 20 10:37:24.525880 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] File "/usr/local/pythonenv/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Sun Nov 20 10:37:24.525885 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] django.setup(set_prefix=False)
[Sun Nov 20 10:37:24.525893 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] File "/usr/local/pythonenv/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
[Sun Nov 20 10:37:24.525909 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Sun Nov 20 10:37:24.525917 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] File "/usr/local/pythonenv/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__
[Sun Nov 20 10:37:24.525921 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] self._setup(name)
[Sun Nov 20 10:37:24.525928 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] File "/usr/local/pythonenv/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
[Sun Nov 20 10:37:24.525932 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] self._wrapped = Settings(settings_module)
[Sun Nov 20 10:37:24.525939 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] File "/usr/local/pythonenv/lib/python3.5/site-packages/django/conf/__init__.py", line 97, in __init__
[Sun Nov 20 10:37:24.525943 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] mod = importlib.import_module(self.SETTINGS_MODULE)
[Sun Nov 20 10:37:24.525950 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] File "/usr/local/pythonenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Sun Nov 20 10:37:24.525954 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] return _bootstrap._gcd_import(name[level:], package, level)
[Sun Nov 20 10:37:24.525961 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Sun Nov 20 10:37:24.525968 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Sun Nov 20 10:37:24.525975 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Sun Nov 20 10:37:24.525982 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Sun Nov 20 10:37:24.525988 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Sun Nov 20 10:37:24.525995 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Sun Nov 20 10:37:24.526002 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
[Sun Nov 20 10:37:24.526023 2016] [wsgi:error] [pid 11755:tid 140507221055232] [client 124.80.108.6:64058] ImportError: No module named 'jaemyun'
[Sun Nov 20 10:37:24.564690 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] mod_wsgi (pid=11755): Target WSGI script '/home/cango/jaemyun/jaemyun/wsgi.py' cannot be loaded as Python module., referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564729 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] mod_wsgi (pid=11755): Exception occurred processing WSGI script '/home/cango/jaemyun/jaemyun/wsgi.py'., referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564889 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] Traceback (most recent call last):, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564947 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] File "/home/cango/jaemyun/jaemyun/wsgi.py", line 16, in <module>, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564953 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] application = get_wsgi_application(), referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564961 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] File "/usr/local/pythonenv/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564972 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] django.setup(set_prefix=False), referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564981 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] File "/usr/local/pythonenv/lib/python3.5/site-packages/django/__init__.py", line 22, in setup, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564985 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING), referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564992 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] File "/usr/local/pythonenv/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.564997 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] self._setup(name), referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565004 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] File "/usr/local/pythonenv/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565008 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] self._wrapped = Settings(settings_module), referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565015 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] File "/usr/local/pythonenv/lib/python3.5/site-packages/django/conf/__init__.py", line 97, in __init__, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565019 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] mod = importlib.import_module(self.SETTINGS_MODULE), referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565026 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] File "/usr/local/pythonenv/lib/python3.5/importlib/__init__.py", line 126, in import_module, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565030 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] return _bootstrap._gcd_import(name[level:], package, level), referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565037 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] File "<frozen importlib._bootstrap>", line 986, in _gcd_import, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565044 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] File "<frozen importlib._bootstrap>", line 969, in _find_and_load, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565052 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565059 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565066 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] File "<frozen importlib._bootstrap>", line 986, in _gcd_import, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565073 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] File "<frozen importlib._bootstrap>", line 969, in _find_and_load, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565080 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked, referer: http://169.56.70.181/
[Sun Nov 20 10:37:24.565097 2016] [wsgi:error] [pid 11755:tid 140507204269824] [client 124.80.108.6:64057] ImportError: No module named 'jaemyun', referer: http://169.56.70.181/
last change.
wsgi.conf
<IfModule mod_wsgi.c>
WSGIPythonHome /usr/local/pythonenv
WSGIPythonPath /home/cango/jaemyun
I had to set WSGIPythonPath and direct to my django project path. Now it works!
Related
I have been pulling my hair out with this and I dont know what else I should do.
For context, I am trying to host 2 django websites on the same Apache2 server. I am able to setup 1 website with ease but whenever I try to add a second one everything breaks.
At the momento both webs are located in in /home/my_usr dir.
So we would have /home/my_usr/web1 and /home/my_usr/web2.
My web1-web2.conf file looks like:
WSGIDaemonProcess web1 python-home=/home/my_usr/web1/env python-path=/home/my_usr/web1
WSGIProcessGroup web1
WSGIDaemonProcess web2 python-home=/home/my_usr/web2/env python-path=/home/my_usr/web2
WSGIProcessGroup web2
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that the server uses to
# identify itself. This is used when creating redirection URLs. In the context of virtual hosts, the
# ServerName specifies what hostname must appear in the request's Host: header to match this virtual
# host. For the default virtual host (this file) this value is not decisive as it is used as a last
# resort host regardless. However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg. It is also possible to configure the loglevel for particular modules,
# e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are enabled or disabled at a global level,
# it is possible to include a line for only one particular virtual host. For example the following
# line enables the CGI configuration for this host only after it has been globally disabled with
# "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Alias /web1/static /home/my_usr/web1/static
<Directory /home/my_usr/web1/static>
Require all granted
</Directory>
Alias /media /home/my_usr/web1/media
<Directory /home/my_usr/web1/media>
Require all granted
</Directory>
<Directory /home/my_usr/web1/web1>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias /web1 /home/my_usr/web1/web1/wsgi.py
Alias /web2/static/ /home/my_usr/web2/static
<Directory /home/my_usr/web2/static>
Require all granted
</Directory>
Alias /media /home/my_usr/web2/media
<Directory /home/my_usr/web2/media>
Require all granted
</Directory>
<Directory /home/my_usr/web2//web>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias /web2 /home/my_usr/web2/web2/wsgi.py
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
With this conf file, and right after I execute sudo service apache2 restart, if I go onto host/web1 web1 works perfeclty fine but if I want to access to host/web2 I get an error message and this error in the error.log file:
[Fri Jul 02 11:45:45.210666 2021] [mpm_prefork:notice] [pid 29254] AH00163: Apache/2.4.46 (Debian) mod_wsgi/4.6.5 Python/3.7 configured -- resuming normal operations
[Fri Jul 02 11:45:45.210780 2021] [core:notice] [pid 29254] AH00094: Command line: '/usr/sbin/apache2'
[Fri Jul 02 11:46:13.416556 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] mod_wsgi (pid=29256): Failed to exec Python script file '/home/my_usr/web2/web2/wsgi.py'.
[Fri Jul 02 11:46:13.416650 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] mod_wsgi (pid=29256): Exception occurred processing WSGI script '/home/my_usr/web2/web2/wsgi.py'.
[Fri Jul 02 11:46:13.417397 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] Traceback (most recent call last):
[Fri Jul 02 11:46:13.417464 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/web2/wsgi.py", line 24, in <module>
[Fri Jul 02 11:46:13.417517 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] application = get_wsgi_application()
[Fri Jul 02 11:46:13.417526 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/env/lib/python3.7/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Fri Jul 02 11:46:13.417530 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] django.setup(set_prefix=False)
[Fri Jul 02 11:46:13.417546 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/env/lib/python3.7/site-packages/django/__init__.py", line 19, in setup
[Fri Jul 02 11:46:13.417550 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Fri Jul 02 11:46:13.417555 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/env/lib/python3.7/site-packages/django/conf/__init__.py", line 82, in __getattr__
[Fri Jul 02 11:46:13.417559 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] self._setup(name)
[Fri Jul 02 11:46:13.417564 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/env/lib/python3.7/site-packages/django/conf/__init__.py", line 69, in _setup
[Fri Jul 02 11:46:13.417567 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] self._wrapped = Settings(settings_module)
[Fri Jul 02 11:46:13.417572 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/home/my_usr/web2/env/lib/python3.7/site-packages/django/conf/__init__.py", line 170, in __init__
[Fri Jul 02 11:46:13.417575 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] mod = importlib.import_module(self.SETTINGS_MODULE)
[Fri Jul 02 11:46:13.417580 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
[Fri Jul 02 11:46:13.417583 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] return _bootstrap._gcd_import(name[level:], package, level)
[Fri Jul 02 11:46:13.417588 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
[Fri Jul 02 11:46:13.417593 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 983, in _find_and_load
[Fri Jul 02 11:46:13.417598 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
[Fri Jul 02 11:46:13.417603 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[Fri Jul 02 11:46:13.417608 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
[Fri Jul 02 11:46:13.417612 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 983, in _find_and_load
[Fri Jul 02 11:46:13.417617 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
[Fri Jul 02 11:46:13.417634 2021] [wsgi:error] [pid 29256] [remote 192.168.20.32:25195] ModuleNotFoundError: No module named 'web1'
Whereas, if I execute sudo service apache2 restart and directly go onto host/web2 now web2 works perfectly fine but if after that I try to go onto host/web1 it shows web2.
So basically if I restart apache2 service then go to host/web1 and after that host/web2 I get a message error and the logs posted.
If I restart apache2 service then go to host/web2 and after that host/web1 I get web2 contents even though the url is host/web2
Any clue what could be going?
Thanks in advance
Okey I figured it out! Hope this helps someone in the future.
So my .conf file was all good from the headstart what was giving me trouble was the .settings files of my django projects.
Thanks to this article I fixed it.
I had my settings files like this:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
And changed it to:
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
we are trying to run django3 (python3.6 on Centos7) with httpd.
i get following error when i run it by HTTPD.
[Tue Jan 12 06:52:04.575618 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] Traceback (most recent call last):
[Tue Jan 12 06:52:04.575665 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "/srv/django/django/wsgi.py", line 16, in <module>
[Tue Jan 12 06:52:04.575670 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] application=get_wsgi_application()
[Tue Jan 12 06:52:04.575678 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "/usr/local/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Tue Jan 12 06:52:04.575682 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] django.setup(set_prefix=False)
[Tue Jan 12 06:52:04.575690 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 19, in setup
[Tue Jan 12 06:52:04.575694 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Tue Jan 12 06:52:04.575701 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 82, in __getattr__
[Tue Jan 12 06:52:04.575706 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] self._setup(name)
[Tue Jan 12 06:52:04.575713 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 69, in _setup
[Tue Jan 12 06:52:04.575717 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] self._wrapped = Settings(settings_module)
[Tue Jan 12 06:52:04.575724 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 170, in __init__
[Tue Jan 12 06:52:04.575729 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] mod = importlib.import_module(self.SETTINGS_MODULE)
[Tue Jan 12 06:52:04.575736 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module
[Tue Jan 12 06:52:04.575740 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] return _bootstrap._gcd_import(name[level:], package, level)
[Tue Jan 12 06:52:04.575747 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "<frozen importlib._bootstrap>", line 994, in _gcd_import
[Tue Jan 12 06:52:04.575755 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "<frozen importlib._bootstrap>", line 971, in _find_and_load
[Tue Jan 12 06:52:04.575762 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
[Tue Jan 12 06:52:04.575780 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] ModuleNotFoundError: No module named 'django.settings'```
my django.conf file in /etc/httpd/conf.d is as follows:
Alias /static /srv/static
<Directory /srv/static>
Require all granted
</Directory>
<Directory /srv/django/django>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess django python-path=/srv/django:/usr/local/lib/python3.6/site-packages
WSGIProcessGroup django
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /srv/django/django/wsgi.py
and my wsgi.py as as follows:
import os
import sys
from django.core.wsgi import get_wsgi_application
os.chdir('/srv/django/')
sys.path.append('/srv/django/')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django.settings')
application=get_wsgi_application()
we installed following packages
python3-devel.x86_64
rh-python36-mod_wsgi.x86_64
yum install httpd-devel
django project as such is working fine
100.10.100.10 (/srv/django/)-1003> python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
January 12, 2021 - 05:22:31
Django version 3.1.5, using settings 'django.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
what am i missing?
I am not too familiar on how well Apache Httpd works with WSGI, but if you're starting Django separately and you simply want to a reverse proxy up an running quickly you could do the following:
<VirtualHost *:80>
ServerAdmin admin#localhost
ProxyRequests off
DocumentRoot /var/www
# SSLProxyEngine on
# ProxyPreserveHost On
ServerName www.yoursite.net
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel error
<Location />
ProxyPass http://www.yourwebsite.net/
ProxyPassReverse http://localhost:8080/
Order allow,deny
Allow from all
</Location>
</VirtualHost>
In terms of the error, it looks like wsgi.py can't find django.settings meaning the django directory and settings module aren't in the current path.
I'm trying to get Django 2.2 to run on Apache 2.4 with Python 3.7 on a Windows 2016 Server. Django runs fine on the development server (manage.py runserver) and the Apache Server runs and is reachable on the network but Django doesn't run on Apache. This is the Apache config (httpd.conf) I'm running:
ServerName localhost:80
<Directory />
AllowOverride none
Require all denied
</Directory>
LoadFile "c:/program files/python37/python37.dll"
LoadModule wsgi_module "c:/program files/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "c:/program files/python37"
WSGIScriptAlias \ "D:\Django\myproject\myproject\wsgi.py"
WSGIPythonPath "D:\Django\myproject\myproject"
<Directory "D:\Django\myproject\myproject">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
DocumentRoot "${SRVROOT}/htdocs"
<Directory "${SRVROOT}/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
Here's my wsgi.py (not changed from what was generated during manage.py startproject):
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
application = get_wsgi_application()
When I try to load http://myserver in the browser, I get the Apache Defualt page, but If I try to load http://myserver:8000 I get a connection error.
Edit:
Here are the relevant entries of error.log:
[Fri Feb 08 11:30:15.853857 2019] [wsgi:info] [pid 864:tid 436] mod_wsgi (pid=864): Python home c:/program files/python37.
[Fri Feb 08 11:30:15.853857 2019] [wsgi:info] [pid 864:tid 436] mod_wsgi (pid=864): Initializing Python.
[Fri Feb 08 11:30:15.869484 2019] [wsgi:info] [pid 864:tid 436] mod_wsgi (pid=864): Attach interpreter ''.
[Fri Feb 08 11:30:15.885109 2019] [wsgi:info] [pid 864:tid 436] mod_wsgi (pid=864): Adding 'D:\\Django\\myproject\\myproject' to path.
[Fri Feb 08 11:30:15.885109 2019] [wsgi:info] [pid 864:tid 436] mod_wsgi (pid=864): Imported 'mod_wsgi'.
Looks like it loads fine, but it doesn't even try to listen on port 8000.
Edit2:
So my setting for WSGIScriptAlias seems to have been wrong. Now it's at least trying to load the WSGI app.
[Fri Feb 08 12:28:02.123312 2019] [wsgi:info] [pid 856:tid 1212] mod_wsgi (pid=856): Create interpreter 'localhost|/test'.
[Fri Feb 08 12:28:02.138940 2019] [wsgi:info] [pid 856:tid 1212] mod_wsgi (pid=856): Adding 'D:\\Django\\kte_test\\kte_test' to path.
[Fri Feb 08 12:28:02.138940 2019] [wsgi:info] [pid 856:tid 1212] [client 172.16.27.254:51858] mod_wsgi (pid=856, process='', application='localhost|/test'): Loading Python script file 'D:/Django/kte_test/kte_test/wsgi.py'.
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] mod_wsgi (pid=856): Failed to exec Python script file 'D:/Django/kte_test/kte_test/wsgi.py'.
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] mod_wsgi (pid=856): Exception occurred processing WSGI script 'D:/Django/kte_test/kte_test/wsgi.py'.
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] Traceback (most recent call last):\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] File "D:/Django/kte_test/kte_test/wsgi.py", line 16, in <module>\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] application = get_wsgi_application()\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] File "c:\\program files\\python37\\lib\\site-packages\\django\\core\\wsgi.py", line 12, in get_wsgi_application\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] django.setup(set_prefix=False)\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] File "c:\\program files\\python37\\lib\\site-packages\\django\\__init__.py", line 19, in setup\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] File "c:\\program files\\python37\\lib\\site-packages\\django\\conf\\__init__.py", line 57, in __getattr__\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] self._setup(name)\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] File "c:\\program files\\python37\\lib\\site-packages\\django\\conf\\__init__.py", line 44, in _setup\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] self._wrapped = Settings(settings_module)\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] File "c:\\program files\\python37\\lib\\site-packages\\django\\conf\\__init__.py", line 107, in __init__\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] mod = importlib.import_module(self.SETTINGS_MODULE)\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] File "c:\\program files\\python37\\lib\\importlib\\__init__.py", line 127, in import_module\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] return _bootstrap._gcd_import(name[level:], package, level)\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] File "<frozen importlib._bootstrap>", line 1006, in _gcd_import\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] File "<frozen importlib._bootstrap>", line 983, in _find_and_load\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] File "<frozen importlib._bootstrap>", line 1006, in _gcd_import\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] File "<frozen importlib._bootstrap>", line 983, in _find_and_load\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked\r
[Fri Feb 08 12:28:02.310819 2019] [wsgi:error] [pid 856:tid 1212] [client 172.16.27.254:51858] ModuleNotFoundError: No module named 'myproject'\r
Where do I find the missing module?
I'm running Apache 2.4.18 on Ubuntu 16.04. I've set up a virtual server with the following settings. The virtual host has been registered with a2ensite and appears to be being accessed ok.
<VirtualHost *:80>
ServerName www.factsfromfigures.com
WSGIScriptAlias / /home/user/mycode/mysite/mysite/wsgi.py
<Directory /home/user/mycode/mysite/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
When it runs I get the following error in the Apache log.
[Thu Mar 01 17:35:00.968878 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] mod_wsgi (pid=12278): Target WSGI script '/home/user/mycode/mysite/mysite/wsgi.py' cannot be loaded as Python module.
[Thu Mar 01 17:35:00.968895 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] mod_wsgi (pid=12278): Exception occurred processing WSGI script '/home/user/mycode/mysite/mysite/wsgi.py'.
[Thu Mar 01 17:35:00.968923 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] Traceback (most recent call last):
[Thu Mar 01 17:35:00.968933 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] File "/home/user/mycode/mysite/mysite/wsgi.py", line 18, in <module>
[Thu Mar 01 17:35:00.968964 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] application = get_wsgi_application()
[Thu Mar 01 17:35:00.968971 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] File "/usr/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Thu Mar 01 17:35:00.968988 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] django.setup()
[Thu Mar 01 17:35:00.968992 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] File "/usr/lib/python2.7/dist-packages/django/__init__.py", line 17, in setup
[Thu Mar 01 17:35:00.969007 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Thu Mar 01 17:35:00.969024 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] File "/usr/lib/python2.7/dist-packages/django/conf/__init__.py", line 48, in __getattr__
[Thu Mar 01 17:35:00.969057 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] self._setup(name)
[Thu Mar 01 17:35:00.969062 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] File "/usr/lib/python2.7/dist-packages/django/conf/__init__.py", line 44, in _setup
[Thu Mar 01 17:35:00.969075 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] self._wrapped = Settings(settings_module)
[Thu Mar 01 17:35:00.969079 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] File "/usr/lib/python2.7/dist-packages/django/conf/__init__.py", line 92, in __init__
[Thu Mar 01 17:35:00.969084 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] mod = importlib.import_module(self.SETTINGS_MODULE)
[Thu Mar 01 17:35:00.969087 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Thu Mar 01 17:35:00.969103 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] __import__(name)
[Thu Mar 01 17:35:00.969113 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] ImportError: No module named mysite.settings
The mysite settings.py file is at /home/user/mycode/mysite/mysite/settings.py
The wsgi.py file is at /home/user/mycode/mysite/mysite/wsgi.py
I'm not sure what version of python the server is accessing as I get these warning messages in the log: (I've no idea why it reports 2 different version)
[Thu Mar 01 18:32:03.219991 2018] [wsgi:warn] [pid 13753] mod_wsgi: Compiled for Python/2.7.11.
[Thu Mar 01 18:32:03.220008 2018] [wsgi:warn] [pid 13753] mod_wsgi: Runtime using Python/2.7.12.
My history on this is that I've taken a very good Python and Django course. I have 40 years programming experience so don't find coding a problem. I'm relatively new to Linux/Ubuntu and have spent over a week trying to get as little as a 'hello world' appearing on my browser. The documentation is not clear. For example, the official Django documentation recommends putting various settings in httpd.conf but there isn't one. I've tried just about every suggestion on SO into practice but nothing seems to work.
Now I guess this might be bad etiquette but I'm getting desperate and on the verge of giving up on Django. I would really like someone who knows their way around to help me out. I can reward with a suitable number of Amazon vouchers or goodies. I have RealVNC installed so you would be able to log in and have a look as there is nothing sensitive on the server yet. The truth of the matter is that I'm a developer and find the system admin/configuration side of things to be difficult. Thanks for your attention and hopefully you'll help me out.
The path is incomplete at <Directory /home/user/mycode/mysite/>. It should be <Directory /home/user/mycode/mysite/mysite> (note that mysite should occur twice).
Anyway, from an old project, here's what my apache config file looks like:
WSGIScriptAlias / /home/user/mycode/mysite/mysite/wsgi.py
WSGIPythonPath /home/user/mycode/mysite/mysite
<Directory /home/user/mycode/mysite/mysite>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
Im a bit stuck. I can't get this configuration to work and I don't know why. The code I site below is from https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04. The reason I get stuck on it is because he named his project directory and his project the same thing. If I am right, then where he is locating files like the settings.py and wsgi.py should be /home/user/myproject/myproject/myproject but I'm not sure anymore because I can't even get it right myself. Earlier in the document he cd's into the directory he created, which would put him in /home/user/myproject. He then proceeds to create a virtual environment, enter it, and run django-admin startproject myproject. So, if all this holds true, at least what I see on my own server tells me that when you start a django project it actually creates two folders with the same name, nested. Am I wrong? Can someone help me straighten the below code out to make more sense?
. . .
Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>
Require all granted
</Directory>
<Directory /home/user/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/home/user/myproject:/home/user/myproject/myprojectenv/lib/python2.7/site-packages
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/myproject/myproject/wsgi.py
</VirtualHost>
This is what I consistently see in my apache log:
[Sun Oct 09 11:48:15.875313 2016] [wsgi:warn] [pid 47964] mod_wsgi: Compiled for Python/3.5.1+.
[Sun Oct 09 11:48:15.875353 2016] [wsgi:warn] [pid 47964] mod_wsgi: Runtime using Python/3.5.2.
[Sun Oct 09 11:48:15.877537 2016] [mpm_prefork:notice] [pid 47964] AH00163: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Sun Oct 09 11:48:15.877568 2016] [core:notice] [pid 47964] AH00094: Command line: '/usr/sbin/apache2'
[Sun Oct 09 11:48:18.767800 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] mod_wsgi (pid=47967): Target WSGI script '/home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py' cannot be loaded as Python module.
[Sun Oct 09 11:48:18.767851 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] mod_wsgi (pid=47967): Exception occurred processing WSGI script '/home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py'.
[Sun Oct 09 11:48:18.768339 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] Traceback (most recent call last):
[Sun Oct 09 11:48:18.768385 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "/home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py", line 16, in <module>
[Sun Oct 09 11:48:18.768389 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] application = get_wsgi_application()
[Sun Oct 09 11:48:18.768395 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "/usr/lib/python3/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Sun Oct 09 11:48:18.768398 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] django.setup()
[Sun Oct 09 11:48:18.768405 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "/usr/lib/python3/dist-packages/django/__init__.py", line 17, in setup
[Sun Oct 09 11:48:18.768408 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Sun Oct 09 11:48:18.768413 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "/usr/lib/python3/dist-packages/django/conf/__init__.py", line 48, in __getattr__
[Sun Oct 09 11:48:18.768423 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] self._setup(name)
[Sun Oct 09 11:48:18.768430 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "/usr/lib/python3/dist-packages/django/conf/__init__.py", line 44, in _setup
[Sun Oct 09 11:48:18.768433 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] self._wrapped = Settings(settings_module)
[Sun Oct 09 11:48:18.768438 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "/usr/lib/python3/dist-packages/django/conf/__init__.py", line 92, in __init__
[Sun Oct 09 11:48:18.768441 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] mod = importlib.import_module(self.SETTINGS_MODULE)
[Sun Oct 09 11:48:18.768446 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Sun Oct 09 11:48:18.768449 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] return _bootstrap._gcd_import(name[level:], package, level)
[Sun Oct 09 11:48:18.768454 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Sun Oct 09 11:48:18.768460 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Sun Oct 09 11:48:18.768466 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Sun Oct 09 11:48:18.768471 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Sun Oct 09 11:48:18.768477 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Sun Oct 09 11:48:18.768483 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Sun Oct 09 11:48:18.768488 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
[Sun Oct 09 11:48:18.768505 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] ImportError: No module named 'servicesite'
in your /etc/apache2/apache2.conf file add
<Directory /home/dimitris/mysite>
AllowOverride All
Require all granted
</Directory>
your site.conf file in the example i load mysite with virtualenv
WSGIDaemonProcess myp user=dimitris group=dimitris threads=5 python-path=/home/dimitris/myenv/lib/python2.7/site-packages
<VirtualHost *:80>
#ServerName mysite
#ServerAlias mysite
ServerAdmin webmaster#localhost
WSGIProcessGroup myp
WSGIScriptAlias / /home/dimitris/mysite/mysite/wsgi.py
Alias /favicon.ico /home/dimitris/mysite/favicon.ico
Alias /media/ /home/dimitris/mysite/media/
Alias /static/ /home/dimitris/mysite/project-static/
DocumentRoot /home/dimitris/mysite
<Directory /home/dimitris/mysite/>
Order allow,deny
allow from all
</Directory>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
ErrorLog /var/log/mysite-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/mysite-access.log combined
</VirtualHost>
in django wsgi.py file add
import os
DJANGO_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
sys.path.append(DJANGO_PATH)
in the above code replace dimitris with your username
Either you create your project at /home/user or change the apache conf file by appending an extra /myproject wherever you are specifying the path as below:
Alias /static /home/user/myproject/myproject/static
<Directory /home/user/myproject/myproject/static>
Require all granted
</Directory>
<Directory /home/user/myproject/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/home/user/myproject/myproject:/home/user/myproject/myproject/myprojectenv/lib/python2.7/site-packages
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/myproject/myproject/myproject/wsgi.py
It was confusing because you created your project inside a myproject folder(same as your project name), rather than creating it somewhere else.