Linux 安裝並部署python+django+uwsgi+nginx總結

1.    python和django的環境搭建html

        (1)下載anaconda3並安裝python

        wget https://3230d63b5fc54e62148e-c95ac804525aac4b6dba79b00b39d1d3.ssl.cf1.rackcdn.com/Anaconda3-2.3.0-Linux-x86_64.shnginx

        sh Anaconda3-2.3.0-Linux-x86_64.shweb

        一路enter鍵,而後提示是否加入到環境變量時,輸入yes便可。sql

        (2)安裝djangodjango

        直接pip install djangoubuntu

        安裝成功以後就能夠新建項目瀏覽器

        django-admin startproject demositeapp

        cd demositecurl

        python manage.py startapp blog

        python manage.py migrate (要執行這個命令,讓django生成可運行的app,不然後面使用uwsgi會報錯)

        (3)運行django

        python manage.py runserver

        curl 127.0.0.1:8000進行若是能夠正常訪問,就說明django安裝成功。

2.    安裝uwsgi

        (1)centOS

        yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

        pip install uwsgi

        uwsgi --version    #查看 uwsgi 版本

        (2)test.py

        而後,Run uWSGI:

        uwsgi --http :8000 --wsgi-file test.py

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return ["Hello World"] # python2
    #return [b"Hello World"] # python3

         (3)ubuntu能夠能會出現錯誤:

           若是出現錯誤,!!! no internal routing support, rebuild with pcre support !!!

            sudo apt-get install libpcre3 libpcre3-dev

            sudo pip uninstall uwsgi

            sudo apt-get remove uwsgi

            sudo pip install uwsgi

          (4)測試

               1) 打開下面url,瀏覽器上應該顯示hello world

                curl http://127.0.0.1:8000 若是安裝httpie模塊的話使用http http://127.0.0.1:8000

                若是顯示正確是Hello World,說明上面的環節是暢通的

              2) 測試django

                默認使用django新建工程會在app下面生成一個wsgi.py的文件

                uwsgi --http :8000 --wsgi-file wsgi.py 直接這樣也會報錯

                uwsgi --http :8000 --wsgi-file appname/wsgi.py

                打開瀏覽器輸入http://127.0.0.1:8000  若是現實正確說明web client <-->uwsgi <---> django是暢通的

             

3.  安裝配置nginx

     (1)安裝

            wget http://nginx.org/download/nginx-1.9.5.tar.gz

            tar xf nginx-1.9.5.tar.gz

            cd nginx-1.9.5

            ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module

            make && make install

            或者參考

            http://cantgis.blog.51cto.com/5788192/1540004

       (2)配置文件

            vi /usr/local/nginx/conf/nginx.conf

            通常來講加入個server就OK了

            參考配置以下

user root;
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 {
    use   epoll;
    worker_connections  65535;
}


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   8099;
        server_name 10.117.52.157;              ##對外訪問的IP和端口號
        access_log /tmp/cms/access.log;
        error_log /tmp/cms/error.log;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
         include        uwsgi_params;
         uwsgi_pass     127.0.0.1:8088;
         uwsgi_read_timeout 300;
        }

        #error_page  404              /404.html;

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

        location /static/ {
            alias  /root/cms/cms/static/;
            index  index.html index.htm;
        }

        location /media/ {
            alias  /root/cms/cms/media/;
        }
    }

}

            (3)運行and 重啓

            /usr/local/nginx/sbin/nginx

            啓動: nginx  start

            重啓: nginx -s reload

4.  使用uwsgi的配置文件運行django

      在確保nginx運行以後,就能夠經過uwsgi來運行django了。nginx 在最外層接收請求,靜態的本身處理,動態的經過    socket端口交給uwsgi來處理。

    配置文件內容以下

    [uwsgi]

    socket=:8088               #要和nginx對應的IP和端口號一致

    chdir=/root/cms/cms #APP的目錄

    module=cms.wsgi        #wsgi.py文件位置

    touch-reload=/root/cms/cms/reload  #重啓只要輸入命令touch reload文件便可

    processes=4

    threads=2

    daemonize=/tmp/cms/wsgi.log   #日誌文件位置

    放在APP的上一級目錄

     直接運行uwsgi --ini uwsgi.ini 便可

相關文章
相關標籤/搜索