Deploy Django on Alibaba Cloud ECs Server

Source: Internet
Author: User
Tags django server

Reference


The centos system is installed on the server.

Uwsgi is installed using Pip.

Nginx is installed using Yum install nginx.

Python 2.7 and MySQL 5.5 are installed using yum.


The logical relationship between them is as follows:


the web client <-> the web server <-> the socket <-> uwsgi <-> Django

Uswgi is responsible for retrieving content from Django, transmitting it to Web server such as nginx through socket, and finally displaying it in a web browser.


In a Django project, you do not need to write a series of options after uswgi to create the file uswgi. ini.


# uwsgi.ini file[uwsgi]# Django-related settings# the base directory (full path)chdir           = /var/www/html/# Django's wsgi filemodule          = app.wsgi:application# process-related settings# mastermaster          = true# maximum number of worker processesprocesses       = 10# the socket (use the full path to be safe#socket          = 127.0.0.1:8001socket = /tmp/site.sock# ... with appropriate permissions - may be neededchmod-socket    = 666# clear environment on exitvacuum          = trueprocess= 4threads= 2

# Django's wsgi file corresponds to your own Django project.

Chdir is the directory where Django is located, which is the same as manage. py.

Others can be default.


Create nginx. conf


# nginx.conf# the upstream component nginx needs to connect toupstream django {    server unix:///tmp/site.sock; # for a file socket    #server 127.0.0.1:8001; # for a web port socket (we'll use this first)}# configuration of the serverserver {    # the port your site will be served on    listen      80;    # the domain name it will serve for    server_name demo.mmm.com; # substitute your machine's IP address or FQDN    charset     utf-8;    # max upload size    client_max_body_size 128M;   # adjust to taste    # Django media    location /media  {        alias /var/www/html/media;  # your Django project's media files - amend as required    }    location /static {        alias /var/www/html/static; # your Django project's static files - amend as required    }    # Finally, send all non-media requests to the Django server.    location / {        uwsgi_pass  django;        include     /var/www/html/uwsgi_params; # the uwsgi_params file you installed    }}


In uwsgi_pass Django;, Django corresponds to upstream Django.

The two socket names must be the same. In uwsgi, you need to change the sock permission to 666. The default value is 664, and nginx cannot be connected. in/var/log/nginx/error. log, you can see that connect is denied.


It is said that the use of socket is better than the port, pay attention to Unix: // This prefix, with the sock path behind it, it is 3 //, it looks bad.

Whether socket or TCP port is used, the socket of uwsgi must correspond to the server value of nginx; otherwise, the path cannot be connected.


SERVER_NAME demo.mmm.com; when reading the article, consider SERVER_NAME as a domain name and modify it. As a result, nginx fails to be started. You can use a domain name or IP address.

Ln-S/var/www/html/nginx. CONF/etc/nginx/CONF. d/

After the connection, nginx. conf created under Django is displayed in the conf. d configuration Directory, which is more convenient.


The uwsgi_params file is available under/etc/nginx. If a foreigner copies the file to the Django directory, he does not know what the difference will be when using it directly.


Finally:

Use chkconfig nginx on to set nginx as a self-starting service.

Add a uwsgi/var/www/html/uwsgi. ini -- uid WWW -- gid www to/etc/rc. Local.


I did not add UID and gid. Running uwsgi as root will be warned.


It was originally intended to use Apache, so there is a/var/www/html directory. Mod-Python does not know how to handle the error.

The system comes with python2.6, and mod-python is 2.6 of the call.


Nginx outputs the default nginx page when it cannot obtain data from uwsgi. The Bad Gateway prompt is also displayed.


The biggest problem in Linux is that the program and configuration files are scattered and a program is installed without knowing where it is.

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.