首页 > 其他分享 >django项目部署到服务器

django项目部署到服务器

时间:2024-05-31 15:11:08浏览次数:23  
标签:None 部署 00 dsc1 mydjango 服务器 django root

 

1.安装python
[root@dsc1 nginx]# python -V
Python 3.6.5

 

2.安装数据库
yum install mysql mysql-devel
yum install sqlite-devel

 

3.安装django

[root@dsc1 mydjango]# pip3 install django
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting django
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/django/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/django/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/django/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/django/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/django/
  Could not fetch URL https://pypi.python.org/simple/django/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.python.org', port=443): Max retries exceeded with url: /simple/django/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)) - skipping
  Could not find a version that satisfies the requirement django (from versions: )
No matching distribution found for django

 

解决办法:
yum install openssl-devel
重新编译安装python3

 

3.项目打包

 

4.上传到指定目录解压
[root@dsc1 soft]#mkdir /deploy
[root@dsc1 soft]# mv mydjango.rar /deploy/
[root@dsc1 deploy]# cd /deploy/
[root@dsc1 deploy]# rar x mydjango.rar
windows下的rar文件上传到linux后需要安装rar才能解压

有依赖的需要安装依赖
[root@dsc1 mydjango]#cd /deploy/mydjango
[root@dsc1 mydjango]#pip3 install -r requirements.txt


5.先测试一下不使用Nginx和uWSGI,django能不能正常运行

[root@dsc1 mydjango]# cd /deploy/mydjango
[root@dsc1 mydjango]# python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
May 31, 2024 - 01:14:10
Django version 3.2.25, using settings 'mydjango.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

指定本机ip访问
python3 manage.py runserver 192.168.1.102:8000

浏览器输入:
http://192.168.1.102:8000/

 

6.安装uwsgi
[root@dsc1 mydjango]#pip3 install uwsgi
[root@dsc1 mydjango]# uwsgi --version
2.0.25.1


7.在项目里配置uwsgi.ini配置文件
a.在settings.py所在目录vim新文件 项目名称.ini
[root@dsc1 mydjango]# cd /deploy/mydjango/mydjango
[root@dsc1 mydjango]# echo>mydjango.ini

 

b.配置 项目名称.ini文件

vi mydjango.ini

[uwsgi]
master=true
chdir=/deploy/mydjango
module=mydjango.wsgi
py-autoreload=1
lazy-apps=true  
socket=127.0.0.1:8000
processes=4
buffer-size=32768
daemonize=uwsgi.log
log-maxsize = 5000000
vacuum = true
disable-logging = true

 

c.检查settings.py配置并收集项目静态文件

settings.py配置:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
STATICFILES_DIRS = [
  os.path.join(BASE_DIR, '../mydjango/static'),  ## 实际静态文件所在项目路径,亲测好使
]

 

url.py配置:

from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

 

d.执行收集静态文件命令

[root@dsc1 mydjango]# mkdir -p /deploy/mydjango/static
[root@dsc1 mydjango]# cd /deploy/mydjango
[root@dsc1 mydjango]# python manage.py collectstatic

128 static files copied to '/static'.

 

这里会自动创建/static目录

 

 

8.启动测试
## 在当前目录启动
[root@dsc1 mydjango]# cd /deploy/mydjango/mydjango
[root@dsc1 mydjango]# uwsgi -i mydjango.ini
[uWSGI] getting INI configuration from mydjango.ini

 

## 启动之后查看进程

[root@dsc1 mydjango]# ps -ef | grep uwsgi
root     18835     1  0 11:06 ?        00:00:00 uwsgi -i mydjango.ini
root     18836 18835  1 11:06 ?        00:00:00 uwsgi -i mydjango.ini
root     18837 18835  2 11:06 ?        00:00:00 uwsgi -i mydjango.ini
root     18838 18835  2 11:06 ?        00:00:00 uwsgi -i mydjango.ini
root     18839 18835  2 11:06 ?        00:00:00 uwsgi -i mydjango.ini
root     18849 16852  0 11:06 pts/2    00:00:00 grep --color=auto uwsgi

 

查看日志:

[root@dsc1 mydjango]# tail -200f uwsgi.log
*** Operational MODE: preforking ***
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
spawned uWSGI master process (pid: 18835)
spawned uWSGI worker 1 (pid: 18836, cores: 1)
spawned uWSGI worker 2 (pid: 18837, cores: 1)
spawned uWSGI worker 3 (pid: 18838, cores: 1)
spawned uWSGI worker 4 (pid: 18839, cores: 1)
Python auto-reloader enabled
WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0x28f3090 pid: 18839 (default app)
WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0x28f3090 pid: 18836 (default app)
WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0x28f3090 pid: 18837 (default app)
WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0x28f3090 pid: 18838 (default app)
服务正常启动

 

9.创建文件验证

[root@dsc1 mydjango]# cd /deploy/mydjango/mydjango
vi test.py
输入如下内容:

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

 

执行如下命令(Ctrl + C 中断会话):
uwsgi --http :8080 --wsgi-file /deploy/mydjango/mydjango/test.py

 

10.浏览器访问
http://127.0.0.1:8080

或者是通过本机ip访问
http://192.168.1.102:8080/

 

 

标签:None,部署,00,dsc1,mydjango,服务器,django,root
From: https://www.cnblogs.com/hxlasky/p/18224617

相关文章

  • Django 里的增删改查
    下面是步骤先更新urls.py来添加新的urlfromdjango.contribimportadminfromdjango.urlsimportpathfromapp01importviewsurlpatterns=[path('demo/',views.demo),]在models.py里创建表fromdjango.dbimportmodels#Createyourmodelsher......
  • Django 做migrations时出错,解决方案
    在做migrations的时候,偶尔会出现出错。在已有数据的表中新增字段时,会弹出下面的信息运行这个命令时pythonmanage.pymakemigrationsTrackingfilebyfolderpattern:migrationsItisimpossibletoaddanon-nullablefield‘example’tobookwithoutspecify......
  • 两台windowserver服务器配置Redis哨兵集群
    十年河东,十年河西,莫欺少年穷学无止境,精益求精redis下载地址:https://github.com/tporadowski/redis/releases 这里选择压缩版,不选择安装版1、集群环境 主机master:局域网IP  172.27.40.27从机slave:局域网IP  172.27.40.29 2、修改主从相关配置-两台服务器均需修改......
  • 云端部署mes/云mes系统
    什么是MES制造执行系统?MES系统中文全称“制造执行系统”,英文全称“manufacturingexecutionsystem”,简称“MES”。该系统对企业的制造生产具有很大的作用,帮助企业提高生产效率,管理效率、质量问题,有效的执行生产计划等。MES系统应用包括:MES系统能工艺规格标准管理、MES系统能......
  • ansible部署kubernetes(1.30)
    ansible部署kubernetes(1.30)操作系统使用的是ubuntu24.04,ansible使用rocky9.21.规划节点角色配置地址domainname备注master-012c,2g10.10.50.11k8s.master01.example.comnode-012c,10g10.10.50.14k8s.node01.example.comnode-022c,10g10.10.50.15k8s.node02.example.comn......
  • ocker compose 部署mysql主从。并优化参数。
    原理图:  一、先部署两个mysql可以跨机器也可以同一台  准备两个mysql.yml文件和my.cnf配置文件(yml文件我放一起可以自行拆开部署)version:'3.1'services:mysql:image:mysql:8restart:alwayscontainer_name:mysqlenvironment:MYS......
  • 通用程序部署shell脚本
    简介通常一个独立的服务,程序,中间件成后,为便于测试,上线部署都会编写一一个shell用于对程序的启动,重启,查看状态,停止,卸载等动作,以下是一个使用shell脚本来实现的简单demo,直接见代码。功能启动:start重启:restart停止:stop卸载:uninstall查看运行状态:status获取程序......
  • Centos7部署k8s1.23.9
    !/bin/bashfunctionnode_update_kernel(){启用ELRepo仓库sudorpm--importhttps://www.elrepo.org/RPM-GPG-KEY-elrepo.orgsudorpm-Uvhhttp://www.elrepo.org/elrepo-release-7.0-6.el7.elrepo.noarch.rpm查看可用的系统内核包sudoyum--disablerepo="*"--enabl......
  • Jira安装部署
    简要介绍:版本介绍:Confluence7.19.1服务器地址:192.168.2.106端口号:8080数据目录:/opt/jira-8.14.0每日备份目录:/opt/jira-data/export手动备份目录:/opt/jira-data/export备份保留时间:30天mysql插件:/opt/jira-8.14.0/lib/mysql-connector-java-5.1.49.jarlicense插件:/op......
  • 基于springboot实习管理系统的设计与实现vue(源码+lw+部署文档+讲解等)
    前言......