title: flask项目部署到阿里云 ubuntu16.04
date: 2018.3.6
项目地址: 我的博客
部署思路参考: Flask Web开发》的个人部署版本,包含学习笔记。
开始之前
购买阿里云ecs一周版, 参考
购买万网域名(也是阿里家的)
过程:
# apt update
# apt upgrade
# apt install python-virtualenv
# apt install nginx git
下载源码:
# git clone 我的博客源码地址 && cd 目录
创建虚拟环境(出现问题, 重新ssh后解决?):
# virtualenv venv
...
return _setlocale(category, locale)
locale.Error: unsupported locale setting
----------------------------------------
...Installing setuptools, pkg_resources, pip, wheel...done.
...
OSError: Command /root/myFlasky/venv/bin/python2 - setuptools pkg_resources pip wheel failed with error code 1
# 解决locale.Error:
locale-gen en_US en_US.UTF-8
dpkg-reconfigure locales
# 之后勾选 all 和 en_US.UTF-8
尝试:
apt-get purge python-virtualenv python3-virtualenv virtualenv
pip install --upgrade pip
pip install virtualenv
# 再次创建venv环境出现:
setuptools pkg_resources pip wheel failed with error code 2
# 原因, ssh超时后重新连接未发现出错:
`HTTPError: 404 Client Error: Not Found for url: `
File "/root/myFlasky/venv/share/python-wheels/requests-2.9.1-py2.py3-none-any.whl/requests/models.py", line 840, in raise_for_status
raise HTTPError(http_error_msg, response=self)
HTTPError: 404 Client Error: Not Found for url: http://mirrors.aliyun.com/pypi/simple/pkg-resources/
激活环境,安装mysql并设置编码为utf8
:
# source venv/bin/activate
(venv) # apt install mysql-server mysql-client python-mysqldb libmysqlclient-dev
(venv) # mysql -uroot -p
mysql> show variables like "%character%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
mysql> quit
Bye
(venv) # vim /etc/mysql/mysql.conf.d/mysqld.cnf
# 来源:https://www.douban.com/note/602158227/
# 在 [mysqld] 域下加上:character-set-server=utf8
# 重新启动MySQL服务
(venv) # systemctl restart mysql
(venv) # mysql -uroot -p
mysql> show variables like "%character%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
mysql>
使用(venv) # pip install -r requirements.txt
安装运行环境
EnvironmentError: mysql_config not found
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-Z_FMKV/MySQL-python/
# 安装mysql见上
build/temp.linux-x86_64-2.7/_openssl.c:501:25: fatal error: openssl/aes.h: No such file or directory
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
# To fix this problem, you have to install OpenSSL development package
命令: # apt install libssl-dev
sql.h: No such file or directory
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
# apt install unixodbc unixodbc-dev
sqlite3.h: No such file or directory
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
# apt install libsqlite3-dev
创建数据库, CHARACTER SET 'utf8'
和COLLATE 'utf8_general_ci'
是为了防止中文乱码:
# mysql -uroot -p
mysql> CREATE DATABASE blogdata
-> CHARACTER SET 'utf8'
-> COLLATE 'utf8_general_ci';
Query OK, 1 row affected (0.00 sec)
mysql> quit
配置环境变量:
(venv) # vim ~/os_environ.sh
#! /bin/bash
echo 'start!'
unset MAIL_SERVER MAIL_USERNAME MAIL_PASSWORD BLOG_ADMIN BLOG_MAIL_SENDER
echo 'clean!'
export MAIL_SERVER=* MAIL_USERNAME=* MAIL_PASSWORD=* BLOG_ADMIN=* BLOG_MAIL_SENDER=* DATABASE='blogdata' DB_USERNAME='root' DB_PASSWORD=* FLASK_CONFIG=production
echo $MAIL_SERVER $MAIL_USERNAME $MAIL_PASSWORD $BLOG_ADMIN $BLOG_MAIL_SENDER $DATABASE $DB_USERNAME $DB_PASSWORD $FLASK_CONFIG
echo 'successful!'
配置nginx:
(venv) # /etc/init.d/nginx start
[ ok ] Starting nginx (via systemctl): nginx.service.
# 备份
(venv) # mv /etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/.backup-default
(venv) # touch /etc/nginx/sites-available/flask-blog
(venv) # ln -s /etc/nginx/sites-available/flask-blog /etc/nginx/sites-enabled/flask-blog
(venv) # pwd
{{ url }} # 查看当前路径
(venv) # vim /etc/nginx/sites-enabled/flask-blog
添加下面内容并保存:
server {
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static {
alias {{ url }}/app/static/;
}
}
由于nginx默认以自己的用户(www-data)运行, 所以访问网站时会出现静态文件无法访问的情况(403
), 故需要修改nginx配置文件:
# 测试nginx配置文件, 由此查看配置文件路径
(venv) # nginx -t
(venv) # vim */nginx.conf
# 将用户改为当前用户:
user *;
# 重启nginx
(venv) # /etc/init.d/nginx restart
创建数据库表, 并写入角色和管理员
(venv) # source ~/os_environ.sh
(venv) # python manage.py shell
>>> db.create_all()
>>> Role.insert_roles()
>>> db.session.commit()
>>>
(venv) # python manage.py shell
>>> l = Role.query.all()
>>> l
[<Role u'Moderator'>, <Role u'Administrator'>, <Role u'User'>]
>>> admin = l[1]
>>> admin
<Role u'Administrator'>
>>> u = User(email='*', username='*', password='*', confirmed=True, role=admin)
>>> db.session.add(u)
>>> db.session.commit()
>>>
安装独角兽并尝试启动博客:
(venv) # pip install gunicorn
# manage:app 会直接运行manage.py.__main__ 并绑定到8000端口, 而nginx中已经配置proxy_pass代理转发
(venv) $ gunicorn manage:app -b localhost:8000
通过supervisor启动gunicorn
(venv) # pip install supervisor
(venv) # echo_supervisord_conf > supervisor.conf
(venv) # vim supervisor.conf
(venv) # supervisord -c supervisor.conf
在最后添加:
[program:flask-blog]
command = gunicorn manage:app -b localhost:8000
directory = *
autorestart = true
user = *
启动:
(venv) # supervisord -c supervisor.conf
最后
网站放在阿里云是真的舒服, 之前放在vultr上ssh有时候连不上, 网站部署上去后阿里云的速度也比vultr(迈阿密)快很多, 毕竟才10+延迟, 唯一的缺点就是, 这个价格对穷学生来说有点小贵, 本来还想解析域名的, 结果发现需要备案, 先用一周看看吧
更多:
centos部署flask项目
nginx 配置之 proxy_pass 神器
nginx代理proxy_pass绝对路径和相对路径实验
使用 supervisor 管理进程