《Flask入门》第四章:一块宝藏,如何白嫖pythonanywhere制作爆款网站
想要部署网站从来都不是一个简单的工程,也不是一个传统的项目,需要人力成本和物力成本。最基本的物力域名,服务器等…并且坑也不少,好在我年初就踩完了.因为不可抗力,我的云服务器到期了.所以这期讲讲如何pythoanywhere这种国外的高质量免费服务轻松构建网站.
优点
-
支持多类型框架部署:flask,django等
-
支持多python版本
-
一键强制部署https(免证书)
-
提供简短的子域名(name.pythonanywhere.com)
-
免费提供数据库服务
-
共512M个人存储空间
-
没有流量限制,访问速度也不错
缺点
-
不能自定义Cname到自己的域名(需要会员)
-
不允许发送Socket(无法使用flask_socketio,发送Email等)
-
尴尬的调试和错误日志
-
每三个月要求登入,否则删除网站
-
无法对其他网站进行GET或POST
-
在路径方面十分尴尬
pythonanywhere算良心,按照自己的需求就ok了
开工
网址:https://www.pythonanywhere.com/
提示:不建议在cloud上调试,Pythonanywhere的调试方法也一样蛋疼
注册
观看他们的视频:https://www.youtube.com/watch?v=NH2PhXYvrWs

建议直接转到:https://www.pythonanywhere.com/registration/register/beginner/

哈,注册是轻车熟路,用户名是网站前缀.

Pythonanywhere的服务可以说是一个虚拟主机,你可以在512M的空间内创建文件(linux路径),可以创建终端,运行py程序或者执行cmd命令,例如git clone.
打开Webs app标签页,新建网站,点击flask,把路径简单修改下,如/home/your-name/your-project/app.py,建议project名称与项目的git仓库相同.
新建成功并运行后,访问your-name.pythonanywhere.com,就可以看到默认界面了.

用Git上传自己的项目
在默认路径打开一个终端,直接git clone即可,项目名称与直接的project路径相同,运行文件为app.py,然后回到webs app中点击Reload yourname.pythonanywhere.com即可,如果访问运行失败或者500,在webs app中下拉找到错误日志查看即可.
防止踩坑坑
使用pip安装使用–user
如果按普通方式安装,如
1 | pip install buildapi |
会以下报错
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 04:58 ~ $ pip install buildapi DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/ latest/development/release-process/#python-2-support Looking in links: /usr/share/pip-wheels Collecting buildapi Downloading https://files.pythonhosted.org/packages/b8/bd/ffd2a061322e42903a112ff17099b285d87d1571151f2a9ea62830fe3311/buildapi-0.0.1.tar.gz Building wheels for collected packages: buildapi Building wheel for buildapi (setup.py) ... done Created wheel for buildapi: filename=buildapi-0.0.1-cp27-none-any.whl size=1252 sha256=5d715c80f1283a7d44949269ab986599d8feb3274adc25f4efc94b121c5 1a582 Stored in directory: /home/snbck/.cache/pip/wheels/df/b3/a1/bfeda0ad912aeffbc18e1ee9dfbae4a354f4e3024589490298 Successfully built buildapi Installing collected packages: buildapi ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/buildapi-0.0.1.d ist-info' Consider using the `--user` option or check the permissions. 05:00 ~ $ |
正确的优雅安装方法
1 | pip install --user flyvar |
正确的优雅
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 04:57 ~ $ pip install --user flyvar DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/ latest/development/release-process/#python-2-support Looking in links: /usr/share/pip-wheels Collecting flyvar Downloading https://files.pythonhosted.org/packages/67/f2/0c5070f074eede31853f43379d6c96889773bc4c263613fd4fab98aabee6/flyvar-1.0.4.tar.gz Building wheels for collected packages: flyvar Building wheel for flyvar (setup.py) ... done Created wheel for flyvar: filename=flyvar-1.0.4-cp27-none-any.whl size=2056 sha256=b4d26db83671eb7bfa7e842b4823d0da79855d21f5b8ea9fab7f1480d51c95e 7 Stored in directory: /home/snbck/.cache/pip/wheels/71/6e/c6/4accae2e6a469dc673f76f84f4b1e1a6b25c4694cd9e00248b Successfully built flyvar Installing collected packages: flyvar Successfully installed flyvar-1.0.4 |