skip to Main Content

I’m hosting Django app on AWS ec2 with ubuntu 20.04 lts. I can run the app with venv on various ports. I want to bind the app with apache so that my users can visit my app with just the public DNS no ports, only on the default port. So I have installed apache2, mod_wsgi.
My virtual host configuration.

<VirtualHost *:80>
  ServerAdmin [email protected]
  DocumentRoot /var/www/html/app_folder

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
  
  Alias /static /var/www/html/app_folder/static

  <Directory /var/www/html/app_folder/static>
    Require all granted
  </Directory>

  Alias /static /var/www/html/app_folder/media

  <Directory /var/www/html/app_folder/media>
    Require all granted
  </Directory>


  <Directory /var/www/html/app_folder/main_app>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>

  WSGIDaemonProcess app_folder python-path=/var/www/html/app_folder python-home=/home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py

  WSGIProcessGroup app_folder
  WSGIScriptAlias / /var/www/html/app_folder/main_app/wsgi.py

</VirtualHost>

So after configuring this vhost i restart my apache2 but I get nothing on the public dns. Instead I get the following error logs on my /var/log/apache2/error.log

[Wed Dec 02 08:50:05.967958 2020] [wsgi:warn] [pid 121300] (13)Permission denied: mod_wsgi (pid=121300): Unable to stat Python home /home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py. Python interpreter may not be able to be initialized correctly. Verify the supplied path and access permissions for whole of the path.
Python path configuration:
  PYTHONHOME = '/home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py'
  PYTHONPATH = (not set)
  program name = 'python3'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = '/usr/bin/python3'
  sys.base_prefix = '/home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py'
  sys.base_exec_prefix = '/home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py'
  sys.executable = '/usr/bin/python3'
  sys.prefix = '/home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py'
  sys.exec_prefix = '/home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py'
  sys.path = [
    '/home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py/lib/python38.zip',
    '/home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py/lib/python3.8',
    '/home/ubuntu/.local/lib/python3.8/site-packages/django/__init__.py/lib/python3.8/lib-dynload',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f0e7fa1bc40 (most recent call first):
<no Python frame>
  • So what should I do???
  • I need to upload the Django site and make it live with apache
  • so how could I make the server live?
  • How could I view my site only with http://public_ip?
  • I have read many blogs but I just can’t make the site live with apache!!!
  • Please have a look and suggest solutions.

3

Answers


  1. Chosen as BEST ANSWER

    Serving python or Django app through apache sometimes becomes hectic but it could be done. I had configured Django with apahce and also with nginx. Well when it come to django i think we should use gunicorn to serve django applications. I switched to nginx for this one. i created a service file which runs the app via gunicorn with 3 workers using the apps virtualenv. below is the service file.

    [Unit]
    Description=Softopark gunicorn service by: Softopark.com
    After=network.target
    
    [Service]
    User=nginx
    Group=nginx
    WorkingDirectory=/usr/share/nginx/html/softopark_cms_2
    Environment="/home/ec2-user/Env/cms5-yYbXgITO/bin"
    ExecStart=/home/ec2-user/Env/cms5-yYbXgITO/bin/gunicorn --workers 3 --bind 0.0.0.0:8000 cms_project.wsgi
    
    [Install]
    WantedBy=multi-user.target
    

    Working like a charm


  2. I think you set your python-path wrong on virtual host configuration.
    This should be something like this:

    WSGIDaemonProcess app_folder python-path=/home/ubuntu/.local/lib/python3.8/site-packages
    
    Login or Signup to reply.
  3. I have tasted the same bitter experience. I guess this is a problem of apache/mod_wsgi which is not looking on python3.9 but on python3.8 which is the default python package for ubuntu 20.04.

    Unfortunately, I don’t have anything clever/easy to suggest. I guess, the solution would be to build mod_wsgi module from source against python 3.9. However, this is not a trivial process and I have never done it myself so I cannot really confirm its validity.

    You can also check this link

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search