在linux上配置Django專案
阿新 • • 發佈:2019-01-04
依賴包
[[email protected] ~]# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc gcc-c++ openssl-devel zlib zlib-devel -y
1. 安裝Python3
[[email protected] ~]# wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz [[email protected]~]# tar xf Python-3.6.2.tgz # 解壓python安裝包 [[email protected] ~]# cd Python-3.6.2/ # 切換到解壓的資料夾下面
[[email protected] Python-3.6.2]# ./configure --prefix=/usr/local/ # 進行配置 [[email protected] Python-3.6.2]# make && make install #將解壓後的檔案轉為二進位制並且安裝
[[email protected] Python-3.6.2]#./configure && make && make install # 將上面的兩步合為寫一步
2. 安裝Django框架和uwsgi
安裝Django專案中所用到的包
vim re.txt 下面的包資訊寫入到re.txt檔案中 asn1crypto==0.24.0 beautifulsoup4==4.6.3 bs4==0.0.1 certifi==2018.4.16 cffi==1.11.5 chardet==3.0.4 Click==7.0 cryptography==2.3.1 Django==1.11.9 Flask==1.0.2 Flask-Cors==3.0.6 gevent==1.3.6 greenlet==0.4.15 idna==2.7 ItsDangerous==1.1.0 Jinja2==2.10 lxml==4.2.6 MarkupSafe==1.0 numpy==1.15.3 Pillow==5.3.0 pycparser==2.18 PyMySQL==0.9.2 pytz==2018.7 requests==2.19.1 selenium==3.141.0 six==1.11.0 urllib3==1.23 virtualenv==16.1.0 Werkzeug==0.14.1 wordcloud==1.5.0pip3 install -i https://pypi.doubanio.com/simple/ -r re.txt pip3 install -i https://pypi.doubanio.com/simple/ uwsgi
3. 測試uwsgi是否正常,新建test.py檔案,內容如下:
[[email protected] ~]# vim test.py
在test.py檔案中寫入
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello Django"]
接下來在終端執行:
uwsgi --http :8001 --wsgi-file test.py &
4. 測試Django是否正常,執行:
[[email protected] ~]# django-admin.py startproject demosite [[email protected] ~]# cd demosite [[email protected] demosite]# python3 manage.py runserver 0.0.0.0:8002
在瀏覽器內輸入:http://127.0.0.1:8002,檢查django是否執行正常。
如果正常則在瀏覽器顯示: Hello Django
5. 配置uwsgi
[[email protected] demosite]# vim /root/demosite/uwsgi.ini
在uwsgi.ini中寫入下面的配置資訊
------------------------------------------- [uwsgi] socket = 127.0.0.1:9999 master = true workers = 2 max-requests = 1000 buffer-size = 30000 pidfile = /run/uwsgi.pid daemonize = /var/log/uwsgi.log
-------------------------------------------
執行:
uwsgi --ini /root/demosite/uwsgi.ini &
6. 配置Nginx
[[email protected] demosite]# vim /etc/nginx/conf.d/py.conf
在py.conf中寫入下面的配置資訊:
-----------------------------------------------------
server { listen 80; server_name 10.0.0.100; client_max_body_size 100M; location / { index index.html; include uwsgi_params; uwsgi_pass 127.0.0.1:9999; uwsgi_param UWSGI_SCRIPT demosite.wsgi; uwsgi_param UWSGI_CHDIR /root/demosite; } }
-----------------------------------------------------
重啟nginx:
systemrtl restart nginx