0

I'm trying to set up an Apache2/Django server on Ubuntu 18.04 x64 Digital Ocean droplet. I've went through the tutorials but still get an error. I've searched Stackoverflow and other sources for the solution, but still nothing works. Would someone be able to help?

The project is placed in /root/myproject. I use virtualenv that I named myprojectenv. So the python path is correct with /root/myproject/myprojectenv. I'm using Python 3.6 and the version of /root/myproject/myprojectenv/bin/python is indeed 3.6. I'm using libapache2-mod-wsgi-py3.

I have chown'ed the whole /root/myproject folder to 'www-data' and chmod'ed to 664 the sqlite.db file in accordance with DigitalOcean's tutorial.

The content of my /etc/apache2/sites-available/000-default.conf file is (comments deleted):

<VirtualHost *:80>

        ServerAdmin webmaster@localhost
        DocumentRoot /root/myproject


        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined


        Alias /static/ /root/myproject/static/
        <Directory /root/myproject/static>
                Require all granted
        </Directory>

        <Directory /root/myproject/myproject>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>
        WSGIDaemonProcess myproject python-home=/root/myproject/myprojectenv python-path=/root/myproject
        WSGIProcessGroup myproject
        WSGIScriptAlias / /root/myproject/myproject/wsgi.py process-group=myproject

</VirtualHost>

The content of /root/myproject/myproject/settings.py is:

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = '[SOME STRING]'

DEBUG = True

ALLOWED_HOSTS = ['[IP Address as a string]']

...

The error I get by trying to access the page via browser:

Forbidden You don't have permission to access / on this server. Apache/2.4.29 (Ubuntu) Server at [IP ADDRESS] Port 80

The error log from /var/log/apache2/error.log

[Wed May 16 19:55:47.012027 2018] [wsgi:warn] [pid 3565:tid 140254930643904] (13)Permission denied: mod_wsgi (pid=3565): Unable to stat Python home /root/myproject/myprojectenv. Python interpreter may not be able to be initialized correctly. Verify the supplied path and access permissions for whole of the path.
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

The problem seems to be with apache being unable to run python.

Thanks in advance. Happy to add further information if needed.

Best regards

Saalim

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
Saalim
  • 19
  • 2
  • Did you use the `-R` option when doing `chown` on your project directory? `-R` is used for changing ownership of files and directories inside the parent. – xyres May 16 '18 at 20:35
  • Thanks, Yes, I did call chown recursively. – Saalim May 16 '18 at 20:40
  • Can you run run this command - `python -c "import encodings" - and see if throws an `ImportError`. Run it after activating your virtualenv. If it does raise an error, you might find this helpful - https://stackoverflow.com/q/38132755/1925257 – xyres May 16 '18 at 20:52
  • Are you sure ``libapache2-mod-wsgi-py3`` isn't Python 3.5. You can't mix Python versions. The mod_wsgi module must be compiled for same Python version as the virtual environment else you can get this and other errors. – Graham Dumpleton May 17 '18 at 04:27
  • Also, what do you get for ``ls -lasd /root``. The permissions on that directory means that user Apache run as wouldn't be able to read it. Changing permissions of directories underneath is not enough. Good rule is never put stuff under ``/root`` or a home directory. – Graham Dumpleton May 17 '18 at 04:29
  • Thanks, Graham and xyres. Changing the directory to /var/www/myproject helped. I've tried previously /home/username/myproject and it didn't work too. Seems Graham was right regarding the top level folder permissions. Cheers – Saalim May 19 '18 at 08:51

0 Answers0