groupadd zanhu
useradd -m zanhu -g zanhu
passwd zanhu
chmod +x /home/zanhu
yum install python-devel zlib-devel mysql-devel libffi-devel bzip2-devel openssl-devel java gcc wget
由于之前使用rpm安装过mysql8.0,通过rpm -qa|grep mysql
命令查看已经包含mysql-community-devel所以不需要安装mysql-devel
yum install -y nginx redis supervisor
systemctl enable redis nginx supervisord
su - zanhu
chown zanhu:zanhu -R zanhu/
mkdir logs
cd /root/zanhu
pipenv shell
pip free > /home/zanhu/zanhu/requirements.txt
本方法在使用时出现卡顿,因为原虚拟环境下包含测试环境下的比较小众的依赖包导致下载延时,删除这些依赖后继续执行安装
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ -r requirements.txt
由于django haystack版本问题,只能使用elasticsearch2.4.6版本,官网已经没有资源,可以从github下载,然后通过xftp上传到服务器
点击下载
tar -xf elasticsearch-2.4.6.tar.gz
/usr/local/python37/bin/gunicorn --env DJANGO_SETTINGS_MODULE=config.settings.production -b 127.0.0.1:8888 --chdir /home/zanhu/zanhu config.wsgi
./elasticsearch-2.4.6/bin/elasticsearch
/usr/local/python37/bin/celery --work=/home/zanhu/zanhu -A config.celery_app worker -l info
[tasks]
. config.celery_app.debug_task
. djcelery_email_send_multiple
. zanhu.users.tasks.get_users_count
/usr/local/python37/bin/daphne -p 8000 config.asgi:application
# 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/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 2048;
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;
upstream channels-backend { # websocket请求转发配置
server localhost:8000;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://127.0.0.1:8888; # 动态请求转发给gunicorn服务
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /staticfiles/ {
root /home/zanhu/zanhu/zanhu; # staticfiles文件所在的目录路径
}
location /media/ {
root /home/zanhu/zanhu/zanhu; # media文件所在的目录路径
}
location /ws/ { # /ws/用于区分http请求和websocket
proxy_pass http://channels-backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
systemctl start nginx
tail -f /var/log/nginx/error.log
以上是手动部署django项目的流程,浏览器输入ip查看是否成功运行
使用elasticsearch搜索功能时搜索不到数据,请求正常。原因是仍旧以为是在原来虚拟环境下运行的项目,一个新项目了,忘记生成索引python3 manage.py rebuild_index
部署完成后,静态文件和媒体文件不能加载,可能是静态文件权限问题,或者是Django项目路由设置问题,以下是路由配置问题的解决方案
from django.conf import settings
from django.urls import include, path
from django.conf.urls.static import static
from django.views.generic import TemplateView
from django.views import defaults as default_views
urlpatterns = [
# 用户
path("users/", include("zanhu.users.urls", namespace="users")),
# 第三方应用
path("search/", include("haystack.urls")),
# 项目
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
源码中查看static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
只适用于debug模式下,在生产环境不能加载静态文件,所以要做如下修改
#####修改后的urls.py
from django.conf import settings
from django.urls import include, path
from django.views.static import serve # 注意区别,导入django.views.static下的serve方法
from django.views.generic import TemplateView
from django.views import defaults as default_views
urlpatterns = [
# 用户
path("users/", include("zanhu.users.urls", namespace="users")),
# 第三方应用
path("search/", include("haystack.urls")),
# 项目
re_path("^static/(?P<path>.*)$", serve, document_root=settings.STATIC_ROOT) # 添加静态文件的路由
re_path("^media/(?P<path>.*)$", serve, document_root=settings.MEDIA_ROOT) # 添加media文件的路由
]
cd /etc/supervisord.d
[program:celery]
# 执行用户
user = zanhu
# 执行的命令
command = /usr/local/python37/bin/celery --work=/home/zanhu/zanhu -A config.celery_app worker -l info
# 日志文件配置
loglevel = info
stdout_logfile = /home/zanhu/zanhu/logs/celery.log
stderr_logfile = /home/zanhu/zanhu/logs/celery_error.log
# 日志文件最大字节数
stdout_logfile_maxbytes = 100MB
# 备份数量
stdout_logfile_backups = 3
# 命名每个进程,便于管理
process_name = celery_worker%(process_num)s
# 启动的进程数,设置成服务器的CPU数
numprocs_start = 1
numprocs = 1
# 设置自启和重启
autostart = true
autorestart = true
redirect_stderr = true
[program:daphne]
# 执行用户
user = zanhu
# 在该目录下执行下面command命令
directory = /home/zanhu/zanhu
# 执行的命令
command = /usr/local/python37/bin/daphne -p 8000 config.asgi:application
# 日志文件配置
loglevel = info
stdout_logfile = /home/zanhu/zanhu/logs/daphne.log
stderr_logfile = /home/zanhu/zanhu/logs/daphne_error.log
stdout_logfile_maxbytes = 100MB
stdout_logfile_backups = 3
# 给每个进程命名,便于管理
process_name = daphne_worker%(process_num)s
# 启动的进程数,设置成云服务器的vCPU数
numprocs_start = 1
numprocs = 1
max-requests = 5000
# 设置自启和重启
autostart = true
autorestart = true
redirect_stderr = True
[program:elasticsearch]
# 执行用户
user = zanhu
# 在该目录下执行下面command命令
directory = /home/zanhu/elasticsearch-2.4.6/bin/
# 执行的命令
command = /home/zanhu/elasticsearch-2.4.6/bin/elasticsearch -d
# 日志文件配置
loglevel = info
stdout_logfile = /home/zanhu/zanhu/logs/elasticsearch.log
stderr_logfile = /home/zanhu/zanhu/logs/elasticsearch_error.log
stdout_logfile_maxbytes = 100MB
stdout_logfile_backups = 3
# 给每个进程命名,便于管理
process_name = elasticsearch_worker%(process_num)s
# 启动的进程数,设置成云服务器的vCPU数
numprocs_start = 1
numprocs = 1
# 设置自启和重启
autostart = true
autorestart = true
redirect_stderr = True
[program:gunicorn]
# 执行用户
user = zanhu
# 执行的命令
command = /usr/local/python37/bin/gunicorn --env DJANGO_SETTINGS_MODULE=config.settings.production -b 127.0.0.1:8888 --chdir /home/zanhu/zanhu config.wsgi
# 日志文件配置
loglevel = info
stdout_logfile = /home/zanhu/zanhu/logs/gunicorn.log
stderr_logfile = /home/zanhu/zanhu/logs/gunicorn_error.log
stdout_logfile_maxbytes = 100MB
stdout_logfile_backups = 3
# 给每个进程命名,便于管理
process_name = gunicorn_worker%(process_num)s
# 启动的进程数,设置成云服务器的vCPU数
numprocs_start = 1
numprocs = 1
# 最大访问次数,超过后重新启动
max-requests = 5000
# 设置自启和重启
autostart = true
autorestart = true
redirect_stderr = True
aommand = /usr/local/python3/bin/daphne -p 8000 config.asgi:application
sudo systemctl start supervisord
使用Nginx+Gunicorn+Daphne+supervisor部署Django服务
原文:https://www.cnblogs.com/listash/p/12466904.html