栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

部署代码到基于Django的服务器(腾讯云)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

部署代码到基于Django的服务器(腾讯云)

文章目录
      • 一、用到的一些linux命令
      • 二、常见问题及解决
      • 三、文件路径
      • 四、配置及代码
      • 1、[uwsgi]配置
      • 2、Nginx部署项目
      • 五、部署流程

注意:此篇文章为本人笔记,没有逻辑可言,给本人自己看的而已

基本信息:

 python:3.6.8
 Django:2.1.15
 系统:Cent OS 7
 服务器:腾讯云
Django官网

一、用到的一些linux命令
  • 使用django-admin来创建django项目
django-admin startproject mysite
#这mysite就是就是项目名

  • 启动服务器
python manage.py runserver 8000

注意要在mysite目录下执行,例如:cd mysite,再执行以上语句

  • 进入mysql数据库
mysql -u root -p
  • 查看
#命令:
#用于显示tcp,udp的端口和进程等相关情况
netstat -tunlp
"""
ps:
-t (tcp)仅显示tcp相关选项
-u (udp)仅显示udp相关选项
-n 拒绝显示别名,能显示数字的全部转化成数字
-l 仅列出有Listen(监听)的服务状态
-p 显示建立相关链接的程序名
-a 列出所有的服务状态,默认是连接的
"""
#查看进程端口号及运行的程序
netstat -atunp

#由端口号port(8000)查看进程id
netstat -anp |grep 8000


  • 停止
#命令:
#杀死指定进程根据pid(进程id): 
kill pid
#强制杀死指定进程根据pid(进程id): 
kill -9 pid


  • 查看系统中nginx的进程
ps -ef|grep nginx
二、常见问题及解决
  • nginx: [error] invalid PID number “” in “/run/nginx.pid”

服务器重启之后,执行 nginx -t 是OK的,然而在执行 nginx -s reload 的时候报错

nginx: [error] invalid PID number “” in “/run/nginx.pid”

解决方法:

需要先执行

nginx -c /etc/nginx/nginx.conf

nginx.conf文件的路径可以从nginx -t的返回中找到。

nginx -s reload

三、文件路径
  • 相关文件目录

项目路径:/root/mysite/

[root@VM-8-3-centos ~]# cd /root/mysite
[root@VM-8-3-centos mysite]# tree
.
├── db.sqlite3
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   ├── settings.cpython-36.pyc
│   │   ├── urls.cpython-36.pyc
│   │   └── wsgi.cpython-36.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── mysite.ini



 Nginx路径:/etc/nginx

四、配置及代码 1、[uwsgi]配置

在项目mysite的根目录下创建mysite.ini配置文件,文件名可自定义
mysite.ini文件的路径:
root/mysite/mysite.ini
执行语句:

uwsgi --ini mysite.ini
[uwsgi]
# Django-related settings
socket= :8000

# the base directory (full path)
chdir=/root/mysite

# Django s wsgi file
module=mysite.wsgi

# process-related setttings
# master
maseter=true


buffer-size=65536
evil-reload-on-rss=256
evil-reload-on-as=256

# maximum number of worker processes
processes=4

# ... with appropriate permissions - may be needed
# chmod-socket  = 664
# clear environment on exit
vacuum=true

  • 其它
    在进行以上操作时,是已经在完成uWSGI服务器是否正常运行检测的前提下进行的
    检测uWSCI是否正常运行:
    完成代码配置后(主要是修改数据库等),在CentOS7系统中输入uwsgi命令,测试uWSGI服务器能否正常运行,指令如下:
# /root/mysite是项目mysite的绝对地址
# mysite.wsgi是项目mysite的wsgi.py文件
uwsgi --http :8000 --chdir /root/mysite -w mysite.wsgi

指令运行后,可以在本地系统的浏览器中输入虚拟系统的IP地址+8000端口查看测试结果。在浏览器上访问htttp://114.132.242.162:8000,我们可以看到项目mysite的首页信息。(注:114.132.242.162为服务器的公网ip)

2、Nginx部署项目

编辑/etc/nginx/nginx.conf文件

# For more information on configuration, see:
#   * Official English documentation: http://nginx.org/en/docs/
#   * Official Russian documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    # set up the Nginx server configuration of the project mysite
    server {
	listen 80;
	server_name	114.132.242.162;
	charset utf-8;
	client_max_body_size	75M;
	# configure media resoure files
	location /mysite {
		expires 30d;
		autoindex on;
		add_header Cache-Control private;
		alias /root/mysite/mysite/;
	}
	# configure static resource files
	location /static {
		expires 30d;
		autoindex on;
		add_header Cache-Control private;
		alias /root/mysite/static/;
	}	
	# configure uWSGI server
	location / {
		include uwsgi_params;
		uwsgi_pass 127.0.0.1:8000;
		uwsgi_read_timeout 2;





}

}
    

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}



  • 接着在/etc/nginx/目录下执行:
nginx -t

测试配置是否存在语法问题

  • 如果语法没有问题,则可以重启nginx服务器:
nginx -s reload
五、部署流程

在settings.py等网站源代码数据更改(主要是数据库配置等)没问题的前提下,网站部署到服务器只要弄uwsgi和nginx。
在uwsgi和nginx的代码及配置都写好的情况下执行以下命令启动服务:

# ->Nginx部分
# 重新读取配置文件
nginx -t
# 如出现nginx: [error] invalid PID number “” in “/run/nginx.pid”
# 执行nginx -c /etc/nginx/nginx.conf
nginx -s reload

# ->uWSGI部分
# 启动uWSGI服务器
cd /root/mysite/
uwsgi --ini mysite.ini
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/269582.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号