Running two django instances using WSGI/Apache on localhost - django

I have a web portal that fetches data from another data server(HTTP based) which I need to test on my local machine.
In production, multiple versions of the web portal will exist, but fewer of the data servers.
Here is my WSGI configuration
WSGISocketPrefix /var/run/wsgi
WSGIDaemonProcess portal
WSGIScriptAlias / /home/rep/portal/wsgi/wsgi.py
<Location />
WSGIProcessGroup portal
</Location>
WSGIDaemonProcess dal
WSGIScriptAlias /dal /home/rep/dal/wsgi/wsgi.py
<Location /dal>
WSGIProcessGroup dal
</Location>
The portal code tries to fetch JSON data using a URL like http://localhost/dal/api/foo?bar=baz
The DAL server serves only the URL pattern
url(r'^/api/(?P<apiName>[a-zA-Z]+)', 'dal.dbapi.apiHandler'),
When I open http://127.0.0.1/ I get the portal main page.
When I open http://127.0.0.1/dal/foo?bar=baz I get the 404 page of the portal instead of JSON data from the dal app
I tried configuring virtual hosts, different ports and all, but I can't get this to work properly.
In production, these servers may be on different machines, but I need to be able to test both on my localhost during development.
Thanks in advance

OK I figured it out...
If you do not set a WSGIProcessGroup attribute, making recursive requests will end up on the wrong django instance.
So for each VirtualHost set a unique WSGIProcessGroup

Related

How I can use ionic 2 app as website on production server ?

I would like to know if any specific steps to deploy ionic 2 project on production server.
T tried running ionic serve on production server but when i tried call api its saying cors( allow origin issue).
Please help .
Thanks
The cors problem needs to be resolved on the server, so the server will allow cors. If you are using node you can use the cors package and than call app.use(cors()). If not, you need to find how to enable cors on your production server. Ionic can block requests if it runs in cordova (thats why you can specify a whitelist using the whitelist plugin that ionic provides, but I think that since you are running it as a website, the whitelist plugin cannot help much.
To test how your website works on your production server you can download the cors plugin for chrome, enable it and check if everything works fine :)
it works for me. I have full control of an apache server that does proxying for the domains.
I point my desktop version to the webpacked dist/ folder and the mobile version at the www/ directory leftover from ionic serve.
Proxy /api/ to my pm2/node app running on port 3080 in my case.
Point all my node server requests to http://MYAPP.com/api/ or http://mobile.MYAPP.com/api/ .. Static content is available at http://MYAPP.com/ or http://mobile.MYAPP.com/
<Directory "/usr/local/www/node/MYAPP/dist">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<Directory "/usr/local/www/node/MYAPP/www">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<VirtualHost *:80>
ServerName mobile.MYAPP.com
DocumentRoot "/usr/local/www/node/MYAPP/www/"
ProxyPass /api/ http://localhost:3080/
ProxyPassReverse /api/ http://localhost:3080/
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/usr/local/www/node/MYAPP/dist/"
ProxyPass /api/ http://localhost:3080/
ProxyPassReverse /api/ http://localhost:3080/
</VirtualHost>
At this moment, with Ionic 2 beta 10, I believe running an Ionic app as a 'progressive web app' is not possible. However, the team has stated their intention to eventually allow Ionic 2 apps to be submitted the app stores as well as run on a web server to be accessed via web browser.
This ionicforum answer from a moderator states that Ionic 2 apps cannot be hosted as stand-alone web apps yet, but that the functionality is "in the workds".
Take a look at this blog post from Ionic on further reading about Progressive Web Apps and how they are working toward getting Ionic apps to be PWAs.

How to configure apache2 on ubuntu for django restful services

I have django restful services on my ubuntu server which are running on port 84.
When the request is send, it come through the apache2 server which is running on port 80.
Now let say my server ip is "xx.xx.xx.10" and when i call with this url http://xx.xx.xx.10/user where user is the rest service running on django rest framework on port 84. Then the request should go to my django rest service through apache2 and return the desired output.
I tried by using the below configuration in the apache2 :-
<VirtualHost *:80>
WSGIScriptAlias / /myproject/myapp/test/wsgi.py
<Directory "/myproject/myapp/test/">
<Files "wsgi.py">
Require all granted
</Files>
</Directory>
</VirtualHost>
but this is giving internal error saying that test.settings does not exits.
is this the right way to configure the restful services running on different port on apache server.
Did you specify a python path? If you don't, import mysite will not work.
WSGIPythonPath /path/to/mysite.com
For more info see: Django docs modWSGI
I have solved this issued by using the below configurations :-
<VirtualHost *:80>
ServerName mydomain
ServerAlias *.mydomain
ProxyRequests off
ProxyPass / http://localhost:84/
ProxyPassReverse / http://localhost:84/
</VirtualHost>

SOAP webservice with spyne, django and apache

I have a django based website deployed with apache and mod_wsgi.
I need to develop a SOAP based webservice and to host it with my django project, using the same port (80).
So I've read about spyne:
http://spyne.io/docs/2.10/manual/02_helloworld.html?highlight=django
Seems like it fits my needs. I just couldn't find how to deploy it with apache.
Ultimately you are going to need to end up with:
...
application = WsgiApplication(app)
That is, a WSGI script file which constructs your application and exposes it as application and not wsgi_app as the docs say to use.
Then configure mod_wsgi something like:
WSGIDaemonProcess soapapp
WSGIScriptAlias /suburl /some/path/soapapp.wsgi \
process-group=soapapp application-group=%{GLOBAL}
<Directory /some/path/>
<Files soapapp.wsgi>
# Order allow,deny
# Allow from all
Require all granted
</Files>
</Directory>
That is, create a separate daemon process group for this application. Mount it at a sub URL and delegate it to run in the separate daemon process group rather than in whatever process your Django application is. Also ensure you set up access rights so Apache knows it is allow to host the WSGI application from where it exists.
You would then access the SOAP service at that sub URL.

apache mod_wsgi basic authentication for django app

I've finished a first website based on django and I'm ready to deploy on a liveserver. However, I don't want this site to be visible to the public for now while tweaking and testing.
On PHP sites I always used basic http authentication via .htaccess to make last changes. This is a two liner which denies access for the whole site.
Ideally I want to run a environment like this:
static.mydomain.com (served by apache2 for static files)
mydomain.com (served by apache2 with mod_wsgi latest stable release -> available for public)
dev.mydomain.com (served by apache2 with mod_wsgi dev/testing -> not available for public (basic authentication))
Can this be done with django/apache2/mod_wsgi?
You could setup basic auth for your virtual host dev.mydomain.com. Look at http://httpd.apache.org/docs/2.2/mod/mod_auth_basic.html and http://httpd.apache.org/docs/2.2/vhosts/name-based.html for more details
EDIT: Your virtual host config will look somthing like:
<VirtualHost *:80>
ServerName mydomain.com
DocumentRoot /srv/www/wsgi
<Directory /srv/www/wsgi>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /srv/www/wsgi/app.wsgi
</VirtualHost>
<VirtualHost *:80>
ServerName dev.mydomain.com
DocumentRoot /srv/www/wsgi-dev
<Directory /srv/www/wsgi-dev>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /srv/www/wsgi-dev/app.wsgi
<Location />
AuthType Basic
AuthName "Restricted Files"
AuthUserFile /usr/local/apache/passwd/passwords
Require valid-user
</Location>
</VirtualHost>
mod_wsgi doesn't care if you use HTTP auth over it. The only provision would be that if you want it to be visible to the WSGI application then you'd need to use WSGIPassAuthorization, but this is not a concern in this case since Django has its own authentication scheme independent of HTTP auth.

setting up two Django websites under Apache with WSGI

I've set up a django website as described in the django docs: https://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/
Now I want to setup another version of the site (different source dir, different database) to run on the same server. There are active users and flex apps who use app #1, so I want to keep app #1 access unchanged. I also rather not change the urls.py at all even for app #2.
I was thinking of different port for app #2
For example
http://192.168.1.1/load_book/123/ will load book from app #1
http://192.168.1.1:444/load_book/123/ will load book from app #2
I'm a complete noob to Apache and WSGI... how do I set it up?
What do you mean by they have the same URLs? The same hostname, perhaps?
Let's say you've got 2 apps:
http://example.com/your_app
http://example.com/my_app
These can both be Django apps, served by WSGI, on the same Apache instance. Using either Directory or Location directives in your apache conf to specify the .wsgi loader file as described in the django docs linked above:
<Location /your_app>
WSGIScriptAlias /your_app /path/to/mysite/apache/your_app/django.wsgi
...
</Location>
<Location /my_app>
WSGIScriptAlias /my_app /path/to/mysite/apache/my_app/django.wsgi
...
</Location>
The only real gotcha is that you'll need to tell your_app and my_app that they are no longer on the document root of the host. To do this, add a base_url parameter to your settings.py and prefix all of the entries in your urls.py with this param. This will ensure when the request comes through Apache, your python app can route it accordingly.
For an easy example of how this is done, have a look at the code for Bookworm, a Django app.
You can attatch the wsgi application to different sub-paths under the same domain. If you do this the paths to the views inside Django will still be the same. You do not have to modify the urls.py. In the following example Django will regard /site1 as the root of project1.
Check out http://code.google.com/p/modwsgi/wiki/InstallationInstructions for documentation on mod_wsgi.
<VirtualHost *:80>
ServerName www.example.com
WSGIDaemonProcess example
WSGIProcessGroup example
WSGIScriptAlias /site1 /home/django/project1/deploy/wsgi.py
<Directory /home/django/project1/deploy>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias /site2 /home/django/project2/deploy/wsgi.py
<Directory /home/django/project2/deploy>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Now the two sites will run in the same daemon process using different python sub-interpreters.