在EC2上部署Flask应用程序以进行本地主机访问

时间:2013-12-09 15:27:53

标签: python apache amazon-ec2 flask

我已经完成了一个简单的Flask应用程序,我试图在使用Apache2的AWS EC2实例上托管。我一直关注this tutorial

我从开发过程中做出的唯一更改(当我运行它然后尝试通过localhost访问它时,应用程序运行完全正常)是:

1) Moved all the code in to /var/www
2) Changed it so that 
        if __name__=='__main__':
            app.run(debug = False) #Now False instead of True
3) Added a app.wsgi file
4) Added file my_app to /etc/apache2/sites-available
5) Ran these commands:
    $ sudo a2dissite default
    $ sudo a2ensite sitename.com
    $ sudo /etc/init.d/apache2 restart

这是app.wsgi文件:

import sys 
sys.path.insert(0, '/var/www/my_app')

from app import app as application

以下是/etc/apache2/sites-available中的my_app文件:

<VirtualHost *:5000>
         WSGIDaemonProcess app 
     WSGIScriptAlias / /var/www/my_app/app.wsgi

     <Directory /var/www/my_app>
            WSGIProcessGroup app 
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from 127.0.0.1
     </Directory>
</VirtualHost>

从上面的文件中可以看出,我只想让烧瓶应用程序在localhost上可用。

当我运行apache并尝试访问my_site.com:5000网站时,我收到“无法连接错误”。我无法弄清楚原因。任何帮助,将不胜感激。

此外,如果需要,这里是Flask应用程序本身的目录结构:

/var/www/my_app/
    app/
        __init__.py
        static/
            css/
                bootstrap.css
            favicon.ico
            js/
                bootstrap.js
        templates/
            base.html
            index.html
            search.html
        views.py
    app.wsgi
    flask/           #my virtualenv
        #Your typical virutalenv structure
    flask_util_js.py   #File that is a FLask entension for client-side generation of URLs
    requirements.txt
    run.py
    virtualenv.py    #Creates virutalenv flask

更新

所以,我觉得我设置代码的方式存在问题。所以我把所有内容都放在run.py__init__.pyviews.py中,然后制作了一个大main.py。我已将app.wsgi更新为以下内容:

app.wsgi

import sys 

activate_this = '/home/achumbley/flask/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

sys.path.insert(0, '/home/achumbley/new_flask_app')

from main import app as application

现在,我的/etc/apache2/sites-available/new_flask_app看起来像是:

<VirtualHost *>
  ServerName dev.east.appliedminds.com

  WSGIDaemonProcess app 
  WSGIScriptAlias / /var/www/app.wsgi

 <Directory /home/achumbley/new_flask_app>
   WSGIProcessGroup main
   WSGIScriptReloading On
   WSGIApplicationGroup %{GLOBAL}
   Order deny,allow
   Allow from all
 </Directory>

最后,他是我最新的目录结构:

/home/my_username/new_flask_app/
    logging.log
    flask_util_js.py
    main.py
    static/
    templates/

它仍然不起作用。但是,我可能无法理解如何运行整个事情。我应该跑python main.py对吧?它应该是自动的,至少这是我所假设的。

2 个答案:

答案 0 :(得分:2)

  

将所有代码移至/ var / www

这是错误的。您需要将代码发布在非Web可访问的目录中。只将您的静态文件发布到/var/www

请参阅official deployment guide,了解如何使用Apache和mod_wsgi进行设置。如果您遇到问题,可以考虑安装flask,nginx和uwsgi的this AMI image

nginx + uwsgi堆栈也是detailed in the documentation

以下是您需要遵循的步骤(简化):

假设您的申请是:

my_app/
   static/
      logo.gif
      style.css
   templates/
      index.html
   main.py

然后按照以下说明操作:

  1. 将您的所有代码上传到/home/youruser/
  2. app.wsgi文件上传至/var/www/
  3. 将静态目录的内容上传到/var/www/static
  4. app.wsgi文件中:

    import sys
    sys.path.append(0, '/home/youruser/my_app')
    
    from main import app as application
    

答案 1 :(得分:1)

您是否在apache上启用了mod_wsgi?

a2enmod wsgi 
service apache2 restart

我也会尝试一个简单的'hello world'示例,这将是/home/ubuntu/new_flask_app下的main.py的内容:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

这是我的工作示例的示例烧瓶apache配置:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    ServerName app.com

    WSGIDaemonProcess new_flash_app user=ubuntu group=ubuntu threads=5
    WSGIScriptAlias / /var/www/app/app.wsgi

    <Directory /var/www/app>
        WSGIProcessGroup new_flash_app
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>

</VirtualHost>

这将是app.wsi的内容

import sys

activate_this = '/home/ubuntu/flask/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

sys.path.insert(0, '/home/ubuntu/new_flask_app')

from main import app as application

顺便说一句,您的wsgi和main.py(或应用程序文件)不需要位于同一目录中,只需确保配置指向正确的文件即可。

您可以像这样测试您的服务器:

$ telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /
Hello World!Connection closed by foreign host.
$

我还会检查apache日志/var/log/apache2/error.log

你也可以像(Burhan Khalid)一样尝试nginx。