Hi I’m working on my first flask project, very simple one.
I’m deploying my site on Digital Ocean, ubuntu server. And I complied with the instruction hereHow To Deploy a Flask Application on an Ubuntu VPS
Problem When I tried to open the site in browser, it came with 500 error. Then I looked up the apache error log to find the following sentences.
Error log
[Thu Jan 01 01:35:21 2015] [error] [client 112.64.71.131] Traceback (most recent call last):
[Thu Jan 01 01:35:21 2015] [error] [client 112.64.71.131] File "/var/www/qianshan/qianshan.wsgi", line 7, in <module>
[Thu Jan 01 01:35:21 2015] [error] [client 112.64.71.131] from qianshan import app as application
[Thu Jan 01 01:35:21 2015] [error] [client 112.64.71.131] ImportError: cannot import name app
Tree structure of the project
.
├── qianshan
│ ├── config.ini
│ ├── __init__.py
│ ├── static
│ ├── templates
│ └── venv
└── qianshan.wsgi
Virtual Host configuration
<VirtualHost *:80>
ServerName qianshan.co
ServerAdmin spark@qianshan.co
WSGIScriptAlias / /var/www/qianshan/qianshan.wsgi
<Directory /var/www/qianshan/qianshan/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/qianshan/qianshan/static
<Directory /var/www/qianshan/qianshan/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
wsgi
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/qianshan/")
from qianshan import app as application
application.secret_key = 'Add your secret key'
init.py file
# Filename: __init__.py
# encoding: utf-8
import ConfigParser
import codecs
from flask import Flask
from flask import render_template
app = Flask(__name__)
@app.route('/')
def index():
block_list = getBlockList()
website_dict = getWebsiteDict()
return render_template('index.html', block_list=block_list, website_dict=website_dict)
...
...
if __name__ == '__main__':
app.run()
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
That error generally means that Flask was not installed correctly. Did you receive any error messages when you installed Flask into the virtualenv?
cd /var/www/qianshan/qianshan/
source venv/bin/activate
sudo pip install Flask
Does the Flask app run when you call it directly from inside the virtualenv using sudo python __init__.py ?
If you saw an error similar to this:
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c markupsafe/_speedups.c -o build/temp.linux-x86_64-2.7/markupsafe/_speedups.o
markupsafe/_speedups.c:12:20: fatal error: Python.h: No such file or directory
#include <Python.h>
^
compilation terminated.
==========================================================================
WARNING: The C extension could not be compiled, speedups are not enabled.
Failure information, if any, is above.
Retrying the build without the C extension now.
==========================================================================
WARNING: The C extension could not be compiled, speedups are not enabled.
Plain-Python installation succeeded.
==========================================================================
when trying to install Flask, that means you need to install the development headers for Python in order to compile the C extension. This can be done with:
sudo apt-get install python-dev
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.