Linux學習django-CentOS部署本身本地的django項目

前言

本身本地寫好的django項目,如何部署到linux服務器上,讓其餘的小夥伴也能訪問呢?本篇以centos系統爲例,把本地寫好的django項目部署到linux服務器上
環境準備:python

環境準備:
1.一臺Linux服務器, 操做系統: CentOS 7.4 64位
2.python3.6 (前面已經搭建好)
3.django-2.1.4linux

django環境準備

前面已經安裝好了python3.6.8的環境而且pip也配置好了,安裝django直接用pip安裝就能夠了,安裝的django版本位django-2.1.4django

pip install djangocentos

[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項目代碼

linux服務器上的django環境已經準備好了,接下來就是把在本地電腦已經調通的django項目代碼所有複製到服務器上的某個目錄下
以下圖,經過xftp工具把本地項目代碼所有傳到/opt/helloworld目錄下,前提是在本地瀏覽器測試沒問題了。瀏覽器

啓動django

打開到helloworld目錄,啓動服務: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.

因爲這個是搭建在服務器上的,因此只能本機訪問:http://127.0.0.1:8000/,能夠用python代碼驗證下ruby

[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 >>> 

接下來登陸阿里雲ECS後臺-安全組-配置規則-開放8000端口,在瀏覽器上輸入http://47.104.xx.xx:8000/發現沒法訪問服務器

外網訪問django

若是啓動方式是 python manage.py runserver默認只能本機訪問,爲了放開訪問權限,讓其餘人也能訪問到這臺機器,需加參數0.0.0.0:端口session

python manage.py runserver 0.0.0.0:8000app

^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

啓動服務後在瀏覽器輸入:http://47.104.x.x:8000/ ,會報錯‘Invalid HTTP_HOST header: '47.104.x.x:8000'. You may need to add '47.104.x.x' to ALLOWED_HOSTS.’

關閉debug,設置ALLOWED_HOSTS

打開helloworld/settings.py文件,找到這2行代碼

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

關閉debug調試功能,ALLOWED_HOSTS設置爲所有

# SECURITY WARNING: don't run with debug turned on in production! DEBUG = False ALLOWED_HOSTS = ["*"]

修改完成後保存下,重啓django服務

python manage.py runserver 0.0.0.0:8000

接着在本地瀏覽器上訪問,就能正常的打開頁面了

到這裏一個簡單的django的demo項目就已經成功的部署到linux服務器上了,因而就能夠通知你的小夥伴看你作的網站咯~