Troubles with running django app via wsgi on ubuntu 17 - python

I need your help.
I have an error when trying to run on wsgi my django project.
I'm using Ubuntu 17, Apache2, Django 2.0, Python 3.6
When I running from manage.py, everything is working, but when via wsgi got the next error :
AH01276: Cannot serve directory /var/cardsite/cardsite/: No matching
DirectoryIndex
(index.html,index.cgi,index.pl,index.php,index.xhtml,index.htm) found,
and server-generated directory index forbidden by Options directive
And don't know why, because I guess set everything correct. Bellow my configuration :
apache.conf
<VirtualHost *:80>
CustomLog /var/log/apache2/cardsite-access.log common
ErrorLog /var/log/apache2/cardsite-error.log
DocumentRoot /var/cardsite/cardsite/
Alias /static /var/cardsite/cardsite/static/
<Directory /var/cardsite/cardsite/static>
Require all granted
</Directory>
<Directory /var/cardsite/cardsite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
WSGIDaemonProcess cardsite python-path=/var/cardsite/ python-home=/var/venv_python36/
WSGIProcessGroup cardsite
WSGIScriptAlias / /var/cardsite/cardsite/wsgi.py
</VirtualHost>
wsgi.py
import os
import sys
PROJECT_DIR = '/var/cardsite'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cardsite.settings")
def execfile(filename):
globals = dict( __file__ = filename )
exec( open(filename).read(), globals )
activate_this = os.path.join( '/var/venv_python36/bin', 'activate_this.py' )
execfile( activate_this )
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
P.S. Permissions I have give on folder and all which inside.
P.S.S. Packages like "libapache2-mod-wsgi-py3" or "mod-wsgi" via pip3 also installed.
Thank you all for the any suggestion what's it can be

Don't set DocumentRoot to be a parent directory of your source code. If you were to take the WSGIScriptAlias out, people could download your source code. You should avoid the risk of that, even if WSGIScriptAlias currently intercepts everything under /. That DocumentRoot directory doesn't allow access may be part of the problem also.
Try:
<VirtualHost *:80>
CustomLog /var/log/apache2/cardsite-access.log common
ErrorLog /var/log/apache2/cardsite-error.log
DocumentRoot /var/cardsite/htdocs
<Directory /var/cardsite/htdocs>
Require all granted
</Directory>
Alias /static /var/cardsite/cardsite/static
<Directory /var/cardsite/cardsite/static>
Require all granted
</Directory>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
WSGIDaemonProcess cardsite python-path=/var/cardsite python-home=/var/venv_python36
WSGIProcessGroup cardsite
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /var/cardsite/cardsite/wsgi.py
<Directory /var/cardsite/cardsite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
The WSGIApplicationGroup has been added as always a good idea to have that, with only one WSGI application delegated to the daemon process group.
Make sure you create the directory:
/var/cardsite/htdocs
Finally, you are missing a ServerName directive. So if this isn't the default VirtualHost, your request may not even be getting handled by this VirtualHost.

Related

Multiple wsgi.mod on single apache

I have one django3 application on apache http://myapp.example.com
For now it works, but I want to put second django3 application on the same server with Anaconda.
Is it possible? if so , How should I set WSGIPythonPath and Alias /static/ for each application??
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
ServerName myapp.example.com
DocumentRoot "/var/www/html/myapp/current/"
ErrorLog ${APACHE_LOG_DIR}/myapp_error.log
CustomLog ${APACHE_LOG_DIR}/myapp_access.log combined
<Directory /var/www/html/myapp/current/>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /var/www/html/myapp/current/myapp/wsgi.py
WSGIDaemonProcess py37 user=ubuntu group=ubuntu python-path=/home/ubuntu/anaconda3/envs/py37/lib/python3.7/site-packages
</VirtualHost>
LoadModule wsgi_module /home/ubuntu/anaconda3/envs/py37/lib/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so
WSGIPythonHome /home/ubuntu/anaconda3/envs/py37/
WSGIPythonPath /var/www/html/myapp/current/
<Directory /var/www/html/myapp/current>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
Alias /static/ /var/www/html/myapp/current/static/
Alias /media/ /var/www/html/myapp/current/media/
<Directory /var/www/html/myapp/current/static>
Order allow,deny
Allow from all
</Directory>
simply clone the virtual host and replace the paths relevant for the new application, you may need to move the directory alias:
Alias /static/ /var/www/html/myapp/current/static/
Alias /media/ /var/www/html/myapp/current/media/
<Directory /var/www/html/myapp/current/static>
Order allow,deny
Allow from all
</Directory>
into the virtual host. I do not use the wsgi.py but a site.wsgi. Which runs the app.
Its content is:
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('{{PATH TO}/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('{PATH TO PROJECT}')
os.environ['DJANGO_SETTINGS_MODULE'] = '{PROJECT}.settings'
# Activate your virtual env
activate_env=os.path.expanduser("{PATH TO}/bin/activate_venv.py")
exec(open(activate_env).read(), dict(__file__=activate_env))
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
What works on my stage is(httpd.conf):
LoadModule wsgi_module {PATH TO}/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
WSGISocketPrefix /var/run/wsgi
and the pyhton path/script is set in the virtual host instead in the main file:
WSGIDaemonProcess {GROUP} threads=1 python-home={PATH TO} python-path={PATH TO APP} user={USER}
WSGIProcessGroup {GROUP}
WSGIScriptAlias / {PATH TO FILE}/site.wsgi
you may need to comment the line that starts the application in your wsgi.py if you want to use the settings above

how to use mod_wsgi for hosting multiple django projects under single domain?

I have multiple django projects and i want to host them under same domain
eg: example.com/one<br> example.com/two
I have searched for various solutions and found the below given link which helped me alot.
Is it possible to host multiple django projects under the same domain?
From the above reading , I get to know that I need mod_wsgi for this but I am confused that where to install this mod_wsgi - Do i need to install under every project folder (seperate for every myenv) or it should be installed only once .
Please help me in how and where to install this mod_wsgi and finally how to host multiple projects under same domain name.
Some Code Tried By Another User With Same Problem But Also Not Working
<VirtualHost *:80>
ServerAdmin admin#my_domain.com
ServerName my_domain.com
ServerAlias www.my_domain.com
DocumentRoot /var/www/my_domain.com
ErrorLog ${APACHE_LOG_DIR}/my_domain.com_error.log
CustomLog ${APACHE_LOG_DIR}/my_domain.com_access.log combined
# site_1
Alias /site_1_project/static /var/www/my_domain.com/site_1_project/static
<Directory /var/www/my_domain.com/site_1_project/static>
Require all granted
</Directory>
Alias /site_1_project/media /var/www/my_domain.com/site_1_project/media
<Directory /var/www/my_domain.com/site_1_project/media>
Require all granted
</Directory>
<Directory /var/www/my_domain.com/site_1_project/site_1_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess site_1_project python-path=/var/www/my_domain.com/site_1_project python-home=/var/www/my_domain.com/site_1_project/django_env_site_1
WSGIProcessGroup site_1_project
WSGIScriptAlias /site_1_project/ /var/www/my_domain.com/site_1_project/site_1_project/wsgi.py
# site_2
Alias /site_2_project/static /var/www/my_domain.com/site_2_project/static
<Directory /var/www/my_domain.com/site_2_project/static>
Require all granted
</Directory>
Alias /site_2_project/media /var/www/my_domain.com/site_2_project/media
<Directory /var/www/my_domain.com/site_2_project/media>
Require all granted
</Directory>
<Directory /var/www/my_domain.com/site_2_project/site_2_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess site_2_project python-path=/var/www/my_domain.com/site_2_project python-home=/var/www/my_domain.com/site_2_project/django_env_site_2
WSGIProcessGroup site_2_project
WSGIScriptAlias /site_2_project/ /var/www/my_domain.com/site_2_project/site_2_project/wsgi.py
#RewriteEngine on
#RewriteCond %{SERVER_NAME} =my_domain.com [OR]
#RewriteCond %{SERVER_NAME} =www.my_domain.com
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Installing mod_wsgi depends on what your host OS is. Check the instructions. If you're using CentOS or RedHat, I'd recommend looking at IUS Community; they provide a repository with yum installable packages for Python 3.6 and mod_wsgi. The version of mod_wsgi you install has to be compiled against the same version of Python you are running into your virtual environment.
Then you need to configure your VirtualHost properly. If you have a host at the root, it has to come last in your definition. Here's an example:
<VirtualHost *:443>
TimeOut 300
SSLEngine On
ServerName mysite.example.com
# Set to the lobal Application Group
WSGIApplicationGroup %{GLOBAL}
# Pass Authorizations through to the WSGI app for Django REST Framework Token Auth
WSGIPassAuthorization On
WSGIDaemonProcess subsite-develop-https python-home=/web/subsite-develop/venv request-timeout=300 user=apache group=apache
WSGIProcessGroup subsite-develop-https
WSGIScriptAlias /subsite /web/subsite-develop/config/wsgi.py process-group=subsite-develop-https
<Directory /web/subsite-develop/config>
Require all granted
</Directory>
Alias /subsite/static/ /web/subsite-develop/static/
<Directory /web/subsite-develop/static>
Header always set Access-Control-Allow-Origin "*"
Require all granted
</Directory>
WSGIDaemonProcess django-mysite-develop-https python-home=/web/django-mysite-develop/venv request-timeout=300 user=apache group=apache
WSGIProcessGroup django-mysite-develop-https
WSGIScriptAlias / /web/django-mysite-develop/config/wsgi.py process-group=django-mysite-develop-https
<Directory /web/django-mysite-develop/config>
Require all granted
</Directory>
Alias /static/ /web/django-mysite-develop/static/
<Directory /web/django-mysite-develop/static>
Header always set Access-Control-Allow-Origin "*"
Require all granted
</Directory>
Alias /media/ /var/media/mysite-www/
<Directory /var/media/mysite-www>
Require all granted
</Directory>
</VirtualHost>
This example will host one site at /subsite/ and another at the root, /. Notice that the root site comes last. It also means that you won't be able to use the route /subsite/ within the root project, since Apache will have diverted it to via the WSGIScriptAlias definition.
This is also for a site with TLS; you may have to switch the 443 to 80, and remove SSLEngine On if you're not using TLS. The WSGIPassAuthorization is for Django REST Framework tokens, you can probably remove it as well, but I've left it for a more complete example. This is for Apache 2.4+, when they switched to the Require all granted syntax.
IUS Community, if on RedHat/CentOS: https://ius.io/
I'll tell you how we did in our project. We have a single Django project with different routes. For example /players, /tablet. We hosted our project in two Docker containers. We have NGINX as our reverse proxy. NGINX redirects the request to the appropriate container based on the route. NGINX is exposed to the world. But, I'm not sure if it is useful for you or not.

EC2: Deploying a django project with a apache server

I have set up an ec2 instance and I'm able to connect to it via ssh. I've installed apache, django, git and all the necessary dependencies.
I've managed to clone a git repository which is located in /home/ubuntu/.
I've also set up the configuration file in /etc/apache2/sites-enabled/hyper-blog.conf.
This is my hyper-blog.conf file:
<VirtualHost *:80>
ServerName ec2-52-11-240-28.us-west-2.compute.amazonaws.com
ServerAlias ec2-52-11-240-28.us-west-2.compute.amazonaws.com
ServerAdmin webmaster#localhost
DocumentRoot /home/ubuntu/hyper-blog/
AddDefaultCharset UTF-8
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
alias /static/ /home/ubuntu/hyper-blog/static/
alias /media/ /home/ubuntu/hyper-blog/media/
<Directory "/home/ubuntu/hyper-blog/static">
Require all granted
Options -Indexes
</Directory>
<Directory "/home/ubuntu/hyper-blog/media">
Require all granted
Options -Indexes
</Directory>
<Directory /home/ubuntu/hyper-blog/hyper-blog>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
#RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.hyperiondev.com%{REQUEST_URI}
WSGIDaemonProcess hyper-blog python-path=/home/ubuntu/hyper-blog:/usr/local/lib/python3.4/dist-packages
WSGIProcessGroup hyper-blog
WSGIScriptAlias / /home/ubuntu/hyper-blog/hyper-blog/wsgi.py
The problem is when I go to my public domain: ec2-52-11-240-28.us-west-2.compute.amazonaws.com it takes a while to load and eventually does not render anything.
I need to know how to get my application up and running. I've tried this documentation on digital ocean https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04,
but still doesn't work.
How can I get my application working without installing virtualenv?

Unstable loading time when using mod_wsgi on Windows

My system has following version of software installed:
httpd 2.4.16 win64
Python 3.4.3 amd64
mod_wsgi 4.4.13 ap24vc10 cp34 none win amd64
I have created two environment using virtualenv for two Django projects(just default It worked! page) one hosted on one.local.com(VirtualHost) and other on two.local.com(VirtualHost). Below code is the VirtualHost configuration for Apache's httpd-vhost.conf file.
<VirtualHost *:80>
WSGIApplicationGroup %{ENV:ONE_GROUP}
ServerName one.local.com
ServerAdmin admin#example.com
ErrorLog "D:/_pythonDev/Projects/logs/one.local.com-error.log"
CustomLog "D:/_pythonDev/Projects/logs/one.local.com-access.log" common
WSGIScriptAlias / "D:/_pythonDev/Projects/Project1/Project1/wsgi.py" application-group=%{ENV:ONE_GROUP}
# I also tried WSGIImportScript
<Directory "D:/_pythonDev/Projects/Project1/Project1">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /favicon.ico "D:/_pythonDev/Projects/Project1/static/favicon.ico"
Alias /static/ "D:/_pythonDev/Projects/Project1/static/"
<Directory "D:/_pythonDev/Projects/Project1/static">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
WSGIApplicationGroup %{ENV:TWO_GROUP}
ServerName two.local.com
ServerAdmin admin#example.com
ErrorLog "D:/_pythonDev/Projects/logs/two.local.com-error.log"
CustomLog "D:/_pythonDev/Projects/logs/two.local.com-access.log" common
WSGIScriptAlias / "D:/_pythonDev/Projects/Project2/Project2/wsgi.py" application-group=%{ENV:TWO_GROUP}
<Directory "D:/_pythonDev/Projects/Project2/Project2">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /favicon.ico "D:/_pythonDev/Projects/Project2/static/favicon.ico"
Alias /static/ "D:/_pythonDev/Projects/Project2/static/"
<Directory "D:/_pythonDev/Projects/Project2/static">
Require all granted
</Directory>
</VirtualHost>
Following is the wsgi.py for one.local.com
import os
import sys
import site
site.addsitedir("D:/_pythonDev/env/env1/Lib/site-packages")
sys.path.append("D:/_pythonDev/Projects/Project1/Project1")
sys.path.append("D:/_pythonDev/Projects/Project1")
activate_env_file = "D:/_pythonDev/env/env1/Scripts/activate_this.py"
exec(open(activate_env_file).read(), dict(__file__=activate_env_file))
from django.core.wsgi import get_wsgi_application
os.environ["DJANGO_SETTINGS_MODULE"] = "Project1.settings"
application = get_wsgi_application()
Problem:
Loading time is unstable it takes about 10s.
Answer required:
Reason of unstable loading time.
Answer to solve it.

WSGIScriptAlias overrides Alias

I'm having an issue configuring mod_wsgi with Apache 2.2. I need mod_wsgi to handle everything under root directory of a virtual server except certain paths. For those paths I want ordinary Apache directory listing to work. The following virtual server config half works. mod_wsgi does not kick in for paths directly to files but does for directories. I can't figure this out. Looked at docs here - https://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide. It shows using aliases in the way I have to disable mod_wsgi but does not mention this problem. (Note not that it matters but the foo.wsgi end point is just the hello world example).
<VirtualHost *:80>
ServerAdmin webmaster#test.wsgi.sethandler.localdomain
ServerName test.wsgi.sethandler.localdomain
DocumentRoot /var/www/test.wsgi.sethandler.localdomain
<Directory /var/www/test.wsgi.sethandler.localdomain>
Options Indexes FollowSymLinks MultiViews
AllowOverride FileInfo Options Indexes
Order allow,deny
allow from all
</Directory>
Alias /static /var/www/test.wsgi.sethandler.localdomain/static
WSGIScriptAlias / /var/www/test.wsgi.sethandler.localdomain/cgi-bin/foo.wsgi
<Directory /var/www/test.wsgi.sethandler.localdomain/cgi-bin>
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
I've tried disabling any handler with the following but it does not work:
<Location "/static">
SetHandler None
</Location>
<LocationMatch ^/static>
SetHandler None
</LocationMatch>
Have you tried putting and end slash after static?
Alias /static/ /var/www/test.wsgi.sethandler.localdomain/static/
<Directory /var/www/test.wsgi.sethandler.localdomain/static/>
Require all granted
</Directory>
WSGIScriptAlias / /var/www/test.wsgi.sethandler.localdomain/cgi-bin/foo.wsgi

Categories

Resources