django-基础-部署&邮件
Apache
免费的:
BAE、SAE
https://github.com/twz915/BAE_Django
服务器的
sudo apt-get install apache2
sudo apt-get install libapache2-mod-wsgi
配置文件sudo vi /etc/apache2/sites-available/sitename.conf
示例内容如下
<VirtualHost *:80>
    ServerName www.yourdomain.com
    ServerAlias otherdomain.com
    ServerAdmin tuweizhong@163.com
  
    Alias /media/ /home/tu/blog/media/
    Alias /static/ /home/tu/blog/static/
  
    <Directory /home/tu/blog/media>
        Require all granted
    </Directory>
  
    <Directory /home/tu/blog/static>
        Require all granted
    </Directory>
  
    WSGIScriptAlias / /home/tu/blog/blog/wsgi.py
    # WSGIDaemonProcess ziqiangxuetang.com python-path=/home/tu/blog:/home/tu/.virtualenvs/blog/lib/python2.7/site-packages
    # WSGIProcessGroup ziqiangxuetang.com
  
    <Directory /home/tu/blog/blog>
    <Files wsgi.py>
        Require all granted
    </Files>
    </Directory>
</VirtualHost>
如果是 2.2.x,需要用下面的代替Require all granted
Order deny,allow
Allow from all
把#去掉可以用virtualenv部署网站
# WSGIDaemonProcess ziqiangxuetang.com python-path=/home/tu/blog:/home/tu/.virtualenvs/blog/lib/python2.7/site-packages
# WSGIProcessGroup ziqiangxuetang.com
修改wsgi.py文件
如果上面写了WSGIDaemonProcess就可以跳过这一步
上面写WSGIScriptAlias / /home/tu/blog/blog/wsgi.py
就可以把apache2和网站的project联系起来
import os
from os.path import join,dirname,abspath
 
PROJECT_DIR = dirname(dirname(abspath(__file__)))#3
import sys # 4
sys.path.insert(0,PROJECT_DIR) # 5
 
os.environ["DJANGO_SETTINGS_MODULE"] = "blog.settings" # 7
 
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
第 3,4,5 行为新加的内容,作用是让脚本找到django项目的位置,也可以在sitename.conf中做;
用WSGIPythonPath,想了解的自行搜索, 第 7 行如果一台服务器有多个django project时一定要修改成上面那样
设置目录和文件权限
目录:755
文件:644
cd /home/tu/
sudo chmod -R 644 zqxt
sudo find zqxt -type d -exec chmod 755 \{\} \;
apache 服务器运行用户可以在 /etc/apache2/envvars 文件里面改,这里使用的是默认值,当然也可以更改成自己的当前用户,这样的话权限问题就简单很多,但在服务器上推荐有 www-data 用户,更安全。以下是默认设置:
# Since there is no sane way to get the parsed apache2 config in scripts, some
# settings are defined via environment variables and then used in apache2ctl,
# /etc/init.d/apache2, /etc/logrotate.d/apache2, etc.
 
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
激活新网站
sudo a2ensite sitename 或 sudo a2ensite sitename.conf
上传文件夹权限
media:存放用户上传文件
static:js,css,图片
STATIC_URL:静态文件网址
STATIC_ROOT:静态文件根目录
MEDIA_URL:用户上传文件夹根目录
MEDIA_URL:对应访问网址
settings.py中设置
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/dev/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
 
# upload folder
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
用户上传目录还需要设置给www-data用户写权限
zqxt/media/uploads 文件夹,进入media文件夹,将 uploads 用户组改为www-data,并且赋予该组写权限:
cd media/ # 进入media文件夹
sudo chgrp -R www-data uploads
sudo chmod -R g+w uploads
对于嵌入式数据库,需要修改数据库权限给www-data
错误排查
网站没有布局
- 确保你的配置文件中的路径是正确的
- 确保你的settings.py中的文件设置正确
- 收集静态文件(详细静态文件部署教程)
 python manage.py collectstatic
网站打开后报错
在settings.py中打开DEBUG = True
重启服务器sudo service apache2 restart
查看apache2的错误日志cat /var/log/apache2/error.log
部署时文件对应关系
sitename.conf --> wsgi.py --> settings.py --> urls.py --> views.py
多个域名或让app使用子域名,只要新建一个 wsgi.py
更改里面对应的settings,新的settings可以对应新的urls.py,从而做到访问与原来不同的地址
Nginx
测试python manage.py runserver
安装ng,安装supervisor
需要研究下:SELinux 和iptables ,不懂要关闭
SELinux设置兼容模式
sudo setenforce 0
防火墙设置
可以选择临时关闭防火墙
sudo service iptables stop
 
或者开放一些需要的端口,比如 80
sudo iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
使用gunicorn 或者 uwsgi部署
gunicorn
sudo pip install gunicorn
gunicorn -w4 -b0.0.0.0:8001 zqxt.wsgi
-w表示开启多少个worker
-b 表示要使用的ip和port
0.0.0.0代表监控电脑的所有 ip
如果使用了virtualenv可以这样
/path/to/env/bin/gunicorn --chdir /path/to/project --pythonpath /path/to/env/ -w4 -b0.0.0.0:8017 project.wsgi:application
用--pythonpath指定依赖包的路径,多个可以用分隔,比如/path/to/lib,/home/tu/lib'
uwsgi
pip install uwsgi
uwsgi --http :8001 --chdir /path/to/project --home=/path/to/env --module project.wsgi
--home 指定virtualenv 路径
project.wsgi 指 project/wsgi.py 文件
如果端口已被占用使用lsof -i :8002查看进程,然后kill
supervisor
生成配置文件
echo_supervisord_conf > /etc/supervisord.conf
在supervisor.conf最底部添加
[program:zqxt]
command=/path/to/uwsgi --http :8003 --chdir /path/to/zqxt --module zqxt.wsgi
directory=/path/to/zqxt
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
command中写上对应的命令,就可以用supervisor管理
启动:supervisord -c /etc/supervisord.conf
重启项目:supervisorctl -c /etc/supervisord.conf restart zqxt
启动/停止某个程序:supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]
uwsgi文件,上面的命令行太长了,需要用ini配置文件
[uwsgi]
socket = /tmp/zqxt.sock
chdir=/home/tu/zqxt
wsgi-file = zqxt/wsgi.py
touch-reload=/home/tu/zqxt/reload
 
processes = 2
threads = 4
 
chmod-socket = 664
chown-socket=tu:www-data
上面的/tmp/zqxt.sock,需要和ng关联起来
项目上新建一个reload文件,只需要touch reload项目就会重启
修改 supervisor 配置文件中的 command 一行
[program:zqxt]
command=/path/to/uwsgi --ini /home/tu/zqxt/uwsgi.ini
directory=/path/to/zqxt
startsecs=0
重启一下supervisor
sudo vim /etc/nginx/sites-available/zqxt.conf
写入以下内容
server {
    listen      80;
    server_name www.ziqiangxuetang.com;
    charset     utf-8;
 
    client_max_body_size 75M;
 
    location /media  {
        alias /path/to/project/media;
    }
 
    location /static {
        alias /path/to/project/static;
    }
 
    location / {
        uwsgi_pass  unix:///tmp/zqxt.sock;
        include     /etc/nginx/uwsgi_params;
    }
}
ng配置
新建网站写入内容
sudo vim /etc/nginx/sites-available/zqxt.conf
写入
server {
    listen      80;
    server_name www.ziqiangxuetang.com;
    charset     utf-8;
 
    client_max_body_size 75M;
 
    location /media  {
        alias /path/to/project/media;
    }
 
    location /static {
        alias /path/to/project/static;
    }
 
    location / {
        uwsgi_pass  unix:///tmp/zqxt.sock;
        include     /etc/nginx/uwsgi_params;
    }
}
激活网站
#激活网站
sudo ln -s /etc/nginx/sites-available/zqxt.conf /etc/nginx/sites-enabled/zqxt.conf
#测试配置语法
sudo service nginx configtest
#重启ng
sudo service nginx reload 或者 sudo service nginx restart
部署:
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/uwsgi/
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/gunicorn/
ng:
http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html#configure-nginx-for-your-site
uwsgi ini:
http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html#configuring-uwsgi-to-run-with-a-ini-file
邮件
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
  
EMAIL_USE_TLS = False
EMAIL_HOST = 'smtp.tuweizhong.com'
EMAIL_PORT = 25
EMAIL_HOST_USER = 'mail@tuweizhong.com'
EMAIL_HOST_PASSWORD = 'xxxx'
DEFAULT_FROM_EMAIL = 'mail@tuweizhong.com'
DEFAULT_FROM_EMAIL也可以这样写,别人收到邮件会有设定的名称
DEFAULT_FROM_EMAIL = 'tuweizhong <tuweizhong@163.com>'
发送邮件
from django.core.mail import send_mail
 
send_mail('Subject here', 'Here is the message.', 'from@example.com',
    ['to@example.com'], fail_silently=False)
一次发送多个
from django.core.mail import send_mass_mail
message1 = ('Subject here', 'Here is the message', 'from@example.com', ['first@example.com', 'other@example.com'])
message2 = ('Another Subject', 'Here is another message', 'from@example.com', ['second@test.com'])
send_mass_mail((message1, message2), fail_silently=False)
send_mail发送一个邮件建立一个连接
send_mass_mail发送多个邮件建立一个连接
添加附件
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from_email = settings.DEFAULT_FROM_EMAIL
# subject 主题 content 内容 to_addr 是一个列表,发送给哪些人
msg = EmailMultiAlternatives(subject, content, from_email, [to_addr])
msg.content_subtype = "html"
 
# 添加附件(可选)
msg.attach_file('./twz.pdf')
 
# 发送
msg.send()
 
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号