简体   繁体   English

在EC2上部署Flask应用以进行本地访问

[英]Deploying Flask app on EC2 for localhost access

I have finished up a simple Flask app that I am trying to host on an AWS EC2 instance with Apache2. 我已经完成了一个简单的Flask应用,该应用正在尝试使用Apache2托管在AWS EC2实例上。 I've been following this tutorial . 我一直在关注本教程

The only changes I've made from the development process (in which the app runs totally fine when I run it and then try to access it via localhost) are: 我在开发过程中所做的唯一更改(在运行应用程序时,应用程序运行完全正常,然后尝试通过本地主机访问它):

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

Here is the app.wsgi file: 这是app.wsgi文件:

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

from app import app as application

Here is the my_app file in /etc/apache2/sites-available : 这是/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>

As you can see from the above file, I only want the flask app to be available on localhost. 从上面的文件中可以看到,我只希望flask应用程序在本地主机上可用。

When I run apache and try to access the website at my_site.com:5000 I get an "Unable to connect error". 当我运行apache并尝试访问my_site.com:5000的网站时,出现“无法连接错误”。 I can't really figure out why. 我真的不知道为什么。 Any help would be appreciated. 任何帮助,将不胜感激。

Also, here is my directory structure for the Flask app itself if that's needed: 另外,如果需要的话,这是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

UPDATE: 更新:

So, I got the feeling that the way I had my code set up was being problematic. 因此,我感到我的代码设置方式存在问题。 So I took everything in run.py , __init__.py , and views.py and made one big main.py . 因此,我将所有内容都放入run.py__init__.py run.pyviews.py并制作了一个大main.py I've updated my app.wsgi to look like this: 我已经更新了app.wsgi使其看起来像这样:

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

And now, my /etc/apache2/sites-available/new_flask_app looks like: 现在,我的/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>

Finally, he is my newest directory structure: 最后,他是我最新的目录结构:

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

It still does not work. 它仍然不起作用。 But, it's possible I don't understand how to run the whole thing. 但是,可能我不了解如何运行整个程序。 I should have to run a python main.py right? 我应该运行python main.py对吗? It should just be automatic, at least that is what I assumed. 它应该只是自动的,至少这是我所假设的。

Moved all the code in to /var/www 将所有代码移至/ var / www

This is wrong. 错了 You need to post your code in a non-web accessible directory. 您需要将代码发布在不可通过网络访问的目录中。 Only post your static files to /var/www 仅将您的静态文件发布到/var/www

Please see the official deployment guide on how to set it up with Apache and mod_wsgi. 请参阅官方部署指南 ,了解如何使用Apache和mod_wsgi进行设置。 If you are having problems, you might consider this AMI image which has flask, nginx and uwsgi installed. 如果遇到问题,可以考虑安装了flask,nginx和uwsgi的AMI映像

The nginx+uwsgi stack is also detailed in the documentation . nginx + uwsgi堆栈在文档中也有详细介绍

Here are the steps (simplified) that you need to follow: 这是您需要遵循的步骤(简化):

Assume that your application is: 假设您的应用程序是:

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

Then follow these instructions: 然后按照以下说明进行操作:

  1. Upload all your code into /home/youruser/ 将所有代码上传到/home/youruser/
  2. Upload the app.wsgi file to /var/www/ app.wsgi文件上传到/var/www/
  3. Upload the contents of the static directory to /var/www/static 将静态目录的内容上传到/var/www/static
  4. In the app.wsgi file: app.wsgi文件中:

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

Have you enabled mod_wsgi on apache? 您是否在apache上启用了mod_wsgi?

a2enmod wsgi 
service apache2 restart

I would also try with a simple 'hello world' example, this would be the content of main.py under /home/ubuntu/new_flask_app : 我还将尝试一个简单的“ hello world”示例,这将是/home/ubuntu/new_flask_app

from flask import Flask
app = Flask(__name__)

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

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

This is a sample flask apache config for my working example: 这是我的工作示例的示例烧瓶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>

and this would be the content for app.wsi 这就是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

Btw, your wsgi and main.py (or app file) don't need to be in the same directory, just make sure the configuration points to the right files. 顺便说一句,您的wsgi和main.py(或应用程序文件)不需要在同一目录中,只需确保配置指向正确的文件即可。

You can test your server like this: 您可以像这样测试服务器:

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

I would also check for the apache logs /var/log/apache2/error.log 我还将检查apache日志/var/log/apache2/error.log

You can also try nginx just like (Burhan Khalid) mentioned. 您也可以像(Burhan Khalid)提到的那样尝试nginx。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM