issues with serving 2 django applications with apache in ubuntu server using wsgi - django

i'm trying to host a second domain in my ubuntu vps (domain2), i read online that is possible to host 2 domains in one vps (with one ip adress) yet i'm having some issues with it.
i have this configuration in apache2 ubuntu under /etc/apache2/sites-enabled/000-default-le-ssl.conf:
Define wsgi_daemon1 "domain1"
Define wsgi_daemon2 "domain2"
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/my_linux_user/myproject/static
<Directory /home/my_linux_user/myproject/static>
Require all granted
</Directory>
<Directory /home/my_linux_user/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<IfDefine !wsgi_init>
WSGIDaemonProcess ${wsgi_daemon1} python-path=/home/my_linux_user/myproject python-home=/home/my_linux_user/myproject/myprojectenv
WSGIProcessGroup ${wsgi_daemon1}
WSGIScriptAlias / /home/my_linux_user/myproject/myproject/wsgi.py
Define wsgi_init 1
</IfDefine>
ServerName domain1.tn
SSLCertificateFile /etc/letsencrypt/live/domain1.tn/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain1.tn/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error_project2.log
CustomLog ${APACHE_LOG_DIR}/access_project2.log combined
Alias /static /home/my_linux_user/project2/static
<Directory /home/my_linux_user/project2/static>
Require all granted
</Directory>
<Directory /home/my_linux_user/project2/django_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<IfDefine !wsgi_init>
WSGIDaemonProcess ${wsgi_daemon2} python-path=/home/my_linux_user/project2 python-home=/home/my_linux_user/project2/project2ENV
WSGIProcessGroup ${wsgi_daemon2}
WSGIScriptAlias / /home/my_linux_user/project2/django_project/wsgi.py
Define wsgi_init 1
</IfDefine>
ServerName domain2
SSLCertificateFile /etc/letsencrypt/live/domain2/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain2/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
the first domain is served correctly but the second domain (domain2) is not, it actually still pointing to the default apache2 index page
i tried
restarting apache2 with:
apachectl restart
and with
apachectl graceful
checking the DNS configuration, the domaain in up and working
when i tried to just point the domain with a seperate vhost and a simple web page it works!, yet with wsgi it doesn't
i tried to see what are the enabled sites with
apache2ctl -S
and i something like this:
*:443 is a NameVirtualHost
default server domain1.tn (/etc/apache2/sites-enabled/000-default-le-ssl.conf:5)
port 443 namevhost domain1.tn (/etc/apache2/sites-enabled/000-default-le-ssl.conf:5)
port 443 namevhost domain2.com (/etc/apache2/sites-enabled/000-default-le-ssl.conf:106)
port 443 namevhost mail.domain1.tn
(/etc/apache2/sites-enabled/mail.domain1.tn.conf:3)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex ssl-stapling: using_defaults
Mutex proxy: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default
Mutex mpm-accept: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
Define: MODSEC_2.5
Define: MODSEC_2.9
Define: wsgi_daemon1=domain1
Define: wsgi_daemon2=domain2
Define: wsgi_init=1
User: name="www-data" id=33
Group: name="www-data" id=33
so my question does it take some time for changes to take effect (it was not the case when i hosted the first domain (domain1)), if not i will appreciate any help :)
ps: i'm replacing the actual domain names

Related

Django project set up with Apache2 - i want to access it through I.P/application_name/

Hi I want to set up my django project to be access through apache2 by I.P/application_name/
So "192.0.0.0/app/" (Ip being an example)
Here is my 000-default.conf file in etc/apache2/sites-available
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
WSGIDaemonProcess HTSv2 python-home=/home/django/config/env python-path=/usr/local/django/app
WSGIProcessGroup HTSv2
WSGIScriptAlias / /usr/local/django/app/application/wsgi.py
<Directory /usr/local/django/app/application>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static/ /usr/local/django/app/static/
<Directory /usr/local/django/app/static>
Require all granted
</Directory>
<Directory /usr/local/django/app>
Deny from all
Allow from ...
</Directory>
</VirtualHost>
When I change the line:
WSGIScriptAlias / /usr/local/django/app/application/wsgi.py
to
WSGIScriptAlias /app/ /usr/local/django/app/application/wsgi.py
I can access the home page of my application by 192.0.0.0/app/
However when I click on any buttons, obviously does not keep to this, so for example going to page1, does not go to 192.0.0.0/app/page1, it goes to 192.0.0.0/page1
How can I configure apache2 to do this? The reason I want to do this is so I can host multiple projects on the same server, so the root of 1 project is 192.0.0.0/app1/ and then the root of the other is 192.0.0.0/app2/ etc...

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!

VirtualHost configuration doesn't allow second website to run

I have this in my /etc/apache2/sites-available/staginnx.com.conf
WSGIScriptAlias / /home/ubuntu/v1/staginnx-info/app/website/website/wsgi.py
WSGIPythonPath /home/ubuntu/v1/staginnx-info/app/website
<VirtualHost *:80>
# Admin email, Server Name (domain name) and any aliases
ServerAdmin info#staginnx.com
ServerName staginnx.com
ServerAlias www.staginnx.com
<Directory /home/ubuntu/v1/staginnx-info/app/website/website>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static/ /home/ubuntu/v1/staginnx-info/app/website/ui/static/
<Directory /home/ubuntu/v1/staginnx-info/app/website/ui/static>
Require all granted
</Directory>
Alias /favicon.ico /home/ubuntu/v1/staginnx-info/app/website/ui/static/images/favicon.ico
ErrorLog /var/log/apache2/error.log
</VirtualHost>
<VirtualHost *:80>
# Admin email, Server Name (domain name) and any aliases
ServerAdmin blog.staginnx#staginnx.com
ServerName blog.staginnx.com
ServerAlias blog.staginnx.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.php
DocumentRoot /var/www/html/blog/
<Directory /var/www/html/blog>
Require all granted
</Directory>
# Custom log file locations
LogLevel warn
ErrorLog /var/www/html/blog/log/error.log
CustomLog /var/www/html/blog/log/access.log combined
</VirtualHost>
Now the first one, the main website is working fine. The second one, BLOG is not working.
I have done sudo a2ensite staginnx.com.conf and a symlink is generated at sites-enabled. I have done reload apache and restart too.
Surfed around but no luck.
Want to install wordpress blog in my blog folder.
In order to use your django application inside a virtual host you need to set the WSGIScriptAlias inside the virtualhost settings and also you can't use the WSGIPythonPath, you need to use WSGIDaemonProcess like in the following settings.
<VirtualHost *:80>
WSGIScriptAlias / /home/ubuntu/v1/staginnx-info/app/website/website/wsgi.py
WSGIDaemonProcess example.com python-path=/home/ubuntu/v1/staginnx-info/app/website
# Admin email, Server Name (domain name) and any aliases
ServerAdmin info#staginnx.com
ServerName staginnx.com
ServerAlias www.staginnx.com
<Directory /home/ubuntu/v1/staginnx-info/app/website/website>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static/ /home/ubuntu/v1/staginnx-info/app/website/ui/static/
<Directory /home/ubuntu/v1/staginnx-info/app/website/ui/static>
Require all granted
</Directory>
Alias /favicon.ico /home/ubuntu/v1/staginnx-info/app/website/ui/static/images/favicon.ico
ErrorLog /var/log/apache2/error.log
</VirtualHost>
Also in case you want to host multiple application on the same host then you need to change a small setting in the wsgi.py file as well.
instead of using
os.environ.setdefault(DJANGO_SETTINGS_MODULE, "app.settings")
you should use
os.environ["DJANGO_SETTINGS_MODULE"] = "app.settings"
I hope this helps you.
For WSGIDaemonProcess setting

Deploying multiple django projects on apache with mod_wsgi

I'm trying to deploy 2 django projects on Apache with mod_wsgi using daemon mode, but right now I just get 404's when I try to load the pages.
The two sites are hosted on the same server, both on port 80. The two entries in sites-avalable are below:
<VirtualHost *:80>
ServerAdmin admin#site1.com
ServerName site1.com
ServerAlias www.site1.com
DocumentRoot /var/www/site1.com
ErrorLog /srv/www/site1.com/logs/error.log
CustomLog /srv/www/site1.com/logs/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#site2.org
ServerName site2.org
ServerAlias www.site2.org
DocumentRoot /var/www/site2.org
ErrorLog /srv/www/site2.org/logs/error.log
CustomLog /srv/www/site2.org/logs/access.log combined
</VirtualHost>
And in my httpd.conf,
WSGIDaemonProcess site1.com python-path=/usr/local/www/site1/
WSGIProcessGroup site1.com
WSGIScriptAlias site1.com/blog /usr/local/www/site1/site1/wsgi.py
WSGIDaemonProcess site2.org python-path=/usr/local/www/site2/
WSGIProcessGroup site2.org
WSGIScriptAlias site2.org/ /usr/local/www/site2/site2/wsgi.py
<Directory /usr/local/www/site1/site1>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
<Directory /usr/local/www/site2/site2>
<Files wsgi.py>
Order deny, allow
Allow from all
</Files>
</Directory>
I took the httpd.conf stuff right out of the Django docs, but it's still not working properly. site1.com/blog gives me a 404, and site2.org gives me a generic Apache filetree. What am I missing here?
If you have a sites-available directory, you're on a Debian-derived distro, and you shouldn't be editing httpd.conf at all. All the stuff that you've put into httpd.conf should go in the relevant files in sites-available.