How to deploy Django and run it

nginx+uwsgi+Django installation

1. Introduction

linux edition Centos7.5
uwsgi-2.0.20
Django-3.2.9
python3.9.0
nginx-1.16.0

2. Install nginx

For the connection of installing nginx, please refer to this: https://blog.csdn.net/weixin_44217786/article/details/121608813

3. Install uwsgi

Download the official website: https://uwsgi-docs.readthedocs.io/en/latest/Download.html
Note that the recently released stable version is selected. There may be problems with previous versions. I understand it here. I don't understand. There's a pit waiting for you!

Then install directly:

python3 setup.py install

be careful! be careful! Be careful! If you use a virtual environment, you must use the Python interpreter of the virtual environment to install uWSGI!!!
To install uWSGI, refer to: https://www.runoob.com/python3/python-uwsgi.html
Try running uwsgi after installation

This shows that success is not far away.
If:

[uwsgi: command not found]

A soft connection needs to be established

ln -s /usr/local/python3.7.0/bin/uwsgi /usr/bin/uwsgi
`/usr/local/python3.7.0/bin/uwsgi  ---This is the installation python Create a path according to your actual directory`

At this time, enter the directory and create a file of foobar.py

Just add the following lines of code

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
	return [b"Hello World"]

Then start directly:

uwsgi --http :9090 --wsgi-file foobar.py

At this time, if you use the ip + port to access and print out Hello World, the uwsgi installation is successful.

4. Install Django

First, install python. The version of Python 3.9.0 is used here,
For installation and connection, refer to: https://blog.csdn.net/weixin_44217786/article/details/121614825
Download the source package: https://www.djangoproject.com/download/

tar xzvf Django-3.2.9.tar.gz    # Unzip the download package
cd Django-3.2.9                # Enter Django directory
python setup.py install       # Execute installation command

Then use python to see if django was successfully installed

Isn't it exciting to get here, ready to deploy our Django project, and then we start creating a project

Django-admin startproject apptest

so what? Errors are reported as follows:

DjangoTraceback (most recent call last):
	  File "/usr/local/python3/lib/python3.9/site-packages/django/bin/django-admin.py", line 2, in <module>
	    from django.core import management
	ImportError: No module named django.core

At this time, it is clear that I have installed Django, and I can check it with python. Why are the errors reported? The problems here may only appear when you have just learned to deploy! I just got in touch with the configuration. Then infer that we can't find the command to install Django, so add a soft connection to it.

ln -s /usr/local/python3/bin/django-admin /usr/local/bin/django-admin
ln -s /usr/local/python3/bin/django-admin.py /usr/local/bin/django-admin.py

Tip: pay special attention to the addition of soft connections, otherwise there will be many problems later. Then continue to use Django to create app s, and there is a problem again. It's really one wave after another. It collapsed.

python manage.py startapp apptest

Wrong report????

Django-django.core.exceptions.ImproperlyConfigured: SQLite 3.9.0 or later is required (found 3.7.17)

That means our version is too low. Just upgrade it.

Download source code: wget https://www.sqlite.org/2021/sqlite-autoconf-3370000.tar.gz
	#Decompress and compile
	Tar -zxvf  sqlite-autoconf-3290000.tar.gz
	./configure --prefix=/usr/local
	Make && make install
	#Replace previous versions
	mv /usr/bin/sqlite3 /usr/bin/sqlite3_old 
	ln -s /usr/local/bin/sqlite3 /usr/bin/sqlite3
	#Configuration environment
	vim ~/.bashrc 
	export LD_LIBRARY_PATH="/usr/local/lib" 
	source ~/.bashrc  #Effective immediately

Check whether the version is changed to the latest installed version.
Then it's created, and then we'll try it

 python manage.py runserver 0.0.0.0:8000


Then the problem appears again. It's really speechless, and then continue to solve it!!
You can refer to this connection for processing: https://www.jianshu.com/p/e81d59ccb80d

There is a problem here. I don't know if someone has encountered it. Although this error will occur, it will not affect the deployment of the project. Then I directly ignored this place and did not deal with this problem.

5. Configure nginx and upload project package

Configure nginx

http {
  
    server {
        listen       80;
     
        listen [::]:80;
        server_name  #Fill in your own internet access ip address;
        location / {
            include   uwsgi_params;
            uwsgi_pass 127.0.0.1:8000;
        }
}

Here, we use uwsgi to start port 8000. We map it to port 80 for access
Note that the punctuation mark inside is the end of the semicolon, and it is best to use it after configuration
. / nginx -t test the configuration file for problems

[root@VM-0-17-centos sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

With this successful logo, you can start nginx safely.
Next, upload the project package to the folder you defined, and then go into the project root directory to create a. ini startup file

[uwsgi]
chdir=/root/django-project/apptest #Project file location
module=apptest.wsgi:application    #Start the application that needs to be pointed to
socket=127.0.0.1:8000            #Address and port of this machine
master=true
daemonize = /root/django-project/apptest/run.log   #Log of project operation
disable-logging = true

Then save the startup

uwsgi uwsgi.ini

Then access the ip address and the path you set

It's a success here.

6. Summary:

1. When installing Python, pay attention to replacing the Python version of the system.
2. For Django installation, you must create a soft connection, otherwise you cannot use Django admin
3. The most important thing is that you are unlucky when you encounter problems between versions.

Keywords: Python Linux Django Nginx server

Added by AXiSS on Wed, 01 Dec 2021 10:50:02 +0200