Linux learning 8-CentOS deploys its own local django project

Posted by ChemicalBliss on Mon, 07 Feb 2022 21:37:30 +0100

preface

How to deploy the django project written locally to the linux server so that other small partners can access it? Taking centos system as an example, this paper deploys the locally written django project to the linux server
Environmental preparation:

Environmental preparation:
1. One Linux server, operating system: CentOS 7.4 64 bit
2.python3.6 (the front has been built)
3.django-2.1.4

django environment preparation

Python 3.0 has already been installed 6.8 environment and pip are also configured. You can install Django directly with PIP. The installed version of Django is django-2.1.4

pip install django

[root@yoyo ~]# pip -V
pip 18.1 from /usr/local/python3/lib/python3.6/site-packages/pip (python 3.6)
[root@yoyo ~]# pip install django
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting django
  Downloading http://mirrors.aliyun.com/pypi/packages/fd/9a/0c028ea0fe4f5803dda1a7afabeed958d0c8b79b0fe762ffbf728db3b90d/Django-2.1.4-py3-none-any.whl (7.3MB)
    100% |████████████████████████████████| 7.3MB 4.8MB/s 
Collecting pytz (from django)
  Downloading http://mirrors.aliyun.com/pypi/packages/f8/0e/2365ddc010afb3d79147f1dd544e5ee24bf4ece58ab99b16fbb465ce6dc0/pytz-2018.7-py2.py3-none-any.whl (506kB)
    100% |████████████████████████████████| 512kB 60.9MB/s 
Installing collected packages: pytz, django
Successfully installed django-2.1.4 pytz-2018.7

django project code

The django environment on the linux server is ready. The next step is to copy all the django project codes that have been transferred on the local computer to a directory on the server
As shown in the figure below, transfer all the local project codes to / opt/helloworld directory through xftp tool, provided that there is no problem in the local browser test.

Start django

Open the helloworld directory and start the service: Python manage py runserver

[root@yoyo ~]# cd /opt/helloworld/
[root@yoyo helloworld]# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, hello, sessions.
Run 'python manage.py migrate' to apply them.

January 04, 2019 - 08:31:40
Django version 2.1.4, using settings 'helloworld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Since this is built on the server, it can only be accessed locally: http://127.0.0.1:8000/ , you can verify it with python code

[root@yoyo ~]# python
Python 3.6.8 (default, Jan  2 2019, 16:43:17) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> r = requests.get("http://127.0.0.1:8000/")
>>> r.status
>>> r.status_code
200
>>> 

Next, log in to Alibaba cloud ECS background - Security Group - configuration rules - Open 8000 port and enter it in the browser http://47.104.xx.xx:8000/ Found inaccessible

Internet access django

If the startup method is Python manage Py runserver can only be accessed locally by default. In order to release the access permission and allow others to access this machine, you need to add the parameter 0.0.0.0: port

python manage.py runserver 0.0.0.0:8000

^C[root@yoyo helloworld]# python manage.py runserver 0.0.0.0:8000
Performing system checks...

System check identified no issues (0 silenced).
January 04, 2019 - 09:57:15
Django version 2.1.4, using settings 'helloworld.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
Invalid HTTP_HOST header: '47.104.x.x:8000'. You may need to add '47.104.x.x' to ALLOWED_HOSTS.
Bad Request: /
[04/Jan/2019 09:57:20] "GET / HTTP/1.1" 400 59588

After starting the service, enter in the browser: http://47.104.x.x:8000/ , an error 'invalid HTTP' will be reported_ HOST header: ‘47.104. x.x:8000’. You may need to add ‘47.104. x.x’ to ALLOWED_HOSTS.’

Close debug and set ALLOWED_HOSTS

Open HelloWorld / settings Py file, find these 2 lines of code

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

Close the debug function and allow_ Hosts set to all

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ["*"]

After modification, save it and restart django service

python manage.py runserver 0.0.0.0:8000

Then visit it on the local browser and you can open the page normally

Here, a simple django demo project has been successfully deployed to the linux server, so you can inform your friends to see your website~

Communication QQ group: 779429633

Topics: Linux