跳转至

Apache部署Django

2018-02-06 更新使用python3.6版本

Linux部署Apache生产环境

Ubuntu 14.04
Apache 2.4.7
Python 3.6
Django 2.0.2

1、ubuntu安装python3.6

ubuntu14.04系统会自带python2.7和python3.4,请不要卸载python2.7,也不要将新安装的python3.6指向python/python3。

sudo apt-get install python-software-properties
sudo apt-get install software-properties-common

# sudo add-apt-repository ppa:deadsnakes/ppa
sudo add-apt-repository ppa:jonathonf/python-3.6

sudo apt-get update
sudo apt-get install python3.6 python3.6-dev python3.6-venv

How to install pip for Python 3.6 on Ubuntu 16.10?

2、安装apache2

sudo apt-get update
sudo apt-get install apache2 apache2-dev

On Linux systems, if Apache has been installed from a package repository, you must have installed the corresponding Apache "dev" package as well.

3、安装mod_wsgi模块

mod_wsgi依赖于pythonc api,故只可支持一个版本的python。这就要求Django项目使用的虚拟环境中的python版本和mod_wsgi编译使用的python版本完全相同,不然将无法使用。

pip 安装wsgi的库时会自动向/etc/apache2/mods-available/中添加wsgi.conf(空的注释文件)和wsgi.load,手动安装需要手动新增wsgi.load文件。

wsgi.load:

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
mod_wsgi.so位于文件夹/usr/lib/apache2/modules/中
  • 方法1:pip 安装
sudo apt-get install libapache2-mod-wsgi-py3

该方法将直接使用系统的python3对应的python版本编译mod_wsgi,ubuntu14.04系统的默认python3python3.4,而python3.4django2.02后将无法使用。ubuntu16.06系统默认的python3python3.5

同一系统有多个python3,系统默认取最低版本的python3以提供更好的兼容性。

可以使用下述命令来修改默认pythonpython3的版本,但是强烈不推荐,修改默认python\python3版本可能会导致系统无法正常运行。

# 设置python->python3.6 python3->python3.6
sudo cp /usr/bin/python /usr/bin/python_bak
sudo rm /usr/bin/python')
sudo ln -s /usr/bin/python3.6 /usr/bin/python

sudo cp /usr/bin/python3 /usr/bin/python3_bak
sudo rm /usr/bin/python3')
sudo ln -s /usr/bin/python3.6 /usr/bin/python3

python生成的虚拟环境版本必须和编译使用的mod_wsgi版本相同:链接1 链接2 链接3 链接4 链接5

  • 方法2(推荐):安装包安装

前往github下载最新版本的mod_wsgi,上传至服务器,或使用

wget -O mod_wsgi-4.6.4.tar.gz https://codeload.github.com/GrahamDumpleton/mod_wsgi/tar.gz/4.6.4

下载,执行下列命令安装:

tar xvfz mod_wsgi-X.Y.tar.gz # 解压
cd mod_wsgi-X.Y/ # 进入解压目录
sudo ./configure --with-python=/usr/bin/python3.6 # 指定python版本编译mod_wsgi模块
sudo make # 编译
sudo make install # 安装

sudo nano /etc/apache2/mods-available/wsgi.load # 新增wsgi.load
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so # wsgi.load文件写入 加载mod_wsgi

sudo a2enmod wsgi # 开启mod_wsgi
sudo service apache2 restart # 重启apache2

mod_wsgi 【Unpacking The Source Code部分】 Stack Overflow中Valentin Kantor的回答

4、安装xsendfile模块

xsendfile主要用于文件上传下载,不需要该功能请直接跳过,该模块与编译python无关,可直接pip安装。

sudo apt-get install libapache2-mod-xsendfile
sudo a2enmod xsendfile

5、创建虚拟环境

  • 方法一(推荐):
python3.6 -m venv environment # 创建虚拟环境 python3.3后版本支持
  • 方法二:
sudo apt-get install python3-pip # 此pip对应python3.4
sudo pip3 install virtualenv # 此pip3对应python3.4
virtualenv environment --python=python3.6 # 指定创建的环境使用python3.6,不指定将使用默认的python3.4而导致无法兼容mod_wsgi编译版本。

激活\关闭虚拟环境

source environment/bin/activate 激活虚拟环境
deactivate 关闭虚拟环境

6、配置apache2

sudo chmod -R 777 /etc/apache2 # 为apache2目录开启读写权限,防止设置保存失败
sudo chmod -R /django/demo # 为项目开启读写权限,方便编辑

sudo nano /etc/apache2/site-available/demo.conf # 编辑配置文件

demo.conf:

<VirtualHost *:8000>
    ServerAdmin ykh@dreamgo.tech
    ServerName 192.168.1.102

    DocumentRoot /django/demo

    Alias /static /django/demo/static
    <Directory /django/demo/static>
        Require all granted
    </Directory>

    Alias /static /django/demo/media
    <Directory /django/demo/media>
        Require all granted
    </Directory>

    WSGIScriptAlias / /django/demo/demo/wsgi.py
    <Directory /django/demo/demo>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess 192.168.1.102 python-path=/django/demo python-home=/django/demo/environment
    WSGIProcessGroup 192.168.1.102
</VirtualHost>

使用8000端口需要配置:

sudo ufw allow 8000 防火墙开放8000端口

编辑/etc/apache2/ports.conf文件,新增Listen 8000,保存并重启Apache2。

使用命令开启站点:

sudo a2enmod wsgi
sudo a2ensite 000-default.conf
sudo service apache2 reload
sudo service apache2 restart

站点关闭命令

sudo a2dissite 000-default.conf

Apache默认的日志文件位于/var/log/apache2/error.log

配置虚拟主机:ErrorLog /django/dreamgo/error.log

Django中文语言错误:

错误提示: no translation files found for default language zh_Hans
解决方案: 在settings中将LANGUAGE_CODE = 'zh_Hans'改为LANGUAGE_CODE = 'zh-hans'
可至,对应的语言代码可至django/conf/locale/__init__.py中查找。

7、安装mysql5.7

ubuntu16.06请直接安装,apt-get install mysql-server,版本为5.7.12。

ubuntu14.04: 由于不同版本的sql无法备份,故使用和windows上相同的mysql5.7,linux apt-get 安装的为5.5.54。

步骤:

wget http://dev.mysql.com/get/mysql-apt-config_0.6.0-1_all.deb
sudo dpkg -i mysql-apt-config_0.6.0-1_all.deb
sudo apt-get update
sudo apt-get install mysql-server
mysql --version
  • 出现中文乱码错误
CREATE DATABASE <dbname> CHARACTER SET utf8;
请在创造数据库的时候指定数据库编码集为utf8;
  • pip mysqlclient时提示 mysql_config not found
sudo apt-get install libmysqlclient-dev

8、MySql由Windows迁移至Ubuntu

Windows进入C:\Program Files\MySQL\MySQL Server 5.7\bin打开cmd:

Windows:

mysqldump -uroot -proot --all-databases >C:\all_data.sql 备份全部数据库

Linux:

mysql -uroot -proot 进入mysql命令交互终端
    create database dreamgo; 新建数据库
    drop database dreamgo; 删除数据库
    show databases; 列出所有数据库
    use database_name; 使用某个数据库

通用:

mysql --version 查看mysql版本
mysqldump -uroot -p dreamgo >C:\dreamgo.sql 备份一个数据库 需要迁移的两个数据库版本相同
mysqldump -uroot -p database_name <C:\dreamgo.sql 恢复数据提示成功但是无表被添加
mysql -uroot -p database_name <C:\dreamgo.sql 恢复数据成功
mysql_upgrade -uroot -proot --force 提示系统的session表不存在时执行

9、Https支持

  1. 前往阿里云注册并下载免费CA证书,请注意绑定域名时免费的CA证书仅可绑定单个普通域名,不可绑定通用域名,既若使用的域名是二级域名的话,需要直接绑定到二级域名而不是父级域名。

  2. /etc/apache2/文件夹根目录下创建一个cert文件夹,再在cert文件夹下创建一个blog_dreamgotech_com文件夹,拷贝阿里云下载的证书压缩包中的四个文件到在/etc/apache2/cert/blog_dreamgotech_com/文件夹下。

  3. 开启ssl以及rewrite模块

sudo a2enmod rewrite
sudo a2enmod ssl
  1. 确认ports.conf如下:
Listen 80

<IfModule ssl_module>
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>
  1. 配置blog.conf文件为
<VirtualHost *:80>
    # Http重定向至Https站点
    ServerName blog.dreamgotech.com

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^blog\.dreamgotech\.com
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

<VirtualHost *:443>
    ServerName blog.dreamgotech.com
    ServerAdmin ykh@dreamgo.tech

    DocumentRoot /django/Blog/

    SSLEngine on
    # 添加 SSL 协议支持协议,去掉不安全的协议
    SSLProtocol all -SSLv2 -SSLv3
    # 修改加密套件如下
    SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
    SSLHonorCipherOrder on
    # 证书公钥配置
    SSLCertificateFile cert/blog_dreamgotech_com/public.pem
    # 证书私钥配置
    SSLCertificateKeyFile cert/blog_dreamgotech_com/xxxx.key
    # 证书链配置,如果该属性开头有 '#'字符,请删除掉
    SSLCertificateChainFile cert/blog_dreamgotech_com/chain.pem

    Alias /static /django/Blog/static/
    <Directory /django/Blog/static/>
        Require all granted
    </Directory>

    Alias /media /django/Blog/media/
    <Directory /django/Blog/media/>
        Require all granted
    </Directory>

    WSGIScriptAlias / /django/Blog/Blog/wsgi.py
    <Directory /django/Blog/Blog>
         <Files wsgi.py>
             Require all granted
         </Files>
    </Directory>

    WSGIPassAuthorization On
    WSGIDaemonProcess blog.dreamgo.tech python-path=/django/Blog python-home=/django/Blog/environment
    WSGIProcessGroup blog.dreamgo.tech
    WSGIScriptAlias / /django/Blog/Blog/wsgi.py

    ErrorLog /django/Blog/log/error.log
    CustomLog /django/Blog/log/access.log combined
</VirtualHost>
  1. 重启Apache

10、pip修改源

window上修改user/Administrator/AppData/Local/pip/pip.ini为

[global]  
index-url = http://pypi.douban.com/simple/ 
[install] 
trusted-host=pypi.douban.com 

阿里云的linux云服务器自动会修改源为阿里云,配置本地linux主机pip配置文件:

sudo mkdir ~/.pip
cd ~/.pip
sudo touch pip.conf
sudo nano pip.conf
内容修改如上

11、mysql自动备份

sudo apt-get install automysqlbackup
安装时若提示选择mail时使用左右键选中OK继续即可

automysqlbackup的默认配置文件位于/etc/default/automysqlbackup文件
sudo nano /etc/default/automysqlbackup

新建备份目录"/django/mysql_backup"并赋予777权限

automysqlbackup,需要配置账户、密码、备份目录、备份频率、待备份数据库名称列表:

# By default, the Debian version of automysqlbackup will use:
# mysqldump --defaults-file=/etc/mysql/debian.cnf
# but you might want to overwrite with a specific user & pass.
# To do this, simply edit bellow.

# Username to access the MySQL server e.g. dbuser
USERNAME="root"

# Username to access the MySQL server e.g. password
PASSWORD="root"

# Host name (or IP address) of MySQL server e.g localhost
DBHOST=localhost

# List of DBNAMES for Daily/Weekly Backup e.g. "DB1 DB2 DB3"
# Note that it's absolutely normal that the db named "mysql" is not in this
# list, as it's added later by the script. See the MDBNAMES directives below
# in this file (advanced options).
# This is ONLY a convenient default, if you don't like it, don't complain
# and write your own.
# The following is a quick hack that will find the names of the databases by
# reading the mysql folder content. Feel free to replace by something else.
DBNAMES="all"

# Backup directory location e.g /backups
# Folders inside this one will be created (daily, weekly, etc.), and the
# subfolders will be database names. Note that backups will be owned by
# root, with Unix rights 0600.
BACKUPDIR="/django/mysql_backup"

# Mail setup
# What would you like to be mailed to you?
# - log   : send only log file
# - files : send log file and sql files as attachments (see docs)
# - stdout : will simply output the log to the screen if run manually.
# - quiet : Only send logs if an error occurs to the MAILADDR.
MAILCONTENT="files"

# Set the maximum allowed email size in k. (4000 = approx 5MB email [see
# docs])
MAXATTSIZE="4000"

# Email Address to send mail to? (user@domain.com)
MAILADDR="ykh@dreamgo.tech"

# ============================================================
# === ADVANCED OPTIONS ( Read the doc's below for details )===
#=============================================================

# List of DBBNAMES for Monthly Backups.
MDBNAMES="mysql $DBNAMES"

# List of DBNAMES to EXLUCDE if DBNAMES are set to all (must be in " quotes)
DBEXCLUDE="information_schema performance_schema sys mysql"

# Include CREATE DATABASE in backup?
CREATE_DATABASE=yes

# Separate backup directory and file for each DB? (yes or no)
SEPDIR=yes

# Which day do you want weekly backups? (1 to 7 where 1 is Monday)
DOWEEKLY=6

# Choose Compression type. (gzip or bzip2)
COMP=gzip

# Compress communications between backup server and MySQL server?
COMMCOMP=no

# Additionally keep a copy of the most recent backup in a seperate
# directory.
LATEST=no

#  The maximum size of the buffer for client/server communication. e.g. 16MB
#  (maximum is 1GB)
MAX_ALLOWED_PACKET=

#  For connections to localhost. Sometimes the Unix socket file must be
#  specified.
SOCKET=

# Command to run before backups (uncomment to use)
#PREBACKUP="/etc/mysql-backup-pre"

# Command run after backups (uncomment to use)
#POSTBACKUP="/etc/mysql-backup-post"

# Backup of stored procedures and routines (comment to remove)
ROUTINES=yes

执行命令

sudo automysqlbackup /etc/default/automysqlbackup 

运行配置即可在对应的备份目录看到备份的数据库文件

10、常见问题&备注

  • ubuntu文件操作
sudo touch xxx.etx 新建文件
sudo mkdir xxx 新建目录
sudo mv dir1 dir2 剪切文件夹
sudo rm -rf dir 删除文件夹
sudo cp file dir 拷贝文件至某个文件夹
  • Pillow安装错误
sudo apt-get install python3-dev
sudo apt-get install libjpeg8-dev
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib

pip install pillow
  • 服务器500错误

如果有log文件,必须对log文件所在的父级目录授权。

  • Nginx VS Apache2

Apache适用于计算密集型的动态请求(后端),Nginx适合IO密集型的静态文件分发和反向代理(前端)。

  • 域名重定向

最近为博客更换了新域名,不希望之前看博客的人找不到新域名,又不想部署两个站点导致内容割裂。于是利用Apache的域名重定向功能,将旧的域名请求直接重定向至新域名下。如访问旧域名blog.dreamgotech.com/article/26/将被重定向到新域名www.yinkh.top/article/26/。具体实现:

<VirtualHost *:80>
    ServerName xy.example.com
    RedirectPermanent / http://abc.example.com/
</VirtualHost>

对于我的博客来说,由于有httphttps之间的重定向,于是设置如下:

<VirtualHost *:80>
    ServerName blog.dreamgotech.com

    RedirectPermanent / http://www.yinkh.top/
</VirtualHost>

<VirtualHost *:443>
    ServerName blog.dreamgotech.com
    ServerAdmin ykh@dreamgo.tech

    RedirectPermanent / https://www.yinkh.top/

    SSLEngine on
    SSLProtocol all -SSLv2 -SSLv3
    SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
    SSLHonorCipherOrder on
    SSLCertificateFile cert/blog_dreamgotech_com/public.pem
    SSLCertificateKeyFile cert/blog_dreamgotech_com/214278817430753.key
    SSLCertificateChainFile cert/blog_dreamgotech_com/chain.pem
</VirtualHost>

2017-08-30 版本

Linux部署Apache生产环境

Ubuntu 14.04
Apache 2.4.7
Python 3.4
Django 1.10.6

1、ubuntu安装python3.5

ubuntu14.04系统会自带python2.7,请不要卸载它。

sudo apt-get install python-software-properties
sudo apt-get install software-properties-common

sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python3.5

sudo cp /usr/bin/python /usr/bin/python_bak 先备份
sudo rm /usr/bin/python 删除
sudo ln -s /usr/bin/python3.5 /usr/bin/python 默认设置成python3.5

2、安装apache等必要模块

sudo apt-get update
sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3

在python3环境下使用pip3安装软件而不是pip(python2使用)

sudo pip3 install virtualenv

virtualenv dreamgo_env 新建虚拟环境
source dreamgo/bin/activate 激活虚拟环境
deactivate 关闭虚拟环境

sudo apt-get install gedit

3、配置apache2

sudo chmod -R 777 /etc/apache2 为apache2目录开启读写权限,防止设置保存失败
sudo chmod -R /home/dreamgo/dreamgo_official_site 为项目开启读写权限,方便编辑

直接进入/etc/apache2/site-available/文件夹编辑000-default.conf或者使用命令:
sudo gedit /etc/apache2/site-available/000-default.conf编辑配置文件

000-default.conf:

<VirtualHost *:8000>
    ServerAdmin ykh@dreamgo.tech
    ServerName 192.168.1.102

    DocumentRoot /home/dreamgo/dreamgo_official_site

    Alias /static /home/dreamgo/dreamgo_official_site/static
    <Directory /home/dreamgo/dreamgo_official_site/static>
        Require all granted
    </Directory>

    <Directory /home/dreamgo/dreamgo_official_site/dreamgo_official_site>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess 192.168.1.102 python-path=/home/dreamgo/dreamgo_official_site python-home=/home/dreamgo/dreamgo_official_site/dreamgo_env
    WSGIProcessGroup 192.168.1.102
    WSGIScriptAlias / /home/dreamgo/dreamgo_official_site/dreamgo_official_site/wsgi.py
</VirtualHost>

使用8000端口需要配置:

sudo ufw allow 8000 防火墙开放8000端口
编辑/etc/apache2/ports.conf文件,新增Listen 8000,保存。

使用命令开启站点:

sudo a2enmod wsgi
sudo a2ensite 000-default.conf
sudo service apache2 reload
sudo service apache2 restart

站点关闭命令

sudo a2dissite 000-default.conf

Django中文语言错误:

错误提示: no translation files found for default language zh_Hans
解决方案: 在settings中将LANGUAGE_CODE = 'zh_Hans'改为LANGUAGE_CODE = 'zh-hans'
可至,对应的语言代码可至django/conf/locale/__init__.py中查找。

4、MySql由Windows迁移至Ubuntu

Windows进入C:\Program Files\MySQL\MySQL Server 5.7\bin打开cmd:

Windows:

mysqldump -uroot -proot --all-databases >C:\all_data.sql 备份全部数据库

Linux:

mysql -uroot -proot 进入mysql命令交互终端
    create database dreamgo; 新建数据库
    drop database dreamgo; 删除数据库
    show databases; 列出所有数据库
    use database_name; 使用某个数据库

通用:

mysql --version 查看mysql版本
mysqldump -uroot -p dreamgo >C:\dreamgo.sql 备份一个数据库 需要迁移的两个数据库版本相同
mysqldump -uroot -p database_name <C:\dreamgo.sql 恢复数据提示成功但是无表被添加
mysql -uroot -p database_name <C:\dreamgo.sql 恢复数据成功
mysql_upgrade -uroot -proot --force 提示系统的session表不存在时执行

5、Apache模块配置

pip 安装wsgi的库时会自动向/etc/apache2/mods-available/中添加wsgi.conf和wsgi.load

wsgi.load:

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
mod_wsgi.so位于文件夹/usr/lib/apache2/modules/中

Apache默认的日志文件位于/var/log/apache2/error.log

配置虚拟主机:ErrorLog /django/dreamgo/error.log

安装xsendfile,用于文件上传下载。

sudo apt-get install libapache2-mod-xsendfile
sudo a2enmod xsendfile

6、文件操作

sudo touch xxx.etx 新建文件
sudo mkdir xxx 新建目录
sudo mv dir1 dir2 剪切文件夹
sudo rm -rf dir 删除文件夹

7、配置项目

Pillow安装错误

sudo apt-get install python3-dev
sudo apt-get install libjpeg8-dev
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib

pip install pillow

如果有log文件,必须对log文件所在的父级目录授权。

8、安装mysql5.7

由于不同版本的sql无法备份,故使用和windows上相同的mysql5.7,linux apt-get 安装的为5.5.54。

步骤:

wget http://dev.mysql.com/get/mysql-apt-config_0.6.0-1_all.deb
sudo dpkg -i mysql-apt-config_0.6.0-1_all.deb
sudo apt-get update
sudo apt-get install mysql-server
mysql --version
  • 出现中文乱码错误
CREATE DATABASE <dbname> CHARACTER SET utf8;
请在创造数据库的时候指定数据库编码集为utf8;
  • pip mysqlclient时提示 mysql_config not found
sudo apt-get install libmysqlclient-dev

9、Https支持

  1. 前往阿里云注册并下载免费CA证书,请注意绑定域名时免费的CA证书仅可绑定单个普通域名,不可绑定通用域名,既若使用的域名是二级域名的话,需要直接绑定到二级域名而不是父级域名。

  2. /etc/apache2/文件夹根目录下创建一个cert文件夹,再在cert文件夹下创建一个blog_dreamgotech_com文件夹,拷贝阿里云下载的证书压缩包中的四个文件到在/etc/apache2/cert/blog_dreamgotech_com/文件夹下。

  3. 开启ssl以及rewrite模块

sudo a2enmod rewrite
sudo a2enmod ssl
  1. 确认ports.conf如下:
Listen 80

<IfModule ssl_module>
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>
  1. 配置blog.conf文件为
<VirtualHost *:80>
    # Http重定向至Https站点
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

<VirtualHost *:443>
    ServerName blog.dreamgotech.com
    ServerAdmin ykh@dreamgo.tech

    DocumentRoot /django/Blog/

    SSLEngine on
    # 添加 SSL 协议支持协议,去掉不安全的协议
    SSLProtocol all -SSLv2 -SSLv3
    # 修改加密套件如下
    SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
    SSLHonorCipherOrder on
    # 证书公钥配置
    SSLCertificateFile cert/blog_dreamgotech_com/public.pem
    # 证书私钥配置
    SSLCertificateKeyFile cert/blog_dreamgotech_com/xxxx.key
    # 证书链配置,如果该属性开头有 '#'字符,请删除掉
    SSLCertificateChainFile cert/blog_dreamgotech_com/chain.pem

    Alias /static /django/Blog/static/
    <Directory /django/Blog/static/>
        Require all granted
    </Directory>

    Alias /media /django/Blog/media/
    <Directory /django/Blog/media/>
        Require all granted
    </Directory>

    WSGIScriptAlias / /django/Blog/Blog/wsgi.py
    <Directory /django/Blog/Blog>
         <Files wsgi.py>
             Require all granted
         </Files>
    </Directory>

    WSGIPassAuthorization On
    WSGIDaemonProcess blog.dreamgo.tech python-path=/django/Blog python-home=/django/Blog/environment
    WSGIProcessGroup blog.dreamgo.tech
    WSGIScriptAlias / /django/Blog/Blog/wsgi.py

    ErrorLog /django/Blog/log/error.log
    CustomLog /django/Blog/log/access.log combined
</VirtualHost>
  1. 重启Apache

10、pip修改源

window上修改user/Administrator/AppData/Local/pip/pip.ini为

[global]  
index-url = http://pypi.douban.com/simple/ 
[install] 
trusted-host=pypi.douban.com 

阿里云的linux云服务器自动会修改源为阿里云,配置本地linux主机pip配置文件:

sudo mkdir ~/.pip
cd ~/.pip
sudo touch pip.conf
sudo gedit pip.conf
内容修改如上

11、mysql自动备份

sudo apt-get install automysqlbackup
安装时若提示选择mail时使用左右键选中OK继续即可

automysqlbackup的默认配置文件位于/etc/default/automysqlbackup文件
sudo gedit /etc/default/automysqlbackup

新建备份目录"/django/mysql_backup"并赋予777权限

automysqlbackup,需要配置账户、密码、备份目录、备份频率、待备份数据库名称列表:

# By default, the Debian version of automysqlbackup will use:
# mysqldump --defaults-file=/etc/mysql/debian.cnf
# but you might want to overwrite with a specific user & pass.
# To do this, simply edit bellow.

# Username to access the MySQL server e.g. dbuser
USERNAME="root"

# Username to access the MySQL server e.g. password
PASSWORD="root"

# Host name (or IP address) of MySQL server e.g localhost
DBHOST=localhost

# List of DBNAMES for Daily/Weekly Backup e.g. "DB1 DB2 DB3"
# Note that it's absolutely normal that the db named "mysql" is not in this
# list, as it's added later by the script. See the MDBNAMES directives below
# in this file (advanced options).
# This is ONLY a convenient default, if you don't like it, don't complain
# and write your own.
# The following is a quick hack that will find the names of the databases by
# reading the mysql folder content. Feel free to replace by something else.
DBNAMES="all"

# Backup directory location e.g /backups
# Folders inside this one will be created (daily, weekly, etc.), and the
# subfolders will be database names. Note that backups will be owned by
# root, with Unix rights 0600.
BACKUPDIR="/django/mysql_backup"

# Mail setup
# What would you like to be mailed to you?
# - log   : send only log file
# - files : send log file and sql files as attachments (see docs)
# - stdout : will simply output the log to the screen if run manually.
# - quiet : Only send logs if an error occurs to the MAILADDR.
MAILCONTENT="quiet"

# Set the maximum allowed email size in k. (4000 = approx 5MB email [see
# docs])
MAXATTSIZE="4000"

# Email Address to send mail to? (user@domain.com)
MAILADDR="ykh@dreamgo.tech"

# ============================================================
# === ADVANCED OPTIONS ( Read the doc's below for details )===
#=============================================================

# List of DBBNAMES for Monthly Backups.
MDBNAMES="mysql $DBNAMES"

# List of DBNAMES to EXLUCDE if DBNAMES are set to all (must be in " quotes)
DBEXCLUDE="information_schema performance_schema sys"

# Include CREATE DATABASE in backup?
CREATE_DATABASE=yes

# Separate backup directory and file for each DB? (yes or no)
SEPDIR=yes

# Which day do you want weekly backups? (1 to 7 where 1 is Monday)
DOWEEKLY=6

# Choose Compression type. (gzip or bzip2)
COMP=gzip

# Compress communications between backup server and MySQL server?
COMMCOMP=no

# Additionally keep a copy of the most recent backup in a seperate
# directory.
LATEST=no

#  The maximum size of the buffer for client/server communication. e.g. 16MB
#  (maximum is 1GB)
MAX_ALLOWED_PACKET=

#  For connections to localhost. Sometimes the Unix socket file must be
#  specified.
SOCKET=

# Command to run before backups (uncomment to use)
#PREBACKUP="/etc/mysql-backup-pre"

# Command run after backups (uncomment to use)
#POSTBACKUP="/etc/mysql-backup-post"

# Backup of stored procedures and routines (comment to remove)
ROUTINES=yes

执行命令

sudo automysqlbackup /etc/default/automysqlbackup 

运行配置即可在对应的备份目录看到备份的数据库文件

12、阿里云上xfce4中文乱码

下载中文支持包:
sudo apt-get install language-pack-zh-hans
下载中文字体:
sudo apt-get install fonts-wqy-zenhei