django+nginx+uwsgi+https

1.首先確認已安裝django,nginx,uwsgiphp

pip install django
pip install uwsgi
安裝django,uwsgi

 

2.利用命令uwsgi啓動,在django項目下,django目錄結構以下:html

圈出來的文件是先無論,後面建立的或者配置文件生成的python

 

3.命令測試啓動nginx

uwsgi --http 192.168.10.19:8080  --file cmdb/wsgi.py --static-map=/static=st
atic
uwsgi命令啓動django

參數說明:web

--http 這個就和runserver同樣指定IP 端口
--file 這個文件就裏有一個反射,若是你在調用他的時候沒有指定Web Server就使用默認的
-- static 作一個映射,指定靜態文件算法

此時,訪問http://192.168.10.19:8080/index,如圖所示,表示項目啓動成功django

 

 

4.建立uwsgi.ini文件vim

vim cmdb_uwsgi.ini
建立uwsgi.ini
[uwsgi]
#指定IP端口
http = 127.0.0.1:8080
#項目目錄
chdir = /home/deepcam/python/cmdb/
#項目的app下的wsgi
module = cmdb.wsgi
#sock的文件路徑
socket=/home/deepcam/python/cmdb/uwsgi.sock
#socket =127.0.0.1:8080   #用這個形式經過nginx訪問django目前沒有成功,有點坑
#啓用主進程
master = true
#運行進程數
processes = 4
#線程數
threads = 2
#啓用線程
enable-threads = True
#設置日誌目錄
daemonize = /var/log/uwsgi.log
#緩存大小
buffer-size = 21573
# 自動移除unix Socket和pid文件當服務中止的時候
vacuum = true
uid=root
gid=root
#指定靜態文件
static-map=/static=/home/deepcam/python/cmdb/static
ini內容

 

5.驗證uwsgi.ini配置文件是否成功centos

啓動項目緩存

訪問:127.0.0.1:8080/index,如正常則成功

 

6.配置nginx配置文件,我這是編譯安裝的nginx,在/usr/local/nginx

cd /usr/local/nginx
vim conf/nginx.conf
nginx
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        charset utf-8;
        #access_log  logs/host.access.log  main;

        location / {
            include uwsgi_params;  #主要添加這個
            uwsgi_connect_timeout 30;
            uwsgi_pass unix:/home/deepcam/python/cmdb/uwsgi.sock;  #主要這個
            index index.html index.htm;
            client_max_body_size 75M;
        }

        location /static {
            alias /home/deepcam/python/cmdb/static;
            index index.html index.htm;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
nginx配置文件內容

7.驗證是否正常訪問127.0.0.1/index

 啓動nginx

./sbin/nginx    啓動
./sbin/nginx -s stop  中止
./sbin/nginx -s reload 加載配置文件

 

 

 

 說明uwsgi+nginx+django結合成功了,若是不須要進行https訪問那麼下面就能夠不作了

 

8.經過openssl生成證書

cd /usr/local/nginx
mkdir
ssl
cd ssl

建立服務器私鑰,長度1024位, des3加密算法的

openssl genrsa -des3 -out server.key 1024

建立簽名請求證書.csr

openssl req -new -key server.key -out server.csr 

在加載SSL支持的Nginx並使用上述私鑰時除去必須的口令

cp server.key server.key.org

openssl rsa -in server.key.org -out server.key

標記證書使用上述私鑰和CSR 

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

修改nginx.conf

 1 http {
 2     include       mime.types;
 3     default_type  application/octet-stream;
 4 
 5     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 6     #                  '$status $body_bytes_sent "$http_referer" '
 7     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 8 
 9     #access_log  logs/access.log  main;
10 
11     sendfile        on;
12     #tcp_nopush     on;
13 
14     #keepalive_timeout  0;
15     keepalive_timeout  65;
16 
17     #gzip  on;
18 
19     server {
20        #listen       80;
21         listen       443;
22         server_name  localhost;
23 
24         #charset koi8-r;
25         charset utf-8;
26 
27        ssl on;
28        ssl_certificate /usr/local/nginx/ssl/server.crt;
29        ssl_certificate_key /usr/local/nginx/ssl/server.key;
30         #access_log  logs/host.access.log  main;
31 
32         location / {
33             include uwsgi_params;
34             uwsgi_connect_timeout 30;
35             uwsgi_pass unix:/home/deepcam/python/cmdb/uwsgi.sock;
36             index index.html index.htm;
37             client_max_body_size 75M;
38         }
nginx

驗證https訪問

 

 說明結合好了

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息