18

尝试在 Rackspace.com 上设置服务器。

做了以下几件事:

  • 已安装 Centos 6.3
  • 安装 Python 2.7
  • 使用主页上的“快速启动”安装 gunicorn:gunicorn.org/

在快速启动中,似乎初始化了一个“hello world”应用程序:

创建文件“ myapp.py ”:

(教程)$ vi myapp.py
(教程)$ cat myapp.py

myapp.py ”的内容

def app(environ, start_response):
   data = "Hello, World!\n"
   start_response("200 OK", [
       ("Content-Type", "text/plain"),
       ("Content-Length", str(len(data)))
   ])
   return iter([data])

由于我对服务器知之甚少,我不知道下一步该做什么。我尝试在浏览器中输入服务器的 IP 地址,但这似乎会导致超时。

我不确定是否有:

  • 其他需要安装的东西。gunicorn 网站上的“部署”下提到了 Nginx 。看起来 Nginx 是一个代理服务器,这让我很困惑,因为我认为 gunicorn 是一个服务器。不知道为什么我需要两台服务器?
  • 需要在 gunicorn 中配置的东西
  • 需要在服务器本身上配置的东西
  • 为了实际服务请求而需要完成的其他事情

什么是下一个步骤?

非常感谢!

4

3 回答 3

36

因为 gunicorn 是您案例中的 Web 服务器,所以 Nginx 将充当反向代理,将 HTTP 请求从 Nginx 传递到 gunicorn。

因此,我将在这里介绍在同一台机器上运行简单的 Nginx 和 Gunicorn 配置的步骤。

  • 从 nginx 配置开始

转到您的/etc/nginx/nginx.conf并在 http{} 下确保您有: include /etc/nginx/site-enabled/*;

http{
    # other configurations (...)
    include /etc/nginx/sites-enabled/*;
}

现在,在 /etc/nginx/sites-enabled/mysite.conf 中包含一个文件,您将在其中将您的请求代理到您的 gunicorn 应用程序。

server {
    listen 80 default; # this means nginx will be 
                       # listening requests on port 80 and 
                       # this will be the default nginx server
    server_name localhost;

    # declare proxy params and values to forward to your gunicorn webserver
    proxy_pass_request_headers on;
    proxy_pass_request_body on;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_read_timeout 120s;

    location / {
        # here is where you declare that every request to / 
        # should be proxy to 127.0.0.1:8000 (which is where
        # your gunicorn will be running on)          
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;

        proxy_pass http://127.0.0.1:8000/; # the actual nginx directive to 
                                           # forward the request
    }
}

好的,此时您所拥有的只是一个充当代理的 Nginx,其中所有发往 127.0.0.1:80 的请求将被传递到 127.0.0.1:8000。

  • 现在是时候配置你的Gunicorn 网络服务器了:

通常我使用配置文件的方式是,Gunicorn 配置文件可以是一个普通的 python 文件。所以现在,在你喜欢的任何位置创建一个文件,我假设这个文件是/etc/gunicorn/mysite.py

workers = 3              # number of workers Gunicorn will spawn 

bind = '127.0.0.1:8000'  # this is where you declare on which address your 
                         # gunicorn app is running.
                         # Basically where Nginx will forward the request to

pidfile = '/var/run/gunicorn/mysite.pid' # create a simple pid file for gunicorn. 

user = 'user'          # the user gunicorn will run on

daemon = True          # this is only to tell gunicorn to deamonize the server process

errorlog = '/var/log/gunicorn/error-mysite.log'    # error log

accesslog = '/var/log/gunicorn/access-mysite.log'  # access log

proc_name = 'gunicorn-mysite'            # the gunicorn process name

好的,所有设置都在配置中。现在你要做的就是启动服务器。

启动 gunicorn 并告诉它使用哪个应用程序和哪个配置文件。从命令行和myapp.py文件所在的文件夹运行:

gunicorn -c /etc/gunicorn/mysite.py mysite:app

现在,只启动 nginx。

/etc/init.d/nginx start 

或者

service nginx start

希望这可以帮助。

于 2012-10-12T12:44:41.777 回答
24

查看快速入门指南,您可能应该运行

(tutorial) $ ../bin/gunicorn -w 4 myapp:app

这应该产生了一条看起来有点像的线:

Listening at: http://127.0.0.1:8000

其中。看看您是否可以通过该地址访问您的网站。

另请注意,这127.0.0.1是环回地址;只能从该主机本身访问。要让 gunicorn 绑定到不同的选项,请传递它--bind 0.0.0.0:80,正如 Jan-Philip 建议的那样。

由于您提到机架空间,因此您可能需要调整防火墙设置以允许传入连接到所需端口。

于 2012-10-12T12:48:20.543 回答
7

到目前为止,您似乎还没有开发 Web 应用程序。所以,我假设你现在的目标是建立一个开发环境。目前,使用大多数框架中包含的开发 Web 服务器来开发您的 Web 应用程序,例如 Flask。

无论您使用什么框架,让开发 Web 服务器在 0.0.0.0 上进行侦听,以便该服务正在侦听所有已配置的网络接口,并确保该端口对外部开放(检查 Rackspace 设置)。

当您完成应用程序的开发或正在研究现有的应用程序时,您必须以可靠的方式部署它。那么,nginx 后面的 gunicorn 就是一个选择。

我将大致解决您的问题。看来你必须多读一点:-)

  • gunicorn 网站上的“部署”下提到了 Nginx。看起来 Nginx 是一个代理服务器,这让我很困惑,因为我认为 gunicorn 是一个服务器。不知道为什么我需要两台服务器?

Nginx 是一个功能齐全的 Web 服务器。它因其性能和稳定性而受到赞赏。人们使用它来提供静态文件(不让动态 Web 应用程序负担这个任务),在必要时将请求转发到 Web 应用程序,用于 SSL 终止和负载平衡。请注意,这是一张不完整的图片。

gunicorn 是一个服务于 WSGI 应用的服务器。主要是管理实际执行 Web 应用程序的工作进程。

  • 需要在 gunicorn 中配置的东西。需要在服务器本身上配置的东西。为了实际服务请求而需要完成的其他事情。

实际上,您可以用无穷无尽的方式优化您的 linux 机器(为了性能,例如增加文件描述符限制和安全性)。在 gunicorn 中,您可以配置工作进程的数量等等。如果你有 nginx 作为前端甚至另一个负载均衡器,这个有自己的配置。您会看到,您的设置对于现实世界场景中的实际部署可能会变得非常复杂。这不是微不足道的。

但是,要玩 WSGI 应用程序,只需正确设置您的开发框架,这在大多数情况下非常简单,并确保没有防火墙问题。就这样。

于 2012-10-12T12:31:06.860 回答