简体   繁体   English

如何在AppFog中使用Apache提供静态内容(WSGI Python应用程序)

[英]How to serve static content with Apache in AppFog (WSGI Python app)

I'm using AppFog PaaS system for a few days, and I love it, It's probably the best PaaS system that I've tested (I've used other 3 ones previously), but didn't find information about how to serve static content with the Web server in frontend (Apache https or nginx) I'm not sure what server is being used. 我正在使用AppFog PaaS系统几天,我喜欢它,它可能是我测试过的最好的PaaS系统(之前我使用过其他3个),但没有找到有关如何提供静态服务的信息前端Web服务器的内容(Apache https或nginx)我不确定使用的服务器是什么。

My app is a Python WSGI with CherryPy and works perfectly in AppFog but I don't wan't CherryPy to serve static content, I think that Apache httpd or nginx is a better option for that. 我的应用程序是一个带有CherryPy的Python WSGI,并且在AppFog中运行得很好,但我不想使用CherryPy来提供静态内容,我认为Apache httpd或nginx是一个更好的选择。

With Ryan's support, I'm finally able to load static files! 在Ryan的支持下,我终于能够加载静态文件了! Here are the steps: 以下是步骤:

  1. Created a 'static' directory in the project root - here all static files will be collected running the collectstatic command. 在项目根目录中创建了一个“静态”目录 - 这里将收集运行collectstatic命令的所有静态文件。

  2. Edit the settings.py file: 编辑settings.py文件:

    STATIC_ROOT = os.path.join( os.path.abspath( os.path.dirname( file ) ), '../static' ) # May change depending on where your settings.py file is! STATIC_ROOT = os.path.join(os.path.abspath(os.path.dirname( file )),'.. / static')#可能会根据settings.py文件的位置而改变!

    STATIC_URL = '/static/' STATIC_URL ='/ static /'

  3. Add following line in urlpatterns variable in urls.py file: urls.py文件中的urlpatterns变量中添加以下行:

    url(r'^static/(?P.*)$', 'django.views.static.serve', { 'document_root': settings.STATIC_ROOT} ) , url(r'^ static /(?P。*)$','django.views.static.serve',{'document_root':settings.STATIC_ROOT}),

  4. Finally, run collectstatic command in your local machine. 最后,在本地计算机上运行collectstatic命令。 This will copy all static files from the apps you are using: 这将复制您正在使用的应用程序中的所有静态文件:

    python manage.py collectstatic python manage.py collectstatic

That's it. 而已。 Push in AF :) 推入AF :)

Downside: Need to run collectstatic every time we have a new static file... 缺点:每次我们有一个新的静态文件时都需要运行collectstatic ...

Edit your nginx.conf file. 编辑您的nginx.conf文件。 In the server section enter... 在服务器部分输入...

   # serve static files
      location ~ ^/(images|javascript|css)/  {
      root    /var/www/html/appname;
    }

images, javascript and css would be folders in your document root folder. images,javascript和css将是文档根文件夹中的文件夹。 Update all your urls accordingly. 相应地更新所有网址。

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

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