我是一个n00b,试图让django在一个新的安装上与apache一起工作。端口8000上的测试服务器工作正常,但apache没有。似乎apache无法读取虚拟环境。
日志中的错误:
[Mon Apr 03 17:14:32.560566 2017] [wsgi:error] [pid 10799] [client 72.178.8.113:37774] mod_wsgi (pid=10799): Target WSGI script '/home/palace/palace/palace/wsgi.py' cannot be loaded as Python module.
[Mon Apr 03 17:14:32.560944 2017] [wsgi:error] [pid 10799] [client 72.178.8.113:37774] mod_wsgi (pid=10799): Exception occurred processing WSGI script '/home/palace/palace/palace/wsgi.py'.
[Mon Apr 03 17:14:32.561249 2017] [wsgi:error] [pid 10799] [client 72.178.8.113:37774] Traceback (most recent call last):
[Mon Apr 03 17:14:32.561338 2017] [wsgi:error] [pid 10799] [client 72.178.8.113:37774] File "/home/palace/palace/palace/wsgi.py", line 12, in <module>
[Mon Apr 03 17:14:32.561386 2017] [wsgi:error] [pid 10799] [client 72.178.8.113:37774] from django.core.wsgi import get_wsgi_application
[Mon Apr 03 17:14:32.561470 2017] [wsgi:error] [pid 10799] [client 72.178.8.113:37774] ImportError: No module named 'django'apache配置:
<VirtualHost *:80>
ServerName www.persiaspalace.us
ServerAdmin webmaster@localhost
DocumentRoot /home/palace/
WSGIDaemonProcess myproject python-home=/home/palace/palace/:/home/palace/vpalace/bin:/home/palace/vpalace/lib/python3.5/site-packages/
#WSGIDaemonProcess myproject python-home=/home/palace/vpalace/lib/python3.5/site-packages/ python-path=/home/palace/palace/
WSGIScriptAlias / /home/palace/palace/palace/wsgi.py
#WSGIPythonHome /home/palace/vpalace/
#WSGIPythonPath /home/palace/palace/
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/palace/
<Directory /home/palace/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>django安装在虚拟环境中:
(vpalace) root@pieceofshiot:/home/palace# pip install django
Requirement already satisfied: django in ./vpalace/lib/python3.5/site-packages目录权限结构:文件为0644,目录为0744
(vpalace) root@pieceofshiot:/home/palace# ls -l
total 12
-rw-r--r-- 1 www-data www-data 0 Apr 3 04:14 index.php.bak
-rwxr-xr-x 1 www-data www-data 807 Apr 2 22:36 manage.py
drwxr--r-- 3 www-data www-data 4096 Apr 2 22:46 palace
drwxr-xr-x 4 www-data www-data 4096 Apr 2 22:32 vpalacedjango版本:
>>> django.VERSION
(1, 10, 6, 'final', 0)mod_wsgi版本:
ii libapache2-mod-wsgi-py3 4.3.0-1.1build1 amd64 Python 3 WSGI adapter module for Apache如何让django在apache/ubuntu上运行?
发布于 2017-04-03 20:42:57
两者均:
WSGIDaemonProcess myproject python-home=/home/palace/palace/:/home/palace/vpalace/bin:/home/palace/vpalace/lib/python3.5/site-packages/
#WSGIDaemonProcess myproject python-home=/home/palace/vpalace/lib/python3.5/site-packages/ python-path=/home/palace/palace/是错误的尝试。注释掉的那个是最近的,但仍然是错的。
去读:
如何设置Python虚拟环境。
python-home选项应该指定一个目录(而不是列表),它是(不是site-packages)的根。这应该是sys.prefix在导入sys并查看该值时为提供的内容。
不过,你也有其他的错误。
您缺少了WSGIProcessGroup myproject指令或WSGIScriptAlias的process-group=myproject选项。这意味着您没有委托给守护进程组,在那里尝试设置Python虚拟环境。
您也不应该将DocumentRoot设置为包含任何敏感内容的目录,因为Apache中的任何内容都可以公开供下载的所有文件。
最后,当将单个WSGI应用程序委托给守护进程组时,设置:
WSGIApplicationGroup %{GLOBAL}这避免了一些第三方Python包无法在子解释器中正确工作的问题。
发布于 2017-04-03 18:16:56
你能试一下这个配置吗,看看它能不能工作。问题是apache无法找到python模块路径。应该注意WSGIPythonPath。
# this section will be commented in the httpd.conf, uncomment them to use virtualhosts
NameVirtualHost *:80
# Add the WSGI settings
WSGISocketPrefix /var/run/wsgi
WSGIPythonPath /home/vpalace/lib/python3.5/site-packages
WSGIDaemonProcess vpalace processes=1 maximum-requests=500 threads=1
WSGIProcessGroup vpalace
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/palace/
ServerName www.persiaspalace.us
Alias /static /home/palace/
<Directory /home/palace/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/palace/palace/palace/wsgi.py
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>发布于 2017-04-03 19:03:44
下面是一个基于RHEL7、Python3.5、Apache2.4的解决方案。相应改变。mod_wsgi的编译部分是其中的重要部分。这是使用virtualenv。
Apache2.4
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
chgrp -R apache /var/www/html
find /var/www/html -type d -exec chmod g+rx {} +
find /var/www/html -type f -exec chmod g+r {} +
chown -R myuser /var/www/html/
find /var/www/html -type d -exec chmod u+rwx {} +
find /var/www/html -type f -exec chmod u+rw {} +
find /var/www/html -type d -exec chmod g+s {} +Python3.5
yum install -y https://rhel7.iuscommunity.org/ius-release.rpm
yum install -y python35u python35u-libs python35u-devel python35u-pip
pip3.5 install virtualenvmod_wsgi
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.5.14.tar.gz
tar -zxvf 4.5.14.tar.gz
cd mod_wsgi-4.5.14
./configure --with-python=/usr/bin/python3.5
make
make install
chmod 755 /usr/lib64/httpd/modules/mod_wsgi.somyportal.conf
vi /etc/httpd/con.d/myportal.conf
LoadModule wsgi_module /usr/lib64/httpd/modules/mod_wsgi.so
<VirtualHost *:80>
ServerAdmin admin@email.com
ServerName myportal
ServerAlias myportal.com
DocumentRoot /var/www/html/myportal/src/
WSGIDaemonProcess myportal python-path=/var/www/html/myportal/src:/var/www/html/myportal/venv/lib/python3.5/site-packages
WSGIApplicationGroup myportal
WSGIScriptAlias / /var/www/html/myportal/src/myportal/wsgi.py process-group=myportal
<Directory /var/www/html/myportal/src>
Require all granted
</Directory>
<Directory /var/www/html/myportal/src/myportal>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>然后在shell systemctl restart httpd上执行以下命令
https://stackoverflow.com/questions/43190616
复制相似问题