ubuntu18.04中为django项目搭建nginx+gunicorn环境

今天介绍一下如何在ubuntu18.04中为django项目搭建nginx+gunicorn环境。

具体步骤如下:
1.安装gunicorn: pip install gunicorn
2.使用gunicorn启动django项目: gunicorn 项目名称.wsgi:application –bind 127.0.0.1:8000
3.安装并配置nginx:
编译安装时,在安装nginx前,需要先安装其他几个软件包,具体安装如下:
1)安装openssl:

wget https://www.openssl.org/source/openssl-1.1.0e.tar.gz
tar zxvf openssl-1.1.0e.tar.gz

解压完成后,进入openssl所在目录,依次执行./config、make和make install进行安装;

2)安装zlib:

wget https://downloads.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz
tar zxvf zlib-1.2.11.tar.gz

解压完成后,进入zlib所在目录,依次执行./configure、make和make install进行安装;

3)安装pcre:

wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz
tar zxvf pcre-8.40.tar.gz

解压完成后,进入pcre所在目录,依次执行./configure、make和make install进行安装;

当然,以上软件包还可以下载并安装其他版本。

4)安装nginx,可以点击 http://nginx.org/en/download.... 来下载你需要的nginx版本。下载完成后,使用tar命令解压: tar zxvf nginx-1.14.0.tar.gz
解压完成后进入nginx目录,执行以下命令进行配置:

./configure --with-http_ssl_module --with-cc-opt="-Wno-error" --with-pcre=/home/wyzane/install_package/pcre-8.40 --with-zlib=/home/wyzane/install_package/zlib-1.2.11 --with-openssl=/home/wyzane/install_package/openssl-1.1.0e

以上配置中,我将软件包都安装在了/home/wyzane/install_package/目录下,也可以安装在其他目录中。
执行以上配置后,我在编译(make)时遇到了一个错误,错误信息大致如下:

src/core/ngx_murmurhash.c:37:11: error: this statement may fall through [-Werror=implicit-fallthrough

后来在 https://trac.nginx.org/nginx/ 中找到了解决办法,即在执行./configure时加上 –with-cc-opt=”-Wno-error” 这个参数。
以后再安装nginx遇到问题时,都可以取上面那个网站寻找解决方案。

执行完./configure后,再执行make和make install进行编译和安装。
安装完成后,即可启动ngin并访问nginx首页验证是否安装成功。

4.配置nginx: 进入nginx.conf文件中修改配置
配置下面的部分即可:

server {
        listen       80;
        server_name  192.168.114.113;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
             proxy_pass http://192.168.114.113:8000;
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location /static {
             alias /home/wyzane/pyprojects/test/test/static;
        }
}

主要配置listen,server_name,location / 和location /static
listen是需要监听的端口,server_name是gunicorn启动后服务器的ip,location /指定根路径,location /static指定静态文件路径

5.配置完nginx后,搜集静态文件: python manage.py collectstatic
6.启动nginx和gunicorn,即可访问项目。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值