IP based host not working on apache2 - flask

I am trying to run my Python Flask backend on apache2. My conf file looks like this:
<virtualhost 192.168.11.11:80>
ServerName travelhook
WSGIDaemonProcess travelhook group=www-data threads=5
WSGIScriptAlias / /var/www/TravelHookBE/travelhookBE.wsgi
<directory /var/www/TravelHookBE>
WSGIProcessGroup travelhook
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Order deny,allow
Allow from all
</directory>
</virtualhost>
But when i use 192.168.11.11:80 in my browser it doesn't work. I am running it on Ubuntu from the browser on the same machine

Related

Serving multiple domains with Django and Apache maps to one location only

I am attempting to setup Django to serve multiple sites (in the below configuration I am using examples www.domain1.com and www.domain2.com). I have installed Apache2 successfully, configured wsgi.py successfully (or so it seems).
I have built my appache configuration file to attempt the following mapping:
www.example1.com to serve from /var/www/mysite1
www.example2.com to serve from /var/www/mysite2
DNS A records for both example1.com and example2.com point to the same IP address.
The trouble with my setup as it exists is that both domain1.com and domain2.com map to the Django residing at /var/www/mysite1, despite the fact that I have (in theory) set them to map to their respective locations.
My apache config files are as follows, and both of the files (mysite1.conf and mysite2.conf) have had symlinks made for them using a2ensite:
#/etc/apache2/sites-available/mysite1.conf
WSGIDaemonProcess mysite processes=2 threads=25 python-home=/var/www/mysite1/myenv1 python-path=/var/www/mysite1/myenv1/mys
ite1
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/mysite1/myenv1/mysite1/mysite1/wsgi.py
<VirtualHost *:80>
ServerName domain1.com
ServerAlias xx.xx.xx.xx
DocumentRoot /var/www/mysite1
ErrorLog ${APACHE_LOG_DIR}/mysite1-error.log
CustomLog ${APACHE_LOG_DIR}/mysite1-access.log combined
Alias /robots.txt /var/www/mysite1/myenv1/mysite1/static/robots.txt
Alias /favicon.ico /var/www/mysite1/myenv1/mysite1/static/favicon.ico
Alias /static/ /var/www/mysite1/myenv1/mysite1/static/
<Directory /var/www/mysite1/myenv1/mysite1/mysite1>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /var/www/mysite1/myenv1/mysite1/static>
Require all granted
</Directory>
</VirtualHost>
#/etc/apache2/sites-available/mysite2.conf
WSGIDaemonProcess mysite2 processes=2 threads=25 python-home=/var/www/mysite2/myenv2 python-path=/var/www/mysite2/myenv
2/mysite2
WSGIProcessGroup mysite2
WSGIScriptAlias / /var/www/mysite2/myenv2/mysite2/mysite2/wsgi.py process-group=mysite2
WSGISocketPrefix /var/run/wsgi
<VirtualHost *:80>
ServerName domain2.com
ErrorLog ${APACHE_LOG_DIR}/mysite2-error.log
CustomLog ${APACHE_LOG_DIR}/mysite2-access.log combined
DocumentRoot /var/www/mysite2
Alias /robots.txt /var/www/mysite2/myenv2/mysite2/static/robots.txt
Alias /favicon.ico /var/www/mysite2/myenv2/mysite2/static/favicon.ico
Alias /static/ /var/www/mysite2/myenv2/mysite2/static/
<Directory /var/www/mysite2/myenv2/mysite2/mysite2>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /var/www/mysite2/myenv2/mysite2/static>
Require all granted
</Directory>
</VirtualHost>
An example wsgi.py file looks as below, and is working correctly (ie loading from the correct location) when it does load:
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite1.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite1.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
There are no errors being reported in either logs (/var/syslog/apache2/mysiteX.conf where x=1 or 2)
What am I doing wrong? How can I correct this so that domain2 starts to map to /var/www/mysite2?
Many thanks in advance!
The WSGI tags need to be inside the VirtualHost block for their respective domain names.
E.g.
<VirtualHost *:80>
ServerName domain1.com
ServerAlias xx.xx.xx.xx
DocumentRoot /var/www/mysite1
WSGIDaemonProcess mysite processes=2 threads=25 python-home=/var/www/mysite1/myenv1 python-path=/var/www/mysite1/myenv1/mys
ite1
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/mysite1/myenv1/mysite1/mysite1/wsgi.py
...
</VirtualHost>

Apache Error: While writing python logs into file outside the project directory

I am working on a Django project and using Apache as web server. Everything is working fine with
python manage.py runserver
But while running the application through apache I am not able to write my python app logs into the stated path which is outside the project directory.
Project directory /home/ubuntu/saas-DocumentProcessing
Log files are in
/home/ubuntu/log/SaasAap/SaasAap.log and /home/ubuntu/log/error/error.log
My 000-default.conf content
<VirtualHost *:8000>
ServerAdmin abc#xyz.com
ServerName my_ip
ServerAlias my_ip
DocumentRoot /home/ubuntu/saas-DocumentProcessing/
WSGIScriptAlias / /home/ubuntu/saas-DocumentProcessing/src/wsgi.py
Alias /static/ /home/ubuntu/saas-DocumentProcessing/static/
ErrorLog /home/ubuntu/log/error.log
CustomLog /home/ubuntu/log/custom.log combined
<Location "/static/">
Options -Indexes
AllowOverride All
Require all granted
</Location>
<Location "/">
AllowOverride All
Require all granted
</Location>
<Directory /home/ubuntu/saas-DocumentProcessing/static>
Order allow,deny
Allow from all
</Directory>
<Directory /home/ubuntu/log>
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess saas-DocumentProcessing python-path=/home/ubuntu/
saas-DocumentProcessing python-home=/home/ubuntu/saas-DocumentProcessing/ve
nv
WSGIProcessGroup saas-DocumentProcessing
</VirtualHost>

Serving Multiple WSGI Applications As Different Virtual Hosts on Apache

I have an EC2 AWS server on which I would like to host a couple of Django applications. Each of these apps has its own URL. For instance,
example1.com
example2.com
By itself, example1.com works. The problem is getting example2.com to work with it at the same time.
When I visit example2.com, I get an error:
DisallowedHost at /
Invalid HTTP_HOST header: 'example2.com'. You may need to add 'example2.com' to ALLOWED_HOSTS.
Request Method: GET
Request URL: http://example2.com
Django Version: 1.9.13
Exception Type: DisallowedHost
Exception Value:
Invalid HTTP_HOST header: 'example2.com'. You may need to add 'example2.com' to ALLOWED_HOSTS.
Exception Location: /var/www/vhosts/example1/example1-env/lib/python3.5/site-packages/django/http/request.py in get_host, line 109
Python Executable: /usr/bin/python3
Python Version: 3.5.1
Python Path:
['/usr/lib64/python3.5',
'/usr/lib64/python3.5/plat-linux',
'/usr/lib64/python3.5/lib-dynload',
'/usr/local/lib64/python3.5/site-packages',
'/usr/local/lib/python3.5/site-packages',
'/usr/lib64/python3.5/dist-packages',
'/usr/lib/python3.5/dist-packages',
'/var/www/vhosts/example1/',
'/var/www/vhosts/example1/example1-env/lib/python3.5/site-packages']
Server time: Wed, 14 Jun 2017 20:31:27 +0000
As you can see, somehow Apache is trying to use the virtual environment of example1.com when it serves example2.com. How could I correct that? Each one should be served with its own virtualenv.
Here is the Apache configuration file:
<VirtualHost *:80>
# This is name based virtual hosting. So place an appropriate server name
# here. Example: django.devsrv.local
ServerName example1.com
WSGIDaemonProcess example1 python-home=/var/www/vhosts/example1/example1-env
WSGIProcessGroup %{GLOBAL}
# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/example1/example1/wsgi.py
<Directory /var/www/vhosts/example1/>
Require all granted
</Directory>
Alias /static/ /var/www/vhosts/example1/static/
<Directory /var/www/vhosts/example1/static/>
Order deny,allow
Allow from all
</Directory>
Alias /media/ /var/www/vhosts/example1/media/
<Directory /var/www/vhosts/example1/media/>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
# This is name based virtual hosting. So place an appropriate server name
# here. Example: django.devsrv.local
ServerName example2.com
WSGIDaemonProcess example2 python-home=/var/www/vhosts/example2/example2-env
WSGIProcessGroup %{GLOBAL}
# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/example2/example2/wsgi.py
<Directory /var/www/vhosts/example2/>
Require all granted
</Directory>
Alias /static/ /var/www/vhosts/example2/static/
<Directory /var/www/vhosts/example2/static/>
Order deny,allow
Allow from all
</Directory>
Alias /media/ /var/www/vhosts/example2/media/
<Directory /var/www/vhosts/example2/media/>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Edit:
Having read some suggestions in the comments, I have come to this. This still does not work.
ServerName example1.com
WSGIDaemonProcess example1 display-name=%{GROUP} python-path=/var/www/vhosts/example1/ python-home=/var/www/vhosts/example1/example1-env/
WSGIApplicationGroup %{GLOBAL}
WSGIProcessGroup example1
# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/example1/example1/wsgi.py process-group=example1
...
ServerName example2.com
WSGIDaemonProcess example2 display-name=%{GROUP} python-home=/var/www/vhosts/example2/example2-env/ python-path=/var/www/vhosts/example2/
WSGIApplicationGroup %{GLOBAL}
WSGIProcessGroup example2
# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/example2/example2/wsgi.py process-group=example2
The following configuration worked for me. In short, it serves two different Django applications at example1.com and example2.com using their respective virtual environments.
As you can see, inserting the ServerAlias AND ServerName made all the difference, alongside a couple of other corrections by suggested by the community.
Apache configuration:
<IfModule !wsgi_module>
LoadModule wsgi_module modules/mod_wsgi.so
</IfModule>
<VirtualHost *:80>
ServerName www.example1.com
ServerAlias example1.com
WSGIDaemonProcess example1 display-name=%{GROUP} python-path=/var/www/vhosts/example1/ python-home=/var/www/vhosts/example1/example1-env/
WSGIApplicationGroup %{GLOBAL}
WSGIProcessGroup example1
# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/example1/example1/wsgi.py process-group=example1
<Directory /var/www/vhosts/example1/>
Require all granted
</Directory>
Alias /static/ /var/www/vhosts/example1/static/
<Directory /var/www/vhosts/example1/static/>
Order deny,allow
Allow from all
</Directory>
Alias /media/ /var/www/vhosts/example1/media/
<Directory /var/www/vhosts/example1/media/>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName www.example2.com
ServerAlias example2.com
WSGIDaemonProcess example2 display-name=%{GROUP} python-home=/var/www/vhosts/example2/example2-env/ python-path=/var/www/vhosts/example2/
WSGIApplicationGroup %{GLOBAL}
WSGIProcessGroup example2
# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/example2/example2/wsgi.py process-group=example2
<Directory /var/www/vhosts/example2/>
Require all granted
</Directory>
Alias /static/ /var/www/vhosts/example2/static/
<Directory /var/www/vhosts/example2/static/>
Order deny,allow
Allow from all
</Directory>
Alias /media/ /var/www/vhosts/example2/media/
<Directory /var/www/vhosts/example2/media/>
</VirtualHost>

How do I receive subdomain wildcards on Apache2 using mod wsgi?

I'm developing an app with Django 1.8 and I'm trying to receive subdomains and then present customized homepages dependent on the subdomain. For example: example.com is my company's homepage, a user signs up as conqueryor.example.com and they get a new homepage named "conqueryor.example.com" or whatever they want.
Sounds simple enough, there's even the django subdomains library that I'm using. My current issue lies in setting up Apache2 and mod WSGI locally so that I can test it out locally before I affect everyone else on the project. I'm currently able to use the following .conf file with the lines 127.0.0.1 example.dev and 127.0.0.1 .example.dev in my /etc/hosts file. In my browser I'm able to access my app from example.dev, but if I try any subdomains I receive the Server Not Found page. I've also attempted using dnsmasq and adding the line address=/.example.dev/127.0.0.1
Current environment:
Ubuntu 15.10
Django 1.8.1
Apache 2.4
<VirtualHost *:80>
ServerName example.dev
DocumentRoot /home/example
ServerAlias www.example.dev
WSGIDaemonProcess example python-path=/home/example:/home/venv/example/lib/python2.7/site-packages
WSGIProcessGroup example
WSGIScriptAlias / /home/example/saas/wsgi.py
<Directory /home/example/static>
Require all granted
</Directory>
<Directory /home/example/saas>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName example.dev
DocumentRoot /home/example
ServerAlias example.dev
WSGIDaemonProcess example2 python-path=/home/example:/home/venv/example/lib/python2.7/site-packages
WSGIProcessGroup example2
WSGIScriptAlias / /home/example/saas/wsgi.py
Alias /static/ /home/example/static/
<Directory /home/example/static>
Require all granted
</Directory>
<Directory /home/example/saas>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName example.dev
DocumentRoot /home/example
ServerAlias *.example.dev
WSGIDaemonProcess example3 python-path=/home/example:/home/venv/example/lib/python2.7/site-packages
WSGIProcessGroup example3
WSGIScriptAlias / /home/example/saas/wsgi.py
Alias /static/ /home/example/static/
<Directory /home/example/static>
Require all granted
</Directory>
<Directory /home/example/saas>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Thanks!
I'm going to leave the question in case this helps anyone else, but I found the solution... I forgot I hadn't restarted dnsmasq, so after adding the address=/.example.dev/127.0.0.1 line to /etc/dnsmasq.conf and restarting dnsmasq with sudo /etc/init.d/dnsmasq restart I was able to access sites on subdomains. Oops!

Two Django projects running simultaneously and mod_wsgi acting werid

I'm trying to run two Django projects simultaneously. I happened to be using mod_wsgi, and found the site is acting weird. Perhaps there would be a workaround, but I'd like to know what I'm missing and how to solve the problem.
In the apache configuration
# Setup the Python environment
# As root owns basically everything on a Amazon AMI and root
# cannot be used. Create a folder under /var/run/wsgi
# with the owner as ec2-user and group ec2-user.
WSGISocketPrefix /var/run/wsgi
# Call your daemon process a name
WSGIDaemonProcess pydaemon processes=1 threads=5
# Call your daemon process group a name
WSGIProcessGroup pydaemon
# Point to where the handler file is. This will be different
# If you are using some other framework.
WSGIScriptAlias /test /var/www/html/test/wsgi.py
WSGIScriptAlias /proto /var/www/html/proto/wsgi.py
After Apache restarts, if I connect to '/proto', then the proto site is shown. However, then I connect to '/test', without restarting Apache, the proto site is still shown, and I cannot access to the test site.
Now I restart Apache, this time I go to '/test' first. The test site comes up! However, if I go to '/proto' it still shows the test site, not the proto site.
What could make this happen? I added SESSION_COOKIE_PATH differently for each application just in case, but the problem still exists.
[UPDATED]
I also tried as the following, to give different WSGI application group names, but without luck.
Alias /cuedit /var/local/test/wsgi.py
<Location /test>
SetHandler wsgi-script
Options +ExecCGI
WSGIApplicationGroup test
</Location>
Alias /proto /var/local/proto/wsgi.py
<Location /proto>
SetHandler wsgi-script
Options +ExecCGI
WSGIApplicationGroup proto
</Location>
[UPDATED]
I changed from the daemon mode to the embedded mode. I guess the problem was two instances shared the same mod_wsgi daemon process so their namespace collide.
I would expect they should be handled correctly, but in the daemon mode I couldn't get it right.
Use this as a workaround:
WSGIDaemonProcess pydaemon-1 processes=1 threads=5
WSGIDaemonProcess pydaemon-2 processes=1 threads=5
WSGIScriptAlias /test /var/www/html/test/wsgi.py
<Location /test>
WSGIProcessGroup pydaemon-1
WSGIApplicationGroup %{GLOBAL}
</Location>
WSGIScriptAlias /proto /var/www/html/proto/wsgi.py
<Location /proto>
WSGIProcessGroup pydaemon-2
WSGIApplicationGroup %{GLOBAL}
</Location>
This will force each application into separate daemon process group and no way they should be able to interfere with each other.
If that still doesn't work, you have problems with your WSGI script file somehow.
I also have 2 Django projects however each one is running on a different port (httpd config), it looks something like this:
<VirtualHost *:80>
ServerAdmin xx
ServerName xx
ServerAlias xx
ErrorLog /path/to/first/project/logs/error.log
CustomLog /path/to/first/project/logs/access.log combined
Alias /static/ /path/to/first/project/sitestatic
WSGIDaemonProcess app processes=1 threads=15 display-name=%{GROUP}
WSGIProcessGroup app
WSGIScriptAlias / /path/to/first/project/django.wsgi
<Directory /path/to/first/project/apache>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:8080>
ServerAdmin xx
ServerName xx
ServerAlias xx
ErrorLog /path/to/second/project/logs/error.log
CustomLog /path/to/second/project/logs/access.log combined
WSGIDaemonProcess app1 processes=1 threads=15 display-name=%{GROUP}
WSGIProcessGroup app1
WSGIScriptAlias / /path/to/second/project/apache/django.wsgi
<Directory /path/to/second/project/apache>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
The problem might be related to Apache sharing the Python sub interpreter between WSGI applications. Try adding this to the Apache configuration to avoid sharing:
WSGIApplicationGroup %{GLOBAL}
Check this blog post for in-depth explanation and additional tips (check the comments too).
Can't comment on the answer given by Graham, so adding one of my own.
The problem for me really was the Python Interpreter, but I also had to add the python-path for each interpreter. Here follows an example configuration:
WSGIDaemonProcess py_app1 processes=1 threads=5 python-path=/path/to/app1
WSGIScriptAlias /app1 /path/to/app1/wsgi.py
<Directory /path/to/app1>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
<Location /app1>
WSGIProcessGroup py_app1
WSGIApplicationGroup %{GLOBAL}
</Location>
WSGIDaemonProcess py_app2 processes=1 threads=5 python-path=/path/to/app2
WSGIScriptAlias /app2 /path/to/app2/wsgi.py
<Directory /path/to/app2>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
<Location /app2>
WSGIProcessGroup py_app2
WSGIApplicationGroup %{GLOBAL}
</Location>