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
Related
I'm trying to run a dash app on our website in a sub-directory. I'm getting a 403 forbidden when trying to access to website.
In the /etc/httpd/logs/ssl_error_logs i get the error:
[Tue Jan 03 13:53:42.973046 2023] [autoindex:error] [pid 50576] [client 134.94.71.5:60550] AH01276: Cannot serve directory /var/www/Typesetter/appname/: No matching DirectoryIndex (index.html,index.php,index.php) found, and server-generated directory index forbidden by Options directive
I think i might have something wrong with either the permissions or the virtualhost setup. But i can't figure out what is going wrong.
I want to run the dashboard on: our-group.our-uni.com/appname
The server is running on centos7.
I got a data folder with biochemical information to provide to the users in /var/www/html/appname/data/ from which i can download the data if i go to our-group.our-uni.com/appname/data/datapoint1.pt.
Since this works and i can also put a index.html/index.php page there without issues, i think my virtualhost setup pointing to WSGI is incorrect?
The error says cannot serve directory /var/www/Typesetter/appname/, but this directory is a symbolic link to /var/www/html/appname/.
The virtualhost file appname.conf located in /etc/httpd/conf.d/:
<VirtualHost *:80>
ServerName our-group.our-uni.com
DocumentRoot /var/www/html/appname
Errorlog /var/www/html/appname/appname-error.log
Alias /topenzyme /var/www/html/topenzmye
WSGIDaemonProcess appname user=apache group=apache threads=5 python-path=/var/www/html/appname/appname-env
WSGIScriptAlias /appname /var/www/html/appname/testapp.wsgi
<Directory /var/www/html/appname>
WSGIProcessGroup appname
WSGIApplicationGroup %{GLOBAL}
Options Indexes FollowSymLinks ExecCGI
Require all granted
</Directory>
</VirtualHost>
I also tried changing to <Directory /var/www/typesetter/appname>, but this also did not work.
The contents of testapp.wsgi in /var/www/html/appname/:
import sys, os
sys.path.insert(0, '/var/www/html/appname')
from testapp import app as application
The contents of testapp.py in /var/www/html/appname/:
from flask import Flask
app = Flask(__name__)
#app.route('/topenzyme/')
def main():
return 'Hello world'
if __name__ == '__main__':
app.run()
Permission setup:
-rwxr-xr-x. 1 apache apache 148 Jan 3 13:37 testapp.py
-rwxr-xr-x. 1 apache apache 100 Jan 3 13:38 testapp.wsgi
I also tried 777, but from what i've read 755 permissions should be sufficient?
If i do httpd -M i get wsgi_module (shared).
The wsgi module should be available.
I tried various virtualhost configs from other posts with similar issues.
I'm really unsure why this is not working.
Is there anything wrong with my configuration settings or could it be something else?
Found out the issue. I was miss-configuring my Apache setup.
As we are already hosting a website on the domain, i should not have tried to setup a virtualhost for the sub-directory. From what i understand now this only works for setting up (sub)domains. With this structure of setting it up the appname.conf file gets added the already existing virtualhost config.
Instead the /etc/httpd/conf.d/appname.conf is now:
WSGIDaemonProcess appname user=apache group=apache threads=5 python-path=/var/www/html/appname/appnameenv
WSGIProcessGroup appname
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias /appname /var/www/html/appname/testapp.wsgi
<Directory /var/www/html/appname>
Options FollowSymLinks
AllowOverride All
Require all granted
allow from all
</Directory>
This corrently links the wsgi application to my sub-directory location i wanted (our-group.our-uni.com/appname)
I am trying to deploy my Django app with my Digital Ocean droplet. There was a very useful YouTube tutorial that I followed; when he adjusted settings in Apache2, and then refreshes, he sees his Django app, whereas when I do the same, I get the following error:
Forbidden
You don't have permission to access this resource.
Apache/2.4.46 (Ubuntu) Server at 159.223.6.157 Port 80
Here you see the setup of my /etc/apache2/sites-available/hbm_project.conf file:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Alias /static /home/hbm/hbm_project/static
<Directory /home/hbm/hbm_project/static>
Require all granted
</Directory>
<Directory /home/hbm/hbm_project/hbm_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/hbm/hbm_project/hbm_project/wsgi.py
WSGIDaemonProcess hbm_app python-home=/home/hbm/hbm_project/venv python-path=/home/hbm/hbm_project python-ho>
WSGIProcessGroup hbm_app
When I look in the error log of Apache2, I see the following error:
[Thu Oct 21 07:07:57.606866 2021] [wsgi:warn] [pid 35891:tid 139970948001088] (13)Permission denied: mod_wsgi (pid=35891): Unable to stat Python home /home/hbm/hbm_project/venv. 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/hbm/hbm_project/venv'
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/hbm/hbm_project/venv'
sys.base_exec_prefix = '/home/hbm/hbm_project/venv'
sys.platlibdir = 'lib'
sys.executable = '/usr/bin/python3'
sys.prefix = '/home/hbm/hbm_project/venv'
sys.exec_prefix = '/home/hbm/hbm_project/venv'
sys.path = [
'/home/hbm/hbm_project/venv/lib/python39.zip',
'/home/hbm/hbm_project/venv/lib/python3.9',
'/home/hbm/hbm_project/venv/lib/python3.9/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 0x00007f4d86a26d40 (most recent call first):
<no Python frame>
There are some comparable errors on the internet, but none of the things that work for other people seems to work for me.
Hope someone can help me out.
Thanks!
Try chown $USER:$USER /home/hbm/hbm_project
I was deploying my django app on my VPS using the apache web server.
It works totally fine on http://192.46.209.82:8000/ but when I try to access the same using my IP address, 192.46.209.82. I get
Forbidden
You don't have permission to access this resource.
Apache/2.4.41 (Ubuntu) Server at 192.46.209.82 Port 80
Here is my conf file
nano /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster#example.com
DocumentRoot /root/django/myproject
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /root/django/myproject/static
<Directory /root/django/myproject/static>
Require all granted
</Directory>
<Directory /root/django/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/root/django/myproject python-home=/root/django/myprojectenv
WSGIProcessGroup myproject
WSGIScriptAlias / /root/django/myproject/myproject/wsgi.py
</VirtualHost>
I am using python's virtual environment, where myprojectenv is my virtual env path and the pwd is /root/django
Note : I tried the answer from the question that I was getting as suggestion to this question but that did not work for me.
EDIT : Updated permission
In my case even though I set up all the permission correctly, I still was getting error so I moved my entire project and virtual environment to /opt and it worked for me for now. But I would like to know the reason why it was not working under /home even when I gave all permissions
I'm running Django 2.0.4 with mod_wsgi 4.5.20.
I'm getting an error when I try to deploy a site to our dev environment at /parature. What's weird is that the site deployed at the root of the VirtualHost is responding as normal:
[Tue Apr 10 13:34:08.998704 2018] [wsgi:error] [pid 65245] [client xx.yy.zz:65390] Timeout when reading response headers from daemon process 'parature-develop-https': /var/django/html/parature-develop/config/wsgi.py
I can run the site via runserver with the virtualenv activated. It shouldn't be timing out, as I'm just trying to bring up the Django admin site.
<VirtualHost *:443>
SSLEngine On
ServerName wrds-pub1-dev.example.com
ErrorLog "|/usr/sbin/cronolog /var/log/httpd/errorlog/%Y/%Y-%m-wrds-pub1-dev-error.log"
LogLevel info
WSGIApplicationGroup %{GLOBAL}
# The site I'm adding, which isn't working
WSGIDaemonProcess parature-develop-https python-home=/var/django/virtualenvs/parature-develop request-timeout=600
WSGIProcessGroup parature-develop-https
WSGIScriptAlias /parature /var/django/html/parature-develop/config/wsgi.py process-group=parature-develop-https
<Directory /var/django/html/parature-develop/config>
Require all granted
</Directory>
Alias /parature/static/ /var/django/html/parature-develop/static/
<Directory /var/django/html/parature-develop/static>
Require all granted
</Directory>
# The site which has been and continues to work
WSGIDaemonProcess django-wrds-dev-https python-home=/var/django/virtualenvs/django-wrds-dev request-timeout=600
WSGIScriptAlias / /var/django/html/django-wrds-dev/config/wsgi.py process-group=django-wrds-dev-https
<Directory /var/django/html/django-wrds-dev/config>
Require all granted
</Directory>
Alias /static/ /var/django/html/django-wrds-dev/static/
<Directory /var/django/html/django-wrds-dev/static>
Require all granted
</Directory>
Alias /media/ /var/media/wrds-www/
<Directory /var/media/wrds-www>
Require all granted
</Directory>
</VirtualHost>
I feel like I'm missing something obvious, but can't see it. I've got a similar configuration in another VirtualHost with multiple Django projects under the same domain, and that is working fine, as long as the root site comes last.
The wsgi.py is almost exactly the same as the site that is working as well:
import os, sys, logging
from socket import gethostname
from django.core.wsgi import get_wsgi_application
# Since this powers Apache, let's route Python errors to the Apache
# log rather than STDOUT, where they'll never be seen.
logging.basicConfig(stream=sys.stderr)
# Figure out where we're at, and add the parent to the path
sys.path.append(os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2]))
# wrds-pub1-dev server
if 'wrds-pub1-dev' in gethostname():
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
# wrds-pub* production servers.
elif 'wrds-pub' in gethostname():
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
# else use dev settings.
else:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
application = get_wsgi_application()
Any ideas?
I figured it out - our dev server (since there's never a good time for a rebuild) is our only non-Ansiblized server, and still running mod_wsgi built against Python 3.5. The virtualenv was built against Python 3.6.
I rebuilt the virtualenv against Python 3.5, and everything works. Hopefully this saves someone hair-pulling in the future!
I have a micro instance on EC2 with Django installed there. I've also installed mod-WSGI, PostgreSQL, etc following several tutorials.
Finally, I pulled my project from bitbucket and started Apache on my EC2. Unfortunately, the only thing I have is the default Apache page and I've already spent a day and night reading and trying to figure out what am I doing wrong.
my_project is in /home/ubuntu dir. In it's folder I have wsgi.py file:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
This is my .conf file stored in /etc/apache2/httpd.conf:
Listen 80
NameVirtualHost *:80
WSGIPythonPath /home/ubuntu/my_project/
<VirtualHost *:80>
DocumentRoot /home/ubuntu/my_project
ServerName www.ec2-54-***-104-**.us-west-2.compute.amazonaws.com
ServerAlias ec2-54-***-104-**.us-west-2.compute.amazonaws.com
ErrorLog /home/ubuntu/my_project/apache/error_log
CustomLog /home/ubuntu/my_project/access_log_common
WSGIScriptAlias / /home/ubuntu/my_project/wsgi.py
Alias /static/ /home/ubuntu/my_project/static/
<Directory /home/ubuntu/my_project/static>
Require all granted
</Directory>
<Directory /home/ubuntu/my_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
When I'm trying to
/etc/init.d/apache2 stop
and instead:
python manage.py runserver 0.0.0.0:80
It works fine and I'm able to connect to my app from a browser with IP as URL.
It gives me understanding that the setting.py file is fine (am I right?) and the problem is or in wsgi.py or httpd.conf.
When I stop my development server and start Apache again, I get the default Apache page. Can you help me please to find what's wrong with my files?
Permissions for apache:
ubuntu#ip-172-**-**-69:~$ ls -ld my_project/
drwxr-xr-x 10 www-data www-data 4096 May 26 19:51 project/
You normally shouldn't use an IP address for ServerName, it has to be the hostname (FQDN) that the site is accessed as.
You are missing a WSGIDaemonProcess directive to correspond to your use of WSGIProcessGroup.
It is bad security practice to set DocumentRoot to be where your source code for your application is.
Finally, stuff under a users home directory is not usually accessible to the user that Apache runs as.
Suggest go back and review the mod_wsgi deployment documentation on the Django site again for a start.