環境:CentOS6.5 + Nginx1.11.5 + Python3.5.2html
yum install -y zlib-devel bzip2-devel \ pcre-devel openssl-devel ncurses-devel sqlite-devel \ readline-devel tk-devel
源碼包下載,戳我python
wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tar.xz tar xf Python-3.5.2.tar.xz cd Python-3.5.2 ./configure make -j 2 make altinstall
PS:Python3.x默認已經安裝pip等包管理工具,若是沒有須要手動安裝`pip`包管理工具nginx
什麼是uwsgi,what?web
uWSGI是一個Web服務器,它實現了WSGI協議、uwsgi、http等協議。Nginx中HttpUwsgiModule的做用是與uWSGI服務器進行交換。sql
要注意 WSGI / uwsgi / uWSGI 這三個概念的區分。apache
uWSGI的主要特色以下django
安裝uWSGIvim
# Install the latest stable release: pip install uwsgi # ... or if you want to install the latest LTS (long term support) release, pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz
基本測試瀏覽器
建立測試文件服務器
# 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://ip:8000`驗證
使用uWSGI運行Django
uwsgi --http :8000 --module mysite.wsgi
使用瀏覽器訪問`http://ip:8000`驗證
能夠將參數寫到一個配置文件中
[root@localhost Django_test]# vim mysite_uwsgi.ini [uwsgi] http = :9000 #the local unix socket file than commnuincate to Nginx socket = 127.0.0.1:8001 # the base directory (full path) chdir = /usr/local/Django_test # Django's wsgi file wsgi-file = Django_test/wsgi.py # maximum number of worker processes processes = 4 #thread numbers startched in each worker process threads = 2 #monitor uwsgi status stats = 127.0.0.1:9191 # clear environment on exit or restart vacuum = true
啓動
uwsgi --ini mysite_uwsgi.ini
使用瀏覽器訪問`http://ip:9000`驗證
useradd -M -s /sbin/nologin nginx tar zxf nginx-1.11.5.tar.gz cd nginx-1.11.5 ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_gzip_static_module make && make install ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
修改nginx配置文件
#修改主配置文件,引入下面要寫的uwsgi的配置文件,也能夠在當前配置文件中寫 # vim /usr/local/nginx/conf/nginx.conf #在http區段中 include mysite_uwsgi_nginx.conf;
建立一個uwsgi的配置文件
# vim /usr/local/nginx/conf/mysite_uwsgi_nginx.conf # the upstream component nginx needs to connect to upstream django { # server unix:///path/to/your/mysite/mysite.sock; # for a file socket server 127.0.0.1:8001; # for a web port socket (we'll use this first) } # configuration of the server server { # the port your site will be served on listen 8000; # the domain name it will serve for server_name .example.com; # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste location /static { alias /usr/local/Django_test/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 } }
到此Django項目部署已經基本部署完成,你能夠訪問咱們寫的頁面,可是訪問`http://ip:8000/admin`卻發現沒有樣式,waht?
這個問題是因爲admin的樣式文件都在django內部,而不是在咱們的項目的靜態文件的目錄中,到此咱們須要把全部的靜態文件匯聚到一個目錄中
1. 修改項目目錄中的`setting.py`文件,添加 (all_statics能夠本身指定目錄)
STATIC_ROOT = os.path.join(BASE_DIR, "all_statics/")
2.run (提示輸入yes)
python manage.py collectstatic
3.如今咱們須要去修改nginx的配置文件,將靜態文件的目錄修改成匯聚後的靜態文件的目錄
location /static { alias /usr/local/Django_test/all_statics; }
如今去啓動nginx和uwsgi
[root@localhost Django_test]# # nginx [root@localhost Django_test]# uwsgi mysite_uwsgi.ini &
如今admin頁面總算能夠看了