简体   繁体   中英

Django Apache 2 ModuleNotFoundError: No module named 'django'

I am trying to deploy my django application on apache2, I am on an AWS ec2 ubuntu instance. Originally I got an Fatal Python error: Py_Initialize: Unable to get the locale encoding, so I switched my python from 2.7 to 3.6 by removing my env and starting a new virtual env on 3.6, I also purged python 2.7 and 3.7 from my instance. After that I got a no module named 'Django error' in my apache. Any help is appreciated been stuck on a while with this :).

<VirtualHost *:80>
ServerAdmin webmaster@example.com
DocumentRoot /home/ubuntu/pydjangoenv/myproj
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/ubuntu/pydjangoenv/myproj/static
<Directory /home/ubuntu/pydjangoenv/myproj/static>
Require all granted
</Directory>
<Directory /home/ubuntu/pydjangoenv/myproj/myproj>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproj python-path=/home/ubuntu/pydjangoenv/myproj python-home=/home/ubuntu/pydjangoenv/venv2

WSGIScriptAlias / /home/ubuntu/pydjangoenv/myproj/myproj/wsgi.py
</VirtualHost>

Also when I run pip3 install django my django is in Requirement already satisfied: django in /home/ubuntu/pydjangoenv/venv2/lib/python3.6/site-packages (3.2.5)

Once you configured your EC2 instance, I will assume you want backend and frontend in this server, the access to the instance via shell . Then run:

To update packages from Ubuntu:

sudo apt-get update

Install Python with:

sudo apt-get install python3.6

Install pip with:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

Then you need to install a virtual environment, you can use virtualenv :

pip install virtualenv

Create a virtual env with:

virtualenv mynewvenv

Then get into the venv with:

source mynewvnev/bin/activate

Once you are inside your vnev, run this to install Django:

pip install django

Then I recommend uploading your Django project into GitHub or GitLab, then clone that repository.

Is very important to update your settings.py , especially the ALLOWED_HOSTS if you are redirecting with a custom domain:

ALLOWED_HOSTS = ['yourdomain.com',
                 'www.yourdomain.com',]

Also is very important to update your DB access if required, in case you are running DB with default sqlite3 just make sure that the DB name, user, and password match with your actual info.

If you did the above correctly, then try to update the WSGI section on your Apache config file to look something like this:

WSGIPassAuthorization On
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIDaemonProcess yourapp python-home=/usr/local/venvs/yourapp
WSGIProcessGroup DjangoMainAppName

It is likely that the error is on this line:

WSGIDaemonProcess delta_new python-path=/home/ubuntu/pydjangoenv/myproj python-home=/home/ubuntu/pydjangoenv/venv2

Try changing to:

WSGIDaemonProcess delta_new python-path=/home/ubuntu/pydjangoenv/myproj python-home=/home/ubuntu/pydjangoenv/venv2/lib/python3.6/site-packages

Using the accurate Python version.

You can read on how to properly configure the config file for a Django app with daemon mode for a single app here .

通过使用重新安装要求的python 3.6创建新的虚拟环境,我能够通过更改为python 3.6来使我的网站通过apache运行,然后我使用sudo apt-get remove libapache2-mod-wsgi卸载libapache2-mod-wsgi-py3 -py3,然后我在我的 python 3.6 虚拟环境中再次安装它,然后该网站最终工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM