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

2024-05-14 20:29:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经完成了一个简单的Flask应用程序,我正试图用Apache2在awsec2实例上托管它。我一直在关注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

这是应用程序.wsgi文件:

^{pr2}$

以下是/etc/apache2/sites-available中的my\u 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>

从上面的文件中可以看到,我只希望flask应用程序在本地主机上可用。在

当我运行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__.py、和{}中的所有东西做成了一个大的main.py。我已将我的app.wsgi更新为如下所示:

应用程序.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对吗?它应该是自动的,至少我是这么想的。在


Tags: pyapp应用程序flaskwsgimainvarmy
2条回答

Moved all the code in to /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
    

你在apache上启用mod\wsgi了吗?在

a2enmod wsgi 
service apache2 restart

我还将尝试使用一个简单的“hello world”示例,这将是主.py在/home/ubuntu/new_flask_app下:

^{pr2}$

以下是示例flask 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>

这就是应用程序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和主.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

你也可以试试nginx,就像(Burhan Khalid)提到的那样。在

相关问题 更多 >

    热门问题