Ubuntu deploy Django Web in two ways: ubuntudjango

Source: Internet
Author: User
Tags django server

Ubuntu deploy Django Web in two ways: ubuntudjango

1. Use django's built-in Server framework to publish the web

 

The system administrator often needs to remotely log on to the Linux server through SSH or telent, and often runs tasks that take a long time to complete, such as system backup and ftp transmission. Generally, we open a remote terminal window for each of these tasks because they have been executed for too long. You must wait until they are completed. During this period, you cannot close the window or disconnect the connection. Otherwise, the task will be killed and everything will be abandoned. In this case, screen can be used.

Screen to enter a new screen, close the shell window, screen is in the detached state, exit is to exit back to the normal shell window, then close the shell window is in the attached state

When you want to connect to the detached window in the normal window, run the command screen-r to view all the screen and pid. When you want to connect to a specific window, run the command screen-r [pid]. when you want to stop a screen and destroy it, my approach is kill-s 9 [pid]. At this time, the corresponding screen is dead ??? Status, and then execute screen-wipe to clear it.

Reference: http://www.cnblogs.com/mchina/archive/2013/01/30/2880680.html

 

Django comes with Web publishing functions, such as commands

Python manage. py runserver 8080

Its server logs are output directly on the access shell Interface (default) and can be redirected to the log file through certain settings.

However, when we close the shell, it sends a close signal to the django server, causing the server to close. There is a way to solve this problem by using screen.

You only need to create a new screen, and then enter the command python manage. py runserver 0.0.0.0: 8080.

0.0.0.0: Or & cannot be successful.

 

Ii. Use uwsgi + nginx to publish django web

 

Before configuration, let's take a look at the two articles. The first article was completely successful, and the second article was not working.

Http://django-china.cn/topic/101/

Http://django-china.cn/topic/124/

 

By referencing the blogger's statement, nginx is usually used as the front-end of the server. It will receive all WEB requests and manage requests in a unified manner. Nginx handles all static requests by itself (this is NGINX's strength ). NGINX then passes all non-static requests to Django through uwsgi, which is processed by Django to complete a WEB request.

The local components include python2.7, django1.7, nginx1.6, and ubuntu12.04.

Let's talk about the deployment of my django project files.

Cimc2>

Cimc2>

_ Init _. py

Settins. py

Urls. py

Wsgi. py

Hello>

_ Init _. py

Static>

Css

Js

Fonts

Templates

Views. py

Admin. py

Models. py

Manage. py

Note: cimc2 is the main file in the project, hello is the app file, and static stores various html template files that need to be used, js, css, and templates.

 

1. uwsgi and django

In. new django_socket.ini file under the file directory at the same level of py, you can also create a new django_socket.xml file, but also install libxml (command: sudo apt-get install libxml2-dev), because the libxml needs to be compiled before uwsgi compilation, therefore, you must first install libxml and then install uwsgi, so you need to uninstall it first, or even drop the rm folder, and then follow libxml-> uwsgi.

 

However, I failed to use the xml method. For some reason, I configured the django_socket.ini file.

 

For the uwsgi configuration file configuration method, refer:

 

Http://www.cnblogs.com/zhouej/archive/2012/03/25/2379646.html

Http://blog.csdn.net/sasoritattoo/article/details/17187949

 

I did this:

 

[uwsgi]socket=127.0.0.1:8011pidfile=/var/run/nginx.pidmaster=truechmod-socket=777enable-threads=trueworkers=4wsgi-file=django_wsgi.pylogdate=truedaemonize=/home/gugugujiawei/DjangoServer/cimc2/log/django.log

 

Explanations:

[Uwsgi]

Socket ------------- used for communication between uwsgi and nginx

Pidfile ------------ write information to this address when the permission is lost

Master = true

Chmod-socket = 777 --- socket permission for uwsgi to communicate with nginx, Which is 644 on the Internet. I can

Enable-threads ----- multi-thread communication is allowed.

Workers ------------ enable multiple threads. Generally, four threads can be killed at a time during kill.

Wsgi-file ---------- uwsgi and django configuration files

Logdate ------------ added at will

Daemonize ---------- log file. If it is not written, it will be streaking like releasing python on the shell interface.

 

Create a new django_wsgi.py file in the file directory at the same level as manage. py.

#coding:utf-8import osimport sysfrom django.core.wsgi import get_wsgi_applicationreload(sys)sys.setdefaultencoding('utf8') sys.path.append(os.path.abspath(os.path.dirname(__file__)))os.environ.setdefault("DJANGO_SETTINGS_MODULE","cimc2.settings")#from django.core.handlers.wsgi import WSGIHandler#application=WSGIHandler()application=get_wsgi_application()

 

After this is done, we only need to complete uwsgi and django configuration, and the next step is hard work (after a long time)

 

2. Nginx

 

During the deployment of nginx, we found that it has many advantages, such as good planning and maintainability. If various configurations are used well, We can optimize your system for easy maintenance, you can also make good load balancing and system resource allocation.

In the/etc/ngin directory, you can see

 

See n methods and various tests on the Internet. The following test is available:

There is a default file under sites-available, which is used to change the port number, as shown in

 

The configuration file is here, nginx. conf, which can be written here according to the deployment of the previous project file:

 

user root;worker_processes 4;pid /run/nginx.pid; events {worker_connections 768;# multi_accept on;} http { ### Basic Settings## sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;# server_tokens off; # server_names_hash_bucket_size 64;# server_name_in_redirect off; include /etc/nginx/mime.types;default_type application/octet-stream; ### Logging Settings## access_log /var/log/nginx/access.log;error_log /var/log/nginx/error.log; ### Gzip Settings## gzip on;gzip_disable "msie6"; # gzip_vary on;# gzip_proxied any;# gzip_comp_level 6;# gzip_buffers 16 8k;# gzip_http_version 1.1;# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ### Virtual Host Configs## include /etc/nginx/conf.d/*.conf;include /etc/nginx/sites-enabled/*;server{listen 8080;server_name 127.0.0.1;charset utf-8;client_max_body_size 25M;location / {# First attempt to serve request as file, then# as directory, then fall back to displaying a 404.# try_files $uri $uri/ =404;include uwsgi_params;uwsgi_pass 127.0.0.1:8011;access_log off;}location /static{alias /home/gugugujiawei/DjangoServer/cimc2/hello/static;}} }  #mail {## See sample authentication script at:## http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript# ## auth_http localhost/auth.php;## pop3_capabilities "TOP" "USER";## imap_capabilities "IMAP4rev1" "UIDPLUS";# #server {# listen     localhost:110;# protocol   pop3;# proxy      on;#}# #server {# listen     localhost:143;# protocol   imap;# proxy      on;#}#}


Change user to root, and uwsgi_pass corresponds to the socket Configuration Port in ini (Be sure not to be occupied by other programs). Add one more point:

In Html, for example:

<Script src = "/static/js/jquery-1.11.0.js"> </script>

Settings. py is shown in the following figure:

 

And STATIC_URL = '/static/'. I didn't add STATIC_ROOT.

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.