我正在尝试让我的服务器使用 EC2 运行,当访问它时,它会给出:
内部服务器错误:服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序中存在错误。
代码可在以下位置找到: https://github.com/amrfarid140/CatalogEC2
对于 Apache 错误.log:
[Sat Mar 05 14:39:51.558283 2016] [mpm_event:notice] [pid 2651:tid 140164302710656] AH00494: SIGHUP received. Attempting to restart
AH00557: apache2: apr_sockaddr_info_get() failed for ip-10-20-18-130
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
[Sat Mar 05 14:39:51.564079 2016] [mpm_event:notice] [pid 2651:tid 140164302710656] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Sat Mar 05 14:39:51.564095 2016] [core:notice] [pid 2651:tid 140164302710656] AH00094: Command line: '/usr/sbin/apache2'
[Sat Mar 05 14:42:49.305564 2016] [mpm_event:notice] [pid 2651:tid 140164302710656] AH00494: SIGHUP received. Attempting to restart
AH00557: apache2: apr_sockaddr_info_get() failed for ip-10-20-18-130
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
[Sat Mar 05 14:42:49.310625 2016] [mpm_event:notice] [pid 2651:tid 140164302710656] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Sat Mar 05 14:42:49.310640 2016] [core:notice] [pid 2651:tid 140164302710656] AH00094: Command line: '/usr/sbin/apache2'
[Sat Mar 05 15:09:30.180818 2016] [mpm_event:notice] [pid 2651:tid 140164302710656] AH00491: caught SIGTERM, shutting down
[Sat Mar 05 15:09:31.172919 2016] [mpm_event:notice] [pid 3685:tid 139877376771968] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Sat Mar 05 15:09:31.172989 2016] [core:notice] [pid 3685:tid 139877376771968] AH00094: Command line: '/usr/sbin/apache2'
[Sat Mar 05 16:32:21.194956 2016] [mpm_event:notice] [pid 3685:tid 139877376771968] AH00491: caught SIGTERM, shutting down
[Sat Mar 05 16:32:22.184800 2016] [mpm_event:notice] [pid 4556:tid 139681590728576] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Sat Mar 05 16:32:22.184871 2016] [core:notice] [pid 4556:tid 139681590728576] AH00094: Command line: '/usr/sbin/apache2'
[Sat Mar 05 16:33:57.787514 2016] [core:warn] [pid 4556:tid 139681590728576] AH00045: child process 4560 still did not exit, sending a SIGTERM
[Sat Mar 05 16:33:59.792648 2016] [core:warn] [pid 4556:tid 139681590728576] AH00045: child process 4560 still did not exit, sending a SIGTERM
[Sat Mar 05 16:34:01.794970 2016] [core:warn] [pid 4556:tid 139681590728576] AH00045: child process 4560 still did not exit, sending a SIGTERM
[Sat Mar 05 16:34:03.797396 2016] [core:error] [pid 4556:tid 139681590728576] AH00046: child process 4560 still did not exit, sending a SIGKILL
[Sat Mar 05 16:34:04.798716 2016] [mpm_event:notice] [pid 4556:tid 139681590728576] AH00491: caught SIGTERM, shutting down
[Sat Mar 05 16:34:05.523737 2016] [mpm_event:notice] [pid 4749:tid 139813688158080] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Sat Mar 05 16:34:05.523811 2016] [core:notice] [pid 4749:tid 139813688158080] AH00094: Command line: '/usr/sbin/apache2'
[Sat Mar 05 16:37:51.054771 2016] [mpm_event:notice] [pid 4749:tid 139813688158080] AH00494: SIGHUP received. Attempting to restart
AH00557: apache2: apr_sockaddr_info_get() failed for ip-10-20-18-130
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
[Sat Mar 05 16:37:51.060082 2016] [mpm_event:notice] [pid 4749:tid 139813688158080] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Sat Mar 05 16:37:51.060099 2016] [core:notice] [pid 4749:tid 139813688158080] AH00094: Command line: '/usr/sbin/apache2'
目录.wgsi:
import sys
sys.path.insert(0, '/var/www/catalog')
from application import app as application
Application.py:
# setup flask
from flask import Flask, render_template, request, redirect, url_for, jsonify, flash, g, session
from flask.ext.seasurf import SeaSurf
app = Flask(__name__)
csrf = SeaSurf(app)
# setup ssl if needed
"""import ssl
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
context.load_cert_chain('/etc/ssl/private/server.pem')"""
# setup client id and secret of github application for oauth
from flask.ext.github import GitHub
# config application
app.config['GITHUB_CLIENT_ID'] = 'XXX'
app.config['GITHUB_CLIENT_SECRET'] = 'YYY'
app_secret = 'ZZZ'
github_callback_url = "https://dev.anryousef.de/github-callback"
github = GitHub(app)
# setup sqlalchemy
from sqlalchemy import create_engine, desc
from sqlalchemy.orm import sessionmaker, scoped_session
from database_setup import Base, Category, Item, User
engine = create_engine("postgresql://catalog:qazzaq@localhost/catalog")
Base.metadata.bind = engine
# http://docs.sqlalchemy.org/en/rel_0_8/orm/session.html#sqlalchemy.orm.scoping.scoped_session
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base.query = db_session.query_property()
# flask functions
##################
@app.route('/')
@app.route('/catalog/')
def showCatalog():
categories = Category.query.all()
items = Item.query.order_by(Item.created.desc()).all()
return render_template('catalog.html', categories=categories, items=items, user=g.user)
@app.route('/catalog/<string:category_name>/')
def showCategory(category_name):
categories = Category.query.all()
category = Category.query.filter_by(name=category_name).one()
items = Item.query.filter_by(category=category).all()
return render_template('catalog.html', categories=categories, items=items, category=category, user=g.user)
@app.route('/item/<int:item_id>/')
def showItem(item_id):
item = Item.query.filter_by(id=item_id).one()
return render_template('item.html', item=item, user=g.user)
@app.route('/catalog/<string:category_name>/add/', methods=['GET', 'POST'])
def addItem(category_name):
categories = Category.query.all()
category = Category.query.filter_by(name=category_name).one()
user = g.user
# check if user is authenticated
if user is not None:
if request.method == 'POST':
# check if an item name was entered
if request.form['name'] != "":
mycategory = Category.query.filter_by(
name=request.form['category']).one()
item = Item(name=request.form['name'],
image=request.form['image'],
description=request.form['description'],
category=mycategory,
owner=user)
db_session.add(item)
db_session.commit()
flash("Item " + item.name + " added to " + item.category.name)
return redirect(url_for('showCategory', category_name=mycategory.name))
else:
flash("Item name must not be empty")
return redirect(url_for('addItem', category_name=category_name))
else:
return render_template('additem.html', category=category, categories=categories, user=user)
else:
flash("Unauthorized user")
return redirect(url_for('showCatalog'))
@app.route('/item/<int:item_id>/edit/', methods=['GET', 'POST'])
def editItem(item_id):
item = Item.query.filter_by(id=item_id).one()
user = g.user
# check if user is owner
if user == item.owner:
if request.method == 'POST':
# check if an item name was entered
if request.form['name'] != "":
item.name = request.form['name']
item.image = request.form['image']
item.description = request.form['description']
if request.form['category']:
category = Category.query.filter_by(
name=request.form['category']).one()
item.category = category
db_session.add(item)
db_session.commit()
flash("Item " + item.name + " saved")
return redirect(url_for('showItem', item_id=item.id))
else:
flash("Item name must not be empty")
return redirect(url_for('editItem', item_id=item.id))
else:
categories = Category.query.all()
return render_template('edititem.html', item=item, categories=categories, user=user)
else:
flash("Unauthorized user")
return redirect(url_for('showCatalog'))
@app.route('/item/<int:item_id>/delete/', methods=['GET', 'POST'])
def deleteItem(item_id):
item = Item.query.filter_by(id=item_id).one()
category = item.category
user = g.user
# check if user is owner
if user == item.owner:
if request.method == 'POST':
flash("Item " + item.name + " deleted")
db_session.delete(item)
db_session.commit()
return redirect(url_for('showCategory', category_name=category.name))
else:
return render_template('deleteitem.html', item=item, user=g.user)
else:
flash("Unauthorized user")
return redirect(url_for('showCatalog'))
@app.route('/login')
def loginUser():
# send user back to the source page
uri = github_callback_url + "?next=" + request.referrer
return github.authorize(redirect_uri=uri)
# if session.get('user_id', None) is None:
# return github.authorize()
# else:
# flash('User is already logged in')
# return redirect(url_for('showCatalog'))
@app.route('/logout')
def logoutUser():
session.pop('user_id', None)
flash('User logged out')
return redirect(url_for('showCatalog'))
@app.route('/github-callback')
@github.authorized_handler
def authorized(oauth_token):
next_url = request.args.get('next') or url_for('showCatalog')
if oauth_token is None:
# something went wront
flash("Authorization failed")
flash(request.args.get('error'))
flash(request.args.get('error_description'))
flash(request.args.get('error_uri'))
return redirect(next_url)
user = User.query.filter_by(github_access_token=oauth_token).first()
if user is None:
# new user is not in database
user = User(name="", github_access_token=oauth_token)
db_session.add(user)
# save oauth token in database
user.github_access_token = oauth_token
db_session.commit()
session['user_id'] = user.id
flash("User " + user.name + " logged in")
return redirect(next_url)
@github.access_token_getter
def token_getter():
user = g.user
if user is not None:
return user.github_access_token
# Registers a function to run before each request.
@app.before_request
def before_request():
g.user = None
if 'user_id' in session:
g.user = User.query.get(session['user_id'])
g.user.name = github.get('user')["name"]
g.user.avatar = github.get('user')["avatar_url"]
db_session.add(g.user)
db_session.commit()
@app.after_request
def after_request(response):
db_session.remove()
return response
@app.route('/json')
def catalogjson():
list = []
items = Item.query.all()
for item in items:
list.append({"name": item.name,
"id": item.id,
"description": item.description,
"category": item.category.name,
"image": item.image,
"owner": item.owner.name
})
return jsonify({"items": list})
# run flask server if script is started directly
if __name__ == '__main__':
app.debug = False
app.secret_key = app_secret
# app.run(host='0.0.0.0', port=5000)
服务器配置文件:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName ec2-52-37-69-144.us-west-2.compute.amazonaws.com
ServerAdmin catalog@57.37.69.144
WSGIScriptAlias / /var/www/catalog/catalog.wsgi
<Directory /var/www/catalog/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/catalog/static
<Directory /var/www/catalog/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel error
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
会话
被设置为 flask null 会话(这是默认实现),这再次引发错误。
解决方案是将其设置为:
app.config['SESSION_TYPE'] = '文件系统'