如何在Debian机器上使用mod_wsgi部署Django?

时间:2022-10-06 08:17:46

I have installed apache2, mod_wsgi on my Debian machine and added this on my apache2.conf file:

我在我的Debian机器上安装了apache2,mod_wsgi,并将其添加到我的apache2.conf文件中:

WSGIScriptAlias /home/zurelsoft/Documents/workspace/genalytics/genalytics/wsgi.py
WSGIPythonPath /home/zurelsoft/Documents/workspace/genalytics

<Directory /home/zurelsoft/Documents/workspace/genalytics/genalytics>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

My project name is genalytics. I am using Django 1.5. There's already wsgi.py available. What should I do run the django with mod_wsgi and where should I give the path of my static files. Thanks

我的项目名称是genalytics。我正在使用Django 1.5。已经有wsgi.py了。我该怎么做用mod_wsgi运行django,我应该在哪里给出静态文件的路径。谢谢

Edit

I have this on my apache.conf file:

我在我的apache.conf文件中有这个:

Listen 8000

Alias /static/ /home/zurelsoft/Documents/workspace/genalytics/fileupload/static

<Directory /home/zurelsoft/Documents/workspace/genalytics/fileupload/static>
Order deny,allow
Allow from all
</Directory>


WSGIScriptAlias / /home/zurelsoft/Documents/workspace/genalytics/fileupload/static


<Directory /home/zurelsoft/Documents/workspace/genalytics/fileupload/static>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>

But when I run try to start apache I get this error:

但是当我运行尝试启动apache时,我收到此错误:

(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80 no listening sockets available, shutting down Unable to open logs Action 'start' failed.

(98)正在使用的地址:make_sock:无法绑定到地址0.0.0.0:80没有可用的侦听套接字,关闭无法打开日志操作'start'失败。

3 个解决方案

#1


1  

Presuming that you have set up everything correctly, you don't have much left to do.

假设你已正确设置了所有内容,那么你就没有多少工作要做了。

In your application root, create a file named django.wsgi and write the following code.

在应用程序根目录中,创建名为django.wsgi的文件并编写以下代码。

import os
import sys

sys.path.append('/path/to/your/app')

os.environ['PYTHON_EGG_CACHE'] = '/path/to/your/app/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Now, add a virtual host in your apache configuration for serving static and other files and add the following lines:

现在,在apache配置中添加虚拟主机以提供静态和其他文件,并添加以下行:

   WSGIScriptAlias / /path/to/your/app/django.wsgi

   <Directory /path/to/your/app>
      Order allow,deny
      Allow from all
   </Directory>

   Alias /robots.txt /path/to/your/app/robots.txt
   Alias /favicon.ico /path/to/your/app/favicon.ico
   Alias /images /path/to/your/app/images
   Alias /static /path/to/your/app/static

   ErrorLog /path/to/your/app/logs/error.log
   CustomLog /path/to/your/app/access.log combined

Remember to restart apache. You can check this and this links for complete information. Also, if you need to know how to add virtual host, check this out.

记得重启apache。您可以查看此链接以获取完整信息。此外,如果您需要知道如何添加虚拟主机,请查看此信息。

Hope that helps.

希望有所帮助。

#2


0  

There are a number of howtos on the web, most of which work with current Django versions, but I was unhappy with their lack of conformance with Django's current docs and found the easiest path to follow these instructions:

网上有许多声音,其中大部分都与当前的Django版本一起使用,但我对他们对Django当前文档缺乏一致性感到不满,并找到了遵循这些说明的最简单方法:

https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/modwsgi/

There it says: 'As of Django version 1.4, startproject will have created wsgi.py for you' - which looks like this:

在那里它说:'从Django版本1.4开始,startproject将为你创建wsgi.py - 看起来像这样:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

So now it's wsgi.py that connects to mod_wsgi, which you installed with aptitude, and django.wsgi is deprecated.

所以现在是wsgi.py连接到你用aptitude安装的mod_wsgi,并且不推荐使用django.wsgi。

Now we want to honor the debian method of configuring apache sites, so instead of putting the following code into httpd.conf, as django-docs propose, we create a dj-myapp file in /etc/apache2/sites-available, activate it with a2ensite dj-myapp and disable default with a2dissite default. The WSGI-directives are written before the virtualhost section:

现在我们想要尊重配置apache站点的debian方法,所以不像django-docs建议的那样将以下代码放到httpd.conf中,而是在/ etc / apache2 / sites-available中创建一个dj-myapp文件,激活它使用a2ensite dj-myapp并使用默认的a2dissite禁用默认值。 WSGI指令在virtualhost部分之前编写:

WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com
<VirtualHost *:80>
    <Directory /path/to/mysite.com/mysite>
      <Files wsgi.py>
            Order deny,allow
            Allow from all
      </Files>
    </Directory>
</VirtualHost>

This is for apache 2.2x, for 2.4+ use Require all granted instead of Allow from all.

这适用于apache 2.2x,2.4+使用需要全部授权而不是全部允许。

Finally configure the static file serving, as described in the django docs. The directives are also placed in dj-myapp. For the admin static files this line worked for me:

最后配置静态文件服务,如django文档中所述。指令也放在dj-myapp中。对于管理静态文件,这行适用于我:

Alias /static/admin /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin

#3


0  

create file called app.conf in /etc/apache2/sites-available.The app.conf:

在/etc/apache2/sites-available中创建名为app.conf的文件.app.conf:

WSGIPassAuthorization On
WSGIPythonPath /home/brms/manage/web/brms
WSGIDaemonProcess pyramid user=brms group=brms threads=4 \
   python-path=/usr/local/lib/python3.4/dist-packages/
<VirtualHost *:80>
    <Directory /home/brms/manage/>
        <Files wsgi.py>
                WSGIProcessGroup pyramid
                Require all granted
        </Files>
    </Directory>
    Alias /meetingApp /var/www/meetingApp
</VirtualHost>
WSGIScriptAlias / /home/brms/manage/wsgi.py

Enable this site:sudo a2ensite app.conf Restart Apache: sudo service apache2 restart

启用此站点:sudo a2ensite app.conf重新启动Apache:sudo service apache2 restart

#1


1  

Presuming that you have set up everything correctly, you don't have much left to do.

假设你已正确设置了所有内容,那么你就没有多少工作要做了。

In your application root, create a file named django.wsgi and write the following code.

在应用程序根目录中,创建名为django.wsgi的文件并编写以下代码。

import os
import sys

sys.path.append('/path/to/your/app')

os.environ['PYTHON_EGG_CACHE'] = '/path/to/your/app/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Now, add a virtual host in your apache configuration for serving static and other files and add the following lines:

现在,在apache配置中添加虚拟主机以提供静态和其他文件,并添加以下行:

   WSGIScriptAlias / /path/to/your/app/django.wsgi

   <Directory /path/to/your/app>
      Order allow,deny
      Allow from all
   </Directory>

   Alias /robots.txt /path/to/your/app/robots.txt
   Alias /favicon.ico /path/to/your/app/favicon.ico
   Alias /images /path/to/your/app/images
   Alias /static /path/to/your/app/static

   ErrorLog /path/to/your/app/logs/error.log
   CustomLog /path/to/your/app/access.log combined

Remember to restart apache. You can check this and this links for complete information. Also, if you need to know how to add virtual host, check this out.

记得重启apache。您可以查看此链接以获取完整信息。此外,如果您需要知道如何添加虚拟主机,请查看此信息。

Hope that helps.

希望有所帮助。

#2


0  

There are a number of howtos on the web, most of which work with current Django versions, but I was unhappy with their lack of conformance with Django's current docs and found the easiest path to follow these instructions:

网上有许多声音,其中大部分都与当前的Django版本一起使用,但我对他们对Django当前文档缺乏一致性感到不满,并找到了遵循这些说明的最简单方法:

https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/modwsgi/

There it says: 'As of Django version 1.4, startproject will have created wsgi.py for you' - which looks like this:

在那里它说:'从Django版本1.4开始,startproject将为你创建wsgi.py - 看起来像这样:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

So now it's wsgi.py that connects to mod_wsgi, which you installed with aptitude, and django.wsgi is deprecated.

所以现在是wsgi.py连接到你用aptitude安装的mod_wsgi,并且不推荐使用django.wsgi。

Now we want to honor the debian method of configuring apache sites, so instead of putting the following code into httpd.conf, as django-docs propose, we create a dj-myapp file in /etc/apache2/sites-available, activate it with a2ensite dj-myapp and disable default with a2dissite default. The WSGI-directives are written before the virtualhost section:

现在我们想要尊重配置apache站点的debian方法,所以不像django-docs建议的那样将以下代码放到httpd.conf中,而是在/ etc / apache2 / sites-available中创建一个dj-myapp文件,激活它使用a2ensite dj-myapp并使用默认的a2dissite禁用默认值。 WSGI指令在virtualhost部分之前编写:

WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com
<VirtualHost *:80>
    <Directory /path/to/mysite.com/mysite>
      <Files wsgi.py>
            Order deny,allow
            Allow from all
      </Files>
    </Directory>
</VirtualHost>

This is for apache 2.2x, for 2.4+ use Require all granted instead of Allow from all.

这适用于apache 2.2x,2.4+使用需要全部授权而不是全部允许。

Finally configure the static file serving, as described in the django docs. The directives are also placed in dj-myapp. For the admin static files this line worked for me:

最后配置静态文件服务,如django文档中所述。指令也放在dj-myapp中。对于管理静态文件,这行适用于我:

Alias /static/admin /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin

#3


0  

create file called app.conf in /etc/apache2/sites-available.The app.conf:

在/etc/apache2/sites-available中创建名为app.conf的文件.app.conf:

WSGIPassAuthorization On
WSGIPythonPath /home/brms/manage/web/brms
WSGIDaemonProcess pyramid user=brms group=brms threads=4 \
   python-path=/usr/local/lib/python3.4/dist-packages/
<VirtualHost *:80>
    <Directory /home/brms/manage/>
        <Files wsgi.py>
                WSGIProcessGroup pyramid
                Require all granted
        </Files>
    </Directory>
    Alias /meetingApp /var/www/meetingApp
</VirtualHost>
WSGIScriptAlias / /home/brms/manage/wsgi.py

Enable this site:sudo a2ensite app.conf Restart Apache: sudo service apache2 restart

启用此站点:sudo a2ensite app.conf重新启动Apache:sudo service apache2 restart