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