无法在Ubuntu服务器上部署Django应用程序

时间:2017-03-28 12:51:06

标签: python django ubuntu

所以我设法通过this在我的服务器上设置我的django项目 教程。我可以通过

在端口8000上运行它
python manage.py runserver

但我现在想要将其部署到服务器的域名或IP地址而不指定端口。 按照教程中的说明配置000-default.conf后,我得到500 Internal Server Error

error.log中:

[Tue Mar 28 12:34:29.006570 2017] [wsgi:error] [pid 12773:tid 140065216325376] [remote 31.49.113.30:62072] mod_wsgi (pid=12773): Target WSGI script '/home/ubuntu/myproject/myproject/wsgi.py' cannot be loaded as Python module.
[Tue Mar 28 12:34:29.006623 2017] [wsgi:error] [pid 12773:tid 140065216325376] [remote 31.49.113.30:62072] mod_wsgi (pid=12773): Exception occurred processing WSGI script '/home/ubuntu/myproject/myproject/wsgi.py'.
[Tue Mar 28 12:34:29.006695 2017] [wsgi:error] [pid 12773:tid 140065216325376] [remote 31.49.113.30:62072] Traceback (most recent call last):
[Tue Mar 28 12:34:29.006714 2017] [wsgi:error] [pid 12773:tid 140065216325376] [remote 31.49.113.30:62072]   File "/home/ubuntu/myproject/myproject/wsgi.py", line 12, in <module>
[Tue Mar 28 12:34:29.006718 2017] [wsgi:error] [pid 12773:tid 140065216325376] [remote 31.49.113.30:62072]     from django.core.wsgi import get_wsgi_application
[Tue Mar 28 12:34:29.006733 2017] [wsgi:error] [pid 12773:tid 140065216325376] [remote 31.49.113.30:62072] ImportError: No module named 'django'

我是Django和Ubuntu的新手,所以我真的不明白错误,任何帮助都会非常感谢,谢谢

2 个答案:

答案 0 :(得分:1)

Web服务器无法找到现有的django安装。您尚未安装它,或者您尚未将服务器配置为使用虚拟环境(如果您正在使用它)。

  

ImportError:没有名为'django'的模块

答案 1 :(得分:0)

经过一段时间的研究和学习新事物,我能够将它部署到生产中。唯一的问题是我的设置基于RHEL7,MySQL,Python3.5。所以这里有详细信息,您可能希望根据Ubuntu进行更改。 mod_wsgi部分非常重要。

yum check-update 
yum groupinstall -y "Development tools"
----------------------------------------------------------------
Apache2.4
----------------------------------------------------------------
yum install -y httpd httpd-devel
systemctl start httpd.service
systemctl enable httpd.service
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 user /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 {} +

----------------------------------------------------------------
MySQL
----------------------------------------------------------------
yum install -y mariadb-server mariadb
systemctl start mariadb.service
systemctl enable mariadb.service
mysql_secure_installation
----------------------------------------------------------------
----------------------------------------------------------------
Python3.5 along with lib and devel packages
----------------------------------------------------------------
yum install -y https://rhel7.iuscommunity.org/ius-release.rpm
yum install -y python35u python35u-libs python35u-devel python35u-pip
pip3.5 install virtualenv
----------------------------------------------------------------
mod-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.so
----------------------------------------------------------------
portal.conf
----------------------------------------------------------------
vi /etc/httpd/conf.d/portal.conf

LoadModule wsgi_module /usr/lib64/httpd/modules/mod_wsgi.so
<VirtualHost *:80>
  ServerAdmin youradmin@email.address
  ServerName portal
  ServerAlias portal.com
  DocumentRoot /var/www/html/portal/src/

  WSGIDaemonProcess portal python-path=/var/www/html/portal/src:/var/www/html/portal/venv/lib/python3.5/site-packages
  WSGIApplicationGroup portal
  WSGIScriptAlias / /var/www/html/portal/src/portal/wsgi.py process-group=portal

  <Directory /var/www/html/portal/src>
    Require all granted
  </Directory>

  <Directory /var/www/html/portal/src/portal>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>

</VirtualHost>

systemctl restart httpd