Ubuntu Python apache2 wsgi Django Framework

Source: Internet
Author: User

deploy Django on Ubuntu via APATCH2 and Wsgi

(Have done it!!!) )

One, my Python, Django, APATCH2 version:

Python:python-v

2.7.3

Django:python-c "Import Django; Print (Django. VERSION) "

(1, 9, 4, ' final ', 0)

Apache2:/usr/sbin/apachectl-v

Server version:apache/2.2.22 (Ubuntu)
Server Built:jul 24 2015 17:25:52

Two, install Python and Django.

Three, install Apache2 and WSGI.

sudo apt-get insall apache2

sudo apt-get install Libapache2-mod-wsgi

If the previously installed configuration is apache2 and the configuration is messy and cannot be repaired, it is recommended that you uninstall it completely before installing it. Commands for full uninstallation:

sudo apt-get--purge Remove apache*

sudo apt-get--purge Remove Apache-common

After installation, go to/etc/apache2/under the mods-available and mods-enabled to see if there are wsgi.conf and wsgi.load two files. There is an indication that the WSGI module was loaded to apache2 successfully.

Quad, configure Apache and WSGI

(PS assumes you are under directory/var/www/mysite django-admin.py startproject testproject)

The Tree of my project:

1. Apache Configuration:

Create a new file in/etc/apache2/site-available testdjango.conf (PS Chinese comments are deleted)

<VirtualHost*:80>ServerName testdjango.com #ServerName这个无所谓, just map your IP address to this on the host. Do not want to change host the simplest way is to use sudo a2dissite 000-default.conf and other virtual host to disable off, leaving only testdjango.conf.    (The file in ps.site-enabled is the file under Link Site-availabel, if there is a link file is enable, can be viewed according to this) Documentroot/var/www/mysite/testproject #这一条是指向网站的根目录<Directory/var/www/mysite/testproject>Order Allow,deny allow from all</Directory>wsgidaemonprocess testdjango.com processes=2 threads=15 display-name=%{group} wsgiprocessgroup testdjango.com #对应前面的ServerName
Wsgiscriptalias//var/www/mysite/testproject/apache/django.wsgi  #将后面的那个路径指向网站的根目录. The first "/" represents the root directory of the site when it is accessed externally, and when there is a new requset, Apache starts parsing from here.

</ VirtualHost >

2, the WSGI configuration

Create a new Dir:apache under/var/www/mysite/testproject/. Create a new file Django.wsgi under./apache. (The file name corresponds to the file name in the previous Wsgiscriptalias second path)

#coding: Utf-8
ImportOSImportSyspath='/var/www/mysite'ifPath not inchsys.path:sys.path.insert (0,'/var/www/mysite/testproject') #将网站工程目录临时加入ubuntu系统环境中os. environ['Django_settings_module'] ='testproject.settings'#新建一个环境变量DJANGO_SETTINGS_MODULE, the goal is to point to the settings.py of the Django Project, where
# Import Django.core.handlers.wsgi #old version use
#application = Django.core.handlers.wsgi.WSGIHandler ()

from Import
= Get_wsgi_application ()

Five, verify

sudo a2ensite testdjango.conf #enable testjango.conf

sudo service apache2 reload (PS if reload appears apache2:could not reliably determine the server ' s fully qualified domain nam E, using 127.0.1.1 for ServerName. Although not affected but there is a warning annoying, you can add servername localhost in/etc/apatch2/httpd.conf)

The initial page of Django can be seen in the last browser http://127.0.0.1 or localhost or your ubunt IP.

Other machine browsers can also view your Django project's page through your Ubuntu IP.

Six, common errors and logs:

Apache2 error log file:/var/log/apache2/error.log

Error 1:internal Server error as

It should have been the method of Django.wsgi with my annotated django.core.handlers.wsgi. With the get_wsgi_application below, there's no problem.

Ubuntu Python apache2 wsgi Django Framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.