Deploy the Django project using the Nginx + UWSGI socket

Source: Internet
Author: User
Tags django server nginx server
First, preparatory work

1. Create a space on your own server or virtual machine (this case uses Ubuntu's Aliyun server)

mkvirtualenv [space name]
* * View all space * *
Workon * * Enter a space * *
Workon space Name
2, upload the code to the server

3, run the project to see will be normal?

Python manage.py runserver

4, turn off the debug mode (under the project's settings.py file)

DEBUG = False

5, modify allow access to all places

**settings.py file * *
allowed_hosts = [' * ']

6. Implement a 404.html and 500.html template and put it in the Templates folder that Django can identify.

If you create a project with Pycharm, you have the templates directory on the outside.

7, run the command to try to run down

Python manage.py runserver 0.0.0.0:8000

8, the local browser access to the second, using Nginx + UWSGI socket to deploy Django

Perhaps you will be puzzled, according to the way above can directly run the project, why also want to engage in a Nginx + UWSGI socket, in fact, the above is only a test environment, can not be used for real project deployment on the third, installation tools

1. Install Nginx Server

sudo apt-get install Nginx

2, install Uwsgi (belong to Python package)

sudo pip install Uwsgi
test the success of the Nginx and USWGI that have just been installed

1, start Nginx and stop Nginx command

sudo service nginx start # starts Nginx
sudo service nginx stop # closes nginx
sudo/etc/init.d/nginx restart #重启

2, directly enter the Web site to see if there is nginx welcome page

3. Create a test.py file test in just the project USWGI

DEF application (env, start_response):
    start_response (' OK ', [(' Content-type ', ' text/html ')]
    

4, run the command

Uwsgi--http:8000--wsgi-file test.py

5, browser input URL access successfully show Hello Word indicates installation success

6. Use Uwsgi to test your uploaded items

Uwsgi--http:8000--module project name. Wsgi
v. Supplementary Notes

1, the port number is occupied by the processing method

Lsof-i: 9000 # View Port No. 9000
(Djangodemo) root@iz941w016mwz:/home/djangodemo/test02# lsof-i: 9000
COMMAND   PID   USER   FD   TYPE  DEVICE size/off NODE NAME
nginx   21572   root    7u  IPv4 2549884      0t0  TCP *:9000 (LISTEN)
nginx   21573 Nobody    7u  IPv4 2549884      0t0  tcp *:9000 (LISTEN)
Nginx   21574 Nobody    7u  IPv4 2549884      0t0  TCP *:9000 (LISTEN)
nginx   21575 Nobody    7u  IPv4 2549884      0t0  TCP *:9000 (LISTEN)
nginx 21576   Nobody    7u  IPv4      2549884 0t0  TCP *:9000 (LISTEN)

# Kill process release Port

sudo kill-9 21572 21573 21574 21575 21576

2, view the process of a program

PS aux | grep program name (NGINX/MYSQL)
VI. deployment of Projects1, download Uwsgi_param directly into the project can be

2, in the project directory to create a project name _nginx.conf configuration file

# Configure UWSGI Access

upstream Django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001;  # for a Web port socket (we'll use this i)


# Configure Nginx request

server {
    # The port your site would be served On
    listen      8000; # port OK
    # The domain name it will serve for
    server_name 1**.**.156.230; # domain name or IP address
    c Harset     Utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Media storage path
    location/media  {
        alias/home/djangodemo/test02/media;  # The media store path in your project (create one media in this project, use PWD to view the current location)
    }

    location/static {
        alias/home/djangodemo/test02/ Static # Storage path for static files in your project

    # Finally, send all Non-media requests to the Django server.
    Location/{
        uwsgi_pass  Django;
        Include     /home/djangodemo/test02/uwsgi_params; # The Uwsgi_params path you've stored in the previous step
    }
}

3, collect all the static files

1, in the setting.py configuration

Static_root = Os.path.join (Base_dir, "static/")

2. The run command automatically creates a static file under the project

Python manage.py collectstatic

4. Create a soft connection map the project name _nginx.conf file generated in the second step to the Nginx configuration file

sudo ln-s current directory/project name _nginx.conf/etc/nginx/conf.d/

#本人的


#sudo ln-s/home/djangodemo/test02/test02_nginx.conf /etc/nginx/conf.d

5, restart Nginx

Sudo/etc/init.d/nginx restart

6, restart Uwsgi (or test just created the soft connection)

Uwsgi--socket:8001--wsgi-file test.py

7, in the browser access to 8000 ports to see the Hello Word can be shown (no repeat 4-6 steps)

8, start just that project

Uwsgi--socket:8001--module Test02.wsgi

# Specify a static file path Uwsgi--http 0.0.0.0:8000--module test02.wsgi 

=/stat=static

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.