服務器配置是Ubuntu14.04 64位OShtml
ubuntu14.04默認是安裝好了python2.7版本不用本身安裝了。python
先更新下源nginx
sudo apt-get updategit
第一步先安裝pipsql
sudo apt-get install -y python-pipdjango
有些時候安裝pip前須要安裝幾個相關包ubuntu
sudo apt-get install -y python-dev瀏覽器
sudo apt-get install -y libevent-devbash
安裝完pip後咱們開始安裝django服務器
sudo pip install Django==1.7.1
安裝後輸入Python進入交互環境輸入django.VERSION查看版本
開始裝uwsgi
下面這幾個多是相關包
sudo apt-get install -y build-essential
sudo apt-get install -y zliblg-dev
sudo apt-get install -y libsqlite3-dev
sudo apt-get install -y libreadline6-dev
sudo apt-get install -y libgdbm-dev
sudo apt-get install -y libbz2-dev
sudo apt-get install -y tk-dev
sudo apt-get install -y uwsgi-plugin-python
安裝uwsgi
sudo pip install uwsgi
能夠寫個test.py
# test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 #return ["Hello World"] # python2
輸入uwsgi --http :8000 --wsgi-file test.py
瀏覽器輸入http://example.com:8000看是否顯示helloworld
再安裝git
sudo apt-get install -y git
克隆項目
用uwsgi啓動項目
uwsgi --ini ${current_path}${sep}/$uwsgi_name
安裝nginx
sudo apt-get install -y nginx
修改nginx配置文件,刪掉原先包裏的東西,加上軟連接
ln -s ${current_path}${sep}${p_name}${sep}${conf_name} /etc/nginx/sites-enabled/
(
sudo ln -s /data/www/ts/conf/ts_nginx.conf /etc/nginx/sites-enabled/ sudo ln -s /data/www/ts/conf/ts_nginx.conf /etc/nginx/sites-available/
)
service nginx restart
輸入網址就能夠了。
另外你須要
python manage.py collectstatic
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'static2', ).replace('\\', '/')
STATIC_URL = '/static/'
而後將靜態文件從static2移動到static目錄下
輸入網址就能夠了。
幾個配置文件:
nginx.conf:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# the upstream component nginx needs to connect to
upstream django {
server unix:
/
etc
/
nginx
/
django
/
daowang
/
Wangdao.sock;
# for a file socket
}
# configuration of the server
server {
# the port your site will be served on
listen
80
;
# the domain name it will serve for
server_name http:
/
/
114.215
.
157.15
/
;
# substitute your machine's IP address or FQDN
charset utf
-
8
;
# max upload size
client_max_body_size
75M
;
# adjust to taste
# Django media
location
/
media {
alias
/
etc
/
nginx
/
django
/
daowang
/
static
/
media;
# your Django project's media files - amend as required
}
location
/
static {
alias
/
etc
/
nginx
/
django
/
daowang
/
static;
# your Django project's static files - amend as required
}
location
/
FingerHire{
proxy_pass http:
/
/
127.0
.
0.1
:
8080
;
}
# Finally, send all non-media requests to the Django server.
location
/
{
uwsgi_pass django;
include
/
etc
/
nginx
/
django
/
daowang
/
uwsgi_params;
# the uwsgi_params file you installed
}
}
|
uwsgi.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir
=
/
etc
/
nginx
/
django
/
daowang
# Django's wsgi file
module
=
Wangdao.wsgi
# master
master
=
true
# maximum number of worker processes
processes
=
32
max
-
requests
=
1000
# the socket (use the full path to be safe
socket
=
/
etc
/
nginx
/
django
/
daowang
/
Wangdao.sock
# ... with appropriate permissions - may be needed
chmod
-
socket
=
666
#chown-socket = www-data:www-data
# clear environment on exit
vacuum
=
true
daemonize
=
/
var
/
log
/
uwsgi
/
Wangdao.log
pidfile
=
/
tmp
/
project
-
master.pid
# added 2014-09-17
reload
-
on
-
as
=
126
reload
-
on
-
rss
=
126
enable
-
threads
=
true
pythonpath
=
/
etc
/
nginx
/
django
/
daowang
|
uwsgi_params
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
配置下根目錄下的wsgi.py
import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Wangdao.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
經常使用命令:
1.查看端口占用狀況:
netstat -lpnt
2.殺死全部uwsgi進程
killall -9 uwsgi
3.uwsgi啓動命令
uwsgi --ini /etc/nginx/django/lovep2c/lovep2c_uwsgi.ini
4.關閉uwsgi命令
uwsgi --stop /tmp/project-master.pid
5.重啓uwsgi命令
uwsgi --reload /tmp/project-master.pid
nginx錯誤日誌:
tail /var/log/nginx/error.log
uwsgi日誌:
tail /var/log/uwsgi/lovep2c.log
項目錯誤日誌:
tail /etc/nginx/django/debug.log
tail /etc/nginx/django/error.log
參考網址:https://uwsgi.readthedocs.org/en/latest/tutorials/Django_and_nginx.html