前言
自己本地寫好的django項目,如何部署到linux服務器上,讓其他的小伙伴也能訪問呢?本篇以centos系統為例,把本地寫好的django項目部署到linux服務器上
環境准備:
環境准備:
1.一台Linux服務器, 操作系統: CentOS 7.4 64位
2.python3.6 (前面已經搭建好)
3.django-2.1.4
django環境准備
前面已經安裝好了python3.6.8的環境並且pip也配置好了,安裝django直接用pip安裝就可以了,安裝的django版本位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項目代碼
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代碼驗證下
[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:端口
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
啟動服務后在瀏覽器輸入: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服務器上了,於是就可以通知你的小伙伴看你做的網站咯~
交流QQ群:779429633
