Django deploy the project to the Apache server

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 projectcatalog, 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 djangoanddjango 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

  1. Modify the path Django project, this time we use daemon mode, the main need to retain three lines: WSGIDaemonProcess, , WSGIProcessGroupand WSGIScriptAlias
    `` `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

    WSGIDaemonProcess 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 this http://hostname/<URL PATH>/is to visit the static folder <FILE PATH>path
    • The second line <Directory <FILE PATH> >path, fill or static folder

Modify Django code

  1. wsgi.py, Generated by direct
  2. setting.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:
      `` `Python
      # SECURITY the WARNING: do not RUN with Debug turned ON in Production's!
      DEBUG = False

      # ALLOWED_HOSTS = [ '*'] # is not recommended to write this
      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 wsgithe log will be recorded in Apachethe error_logyears:

import sys
print >> sys.stderr, message

If you do not see it, the apachelog level to increase infoto print out the log so long:

About permissions configuration Sqlite3

This problem has relatively wide case, translated as follows, for db.sqlite3databases:

  1. To ensure that Apache can write the parent directory database
  2. Ensure the full path to the database folder does not start with a number
  3. Did not ensure the full path to the dbfolder
  4. Ensure that the /tmpfolder can write on three levels
  5. Make sure settings.pyto write the full path
  6. No special characters on the path to ensure
  7. 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! !

Guess you like

Origin www.cnblogs.com/milliele/p/11078971.html