Code前端首页关于Code前端联系我们

CentOS 7+nginx+uwsgi 部署Django项目

terry 2年前 (2023-09-28) 阅读数 57 #未命名

1.将本地项目上传到服务器

使用Xftp连接服务器,通过Xftp将本地项目上传到服务器指定位置。例如,我将其上传到文件夹 home/username。

2。配置Django项目

在项目的setting.py中,注释掉STATICFILES_DIRS并添加STATIC_ROOT。

# STATICFILES_DIRS = (
#    os.path.join(BASE_DIR,'static'),
# )

STATIC_ROOT = os.path.join(BASE_DIR,'static/'

配置url.py文件,添加:

url(r'media/(?P.*)$', serve, {"document_root": MEDIA_ROOT}),
url(r'^static/(?P.*)$', serve, {"document_root": STATIC_ROOT}),

因为使用了serve、MEDIA_ROOT、STATIC_ROOT,所以需要导入:

from django.views.static import serve
from newblog.settings import MEDIA_ROOT, STATIC_ROOT

3。收集静态文件

进入项目根目录,运行

python manage.py collectstatic

4。安装 uwsgi

pip install uwsgi

5。安装 nginx

pip install uwsgiyum install nginx

6。配置 nginx

创建新的 nginx.cong 文件并输入以下内容:

user root;
events{}
http{
    include      /etc/nginx/mime.types;
    server{
        listen 80;
        server_name 你的域名;
        index index.html ;
        root  你的项目目录;
            location /static {
                alias 你的项目目录/static; # your Django project's static files - amend as required
            }
            location /media {
                alias 你的项目目录/media;
            }
            # Finally, send all non-media requests to the Django server.
            location / {
                include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
                uwsgi_pass 127.0.0.1:8000;
            }
        }
}

7。将默认的 nginx.conf 文件

替换为 新创建的 nginx.conf 文件替换 /etc/nginx/nginx.conf 文件。建议替换之前先备份原来的nginx.conf文件

8。配置uwsgi文件

新建一个uwsgi.ini文件,输入以下内容:

# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir           = 你的项目目录
# Django's wsgi file
module          = 项目名.wsgi
# the virtualenv (full path)

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = 127.0.0.1:8000
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true
virtualenv = 虚拟环境路径

9。运行 uwsgi

uwsgi uwsgi.ini -d conf/uwsgi.log(-d后面为你想存放log的路径)

10。运行nginx

sudo /usr/sbin/nginx

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

热门