通过gunicorn部署django项目,比使用uwsgi方式要简单,具体操作如下:
步骤:
1. 安装依赖
yum install python3
yum install nginx (或者下载nginx包部署)
yum install gunicorn
pip install gevent # 这个是gunicorn运行的一种模式
easy_install -U greenlet
easy_install -U eventlet
pip install gevent
2. 检查python/nginx安装是否成功
a. 输入python3,是否出现交互命令页面
b. 启动nginx, 浏览器访问ip,出现weclome ngix
举例:
启动nginx: ./nginx
2种方式验证nginx是否启动/安装成功:
进程查看: ps -ef |grep nginx
页面访问:
3. gunicorn 简单使用
按照上面的例子,当前目录为 /home/myapp, myapp中有一个包 gunicorn_app,gunicorn_app.py代码如下:
def app(environ, start_response):
data = b"Hello, World!\n"
start_response("200 OK", [("Content-Type", "text/plain"),("Content-Length", str(len(data)))])
return iter([data])
我们将要运行 test.py文件中的 app(当然名字由你决定,可以是myapp,demo等等)
gunicorn -w 2 gunicorn_app:app
上图展示了两个很重要的信息:
第一:启动了两个worker,这是通过"-w 2"指定(默认为1)
第二:worker的工作模型是sync(默认),后面会详细介绍worker模型
Django项目部署
项目结构:
1. 上传django项目到 /data/service目录下,在 /data/service/Django_project目录下 创建 static目录
2. 配置nginx文件, /usr/local/nginx/conf/nginx.conf
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 { 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 192.168.252.79; charset utf-8; #access_log logs/host.access.log main; location / { proxy_pass http://0.0.0.0:8001; # 这里要配合启动文件使用 proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } location /static { alias /data/service/Django_project/static/; } #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; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # 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; # } #} }