前言自己本地寫好的django項目,如何部署到linux服務器上,讓其他的小伙伴也能訪問呢?本篇以centos系統(tǒng)為例,把本地寫好的django項目部署到linux服務器上 環(huán)境準備: django環(huán)境準備前面已經(jīng)安裝好了python3.6.8的環(huán)境并且pip也配置好了,安裝django直接用pip安裝就可以了,安裝的django版本位django-2.1.4
django項目代碼linux服務器上的django環(huán)境已經(jīng)準備好了,接下來就是把在本地電腦已經(jīng)調(diào)通的django項目代碼全部復制到服務器上的某個目錄下 啟動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后臺-安全組-配置規(guī)則-開放8000端口,在瀏覽器上輸入http://47.104.xx.xx:8000/發(fā)現(xiàn)無法訪問 外網(wǎng)訪問django如果啟動方式是 python manage.py runserver默認只能本機訪問,為了放開訪問權(quán)限,讓其他人也能訪問到這臺機器,需加參數(shù)0.0.0.0:端口
^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調(diào)試功能,ALLOWED_HOSTS設置為全部 # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ["*"] 修改完成后保存下,重啟django服務
接著在本地瀏覽器上訪問,就能正常的打開頁面了 到這里一個簡單的django的demo項目就已經(jīng)成功的部署到linux服務器上了,于是就可以通知你的小伙伴看你做的網(wǎng)站咯~ 2019年《python全棧自動化測試課程》2月16號開學! 主講老師:上海-悠悠上課方式:QQ群視頻在線教學上課時間:每周六、周日晚上20:30-22:30 |
|