首页 > 代码库 > django+uwsgi+nginx部署在ubuntu系统上

django+uwsgi+nginx部署在ubuntu系统上

1. 安装之前的准备工作
  • 修改项目下settings.py文件,将调试状态改为False
DEBUG = False
ALLOWED_HOSTS = [*]
  •  能否正常启动项目:python manage.py runserver
2. 安装nginx1.8.1
从http://nginx.org/en/download.html下载

安装gcc g++的依赖库

sudo apt-get install build-essential
sudo apt-get install libtool

安装 pcre依赖库

sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev

安装 zlib依赖库(如果报错,需再安装ruby):

$sudo apt-get install rub
sudo apt-get install zlib1g-dev
 
在nginx官网下载nginx1.8.1然后解压
 
./configure --prefix=/usr/local/nginx 
#编辑nginx:
make
#安装nginx:
sudo make install
#启动nginx:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意:-c 指定配置文件的路径,不加的话,nginx会自动加载默认路径的配置文件,可以通过 -h查看帮助命令。
#查看nginx进程:
ps -ef|grep nginx
 
停止 Nginx服务器(
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx -s quit

可以将nginx路径加入系统的环境变量中,就无需写路径了:nginx -s stop

 
3.安装uwsgi:

 python3 -m pip install uwsgi

 

4.django+nginx+uwsgi
在项目目录下创建myweb_uwsgi.ini配置文件(就是和manag.py同级)
# myweb_uwsgi.ini file[uwsgi]

# Django-related settings
socket = :8000(django项目启动的端口)

# the base directory (full path)
chdir           = /home/fnngj/pydj/myweb(项目路径)

# Django s wsgi file
module          = myweb.wsgi(注意)

# process-related settings
# master
master          = true

# maximum number of worker processes
processes       = 4

# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true
 
接下来,切换到myweb项目目录下,通过uwsgi命令读取myweb_uwsgi.ini文件启动项目
uwsgi --ini django_hotel_uwsgi.ini 
(注意看有没有报错,根据错误信息进行修改)
 
再接下来要做的就是修改nginx.conf配置文件。打开/usr/local/nginx/conf/nginx.conf文件,添加如下内容
server {
    listen         10000; (外网访问端口设置;要防火墙打开了这个端口,不然外网无法访问)
    server_name    127.0.0.1 
    charset UTF-8;
    access_log      /var/log/nginx/myweb_access.log;
    error_log       /var/log/nginx/myweb_error.log;

    client_max_body_size 75M;

    location / { 
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8000;
        uwsgi_read_timeout 2;
    }   
    location /static {
        expires 30d;
        autoindex on; 
        add_header Cache-Control private;
        alias /home/fnngj/pydj/myweb/static/;(静态文件地址)
     }
 }
启动nginx然后,访问:http://127.0.0.1:10000

django+uwsgi+nginx部署在ubuntu系统上