Skip to main content
 首页 » 编程设计

python之使用 mod_wsgi 部署具有域和子域的 django 应用程序

2025年05月04日26mq0036

我是 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 forbidden 错误。我试图消除此错误并引用了其他 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), 
) 

基本上,我要做的是让我的 app1 在 xyz.com 上运行,而我的 app2 在 abc 上运行.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> 

请您参考如下方法:

嗨,@Daniel Roseman 的评论是正确的。我再次浏览了文档并尝试了页面中提到的方式。我终于解决了这个问题。

我就是这样解决的..

app1.conf

<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> 

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

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

setting.py

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。