Multiple wsgi.mod on single apache - python

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

Related

Deploying Multiple Django Websites with Apache & mod_wsgi Windows

I am using mod_wsgi in a virtualenv with Apache 2.4 and I want to serve multiple Django sites from the same server.
httpd.config
### Configuration Site_1
LoadModule wsgi_module " S:/Site_1/VirtualEnvSite_1/Lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd"
WSGIApplicationGroup %{GLOBAL}
WSGIPythonHome "c:/users/mmyuser/appdata/local/programs/python/python36"
WSGIScriptAlias / " S:/Site_1/site_1/site_1/wsgi_windows.py"
WSGIPythonPath " S:/Site_1/VirtualEnvSite_1/Lib/site-packages"
Alias /static " S:/Site_1/site_1/staticfiles"
Alias /media " S:/Site_1/site_1/media"
<Directory " S:/Site_1/site_1/staticfiles">
Require all granted
</Directory>
<Directory " S:/Site_1/site_1/media">
Require all granted
</Directory>
<Directory " S:/Site_1/site_1/PEQ">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
####Configuration Site_2
LoadModule wsgi_module " S:/Site_2/VirtualEnvSite_2/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd"
WSGIApplicationGroup %{GLOBAL}
WSGIPythonHome "c:/users/myuser/appdata/local/programs/python/python36"
WSGIScriptAlias / " S:/Site_2/site_2/site_2/wsgi_windows.py"
WSGIPythonPath " S:/Site_2/VirtualEnvSite_2/Lib/site-packages"
Alias /static " S:/Site_2/site_2/staticfiles"
Alias /media " S:/Site_2/site_2/media"
<Directory " S:/Site_2/site_2/staticfiles">
Require all granted
</Directory>
<Directory " S:/Site_2/site_2/media">
Require all granted
</Directory>
<Directory " S:/Site_2/site_2/site_2">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
httpd-vhosts.config
# Virtual Hosts
#
<VirtualHost *:8080>
ServerAdmin webmaster#localhost
DocumentRoot "c:/wamp/www"
ServerName localhost
ErrorLog "logs/localhost-error.log"
CustomLog "logs/localhost-access.log" common
</VirtualHost>
I have reviewed these posts:
Deploying multiple django apps on Apache with mod_wsgi
multiple-django-sites-with-apache-mod-wsgi
running-multiple-django-projects-on-one-apache-instance-with-mod_wsgi/
MĂșltiples direcciones, un solo proyecto
how-to-use-mod-wsgi-for-hosting-multiple-django-projects-under-single-domain
but they have not worked for me.
The server is a Windows Server 2012R2
Please direct me what I should do to get both sites up and running.
Note: The websites work perfectly separate
I also have a similar problem. And I've already looked for many posts, one thing I noticed different from yours was the line:
WSGIScriptAlias / " S:/Site_1/site_1/site_1/wsgi_windows.py"
WSGIScriptAlias / " S:/Site_2/site_2/site_2/wsgi_windows.py"
I identify the site in the alias:
WSGIScriptAlias /site1 " S:/Site_1/site_1/site_1/wsgi_windows.py"
WSGIScriptAlias /site2 " S:/Site_2/site_2/site_2/wsgi_windows.py"
And I'm using process daemon:
WSGIScriptAlias /site1 /site1/wsgi.py
WSGIDaemonProcess site1_proc python-path=S:/Site_1/VirtualEnvSite_1/Lib/site-packages python-home="c:/users/mmyuser/appdata/local/programs/python/python36"
WSGIProcessGroup site1_proc
In my case it's still not working for some other reason. Which I haven't identified yet. Individual applications run correctly using reverse proxy. But when I activate both, one of them gives the error: AH00128: File does not exist: /var/www/html/myapp2/. Configuration files are identical.
Here are my config for setting up multiple projects apache | windows | wsgi
httpd.conf
LoadFile "c:/python37/python37.dll"
LoadModule wsgi_module "c:/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "c:/python37"
# site 1 config
WSGIScriptAlias /site1 "C:/Payslips/payslips_app/wsgi.py" application-group=site1
<Directory "C:/Payslips/payslips_app/">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static "C:/Payslips/static/"
<Directory "C:/Payslips/static/">
Require all granted
</Directory>
# site 2 config
WSGIScriptAlias /site2 "C:/Subsistance-Payments-System/subsistance_payment/wsgi.py" application-group=site2
<Directory "C:/Subsistance-Payments-System/subsistance_payment/">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
site1 wsgi.py
import os
import sys
from django.core.wsgi import get_wsgi_application
sys.path.append('C:/Payslips/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'payslips_app.settings'
application = get_wsgi_application()
site2 wsgi.py
import os
import sys
from django.core.wsgi import get_wsgi_application
sys.path.append('C:/Subsistance-Payments-System/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'subsistance_payment.settings'
application = get_wsgi_application()

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 on WAMP only seeing directory listings

I am trying to publish a test python flask application to WAMP. I have gone through many tutorials, but face the issue of only seeing directory listings when I navigate to the app. I believe the wsgi module is setup correctly as the apache logs flag that the module has loaded, and as you can see from the screenshot below the server text also suggests the same. Has anyone else came across this, and resolved.
Here is my wsgi file:
import sys
sys.path.insert(0, 'C:/wamp64/www/flaskapp')
from webtool import app as application
Here is my init.py file:
from flask import Flask, request
app = Flask(__ name__)
#app.route('/')
def hello_world():
return "Hello World"
if __ name__ == '__ main__':
app.run()
The additional lines added to my httpd.conf
LoadModule wsgi_module "d:/program files/anaconda3/lib/site-
packages/mod_wsgi/server/mod_wsgi.cp35-win_amd64.pyd"
LoadFile "d:/program files/anaconda3/python35.dll"
WSGIPythonHome "d:/program files/anaconda3"
Virtualhosts file:
<VirtualHost *:80>
ServerName localhost
DocumentRoot c:/wamp64/www
<Directory "c:/wamp64/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName flaskapp
WSGIScriptAlias /flaskapp c:/wamp64/www/flaskapp/flaskapp.wsgi
DocumentRoot c:/wamp64/www/flaskapp
<Directory c:/wamp64/www/flaskapp>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
It turned out to be my virtual hosts file. I cannot have two references to the same location (*:80) in the one file.
Updated file below:
<VirtualHost *:80>
ServerName localhost
DocumentRoot c:/wamp64/www
<Directory "c:/wamp64/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
WSGIScriptAlias /flaskapp c:/wamp64/www/flaskapp/flaskapp.wsgi
<Directory c:/wamp64/www/flaskapp>
Order allow,deny
Allow from all
</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?

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.

Categories

Resources