问题
I am trying to deploy my first Django project on my webserver. I am both new to server configuration and Django so I have a hard time finding my errors.
In most tutorials I have seen online, the Django project on the production server is created in /var/www/myproject - however, the Django documentation recommends putting the code outside of /var/www/ for security reasons (see: https://docs.djangoproject.com/en/1.9/intro/tutorial01/).
So, I put my Django project in a subdirectory of my home folder, which is not accessible to Apache. I added no file to /var/www as no tutorial mentions that this would be necessary.
After setting up Django with mod_wsgi according to these tutorials:
(https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/modwsgi/ ,
https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04 ,
http://tutorial.djangogirls.org/en/deploy/)
when I access my webserver via browser all I see is the default index.html site.
Running and accessing the development server of Django from my webserver does not work either. In both cases, when I try to access the admin page I get a standard 404 Error.
Am I supposed to put some files into /var/www/ after all?
Could it be an issue that I use a Let'sEncrypt certificate and during the setup with auto-certbot chose to automatically redirect http to https?
Some of my configured files (I left out parts that I didn't change/have nothing to do with Django):
/etc/apache2/apache2.conf
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
/etc/apache2/sites-available/000-default.conf
DocumentRoot /var/www/html
Alias /static /home/me/django/mysite/app/static
<Directory /home/me/django/mysite/app/static>
Require all granted
</Directory>
<Directory /home/me/django/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess mysite python-path=/home/me/django/mysite:/home/me/django/djangoenv/lib/python2.7/site-packages
WSGIProcessGroup mysite
WSGIScriptAlias / /home/me/django/mysite/mysite/wsgi.py
/home/me/django/mysite/mysite/wsgi.py
import os
import sys
path = '/home/sirhys/django/mysite'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
from django.core.wsgi import get_wsgi_application
from django.contrib.staticfiles.handlers import StaticFilesHandler
application = StaticFilesHandler(get_wsgi_application())
/home/me/django/mysite/mysite/settings.py
DEBUG = False
ALLOWED_HOSTS = [
'www.mysite.com',
'mysite.com'
]
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATIC_ROOT = os.path.join(SITE_ROOT, '..', 'app/static')
STATIC_URL = '/static/'
Thank you!
Edit:
In the end, I found the issue to be a conflict between the different out-of-the-box config files I have on my server. Apparently, the Let's Encrypt SSL config file was overriding all other files in /etc/apache2/sites-available. When I put the WSGI-config lines there, it worked like a charm.
I have my code in my home directory now, and it is working. I decided to put my static files into /var/www/static via collectstatic
. This is working for me but it seems the exact location of code and static files is really irrelevant.
回答1:
Your config only for one website, need use virtual host
Example deploy:
For python3 install mod_wsgi (debian based system)
apt-get install libapache2-mod-wsgi-py3
Put your project in standard apache location
/var/www/myproj
where myproj = name you django project
Check project
python3 manage.py check
or/and run it
python3 manage.py runserver 0.0.0.0:8000
if your domain example.com, then http://example.com:8000/ or use server IP.
Allow apache2 read and write content
chown www-data:www-data /var/www/myproj -R
Config apache 2.4
cd /etc/apache2/sites-available
editor myproj.conf
and Apache2.4 minimal config (many sites)
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/myproj
WSGIDaemonProcess example.com python-path=/var/www/myproj
WSGIScriptAlias / /var/www/myproj/myproj/wsgi.py \
process-group=example.com application-group=%{GLOBAL}
<Directory /var/www/myproj/myproj>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
# static
Alias /media/ /var/www/myproj/media/
Alias /static/ /var/www/myproj/static/
Alias /robots.txt /var/www/myproj/robots.txt
# logs
ErrorLog /var/log/apache2/myproj-error.log
LogLevel warn
CustomLog /var/log/apache2/myproj-access.log combined
</VirtualHost>
Virtualenv: python path + virtual, example
python-path=/path/www/myproj python-home=/path/to/venv
Using daemon mode exclusively, add to /etc/apache2/mods-available/wsgi.load
WSGIRestrictEmbedded On
Enable config
a2ensite myproj.conf
Check syntax
apachectl -t
Restart apache or reload
service apache2 restart
Example config apache2.4 with define variable. Django > 1.6. Set only domain, path, name project. In example path to dir /var/www/example.com/myproject/myproect/wsgi.py, static url /media/ and /static/
Define:
- domain - your domain name (example.com)
- path - full path to project, folder where manage.py (/var/www/example.com/myproj)
- project - name project, folder name where wsgi.py (myproj)
https://gist.github.com/leotop/9a78c10c0961cdd2bbe9633d6ebf8a5b
<VirtualHost *:80>
Define domain example.com
Define path /var/www/${domain}/myproj
Define project myproj
ServerName ${domain}
ServerAlias www.${domain}
DocumentRoot ${path}
WSGIDaemonProcess ${domain} python-path=${path}
WSGIScriptAlias / ${path}/${project}/wsgi.py \
process-group=${domain} application-group=%{GLOBAL}
<Directory ${path}/${project}>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /media/ ${path}/media/
Alias /static/ ${path}/static/
Alias /robots.txt ${path}/static/robots.txt
Alias /favicon.ico ${path}/static/favicon.ico
<Location "/media/">
Options -Indexes
</Location>
<Location "/static/">
Options -Indexes
</Location>
ErrorLog /var/log/apache2/${domain}-error.log
LogLevel warn
CustomLog /var/log/apache2/${domain}-access.log combined
</VirtualHost>
来源:https://stackoverflow.com/questions/38383407/deploying-django-on-apache2-with-mod-wsgi-correct-location-for-django-project