WSGIScriptAlias overrides Alias - python

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

Related

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.

Serving multiple Django projects in different virtualenv at the same IP (Apache)

I'm trying to serve two Django projects on different virtualenv at the same IP adress on Apache.
My first site is at http://myip/site-1
and the seccond: http://myip/site-2
When I run http://myip/site-1, Apache serves it without issues, but when I run the second (http://myip/site-2) it raises the following:
The requested URL /site-2/ was not found on this server.
Because it searches in the first site's document root.
Here is my apache.conf
<VirtualHost *:80>
ServerName site-1.example.com
DocumentRoot /home/venv/site-1
# adjust the following line to match your Python path
WSGIDaemonProcess site-1.example.com processes=2 threads=15 display-name=%{GROUP} python-home=/home/venv/lib/python2.7
WSGIProcessGroup site-1.example.com
WSGIScriptAlias / /home/venv/site-1/site-1/wsgi.py
<directory /home/venv/site-1>
AllowOverride all
Require all granted
Options FollowSymlinks
</directory>
Alias /static/ /home/venv/site-1/static_root/
<Directory /home/venv/site-1/static_root>
AllowOverride all
Require all granted
Options FollowSymlinks
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName site-2.example.com
DocumentRoot /home/venv_mob/site-2
# adjust the following line to match your Python path
WSGIDaemonProcess site-2.example.com processes=2 threads=15 display-name=%{GROUP} python-home=/home/venv_mob/lib/python2.7
WSGIProcessGroup site-2.example.com
WSGIScriptAlias / /home/venv_mob/site-2/site-2/wsgi.py
<directory /home/venv_mob/site-2>
AllowOverride all
Require all granted
Options FollowSymlinks
</directory>
Alias /static/ /home/venv_mob/site-2/static_root/
<Directory /home/venv_mob/site-2/static_root>
AllowOverride all
Require all granted
Options FollowSymlinks
</Directory>
</VirtualHost>
I have tried many solutions that I found on the web but the problem remains the same.
Any ideas ?
You have to connect to http://site-1.example.com (using terms from your example), not http://myip/site-1. And http://site-2.example.com, not http://myip/site-2.
Generally speaking you can have name-based virtual hosts for which you need to configure DNS (mapping site-1.example.com and site-2.example.com to your IP address) or you can run different sites on different ports.

Troubles with running django app via wsgi on ubuntu 17

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.

Flask App Getting AH01630: client denied by server configuration error in Apache

Getting 403 Forbidden when attempting to send requests to my Flask app.
Finding a lot of examples of this error online but none of the solutions work for me so far.
Error from the apache error log file:
AH01630: client denied by server configuration: /opt/MyTinyURL/webtool.wsgi
Here is my VirtualHost.
<VirtualHost *:80>
ServerName MY_SERVER_NAME
DocumentRoot /opt/MyTinyURL
WSGIDaemonProcess webtool user=www-data group=www-data threads=5 home=/opt/MyTinyURL/
WSGIScriptAlias / "/opt/MyTinyURL/webtool.wsgi"
<Directory "/opt/MyTinyUrl">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride None
Require all granted
WSGIProcessGroup webtool
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
</Directory>
</VirtualHost>
UNIX file path names are case sensitive. The path used with WSGIScriptAlias doesn't match that used in the Directory directive.
Cleaning up a few other issues with things that aren't needed, try using:
<VirtualHost *:80>
ServerName MY_SERVER_NAME
# XXX Bad practice to set DocumentRoot to be directory
# where your code is. Comment out WSGIScriptAlias and
# people could download your source code.
# DocumentRoot /opt/MyTinyURL
# XXX Don't need user/group as the default to Apache user.
WSGIDaemonProcess webtool threads=5 home=/opt/MyTinyURL/
WSGIScriptAlias / "/opt/MyTinyURL/webtool.wsgi"
# XXX You had MyTinyUrl and not MyTinyURL.
<Directory "/opt/MyTinyURL">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride None
Require all granted
WSGIProcessGroup webtool
WSGIApplicationGroup %{GLOBAL}
# XXX Script reloading is the default option.
# WSGIScriptReloading On
</Directory>
</VirtualHost>

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?

Categories

Resources