1.准备条件
- 本地正常能使用的django项目
- linux的python环境和本地一模一样
2.uwsgi安装配置
安装uwsgi
pip install uwsgi
uwsgi测试django启动
- 进入django项目:例如mysite
- 命令启动测试:
uwsgi --http 0.0.0.0:8888 --file mysite/wsgi.py --static-map=/static=static
采用uwsgi配置文件启动django项目
mkdir script
- 进入/script目录,创建一个uwsgi.ini文件,添加以下内容
[uwsgi]
# 项目目录
chdir=/opt/mysite/
# 指定项目的application
module=mysite.wsgi:application
# 指定sock的文件路径
socket=/opt/script/uwsgi.sock
# 进程个数
workers=5
pidfile=/opt/script/uwsgi.pid
# 指定IP端口
http=0.0.0.0:8080
# 指定静态文件
static-map=/static=/opt/mysite/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/opt/script/uwsgi.log
uwsgi --ini uwsgi.ini
3.Nginx安装配置
yum -y install nginx
server {
listen 80;
server_name www.test.cn;
rewrite ^(.*)$ https://${server_name}$1 permanent;
}
server {
listen 443;
server_name www.test.cn;
ssl on;
root /opt/mysite;
index index.html index.htm;
ssl_certificate cert/www.test.cn.pem;
ssl_certificate_key cert/www.test.cn.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
include uwsgi_params;
uwsgi_connect_timeout 30;
uwsgi_pass unix:/opt/script/uwsgi.sock;
}
location /static/ {
alias /opt/mysite/static/;
index index.html index.htm;
}
location /media/ {
alias /opt/mysite/media/;
}
}
service nginx restart