I have done a simple Django Project for university.
Now I want to show it on my own hosted virtual server, like www.myserver.com:8001
I'm using Ubuntu 14.04 with apache2.
In /etc/apache/site-available I have a con file named: uniproject.conf
<VirtualHost *:8001>
DocumentRoot "/var/www/uniproject/"
Servername www.myserver.com:8001
Alias /static /var/www/uniproject/static/
WSGIScriptAlias / /var/www/uniproject/uniproject.wsgi
<Directory /var/www/uniproject/>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
In /etc/apache2/ports.conf I wrote Listen 8001
In /var/www/uniproject/uniproject.wsgi
import os
import sys
sys.path = ['/var/www/uniproject'] + sys.path
os.environ['DJANGO_SETTINGS_MODULE'] = 'uniproject.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
After a2ensite uniproject.conf and service apache2 restart I'm calling www.myserver.com:8001
there comes
Not Found
The requested URL / was not found on this server.
My goal is to have, later on, about running three to five django apps/servers on my site reachable like
www.myserver.com:8001, www.myserver.com:8002, www.myserver.com:8003, etc ...
but what I do wrong?
I'm using Django 1.7.1 without virtualenv
The Apache Log says:
Target WSGI script not found or unable to stat: /var/www/uniproject, referer: http://www.myserver.com:8001/
The file structure in /var/www/uniproject is:
db3.sqlite
uniproject
uniprojectBackend
uniprojectwsgi
manage.py
static
templates
In /var/www/uniproject/uniproject are the default django files when creating a new project (setting.py, urls.py, wsgi.py, __init__.py)
drwxr-xr-x 7 root root 4096 Dec 21 21:50 uniproject
Ok here is how I solved my issue.
I'm now disabling the port stuff and use subdomain instead.
HOWTO: "Deploy various numbers of Django Apps on Apache2 running on Ubuntu 14.04"
First:
cd /etc/apache2/site-available
sudo nano someproject.conf
in someproject.conf:
<VirtualHost *:80>
ServerName someproject.mysite.com
ServerAlias www.someproject.myssite.com
DocumentRoot "/var/www/someproject/"
Alias /static /var/www/someproject/static/
WSGIScriptAlias / /var/www/someproject/wsgi.py
<Directory /var/www/someproject/>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
after that run:
sudo a2ensite someproject.conf
sudo service apache2 restart
Second:
cd /var/www/
django-admin.py startproject someproject
cd someproject
sudo nano wsgi.py
in wsgi.py
import os
import sys
path = '/var/www/someproject'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'someproject.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
now visit: someproject.mysite.com
This is done with django 1.7.1
Make Sure all python modules are also installed (e.g. lxml, feed parser etc.)
Sometimes there will be a bug (happen to me) in settings.py
remove or comment in settings.py:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
#'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
After every edit in the django project run
sudo service apache2 reload
Hope this helps anyone.
With this method I'm running three django server/apps on my VM.
Related
I have moved my django project from development to my production server. After deployment I cannot view static files on my pages.
I know that django does not serve static files once debug is turned off and have tried using whitenoise to serve my static files.
Attempt 1- WhiteNoise
These are the changes that I made to settings.py
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
and wsgi.py:
from whitenoise.django import DjangoWhiteNoise
...
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
and then running collectstatic. However my webpages still were not loading any of the static files.
Attempt 2- Apache- mod_wsgi
With the second attempt, I tried using apache+ mod_wsgi To be specific I connected to my VPS using terminal in cpanel installed apache2 and followed a tutorial.
sudo apt-get install apache2
created my conf file
sudo nano new_config.conf
added this to it
<VirtualHost *:80>
ServerName 127.0.0.1
ServerAlias localhost
Alias /static /var/gradientboostmvp/static/
WSGIScriptAlias / /var/gradientboostmvp/django_school/wsgi.py
<Directory /var/gradientboostmvp/>
Order deny,allow
Allow from all
</Directory>
DocumentRoot /var/gradientboostmvp
</VirtualHost>
enabled the newly created virtual host conf file
sudo a2ensite new_config.conf
sudo /etc/init.d/apache2 restart
added my WSGIPythonPath in apache2.config
WSGIPythonPath /var/gradientboostmvp
saved the changed, but still couldn't load my static files
Attempt 3- Import serve
I also had a similar question that was closed as a duplicate. I tried the solutions offered in the answers
from django.views.static import serve
...
urlpatterns = [
path('', classroom.home, name='home'),
path('about', classroom.about, name='about'),
path('courses', classroom.courses, name='courses'),
path('course_details', classroom.course_details, name='course_details'),
path('static',serve {'document_root':settings.STATIC_ROOT}),
Which then resulted in me getting an error message when I tried visiting my home page
Incomplete response received from application
Well, as you are using Apache to serve django, then why not use it serve static files as well. As per documetation, first you need to use collectstatic to store static files to a specific folder. For example in your settings if you have STATIC_ROOT specified as:
STATIC_ROOT = '/path/to/mysite.com/static/'
Then when you run collect static, the static files will be stored inside /path/to/mysite.com/static/ directory.
Then add that path as alias to apache:
Alias /static/ /path/to/mysite.com/static/
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 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 have a micro instance on EC2 with Django installed there. I've also installed mod-WSGI, PostgreSQL, etc following several tutorials.
Finally, I pulled my project from bitbucket and started Apache on my EC2. Unfortunately, the only thing I have is the default Apache page and I've already spent a day and night reading and trying to figure out what am I doing wrong.
my_project is in /home/ubuntu dir. In it's folder I have wsgi.py file:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
This is my .conf file stored in /etc/apache2/httpd.conf:
Listen 80
NameVirtualHost *:80
WSGIPythonPath /home/ubuntu/my_project/
<VirtualHost *:80>
DocumentRoot /home/ubuntu/my_project
ServerName www.ec2-54-***-104-**.us-west-2.compute.amazonaws.com
ServerAlias ec2-54-***-104-**.us-west-2.compute.amazonaws.com
ErrorLog /home/ubuntu/my_project/apache/error_log
CustomLog /home/ubuntu/my_project/access_log_common
WSGIScriptAlias / /home/ubuntu/my_project/wsgi.py
Alias /static/ /home/ubuntu/my_project/static/
<Directory /home/ubuntu/my_project/static>
Require all granted
</Directory>
<Directory /home/ubuntu/my_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
When I'm trying to
/etc/init.d/apache2 stop
and instead:
python manage.py runserver 0.0.0.0:80
It works fine and I'm able to connect to my app from a browser with IP as URL.
It gives me understanding that the setting.py file is fine (am I right?) and the problem is or in wsgi.py or httpd.conf.
When I stop my development server and start Apache again, I get the default Apache page. Can you help me please to find what's wrong with my files?
Permissions for apache:
ubuntu#ip-172-**-**-69:~$ ls -ld my_project/
drwxr-xr-x 10 www-data www-data 4096 May 26 19:51 project/
You normally shouldn't use an IP address for ServerName, it has to be the hostname (FQDN) that the site is accessed as.
You are missing a WSGIDaemonProcess directive to correspond to your use of WSGIProcessGroup.
It is bad security practice to set DocumentRoot to be where your source code for your application is.
Finally, stuff under a users home directory is not usually accessible to the user that Apache runs as.
Suggest go back and review the mod_wsgi deployment documentation on the Django site again for a start.
Ι have developed a Django project and uploaded it to a cloud VΜ. Currently i have access to it through 8080 port.
python manage.py runserver 0.0.0.0:8080
If i enter the url without the 8080 port, it shows the "it works" page. How can i set my Django project to run by default on 80 port?
I am using Ubuntu 12.04 server
As docs say, runserver isn't meant as a deployment server. It also mentions that you probably can't start it on port 80 unless you run it as root.
Here is the kind of file you will need to put in /etc/apache2/sites-enabled, you need to adjust the paths in it. You also need to load mod_wsgi which you can do via apt-get.
<VirtualHost 192.168.1.14:80>
ServerAdmin youremail#whatever.com
ServerName www.whatever.com
ServerAlias whatever.com
Alias /robots.txt /home/dimitris/Python/mysite/site_media/robots.txt
Alias /favicon.ico /home/dimitris/Python/mysite/site_media/favicon.png
Alias /static/ /home/dimitris/Python/mysite/site_media/static
WSGIDaemonProcess mysite user=dimitris processes=1 threads=5
WSGIProcessGroup mysite
WSGIScriptAlias / /home/dimitris/Python/mysite/deploy/mysqite_wsgi.py
# Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/mysite.error.log
CustomLog ${APACHE_LOG_DIR}/mysite.access.log combined
ServerSignature Off
</VirtualHost>
Most of this assumes you are running in a virtualenv, that means you will need a wsgi file to get things running. The above file sets up apache to run your "wsgi" file which looks something like this:
import os
from os.path import abspath, dirname, join
import sys
with open("/tmp/mysite.sys.path", "w") as f:
for i in sys.path:
f.write(i+"\n")
#redirect sys.stdout to sys.stderr for libraries that use
#print statements for optional import exceptions.
sys.stdout = sys.stderr
sys.path.insert(0, abspath(join(dirname(__file__), "../../")))
sys.path.insert(0, abspath(join(dirname(__file__), "../../lib/python2.7/site-packages/")))
from django.conf import settings
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
mod_wsgi, then opens this one file and executes it. application is the part that waits for requests from the webserver, and the rest behaves just like runserver except it can be multi-process and multi-threaded.
In the terminal while in a virtual environment, run:
sudo ./manage.py runserver 80
You will be asked for your password and it will run the server on port 80.