Ubuntu服务器Apache+WSGI部署双Django应用故障求助
单IP Ubuntu VPS托管第二个域名时WSGI配置失效问题
我在Ubuntu VPS上尝试托管第二个域名domain2,已知单IP可托管多域名,但遇到问题——第一个域名domain1.tn可正常访问,第二个域名domain2却指向Apache默认页面。
我的Apache2配置文件/etc/apache2/sites-enabled/000-default-le-ssl.conf内容如下:
Define wsgi_daemon1 "domain1" Define wsgi_daemon2 "domain2" <IfModule mod_ssl.c> <VirtualHost *:443> ServerAdmin webmaster@localhost DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined Alias /static /home/my_linux_user/myproject/static <Directory /home/my_linux_user/myproject/static> Require all granted </Directory> <Directory /home/my_linux_user/myproject/myproject> <Files wsgi.py> Require all granted </Files> </Directory> <IfDefine !wsgi_init> WSGIDaemonProcess ${wsgi_daemon1} python-path=/home/my_linux_user/myproject python-home=/home/my_linux_user/myproject/myprojectenv WSGIProcessGroup ${wsgi_daemon1} WSGIScriptAlias / /home/my_linux_user/myproject/myproject/wsgi.py Define wsgi_init 1 </IfDefine> ServerName domain1.tn SSLCertificateFile /etc/letsencrypt/live/domain1.tn/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/domain1.tn/privkey.pem Include /etc/letsencrypt/options-ssl-apache.conf </VirtualHost> <VirtualHost *:443> ServerAdmin webmaster@localhost DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error_project2.log CustomLog ${APACHE_LOG_DIR}/access_project2.log combined Alias /static /home/my_linux_user/project2/static <Directory /home/my_linux_user/project2/static> Require all granted </Directory> <Directory /home/my_linux_user/project2/django_project> <Files wsgi.py> Require all granted </Files> </Directory> <IfDefine !wsgi_init> WSGIDaemonProcess ${wsgi_daemon2} python-path=/home/my_linux_user/project2 python-home=/home/my_linux_user/project2/project2ENV WSGIProcessGroup ${wsgi_daemon2} WSGIScriptAlias / /home/my_linux_user/project2/django_project/wsgi.py Define wsgi_init 1 </IfDefine> ServerName domain2 SSLCertificateFile /etc/letsencrypt/live/domain2/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/domain2/privkey.pem Include /etc/letsencrypt/options-ssl-apache.conf </VirtualHost> </IfModule>
我已尝试以下操作:
- 执行
apachectl restart和apachectl graceful重启Apache2 - 确认DNS配置正常解析
- 单独配置虚拟主机托管静态页面可正常工作,但添加WSGI配置后失效
- 用
apache2ctl -S查看已启用站点,输出如下:
*:443 is a NameVirtualHost default server domain1.tn (/etc/apache2/sites-enabled/000-default-le-ssl.conf:5) port 443 namevhost domain1.tn (/etc/apache2/sites-enabled/000-default-le-ssl.conf:5) port 443 namevhost domain2.com (/etc/apache2/sites-enabled/000-default-le-ssl.conf:106) port 443 namevhost mail.domain1.tn (/etc/apache2/sites-enabled/mail.domain1.tn.conf:3) ServerRoot: "/etc/apache2" Main DocumentRoot: "/var/www/html" Main ErrorLog: "/var/log/apache2/error.log" Mutex watchdog-callback: using_defaults Mutex rewrite-map: using_defaults Mutex ssl-stapling-refresh: using_defaults Mutex ssl-stapling: using_defaults Mutex proxy: using_defaults Mutex ssl-cache: using_defaults Mutex default: dir="/var/run/apache2/" mechanism=default Mutex mpm-accept: using_defaults PidFile: "/var/run/apache2/apache2.pid" Define: DUMP_VHOSTS Define: DUMP_RUN_CFG Define: MODSEC_2.5 Define: MODSEC_2.9 Define: wsgi_daemon1=domain1 Define: wsgi_daemon2=domain2 Define: wsgi_init=1 User: name="www-data" id=33 Group: name="www-data" id=33
请问配置更改是否需要时间生效?(部署第一个域名时无此情况)若无需等待,恳请提供解决建议。
解决建议
你的问题核心在于<IfDefine !wsgi_init>的条件判断逻辑:第一个虚拟主机加载时已执行Define wsgi_init 1,导致第二个虚拟主机的该条件不成立,WSGI守护进程、进程组及脚本别名的配置完全未生效,最终只能使用默认的DocumentRoot /var/www/html(即Apache默认页面)。
修复步骤:
- 移除全局条件控制块:删除两个虚拟主机内的
<IfDefine !wsgi_init>和Define wsgi_init 1代码块,直接保留各自的WSGI配置。修改后的第二个虚拟主机WSGI部分应为:
WSGIDaemonProcess ${wsgi_daemon2} python-path=/home/my_linux_user/project2 python-home=/home/my_linux_user/project2/project2ENV WSGIProcessGroup ${wsgi_daemon2} WSGIScriptAlias / /home/my_linux_user/project2/django_project/wsgi.py
第一个虚拟主机做同样修改,去掉IfDefine包裹。
- 重启Apache服务:执行
sudo apachectl restart使配置生效。
额外检查项:
- 确认
/home/my_linux_user/project2/django_project/wsgi.py文件权限,确保www-data用户有读取权限 - 验证project2虚拟环境路径正确性,
python-home应指向包含bin/python的虚拟环境根目录 - 查看第二个域名的错误日志
/var/log/apache2/error_project2.log,通过具体报错辅助排查潜在问题
内容的提问来源于stack exchange,提问作者MedAmine
相关产品推荐
相关产品推荐

