ubuntu django web服務器部署

1.在ubuntu14.04上 安裝pip3  https://bootstrap.pypa.io/get-pip.pycss

 python3 get-pip.py

2.安裝django 最新版   html

pip3 install django

3.修改django 支持中文  在settings.py 設置python

LANGUAGE_CODE = 'zh-Hans'

 

4.修改系統支持中文,同時也可解決軟件源中找不到個別軟件的問題nginx

apt-get install language-pack-zh-hans*

apt-get update

5.安裝虛擬環境web

~/djangogirls$ sudo apt-get install python-virtualenv
~/djangogirls$ virtualenv --python=python3.4 venv

 6.django

使用虛擬環境

上面的命令將建立一個名爲 venv 目錄 (或任何你選擇的名字),其中包含咱們的虛擬環境 (基本上是一堆的目錄和文件)。bootstrap

~/djangogirls$ source venv/bin/activate

7. 安裝web服務器 openresty  安裝指導ubuntu

8.安裝uwsgi 在虛擬環境下安裝,適合不一樣版本的python ,安裝後uwsgi 在虛擬環境的bin目錄下python3.x

pip install uwsgi   若是報錯  fatal error: Python.h: No such file or directory,就要安裝python開發包bash

sudo apt-get install python-dev  # for python2.x installs
sudo apt-get install python3-dev  # for python3.x installs

9.編寫配置文件 mysite_uwsgi.ini   啓動:uwsgi --ini mysite_uwsgi.ini  

從新加載: uwsgi --reload /tmp/uwsgi.pid     中止:uwsgi --stop /tmp/uwsgi.pid

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /root/web/mysite #django 的工程目錄
# Django's wsgi file
module          = mysite.wsgi
# the virtualenv (full path)
home            = /root/web/venv

# process-related settings
# master
master          = true
pidfile            = /tmp/uwsgi.pid #方便管理uwsgi的更新和中止
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /root/web/mysite/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true


10.配置nginx.conf 啓動: nginx (這裏須要配置PATH=/your/nginx/path/)

user  root;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
# the upstream component nginx needs to connect to
upstream django {
    server unix:///root/web/mysite/mysite.sock; # for a file socket
   # server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
    server {
        listen       8000;
        server_name  192.168.1.225;
        #charset koi8-r;
	charset     utf-8;

        #access_log  logs/host.access.log  main;
    # max upload size
    client_max_body_size 75M;   # adjust to taste
    # Django media
    location /media  {
        alias /root/web/mysite/media;  # your Django project's media files - amend as required
    }

    location /static {
	include       mime.types;#若是不加這句,你的css樣式就不會顯示
       alias /root/web/mysite/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     uwsgi_params; # the uwsgi_params file you installed
    }
    }
}

11.注意:uwsgi 和 nginx的啓動都要是同一個user, django 項目要調用python manage.py collectstatic命令,整理靜態文件(js/css等)

12.加入開機啓動項 :crontab -e  編輯添加以下內容

@reboot su - root -c /usr/local/openresty/nginx/sbin/nginx &
@reboot su - root -c "/usr/local/bin/uwsgi --ini /root/web/mysite/mysite_uwsgi.ini" &

 

13.升級最新的python

sudo apt update
sudo apt install software-properties-common

sudo add-apt-repository ppa:deadsnakes/ppa

sudo apt update
sudo apt install python3.7
相關文章
相關標籤/搜索