1. 程式人生 > >Ubuntu16下部署Django+Nginx+uwsgi

Ubuntu16下部署Django+Nginx+uwsgi

try_files 入口 子進程 nag mit available ubun rod worker

1.更新apt-get

apt-get update
apt-get upgrade

2.安裝Nginx

apt-get install nginx

然後在瀏覽器輸入IP地址若有nginx歡迎界面則成功

3.安裝python3-pip

apt-get instll python3-pip

註意安裝python3的pip而不是python,安裝成功後可以更新pip

4.安裝Django以及uwsgi

pip3 install Django
pip3 install uwsgi

也可以采用虛擬環境下的安裝

安裝成功後將項目上傳至服務器

5.配置uwsgi

在Django的manage.py同目錄下創建uwsgi.ini

添加下面內容,註意將地址改成自己的

[uwsgi]
chdir = /home/feixue/python/www/for_test //項目根目錄
module = for_test.wsgi:application //指定wsgi模塊
socket = 127.0.0.1:8000 //對本機8000端口提供服務
master = true         //主進程

#vhost = true          //多站模式
#no-site = true        //多站模式時不設置入口模塊和文件
#workers = 2           //子進程數
#reload-mercy = 10
#vacuum = true         //退出、重啟時清理文件
#max-requests = 1000
#limit-as = 512
#buffer-size = 30000
#pidfile = /var/run/uwsgi9090.pid    //pid文件,用於下腳本啟動、停止該進程
daemonize = /home/feixue/python/www/for_test/run.log    
disable-logging = true   //不記錄正常信息,只記錄錯誤信息

6.配置Nginx

打開/etc/nginx/sites-available/default,更改配置

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    server_name your_ip;

         location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        # try_files $uri $uri/ =404;
        include  uwsgi_params;
                uwsgi_pass  127.0.0.1:8000;  
    }
    location /media  {
        alias /usr/local/mysite/media;  # your Django project's media files - amend as required
    }
        location /static {
            alias  /usr/local/mysite/statics;
        }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php7.0-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
    #   # With php7.0-fpm:
    #   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    #}

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

將your_ip更改為你的ip,且項目地址自己改

7.啟動

sudo uwsgi uwsgi.ini
sudo service nginx restart

若出現You may need to add ‘XXXXXXXX‘ to ALLOWED_HOSTS.

則在seting.py文件中添加即可

8.靜態文件設置

在setting.py中修改下列信息

DEBUG = False
STATIC_ROOT = os.path.join(BASE_DIR, 'statics')

然後執行

python manage.py collectstatic

這樣就產生了static文件,靜態請求由Nginx處理

Ubuntu16下部署Django+Nginx+uwsgi