django服务器搭建

                                  基于centos+uwsgi+nginx来部署项目

大致步骤如下:

安装服务相关的配置(各编译文件、依赖包等)

熟悉uwsgi启动django项目,可参考https://uwsgi-docs.readthedocs.io/en/latest/ 官方网站

配置/etc/nginx/nginx.conf默认(主)配置文件,熟悉使用include导入配置文件

一种方式在/etc/nginx/ 目录下创建项目nginx配置文件sites-enabled,同时创建mysite_nginx.conf文件(另一种方式在项目目录下创建nginx_conf文件夹防止mysite_nginx.conf配置文件)


  • 安装服务相关的配置(各编译文件、依赖包等)
    安装编译工具 yum install -y gcc* pcre-devel openssl-devel

        安装python依赖 yum install -y python-devel

        安装uwsgi pip install uwsgi

        安装mysql等

  • 熟悉uwsgi启动django项目,可参考https://uwsgi-docs.readthedocs.io/en/latest/ 官方网网

        通过官网,熟悉uwsgi各种配置,以及通过uwsgi启动项目

        目录为/home/GuangZhouScenic/uwsgi.ini

[uwsgi]
socket = 127.0.0.1:5001
chdir = /home/GuangZhouScenic/
# 生成wsgi.py文件
wsgi-file = GuangZhouScenic/wsgi.py
#pythonpath = ..
#env = DJANGO_SETTINGS_MODULE=GuangZhouScenic/GuangZhouScenic.settings
#module = django.core.handlers.wsgi:WSGIHandler()
processes = 4
threads = 2
stats = 127.0.0.1:9191
#如果你没用nginx,想直接访问django后台,可以使用http协议
#http = 0.0.0.0:8000
#如果你用了nginx进行反向代理,请使用socket协议,注释掉http协议
#socket          = 0.0.0.0:8000

daemonize=/home/GuangZhouScenic/uwsgi/uwsgi.log  # 日志文件
#stats=/hubao_clean/uwsgi/uwsgi.status           
pidfile=/home/GuangZhouScenic/uwsgi/uwsgi.pid
  • 配置/etc/nginx/nginx.conf默认(主)配置文件,熟悉使用include导入配置文件

       目录为/etc/nginx/nginx_conf

#user  nobody;
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;
    include       /etc/nginx/sites-enabled/mysite_nginx.conf;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

	#server  {
    #            *********
    #		}    
    
}
  • 一种方式在/etc/nginx/ 目录下创建项目nginx配置文件sites-enabled,同时创建mysite_nginx.conf文件(另一种方式在项目目录下创建nginx_conf文件夹并创建mysite_nginx.conf配置文件)

       目录为/etc/nginx/sites-enabled/mysite_nginx.conf;

# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:5001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name 192.168.232.140; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /home/GuangZhouScenic/GuangZhouScenic/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/GuangZhouScenic/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }
}

django+uwsgi 官网  https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

启动项目:

              1  在项目目录下运行   uwsgi uwsgi.ini 

               2  运行 nginx -s reload

即可访问网站,有缘人可邮箱探讨相关问题[email protected]

 

 

 

你可能感兴趣的:(服务器,django,django)