"Permission denied" Error in Django Application Deployed on Amazon Elastic Beanstalk - python

I have deployed a Django project (MyProj) to Amazon beanstalk using the eb CLI. This project is physically located at ~/MyProj. It has an app, named myapp, which needs to execute external python scripts. For this purpose, I have my_launcher.py in ~/MyProj/myapp directory. A code snippet in my_launcher.py is:
import os
import importlib
PROJECTS_DIR = os.path.join(os.path.expanduser(os.environ.get('LOCATION')), 'app/python/')
MY_MODULE = os.path.join(PROJECTS_DIR, 'MyModule/my_script.py')
spec = importlib.util.spec_from_file_location('my_script', My_MODULE)
MyModule = importlib.util.module_from_spec(spec)
spec.loader.exec_module(MyModule)
The reason for using the environment variable LOCATION is to dynamically set the location of external python scripts. I have defined this environment variable and some others through the AWS admin console under the Elastic Beanstalk section. In my example, I have:
LOCATION = '/home/ec2-user/company/'
This script fails at the last line with the error:
PermissionError at /myapp
[Errno 13] Permission denied: '/home/ec2-user/company/app/python/MyModule/my_script.py'
The things I have done so far to no avail:
After reading many online pages (e.g., Django [Errno 13] Permission denied: '/var/www/media/animals/user_uploads'), I thought the issue is Linux permissions on the location where the executable python scripts are (i.e., /home/ec2-user/company/app/python/). For the sake of testing this hypothesis (even though I know it is a bad approach for production), I ran the command sudo chmod -R 777 company while being in /home/ec2-user directory. I redeployed the project and even rebooted the EC2 instance all together. This didn't help.
I moved the company directory inside ~/MyProj/myapp (and updated the environment variable LOCATION on the server) thinking that the wsgi user does not have execution permission on any directory outside ~/MyProj/myapp. This didn't help.
I highly appreciate any help.
By the way, I am using python 3.6 and Django 2.1.7.

Related

Why am I getting PermissionError [Errno 13] when attempting to write to log in Flask hosted by Apache?

I am running Flask 1.1.4 via Python 3.5.3, hosted via an Apache 2 server on Debian Stretch. I am attempting to log various messages from the program, using the python logging module. This works normally. However, if I restart the Apache server using sudo service apache2 restart, the Flask application errors out with PermissionError: [Errno 13] Permission denied: (log file name). Is this an issue anyone else has run into?
If it is not a permissions issue, this usually happens (happened to me a few times) if you are using a relative path for the log file.
The current working directory for an app running with mod_wsgi is not the directory the the app is in. If you are using a relative path for the log file you can try to use absolute path instead.
More info here: https://modwsgi.readthedocs.io/en/master/user-guides/application-issues.html#application-working-directory
It turns out that there was a cron process that was editing the ownership of all files in my log folder to root. Once the program was restarted and needed to re-obtain the file reference, it was unable to do so.

Permission Error: [Errno 13] Permission denied: 'traccert/tesseract.exe' while run our flask application

When I run the application on local host, its working fine, but when I run the application on heroku and try to convert the image to text its show an error. You may check the image below.
This is path to tesseract:
pytesseract.pytesseract.tesseract_cmd = 'traccert/tesseract.exe'
1: click here to see the image of an error.
It looks like you're trying to execute a .exe file, which is only runnable on the Windows operating system. Heroku's platform is Linux-based, which means you won't be able to run that .exe file.
If you're willing to do some work, you might be able to get the .exe file running by building a custom Heroku Buildpack that includes wine, but this depends on a lot of variables: what libraries your Windows executable relies on, etc.

Unable to migrate Django db when using docker container

On my Windows 10 machine, I am developing a database manager. Because the backend uses LDAP and the required development libraries are only available for Linux, I want to use Docker to set up an environment with the appropriate libs.
I managed to write a Dockerfile and compose file, that launch the (currently very basic) Django app in a Docker container with all the libs necessary.
I would like to play around with the django-ldapdb package and for that I want to apply the migrations.
When I open PyCharm's terminal and try to execute python manage.py migrate, I get an error telling me that the module ldapdb is not found. I suspect this is because the command does not use the remote Docker interpreter I set up with PyCharm.
The other thing I tried is using PyCharm's dedicated manage.py console. This does not initialize properly. It says the working directory is invalid and needs to be an absolute path, although the path it shows it the absolute path to the project.
I have to admit that I have no idea how this remote interpreter works and I don't see any Docker container running, so I might have not understood something properly here. I even tried running the app using PyCharm's Django run config, which started a container, but still I get the same errors.
I googled a lot, but I couldn't find more infos about remote interpreters nor something solving my issue.
The only way I managed to do this, is by executing the command inside the container.
To get inside a container named contr, use the docker command
docker exec -ti contr /bin/bash

eb.exe deploy": Errno 13 Permission denied: './pagefile.sys'

I am compiling a website using TeamCity on a server and need to deploy the compiled website to AWS.
As my last build step, I use the Elastic Beanstalk CLI to deploy: "C:\Python34...\eb.exe deploy".
eb init has already been run...but whenever I run "eb deploy", (even when I run it from the command line in an empty directory--which should deploy a default project to AWS), an error appears saying:
Error: PermissionError :: [Errno 13] Permission denied: './pagefile.sys'
I have already run the command on my local machine without any problems; I receive the error on the server regardless of whether I am running the command line as an administrator.
I am wondering if this is a permissions issue with the server, or something else? I haven't been able to achieve much insight from the other questions, because they seem to have been solved on a case-by-case basis.
pagefile.sys is the Windows swap file.
It is a special file which cannot be written or manipulated. Whatever your command is doing you need to fix your command so it that doesn't touch this file and ignores it.

Changing default permission in Flask application with Apache web server

I have a flask application that creates directory with this code:
if not os.path.exists(target_path):
os.makedirs(target_path)
With the created directory the default permission is 0755, and the owner and group is _www.
So, only the owner can write in the directory.
I always have to modify the permission manually to 0775 to make some files in it.
How to make the default directory permission as 0775? I use apache for web server.
This has something to do with Apache setup.
For Mac:
Open /System/Library/LaunchDaemons/org.apache.httpd.plist
Add <key>Umask</key> and <integer>002</integer>
Restart with sudo apachectl restart
I found the solution from this site, for linux I guess Setting the umask of the Apache user can give some hints.
Change the code to
if not os.path.exists(target_path):
os.makedirs(target_path, mode=0775)

Categories

Resources