Before writing the Django deployed to the XAMPP time, but there are bug, turned apache logs can not be found import _ssl
, then I will anger mounted directly on the apache2
Configuration and about this article almost
Install the necessary packages
sudo apt-get update
sudo apt-get install python-pip apache2 libapache2-mod-wsgi
And also to ensure WSGI module in operation:
sudo a2enmod wsgi
Configuring Python virtual environment
In order not to modify the system python, python and in fact I suspect xampp library system may have to be resolved not on this
installation tools:
sudo pip install virtualenv
Into our project
catalog, the new environment:
virtualenv myprojectenv
Enter the virtual environment to install python package:
source myprojectenv/bin/activate
Then this should be the president this:
(myprojectenv)user@host:~/myproject$
Installation django
anddjango rest_framework
pip install django==1.8
pip install djangorestframework==3.6.4
Note: The
django2
start python2 not support, so you want to install a little low, but if djangorestframework not install it if there is no match on low
Modify the apache configuration file/etc/apache2/sites-available/000-default.conf
Modify the path Django project, this time we use daemon mode, the main need to retain three lines:
WSGIDaemonProcess
, ,WSGIProcessGroup
andWSGIScriptAlias
`` `SH
# This is a static configuration file
Alias / demos / todolist / static / / root / web / demos / todolist / work / static /
#Order deny,allow
#Allow from all
Require all granted
<Files wsgi.py>
Require all granted
python-home=/root/web/demos/todolist/tdlenv python-path=/root/web/demos/todolist
WSGIProcessGroup
WSGIScriptAlias /demos/todolist /root/web/demos/todolist/todolist/wsgi.py process-group=
一个Django项目的结构一般是这样的:
- ProjectName
- ProjectName
---- wsgi.py
- AppName
---- static
`` `
Note that there are two names in the same folder, do not confuse
static configuration file: - The first acts of
Alias <URL Path> <FILE PATH>
<URL Path>
meaning and above, from thishttp://hostname/<URL PATH>/
is to visit the static folder<FILE PATH>
path - The second line
<Directory <FILE PATH> >
path, fill or static folder
- ProjectName
Modify Django code
wsgi.py
, Generated by directsetting.py
In order to put on a production environment, you can put off DEBUG, while ALLOWED_HOST have to adapt to the outside world can access:
# ALLOWED_HOSTS = [ '*'] # is not recommended to write this
`` `Python
# SECURITY the WARNING: do not RUN with Debug turned ON in Production's!
DEBUG = False
ALLOWED_HOSTS = [ 'localhost', ' 127.0.0.1', 'your ip', 'your hostname'] # generally written four
`` `Static routes need to modify
`` `Python
# Static Files (CSS, JavaScript, ImagesRF Royalty Free)
# https://docs.djangoproject.com/en/1.11/howto/static-files/STATIC_URL = '/demos/todolist/static/'
``主要是要与
Alias里的
`Consistent
Debugging in prod
There are a lot of time on the local server, to have a bug on the server, but do not know where the bug, this time need to print the necessary information.
The easiest way is to print the information stderr
, so wsgi
the log will be recorded in Apache
the error_log
years:
import sys
print >> sys.stderr, message
If you do not see it, the apache
log level to increase info
to print out the log so long:
About permissions configuration Sqlite3
This problem has relatively wide case, translated as follows, for db.sqlite3
databases:
- To ensure that Apache can write the parent directory database
- Ensure the full path to the database folder does not start with a number
- Did not ensure the full path to the
db
folder - Ensure that the
/tmp
folder can write on three levels - Make sure
settings.py
to write the full path - No special characters on the path to ensure
- In WINDOWS, the path to write double backslash
At last! ! ! ! ! ! !
Restart Apache! ! ! ! ! ! ! !
Each time you modify the source code python have to restart! ! ! ! ! Otherwise WSGI will use the original code! !