Baidu cloud + ubuntu+django+nginx+uwsgi project deployment

 

Install nginx

sudo  apt-get install nginx

start nginx

sudo  /etc/inin.d/nginx start

In browser access, the public IP address of the ECS

Open the browser to access xx.xx.xxx.xxx:80, where xxxx is your public IP address and 80 is the default listening port number of Nginx

Appear

This indicates that Nginx has been successfully installed and running

 

Project running test

We need to make sure that our projects can run on the deployed servers

 

Upload project to server

If not, please see

ubuntu remote link server, upload files

After uploading, we need to import the data of the database,

It is recommended to export the previous database and upload it to the server before importing it

 

After data import, we need to install the libraries that the project depends on

First export the libraries that need to be installed

In the environment of project dependence

pip freeze >requirements.txt

The requirements.txt file will be in the path where you execute the command.

Then install on the server

cd to the directory where you store requirement.txt, run

pip install -r requirements.txt

Wait for the dependency installation to complete, cd to the project directory, and run

python manage.py runserver 0.0.0.0:8000

Open browser, enter server address, view

xx.xx.xxx.xxx:8000 (XXX is the public IP address of the server)

The project is running normally (if the DEBUG in the settings.py file is set to False, it will be observed that the static resource cannot be loaded)

 

Because when DEBUG=True, django will automatically proxy static files. When it changes to False, django thinks that you will not proxy static files for you in the production environment. Here we can configure it in the url to let django continue to proxy, but we will not. We use nginx to proxy static files

 

Nginx agent
Configure Nginx in the project

Add a config folder in the project directory (you can do whatever you want), and the following configuration files will all be put in the folder uniformly (the inconsistent storage path of configuration files will cause unsuccessful operation of the input prompt)

 

In the c onfig file, create a new my nginx.conf file. The contents are as follows

# my_nginx.conf


upstream django {
    
    server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name xx.xx.xxx.xxx; # substitute your machine's IP address or FQDN
    charset     utf-8;

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

    # Django media
    location /media  {
        
        alias /media; # your Django media 
    }

    location /static {
       
        alias /homm/work/Blog/static; # your Django project's static files 
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     uwsgi_params; # the uwsgi_params file you installed
    }
}

Establish file link

sudo ln -s  my_nginx.conf  /etc/nginx/site-enabled/

Deploy static files

Before running Nginx, you need to put all the static files in the django project into the static folder,

Function

python manage.py collectstatic

 

Start (restart) Nginx

sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx restart

If startup fails

nginx -t

View specific errors

 

Simple test

Drop a picture into the media/image folder of the server project (take login.jpg as an example here)
Visit

xx.xx.xxx.xxx:8000/media/image/login.jpg

 

 

The normal display indicates that the configuration is successful

 

Install uwsgi and configure

install

pip install uwsgi

Start automatically after installation

Check whether to start by the following command

ps aux|grep uwsgi

Already started

Test uwsgi

Under the project root

uwsgi --http :8000 --module myproject.wsgi

Replace myproject with its own project name

Access xx.xx.xxx.xxx:8000 after startup

If Internal Server Error appears in browser

  1. Check that the command prompt is entered correctly, (note the space)
  2. Check whether to run under the project root directory
  3. In other cases, check the command line output or log

 

 

Configure uwsgi

#uwsgi.ini file
[uwsgi]

#Project path
chdir           = /homm/work/Blog
# Location of Django's wsgi
module          = Blog.wsgi
#perhaps
#module          = Blog.wsgi:application


# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = 1270.0.0.1:8000  #It needs to be consistent with the port configured by nginx
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true

logto           = %(chdir)/config/Blog.log
stats           = %(chdir)/config/uwsgi.status           

pidfile         = %(chdir)/config/uwsgi.pid

 

Replace the corresponding configuration with your own path~

After saving, run (under the uwsgi.ini directory)

uwsgi --ini uwsgi.ini

Open a browser to access xx.xx.xxx.xxx:80

Because the browser will access port 80 by default

You can also

xx.xx.xxx.xxx

If nothing goes wrong, the project will be able to run.

 

 

Welcome to my

WeChat public address: python learning column

Exchange and study together

Join: python exchange and learning group, (827458740)

 

Extended reading

Error reported when using sudo / etc / init.d/nginix start

No translation files found for default language zh_hans

uwsgi --http: 8000 --module XXXX.wsgi error, pymysql not found

ubuntu remote link server, upload files

 

 

37 original articles published, 15 praised, 20000 visitors+
Private letter follow

Tags: Nginx Django sudo Python

Posted on Fri, 07 Feb 2020 07:59:54 -0800 by php_wiz_kid