I recently moved my Django project to Apache server. I am facing a few issues with respect to dependency modules being inaccessible after moving the project to Apache.
Here are my configuration files:
This is my bemoss.conf file in the sites-available directory of Apache:
WSGIPythonPath /home/kruthika/workspace/bemoss_web_ui
WSGIPythonPath /home/kruthika/workspace/rtunetwork/volttron
WSGIPythonPath /home/kruthika/workspace/bemoss_web_ui/helper
<VirtualHost *:80>
WSGIScriptAlias / /home/kruthika/workspace/bemoss_web_ui/bemoss.wsgi
ServerName bemoss.com
Alias /static /home/kruthika/workspace/bemoss_web_ui/static
<Directory /home/kruthika/workspace/bemoss_web_ui/>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Here is my bemoss.wsgi file:
import os
import sys
sys.path = ['/home/kruthika/workspace']+sys.path
print sys.path
os.environ['DJANGO_SETTINGS_MODULE']='bemoss_web_ui.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
My project is accessing a few more python files from another project. I added a reference to those in my .conf file above and in settings.py file as given below.
PROJECT_DIR = os.path.dirname(__file__)
print PROJECT_DIR
sys.path.insert(3,os.path.join(PROJECT_DIR,'ZMQHelper'))
sys.path.insert(1,os.path.join(PROJECT_DIR,'helper'))
sys.path.insert(2, '/home/kruthika/workspace/rtunetwork/volttron')
print sys.path
This is my project folder definition:
When I try to access a link, whose views.py file refers to the above mentioned dependencies, it throws a import module error as given below.
My question here is how to refer to my dependent modules/python classes so that the files are imported properly.
All these imports were working fine when I executed the project on Django's development server. Is there some Apache configuration I am missing or doing wrong? I am new to server configurations and I think I am missing something here.
Related
I can't seem to find a good answer to this. Who needs to own the virtualenv when running it as a WSGIDaemon? I assume on my OS (Ubuntu 16) www-data, but I want to be sure. Trying some new things to get this thing working based off of the answer from this post...
django apache configuration with WSGIDaemonProcess not working
Does the django project, the virtualenv folder, or both need to be owned by the apache group? What ownerships need to be in place to serve a django project without specifying a port? Why do I get the following?
The root problem:
Call to 'site.addsitedir()' failed for '(null)'
When I start apache, I get this error. I've followed several different guides, including:
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
and
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/
but have had zero success.
My virtual environment path is /usr/local/virtualenvs/servicesite
My django project path is /home/addohm/projects/rtservice/servicesite
this is where manage.py resides,
which leaves /home/addohm/projects/rtservice/servicesite/servicesite as the location of my wsgi.py.
wsgi.py:
SERVICESITE = ['/usr/local/virtualenvs/servicesite/lib/python3.5/site-packages']
import os
import sys
import site
prev_sys_path = list(sys.path)
for directory in SERVICESITE
site.addsitedir(directory)
new_sys_path = []
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
""" **Doesn't seem to work, throwing error in apache logs**
site.addsitedir('/usr/local/virtualenvs/servicesite/lib/python3.5/site-packages')
"""
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "servicesite.settings")
application = get_wsgi_application()
DJANGO_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
sys.path.append(DJANGO_PATH)
apache2.conf
[...]
WSGIDaemonProcess servicesite python-path=/home/addohm/projects/rtservice/servicesite:/usr/local/virtualenvs/servicesite/lib/python3.5/site-packages
WSGIProcessGroup servicesite
WSGIScriptAlias / /home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py
Alias /static/ /home/addohm/projects/rtservice/servicesite/static/
<Directory /home/addohm/projects/rtservice/servicesite/static/>
Require all granted
</Directory>
<Directory /home/addohm/projects/rtservice/servicesite/servicesite>
<Files wsgy.py>
Require all granted
</Files>
</Directory>
[...]
You should not have need to change anything in the original wsgi.py generated by Django for you. It is generally sufficient to have:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "servicesite.settings")
application = get_wsgi_application()
Your Apache configuration should then preferably be:
WSGIDaemonProcess service site python-home=/usr/local/virtualenvs/servicesite \
python-path=/home/addohm/projects/rtservice/servicesite
WSGIProcessGroup servicesite
WSGIScriptAlias / /home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py
Alias /static/ /home/addohm/projects/rtservice/servicesite/static/
<Directory /home/addohm/projects/rtservice/servicesite/static/>
Require all granted
</Directory>
<Directory /home/addohm/projects/rtservice/servicesite/servicesite>
<Files wsgy.py>
Require all granted
</Files>
</Directory>
That is, use python-home for the location of the directory specified by sys.prefix for the virtual environment. Avoid using python-path and referring to the site-packages directory. Using python-home has been preferred way for a very long time and using it ensures that things fail in a more obvious way when you don't do things the correct way.
A few very important things.
The first is that mod_wsgi must be compiled for the specific Python major/minor version you want to use.
Second, the Python virtual environment must be created from the same Python installation as mod_wsgi was compiled for. You can't have mod_wsgi compiled against a system Python installation, but have your virtual environment based off a separate Python installation for same major/minor version in /usr/local.
Third, the user that Apache runs your code as must have read access to any directories/files for the main Python installation, the virtual environment and your application code. When you stick stuff under a home directory, it will not generally have access as the home directory prohibits others from reading anything in the home directory.
Fourth, if the mod_wsgi daemon process group is set to run as a different user than the Apache user, the Apache user still must at least have ability to read the wsgi.py file and all the directories down to that point.
Further reading on virtual environments which is more up to date:
http://blog.dscpl.com.au/2014/09/using-python-virtual-environments-with.html
I cannot for the life of me figure of why this flask application I'm trying to launch is not working. I am running it on a $5 Digital Ocean droplet. Here's (hopefully) everything you need to know about it:
Directory layout (contained within /var/www/):
FlaskApp
FlaskApp
__init__.py
static
templates
venv
flaskapp.wsgi
__init__.py:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "yay it worked"
if __name__ == "__main__":
app.run()
flaskapp.wsgi:
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
from FlaskApp import app as application
application.secret_key = 'Add your secret key'
FlaskApp.conf (contained in /etc/apache2/sites-availble):
<VirtualHost *:80>
ServerName the.ip.blah.blah
ServerAdmin admin#mywebsite.com
WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
<Directory /var/www/FlaskApp/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/FlaskApp/FlaskApp/static
<Directory /var/www/FlaskApp/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
venv was created from calling virtualenv venv within /var/www/FlaskApp/FlaskApp/. I installed flask in venv using pip install flask after entering venv using source venv/bin/activate.
Wsgi has been enabled (a2enmod wsgi). FlaskApp.conf was enabled (a2ensite FlaskApp). And, finally, I restarted apache many times, but to no success (service apache2 restart).
I was following this guide on how to set up a flask application.
Here is a screenshot of what my error looks like:
Any help on getting this to work would be greatly appreciated.
Thanks in advance.
EDIT: I found the problem: ImportError: No module named flask. This is a little strange since I did do pip install flask within the virtualenv. When I just open a python console session in the virtualenv and try import flask I get no error, so not sure what's going on.
Also, how is this application even using venv? I don't see it getting accessed anywhere so how is it even using it? Perhaps this is why i'm getting the ImportError, because I only have flask installed on the virtualenv but it's not being used?
The problem is essentially that you are installing Flask, and possibly other required libraries, in a virtual environment but the python (wsgi interface) is running with the system python which does not have these extra libraries installed.
I have very little recent experience running Python on Apache (I come from an era of mod_python and cgi), but apparently one way to handle this is to use the site package to add the site-packages from your venv to the Python that is executed. This would go in your .wsgi file.
import site
site.addsitedir('/path/to/your/venv/lib/pythonX.X/site-packages')
I think the best way to solve your problem is to add tell your wsgi file about your virtual environment and activate it:
put the following code in your your flaskapp.wsgi
activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
and restart apache.
hope it will help!
find more here
I created a droplet(cloud server) on DigitalOcean and with no-ip.com I gave it the hostname - project.ddns.net.By ssh(ing) into the droplet I installed pip and virtualenv.
Inside /var/www/ I created a virtualenv and cloned the repository from github of my project.The directory struture is -
project_revamped (root of the project)
->requirements
->base.txt
->dev.txt
->project (django project)
->static
->media
->apps (folder which contains apps)
->manage.py
->project
->urls.py
->settings
->base.py
->dev.py
By following the official Django documentation I created httpd.conf in the /etc/apache2 path and included it in apache2.conf.
My httpd.confs reads as this -
WSGIScriptAlias / /var/www/project_revamped/project/project/wsgi.py
WSGIPythonPath /var/www/project_revamped/project:/var/www/.virtualenvs/projectenv/local/l ib/python2.7/site-packages
<Directory /var/www/project_revamped/project/project>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
and my wsgi.py reads as this -
import os
import sys
#Add the app's directory to the python path
sys.path.append('/var/www/project_revamped/project')
sys.path.append('/var/www/project_revamped/project/project')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings.dev'
#Activate your virtualenv
activate_env = os.path.expanduser('/var/www/.virtualenvs/typesetenv/bin/activate_this.py')
execfile(activate_env, dict(__file__=activate_env))
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
After changing the files I finally gave the commands -
service apache2 reload
service apache2 restart
However after doing these things right the corresponding ip says there is some problem the server and sends 500 error.I guess the problem is somewhere in my configuration because apache server was responding working fine.After I include django project with it the problem starts.
After checking the error logs I found these error messages -
mod_wsgi (pid=29458): Target WSGI script '/var/www/project_revamped/project/project/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=29458): Exception occurred processing WSGI script '/var/www/project_revamped/project/project/wsgi.py'.
File "/var/www/project_revamped/project/project/wsgi.py", line 28, in <module>
[:error] [pid 29458:tid 140073924572928] [client 103.16.70.147:33613] application = get_wsgi_application()
Can anybody please help me here in the configuration? I am stuck in this for past 2 days and every different article on the internet tells the different story.
add this line to you wsgi.py file : sys.path.append('/var/www/project_revamped/project') before os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings.dev' and check if it works.
You may have a look at my VirtualHost configuration for Apache with mod_wsgi. This template can be automatically filled during project-creation, but you may be able to fill/replace variables and strip it down to your needs:
<VirtualHost *:80>
# This is name based virtual hosting. So place an appropriate server name
# here. Example: django.devsrv.local
ServerName [[SERVER_NAME]]
ServerAdmin webmaster#localhost
# This alias makes serving static files possible.
# Please note, that this is geared to our settings/common.py
# In production environment, you will propably adjust this!
Alias /static/ {{ project_directory }}/run/static/
# This alias makes serving media files possible.
# Please note, that this is geared to our settings/common.py
# In production environment, you will propably adjust this!
Alias /media/ {{ project_directory }}/run/media/
# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / {{ project_directory }}/{{ project_name }}/wsgi.py
# PROCESS_NAME specifies a distinct name of this process
# see: https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIDaemonProcess
# PATH/TO/PROJECT_ROOT is the full path to your project's root directory,
# containing your project files
# PATH/TO/VIRTUALENV/ROOT: If you are using a virtualenv specify the full
# path to its directory.
# Generally you must specify the path to Python's site-packages.
WSGIDaemonProcess {{ project_name }} python-path={{ project_directory }}:{{ project_directory }}/../lib/python2.7/site-packages
# PROCESS_GROUP specifies a distinct name for the process group
# see: https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIProcessGroup
WSGIProcessGroup {{ project_name }}
# Serving static files from this directory
# Please note, that this is geared to our settings/common.py
# In production environment, you will propably adjust this!
<Directory {{ project_directory }}/run/static>
Options -Indexes
Order deny,allow
Allow from all
</Directory>
# Serving media files from this directory
# Please note, that this is geared to our settings/common.py
# In production environment, you will propably adjust this!
<Directory {{ project_directory }}/run/media>
Options -Indexes
Order deny,allow
Allow from all
</Directory>
LogLevel warn
# PROJECT_NAME is used to seperate the log files of this application
ErrorLog ${APACHE_LOG_DIR}/{{ project_name }}_error.log
CustomLog ${APACHE_LOG_DIR}/{{ project_name }}_access.log combined
</VirtualHost>
You may see my project-skeleton on GitHub and here is the documentation of the Apache2-conf on RTD.org
I am running Ubuntu 14.04 on my notebook and I am using Django 1.6.4 (with virtualenv) together with Apache 2.4.7.
I would like to access two django projects through my local apache. Therefore, theses projects are called kleyboldt (name of a guy I am writing for) and kleyboldt2. Normally I store my web projects under /home/nick/Workspace/Web. After watching tutorial I ended up with two domains pointing to the same django project. To be sure I rewrote the config and placed my projects under /var/www.
At the beginning I wrote two files in order to configure apache.
/etc/apache/sites-available/kleyboldt.conf
WSGIPythonPath /var/www/kleyboldt:/var/www/kleyboldt/env/lib/python2.7/site-packages
<VirtualHost *:80>
WSGIScriptAlias / /var/www/kleyboldt/kleyboldt.wsgi
ServerName kleyboldt.com
Alias /static /var/www/kleyboldt/static
<Directory /var/www/kleyboldt/>
Require all granted
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
/etc/apache2/sites-available/kleyboldt2.conf
WSGIPythonPath /var/www/kleyboldt2:/var/www/kleyboldt2/env/lib/python2.7/site-packages
<VirtualHost *:80>
WSGIScriptAlias / /var/www/kleyboldt2/kleyboldt2.wsgi
ServerName kleyboldt2.com
Alias /static /var/www/kleyboldt2/static
<Directory /var/www/kleyboldt2/>
Require all granted
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Afterwards I enabled both sites via a2ensite. I checked the sites-enabled directory and the links are placed correctly. In order to redirect kleyboldt.com and kleyboldt2.com to my local apache I changed my /etc/hosts to:
127.0.0.1 localhost
127.0.1.1 mars (name of my computer)
127.0.0.1 kleyboldt.com
127.0.0.1 kleyboldt2.com
To use WSGI I wrote for each project a seperated wsgi file in the project folder:
/var/www/kleyboldt/kleyboldt.wsgi
import os
import sys
sys.path.append('/var/www/kleyboldt')
os.environ['DJANGO_SETTINGS_MODULE'] = 'kleyboldt.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
/var/www/kleyboldt2/kleyboldt2.wsgi
import os
import sys
sys.path.append('/var/www/kleyboldt2')
os.environ['DJANGO_SETTINGS_MODULE'] = 'kleyboldt2.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I created two independent python environments using virtualenv that are now located under /var/www/kleyboldt/env and /var/www/kleyboldt2/env.
Afterwarsd I got two django test sites when I typed both domains in the URL bar of my browser. To differ I added some settings to the settings.py files and wrote custom views.
/var/www/kleyboldt/homepage/views.py
from django.http import HttpResponse
def hello(request):
return HttpResponse("kleyboldt1")
/var/www/kleyboldt2/homepage/views.py
from django.http import HttpResponse
def hello(request):
return HttpResponse("kleyboldt2")
When I type localhost in my browser then I get kleyboldt2 back. When I type kleyboldt2.com in my browser then I get kleyboldt2 back too. Unfortunately I see kleyboldt2 also after typing kleyboldt.com in my browser.
Maybe the bug is to simple for me to discover :D Does anybody know a possible solution?
I'm using centos, apache, mod_wsgi on server for django project.
After uploading over sftp of changed project files site opens with changes and without changes randomly.
I think, that changes should not be applied before restarting apache. Isn't it?
Apache settings
<VirtualHost *:88>
ServerName h1.ru
UseCanonicalName Off
ServerAdmin "admin#h1.ru"
DocumentRoot /var/www/h1/h1.ru/
AllowEncodedSlashes On
WSGIDaemonProcess h1 processes=4
#WSGIProcessGroup h1
WSGIScriptAlias /site /var/www/h1/pyh1/h1/wsgi.py
Alias /static /var/www/h1/pyh1/static
<IfModule mod_ssl.c>
SSLEngine off
</IfModule>
<Directory /var/www/h1/h1.ru>
php_admin_flag engine on
php_admin_flag safe_mode off
php_admin_value open_basedir "/var/www/h1/h1.ru:/tmp"
Options -Includes -ExecCGI
</Directory>
</VirtualHost>
wsgi:
#!/usr/local/bin/python2.7
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "h1.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Not true. Normally mod_wsgi will run in multiple separate processes, managing their startup and shutdown automatically. When a new process starts, Python code will be loaded from disk as it is imported, so that process will run with the newest versions of any files. Meanwhile, any processes that were already alive will use the versions they had previously loaded, so will not reflect the changes (unless a little-used code path is activated which leads to importing a so-far-unimported module).
Certainly the only way to be sure you have the latest choose in all processes is to restart Apache.