我在 ubuntu 中使用 django、wsgi 和 nginx 创建了一个 Web 服务。(http://127.0.0.1:8000)。我的网络连接是 NAT。我想在 VMWare 中从 Linux 访问此 Web 服务。我怎样才能做到这一点?
我的 Debian 8 服务器上安装了 datadog-agent。它已经配置好并且可以很好地报告有关 postgres、nginx、系统等的指标。
我想监控我的 gunicorn 守护进程(我在这台服务器上有 2 个 Django 网站)。据我了解,datadog-agent 已经集成了一个 statsd 服务器,所以我不必安装一个:
# ps ax | grep datadog
18816 ? Ss 0:00 /opt/datadog-agent/embedded/bin/python /opt/datadog-agent/bin/supervisord -c /etc/dd-agent/supervisor.conf
18822 ? Sl 0:01 /opt/datadog-agent/embedded/bin/python /opt/datadog-agent/agent/dogstatsd.py --use-local-forwarder
18824 ? S 0:01 /opt/datadog-agent/embedded/bin/python /opt/datadog-agent/agent/ddagent.py
18825 ? S 0:01 /opt/datadog-agent/embedded/bin/python /opt/datadog-agent/agent/agent.py foreground --use-local-forwarder
我认为“dogstatsd.py”是 statsd 服务器,但也许我错了?
然后,我修改了我的 gunicorn 启动脚本以集成--name
选项来为我的进程设置名称并--statsd-host
指示将指标发送到何处。
# ps ax | grep gunicorn
18588 ? Ss 0:00 /var/projects/my_project/venv/bin/python3 /var/projects/my_project/venv/bin/gunicorn --name my_project --statsd-host=localhost:8125 --workers 2 --bind unix:/var/tmp/my_project.sock core.wsgi:application
18630 ? S 0:00 /var/projects/my_project/venv/bin/python3 /var/projects/my_project/venv/bin/gunicorn --name my_project --statsd-host=localhost:8125 --workers 2 --bind unix:/var/tmp/my_project.sock core.wsgi:application
18632 ? S 0:00 /var/projects/my_project/venv/bin/python3 /var/projects/my_project/venv/bin/gunicorn --name my_project --statsd-host=localhost:8125 --workers 2 --bind unix:/var/tmp/my_project.sock core.wsgi:application
现在我从 /etc/dd-agent/conf.d/ 中的示例创建 gunicorn.yaml 如下
# NB: This check requires the python environment on which gunicorn runs to
# have the `setproctitle` module installed (https://pypi.python.org/pypi/setproctitle/)
init_config:
instances:
# The name of the gunicorn process. For the following gunicorn server ...
#
# gunicorn --name my_web_app my_web_app_config.ini
#
# ... we'd use the name `my_web_app`.
#
- proc_name: my_project
重新启动代理后,我等待几秒钟并检查其状态:
# sudo service datadog-agent info
# [...]
Checks
======
gunicorn
--------
- instance #0 [ERROR]: 'Found no master process with name: gunicorn: master [my_project]'
- Collected 0 metrics, 0 events & 1 service check
- Dependencies:
- psutil: 4.4.1
我找不到我无法配置它的地方。有人可以帮助我吗?
我正在使用 Python、Flask 和 Apache,但我被困住了。当我尝试使用 www 访问我的网站时,它工作正常。但是,当我删除 www 时,该网站显示“/ 的索引”,接下来是一个空的文件列表。我已经阅读了所有可能的解决方案,在我的情况下没有任何效果。我正在使用 WSGI,这意味着 DocumentRoot 无关紧要,因为我使用的是 WSGIScriptAlias 变量。我按照指示使用 ServerAlias - 没有帮助。我在我的 dns 选项中设置了 2 条单独的 A 记录,它们都指向同一个 ip。当我用 www 和不带 www ping 网站时,显示相同的 IP 地址
<VirtualHost *:80>
ServerName mysite.com
ServerAlias www.mysite.com
WSGIScriptAlias / /var/www/mysite/mysite.wsgi
<Directory /var/www/mysite/mysite/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/mysite/mysite/app/static
<Directory /var/www/mysite/mysite/app/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
...
</VirtualHost>
有任何想法吗?
这是“apachectl -S”命令的输出:
VirtualHost configuration:
*:80 is a NameVirtualHost
default server mysite.com (/etc/apache2/sites-enabled /000-default.conf:1)
port 80 namevhost mysite.com (/etc/apache2/sites-enabled /000-default.conf:1)
port 80 namevhost mysite.com (/etc/apache2/sites-enabled /mysite.conf:1)
alias www.mysite.com
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
我正在尝试按照本教程uWSGI
使用Django
和设置进行nginx
设置Ubuntu
16.04
。
一切正常,直到我尝试执行此命令的最后一步(哦,讽刺的是......):
sudo service uwsgi start
如果失败并出现以下错误:
启动 uwsgi.service 失败:未找到单元 uwsgi.service。
其他人似乎得到了类似的错误:
uwsgi.service 启动失败:单元 uwsgi.service 加载失败:没有这样的文件或目录。
该问题似乎与 Ubuntu 的版本有关。虽然该教程针对的是 Ubuntu 14.04,但它似乎不适用于较新的版本,因为在版本 15 中,Ubuntu 从upstart
init daemon 切换到了systemd
init daemon。
如何使用systemd
启动 uWSGI 使其与 nginx 和 Django 一起使用?
我刚刚开始使用 wsgi,并试图启动并运行一个简单的 uwsgi 服务器。我已经设置了一个 virtualenv 环境并激活了它。在 lib 中,我有一个文件 hello.py,其内容为:
def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
我在跑:
uwsgi --http :8000 --wsgi-file lib/hello.py --add-header "X-test: hi"
启动服务器。
我的问题是服务器没有发送正文。当我卷曲到 localhost:8000 时,我可以看到 X-test 标头,所以我肯定是在使用 uwsgi。此外,如果我将“200 OK”更改为其他内容,我也可以在 curl 中看到它。
我很确定我正确地遵循了教程(它看起来很简单),任何人都可以发现我做错了什么吗?难道是我用的是python3?我通过 pip 在我的 virtualenv 中安装了 uwsgi,如果这很重要的话。
我有一个类似的问题:Apache : Map one virtualhost to a subdirectory of another virtualhost
除了我的是关于在 Django 的域内提供 PHP 服务:
example.com
服务于 Django,使用 WSGI 等,example.com/some_app
需要完全基于PHP,不能通过Django,
我怎样才能做到这一点?我相信这是可能的,但你能给我任何关于如何实现它的线索吗?
我有一个 Python 3 网络应用程序,所以 mod_wsgi < 3.1 不适合我。但是,在我的 Ubuntu 9.10 安装中,似乎没有 mod_wsgi 3.1 的软件包。
- 是否有包含 mod_wsgi 3.1 包的替代存储库?
- 不久之后会有一个新的 Ubuntu 版本,它会包含 mod_wsgi 3.1 吗?
- 推荐其他一些准备好使用 mod_wsgi 3.1 的发行版?
- 也许我最好的选择是自己编译?从快速谷歌看来,我只需要安装 python 和 apache 开发包。
谢谢!
我在 ubuntu 9.10 上安装了一个带有 mod_wsgi 和 apache2 的 Django Web 应用程序
我使用以下代码通过 HTTP 帖子对我网站的另一部分进行用户身份验证。它只需要一个 HTTP 200。
from django.contrib.auth import authenticate
from django.http import HttpResponse
def post_authentication_api(request):
if request.method == 'POST':
print request
user = authenticate(username=request.POST['user'], password=request.POST['pass'])
if user is not None:
if user.is_active:
print "correct"
return HttpResponse("correct", mimetype="text/plain")
else:
print "disabled"
return HttpResponse("disabled", mimetype="text/plain", status=401)
else:
print "incorrect"
return HttpResponse("incorrect", mimetype="text/plain", status=401)
当我使用 python manage.py runserver 命令运行它并且其他应用程序验证正常时,这一切正常。但是,当我将它加载到 apache 中时,我得到一个 500 内部错误。实际上,当我运行以下 python 代码时
import urllib
import urllib2
url = 'http://192.168.0.5/radiobusi/auth/post_authentication_api'
values = {'user' : 'Michael Foord',
'pass' : 'Northampton',}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
我明白了
Traceback (most recent call last):
File "testURL.py", line 10, in <module>
response = urllib2.urlopen(req)
File "/usr/lib/python2.6/urllib2.py", line 124, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.6/urllib2.py", line 395, in open
response = meth(req, response)
File "/usr/lib/python2.6/urllib2.py", line 508, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.6/urllib2.py", line 433, in error
return self._call_chain(*args)
File "/usr/lib/python2.6/urllib2.py", line 367, in _call_chain
result = func(*args)
File "/usr/lib/python2.6/urllib2.py", line 516, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: INTERNAL SERVER ERROR
这是我的虚拟主机文件
<VirtualHost *:80>
#Basic setup
ServerAdmin [email protected]
ServerName 192.168.0.5
ServerAlias 192.168.0.5
<Directory /home/munderwo/public_html/radiobusi.com/RadioBusiSite/mysite/apache/>
Order deny,allow
Allow from all
</Directory>
Alias /radiobusi/media /home/munderwo/public_html/radiobusi.com/RadioBusiSite/mysite/media
<Location /media>
Order allow,deny
Allow from all
</Location>
LogLevel debug
ErrorLog /home/munderwo/public_html/radiobusi.com/logs/apache_error.log
CustomLog /home/munderwo/public_html/radiobusi.com/logs/apache_access.log combined
WSGIDaemonProcess radiobusi.com user=www-data group=www-data threads=25
WSGIProcessGroup radiobusi.com
WSGIScriptAlias /radiobusi /home/munderwo/public_html/radiobusi.com/RadioBusiSite/mysite/apache/RadioBusi.wsgi
</VirtualHost>
这是我的 wsgi 文件
import os
import sys
apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)
sys.path.append('/usr/local/lib/python2.6/dist-packages/Django-1.1.1-py2.6.egg/django/')
sys.path.append('/home/munderwo/public_html/radiobusi.com/RadioBusiSite/mysite/')
os.environ['PYTHON_EGG_CACHE'] = '/home/munderwo/public_html/radiobusi.com/egg_cache'
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
我已经在 ~/public_html/radiobusi.com/ 上将所有权限设置为 777,并将 loggin 转为在 apache 和虚拟主机上进行调试。任何人有任何其他想法这可能是什么?
干杯
标记。
更新:
以下是来自虚拟主机的日志,包括错误日志和访问日志
[Mon Jan 18 19:54:27 2010] [info] mod_wsgi (pid=27874): Attach interpreter ''.
[Mon Jan 18 19:54:27 2010] [info] mod_wsgi (pid=27874): Enable deadlock thread in process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8668): mod_wsgi (pid=27874): Starting 25 threads in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 1 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [info] mod_wsgi (pid=27874): Enable monitor thread in process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8508): mod_wsgi (pid=27874): Deadlock timeout is 300.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8511): mod_wsgi (pid=27874): Inactivity timeout is 0.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 2 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 3 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 4 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 5 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 6 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 7 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 8 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 9 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 10 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 11 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 12 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 13 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 14 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 15 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 16 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 17 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 18 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 19 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 20 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 21 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 22 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 23 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 24 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 25 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:47 2010] [info] mod_wsgi (pid=27874): Create interpreter '192.168.0.5|/radiobusi'.
[Mon Jan 18 19:54:47 2010] [info] [client 192.168.0.5] mod_wsgi (pid=27874, process='radiobusi.com', application='192.168.0.5|/radiobusi'): Loading WSGI script '/home/munderwo/public_html/radiobusi.com/RadioBusiSite/mysite/apache/RadioBusi.wsgi'.
munderwo@bertha:~/public_html/radiobusi.com/logs$ cat apache_access.log
192.168.0.5 - - [18/Jan/2010:19:54:47 +0800] "POST /radiobusi/auth/post_authentication_api HTTP/1.1" 500 42213 "-" "Icecast 2.3.2"
192.168.0.5 - - [18/Jan/2010:19:55:04 +0800] "POST /radiobusi/auth/post_authentication_api HTTP/1.1" 500 42213 "-" "Icecast 2.3.2"
为了得到这些,我停止了 apache。删除了日志。再次触动了他们。然后重新启动 apache 并尝试了两次 POST 请求。
这是来自 /var/log/apache2/error.log 的主 apache 日志文件。我已经包含了它关闭自己的消息作为时间跟踪它的一种方式。
[Mon Jan 18 19:53:47 2010] [notice] caught SIGTERM, shutting down
[Mon Jan 18 19:54:27 2010] [notice] mod_python: Creating 8 session mutexes based on 6 max processes and 25 max threads.
[Mon Jan 18 19:54:27 2010] [notice] mod_python: using mutex_directory /tmp
[Mon Jan 18 19:54:27 2010] [warn] mod_wsgi: Compiled for Python/2.6.2.
[Mon Jan 18 19:54:27 2010] [warn] mod_wsgi: Runtime using Python/2.6.4.
[Mon Jan 18 19:54:27 2010] [warn] mod_wsgi: Python module path '/usr/lib/python2.6/:/usr/lib/python2.6/plat-linux2:/usr/lib/python2.6/lib-tk:/usr/lib/python2.6/lib-old:/usr/lib/python2.6/lib-dynload'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8070): mod_wsgi (pid=27872): Socket for 'radiobusi.com' is '/var/run/apache2/wsgi.27872.0.1.sock'.
[Mon Jan 18 19:54:27 2010] [info] mod_wsgi (pid=27874): Starting process 'radiobusi.com' with uid=33, gid=33 and threads=25.
[Mon Jan 18 19:54:27 2010] [notice] Apache/2.2.12 (Ubuntu) DAV/2 SVN/1.6.5 mod_python/3.3.1 Python/2.6.4 mod_wsgi/2.5 configured -- resuming normal operations
[Mon Jan 18 19:54:27 2010] [info] Server built: Nov 12 2009 22:50:52
[Mon Jan 18 19:54:27 2010] [debug] worker.c(1740): AcceptMutex: sysvsem (default: sysvsem)
[Mon Jan 18 19:54:27 2010] [info] mod_wsgi (pid=27877): Attach interpreter ''.
[Mon Jan 18 19:54:27 2010] [info] mod_wsgi (pid=27878): Attach interpreter ''.
同样在我的 testURL.py 中,我相信我正在打印出响应。好吧,至少我正在尝试。用线条
response = urllib2.urlopen(req)
the_page = response.read()
有一个更好的方法吗?
更新:根据评论中的要求添加了 urls.py
from django.conf.urls.defaults import *
from django.contrib import admin
from django.contrib.auth import forms
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/(.*)', admin.site.root),
(r'^polls/', include('mysite.polls.urls')),
(r'auth/post_authentication_api$', 'mysite.users.views.post_authentication_api'),
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/Users/munderwood/Documents/Aptana Studio Workspace/RadioBusiSite/mysite/media/'}),
)
据我所知,我的 apache2.conf 没有 ErrorDocument 设置。它被注释掉了。
好的 设法从中得到一个 django 错误页面。相当奇怪。
Environment:
Request Method: POST
Request URL: http://bertha.homeunix.org:3000/radiobusi/auth/post_authentication_api
Django Version: 1.1.1
Python Version: 2.6.4
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.flatpages',
'mysite.polls',
'mysite.RadioBusi',
'mysite.Support',
'mysite.users']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware')
Traceback:
File "/usr/local/lib/python2.6/dist-packages/Django-1.1.1-py2.6.egg/django/core/handlers/base.py" in get_response
92. response = callback(request, *callback_args, **callback_kwargs)
Exception Type: TypeError at /auth/post_authentication_api
Exception Value: post_authentication_api() takes exactly 2 arguments (1 given)
POST
Variable Value
user u'e_username'
pass u'e_password'
POST 下的东西是 post 变量。希望这可以帮助。这显然与我的设置有关。奇怪的是,当我在 django 测试服务器中运行相同的代码时它可以工作,而一旦我把它放在 apache 网络服务器上它就会这样做?
再次感谢你的帮助!
干杯
标记