CentOS 7+nginx+uwsgi部署Django项目

2023-02-0112:05:58服务器及运维Comments1,237 views字数 1611阅读模式

1. 上传本地项目到服务器文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

使用Xftp连接服务器,通过Xftp上传本地项目到服务器指定位置,比如我会上传到 home/username文件夹下.文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

2. 配置Django项目文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

在项目的setting.py里面,注释掉STATICFILES_DIRS,新增STATIC_ROOT。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

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

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

配置url.py文件,在urlpatterns里面新增:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

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

因为用到了serve、MEDIA_ROOT和STATIC_ROOT,需要导入:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

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

3. 收集静态文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

进入到项目根目录,运行文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

python manage.py collectstatic

4. 安装uwsgi文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

pip install uwsgi

5. 安装nginx文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

pip install uwsgiyum install nginx

6. 配置nginx文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

新建nginx.cong文件,输入如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

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文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

用新建的nginx.conf文件替换 /etc/nginx/nginx.conf文件,建议在替换之前先备份原始的nginx.conf文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

8. 配置uwsgi文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

新建uwsgi.ini文件,输入如下内容:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

# 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文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

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

10. 运行nginx文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html

sudo /usr/sbin/nginx
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30653.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/30653.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定