在Ubuntu Server18.04上部署django2.0+python3.7

  • 1,ip绑定
    vim /etc/mysql/mysql.conf.d/mysqld.cnf
    修改bind-address = 127.0.0.1为0.0.0.0支持远程访问.

  • 2,创建用户进行远程访问
    1、创建用户:CREATE USER ‘admin’@’%’ IDENTIFIED BY ‘admin123’;

    username:用户名;host:指定在哪个主机上可以登录,本机可用localhost,%通配所有远程主机;password:用户登录密码;

    2、授权:GRANT ALL PRIVILEGES ON . TO ‘admin’@‘%’ IDENTIFIED BY 'admin123’;

  • 3,使用 pip freeze > requirements.txt 导出开发环境

  • 4,将其拷贝到部署环境的虚拟环境下,使用pip install -r requirements.txt安装到虚拟环境中.

  • 5,sudo apt install libpython3.7-dev防止安装uwsgi报错.

  • 6, 安装uwsgi pip install uwsgi

  • 7, 使用dbeaver导出导入新的数据库

  • 8,使用uwsgi测试在项目根目录下:uwsgi --http :8000 --modu firtproject.wsgi如果显示页面,但没有样式,说明成功.

  • 9,在项目更目录下,创建uc_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:8000; # 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 你的ip地址 ; # 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 你的目录/Mxonline/media;  # 指向django的media目录
}

location /static {
    alias 你的目录/Mxonline/static; # 指向django的static目录
}

# Finally, send all non-media requests to the Django server.
location / {
    uwsgi_pass  django;
    include     uwsgi_params; # the uwsgi_params file you installed
}
}
  • 10,修改servername为你的ip地址或者域名.以及项目路径名

  • 11,将其拷贝或者链接到/etc/nginx/conf.d 下面
    sudo ln -s 你的目录/Mxonline/conf/nginx/uc_nginx.conf /etc/nginx/conf.d/

  • 12, 重新启动nginx

  • 13,添加STATIC_ROOT = os.path.join(BASE_DIR, “static/”)
    并且注释掉staticfiles_dirs属性.
    到setting进行后续的静态文件的拉取.

  • 14, 拉取静态文件python manage.py collectstatic

  • 15,新建uwsgi.ini 配置文件, 内容如下:

    # mysite_uwsgi.ini file
    [uwsgi]

    # Django-related settings
    # the base directory (full path)
    chdir           = /home/***/Projects/ #项目根目录
    # 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 = virtualenv绝对路径/环境名称

    logto = /tmp/mylog.log

注:
    chdir: 表示需要操作的目录,也就是项目的目录.
    module: wsgi文件的路径
    processes: 进程数
    virtualenv:虚拟环境的目录
  • 16,workon 在你虚拟环境下运行uwsgi后面接你刚刚配置的文件名.
    uwsgi -i /绝对路径/uwsgi.ini & 后台运行

或者pkill if uwsgi

参考链接: django部署教程

原文地址:https://www.cnblogs.com/jlxa162hhf/p/14161242.html