使用mod_wsgi

时间:2015-05-27 08:34:50

标签: python django apache subdomain mod-wsgi

我是django的新手,这是我在django的第一个项目。到目前为止,我已经开发了一个django应用程序。它在我的本地机器上流畅地工作,但我无法在线部署它。

我在互联网上看过很多关于在服务器上部署应用程序的教程。但他们似乎都没有对我有用。可能是我在这里做错了。

https://www.youtube.com/watch?v=hBMVVruB9Vs https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/modwsgi/ https://www.digitalocean.com/community/tutorials/how-to-deploy-a-local-django-app-to-a-vps https://www.digitalocean.com/community/tutorials/how-to-run-django-with-mod_wsgi-and-apache-with-a-virtualenv-python-environment-on-a-debian-vps

我按照这些教程尝试在服务器上部署应用程序,但我总是得到403禁止错误。我试图删除此错误并引用其他stackoverflow答案,但没有成功..

403 Forbidden error with Django and mod_wsgi

Apache mod_wsgi error: Forbidden You don't have permission to access / on this server

Django on apache wtih mod_wsgi (Linux) - 403 Forbidden

Installing Django with mod_wsgi

这是我为django项目创建的结构。我的django项目中有两个应用程序,我正在连接我的一个应用程序连接DNS xyz.com,而我的第二个应用程序连接其子域abc.xyz.com

结构

project_folder
|
|->app1
|      |->urls.py
|      |->template_folder
|      |->static_folder
|->app2
|     |->urls.py
|     |->template_folder
|     |->static_folder
|->project_name
      |->urls.py
      |->wsgi.py

project_name/urls.py
urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^app1/', include('app1.urls')),
    url(r'^app2/', include('app2.urls')),
]

app1/urls.py
urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^example1/', views.example1),
    url(r'^example2/', views.example2),
)

app2/urls.py
urlpatterns = patterns('',
    url(r'^example3/', views.example3),
    url(r'^example4/', views.example4),
)

基本上我要做的是我的app1xyz.com上运行而我的app2abc.xyz.com(子域)上运行。这样项目中的所有应用程序都具有相同的用户登录名。如果用户从一个应用程序登录,它也将登录到第二个应用程序。

在我的本地计算机上,该应用程序以

运行

http://localhost:8000/app1

http://localhost:8000/app1/example1

http://localhost:8000/app1/example2

http://localhost:8000/app2/example3

http://localhost:8000/app2/example4

这是我在服务器上创建的apache2 conf文件

<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName xyz.com

DocumentRoot /home/user_name/project_folder/

<Directory />
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
    AddHandler mod_python .py
    PythonHandler mod_python.publisher | .py
    PythonDebug On
</Directory>

<Directory /home/user_name/project_folder/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog /home/user_name/project_folder/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /home/user_name/project_folder/access.log combined
</VirtualHost>

2 个答案:

答案 0 :(得分:1)

你好@Daniel Roseman的评论是正确的。我再次通过文档,尝试按照页面中提到的方式。我终于解决了这个问题。

这是我解决它的方式..

<VirtualHost *:80> ServerName xyz.com WSGIScriptAlias / /var/www/html/project_folder/project_name/wsgi.py WSGIDaemonProcess xyz.com python-path=/var/www/html/project_folder:/usr/local/lib/python2.7/site-packages WSGIProcessGroup xyz.com <Directory /var/www/html/project_folder/project_name> <Files wsgi.py> Require all granted </Files> </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

app1.conf

使用命令sudo a2ensite app1.conf启用setting.py文件。

对于域名和子域名,我使用了django package django-subdomains

INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sites', # For include sites 'app1', 'ap2', ) MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'subdomains.middleware.SubdomainURLRoutingMiddleware', # Subdomain package 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.security.SecurityMiddleware', ) SITE_ID = 1 ROOT_URLCONF = 'app1.urls' SUBDOMAIN_URLCONFS = { None: 'app1.urls', 'www': 'app1.urls', 'abc': 'app2.urls', }

python manage.py migrate

完成所有设置后,运行命令django_site。它将在数据库中创建example.com表,其中包含一个条目。将xyz.com更改为app2.conf

对于子域名,请创建新的xyz.com文件并将abc.xyz.com更改为page.navigate('downloaded/page-name');

答案 1 :(得分:-3)

请不要将mod_wsgi与apache一起使用。试试像gunicorn + nginx或uwsgi + nginx这样的东西。因为mod_wsgi真的很老了!