Django|Nginx|Uwsgi|Redis|Websocket配置、使用与部署

引言

由于工程需要包含聊天功能,希望使用websocket进行通信。然而,dwebsocket模块虽然在runserver时运行正常,但在Nginx+Uwsgi部署下无法正常运行。事实上由于wsgi协议的工作流,长连接如websocket几乎不可能维持。所以需要寻找新的实现方式。下面介绍包括使用Redis作为django的缓存及websocket的开发环境和生产环境配置。

注: 由于我用的是root账号,不需要使用sudo。在安装中如果出现权限不足的问题请自行加sudo。

第一次写这种类型的博客,如有错误敬请指正。

从哪里开始

原先的配置基本是按照黄超的博客进行的,本段内容基本上与该文章一致,截取到安装虚拟环境的内容。如果已经配置了Uwsgi和Nginx,只是需要对websocket的支持,可以跳过本段。

mysql安装

apt install mariadb-server mariadb-client libmariadbd-dev libmysqlclient-dev
service mysql restart
service mysql status
mysql_secure_installation
Enter current password for root:[Just enter]
Set root password?[Y/n] Y

#enter your password
Remove anonymous users?[Y/n] Y
Disallow root login remotely?[Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now?[Y/n] Y


#Thanks for using MariaDB!
service mysql restart

安装Nginx

如果在本机runserver,则不需要安装Nginx。

apt install nginx

安装php

apt install php php-fpm

虚拟环境

apt install python3-dev
apt install python3-venv
python3 -m venv /path/to/.venv

进入虚拟环境:

source /path/to/.venv/bin/activate

退出虚拟环境:

deactivate

注: 请注意把/path/to换成自己的路径。

Redis安装与配置

原文链接

Redis安装

apt-get install redis-server

安装完成后在linux下使用:

service redis-server start

开启redis-server。

可以用这个命令来检测redis-server是否正常工作:

redis-cli ping
PONG

收到PONG说明正常工作。

配置redis使用unix域套接字

这一小段设置使得redis使用unix域套接字而不是TCP/IP。可以选择忽略这一段。这会导致需要在django的settings.py文件中的设置略有不同。

vim /etc/redis/redis.conf
# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
# port 6379

# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for incoming connections.
#
# bind 127.0.0.1

# Specify the path for the unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
unixsocket /path/to/redis.sock
unixsocketperm 777

以上实际是将:

port 6379
bind 
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Linux系统上部署Django + nginx + uWSGI的步骤如下: 1. 安装必要的软件 在Linux系统上安装必要的软件包,包括Python、pip、nginxuWSGI等。 2. 创建Django项目 使用Django创建一个新项目或使用现有的Django项目。 3. 配置uWSGIDjango项目的根目录下创建一个uwsgi.ini文件,用于配置uWSGI。示例配置如下: ``` [uwsgi] # 指定运行模式为WSGI http = :8000 # 指定Django应用的wsgi模块 wsgi-file = myproject.wsgi # 指定进程数 processes = 4 # 指定线程数 threads = 2 # 指定静态文件路径 static-map = /static=/path/to/static # 指定日志路径 logto = /path/to/logfile ``` 其中,http参数指定了监听的端口号,wsgi-file参数指定了Django应用的wsgi模块,processes参数指定了进程数,threads参数指定了线程数,static-map参数指定了静态文件的路径,logto参数指定了日志文件的路径。 4. 配置nginxnginx配置文件中添加以下内容: ``` server { listen 80; server_name example.com; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location / { uwsgi_pass 127.0.0.1:8000; include uwsgi_params; } location /static { alias /path/to/static; } } ``` 其中,server_name指定了域名,access_log和error_log指定了日志文件的路径,uwsgi_pass指定了uWSGI的地址和端口号,include指定了uWSGI的参数。 5. 启动uWSGI服务 使用以下命令启动uWSGI服务: ``` uwsgi --ini uwsgi.ini ``` 6. 启动nginx服务 使用以下命令启动nginx服务: ``` sudo service nginx start ``` 这样就完成了Django + nginx + uWSGI部署。可以通过访问该网站的域名来验证是否部署成功。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值