Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Wsgi : error PermissionError : Permission denied" while trying to write to a log file #737

Closed
Tony-Frank-ML opened this issue Nov 26, 2021 · 12 comments

Comments

@Tony-Frank-ML
Copy link

I have a flask application running on a ubuntu server

I have tried to play around with other possible permission settings. I have tried setting the user 'www-data' as the owner of the folder using chown. Ive tried setting the ownerhsip of www-data using groups. Nothing seems to work.

-rw-rw-r-- 1 www-data www-data 3286 Nov 25 10:09 imm_migration.log

This is my Full error message

[Fri Nov 26 11:05:22.302892 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056] mod_wsgi (pid=1453): Failed to exec Python script file '/home/admin/imm-migration/easyucs_api.wsgi'.
[Fri Nov 26 11:05:22.302940 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056] mod_wsgi (pid=1453): Exception occurred processing WSGI script '/home/admin/imm-migration/easyucs_api.wsgi'.
[Fri Nov 26 11:05:22.303054 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056] Traceback (most recent call last):
[Fri Nov 26 11:05:22.303093 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]   File "/home/admin/imm-migration/easyucs_api.wsgi", line 8, in <module>
[Fri Nov 26 11:05:22.303099 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]     main()
[Fri Nov 26 11:05:22.303105 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]   File "/home/admin/imm-migration/easyucs_api.py", line 225, in main
[Fri Nov 26 11:05:22.303119 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]     easyucs = Easyucs(logger_handle_log_level="debug", log_file_path="imm_migration.log")
[Fri Nov 26 11:05:22.303127 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]   File "/home/admin/imm-migration/easyucs_api.py", line 38, in __init__
[Fri Nov 26 11:05:22.303144 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]     self._init_logger()
[Fri Nov 26 11:05:22.303149 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]   File "/home/admin/imm-migration/easyucs_api.py", line 102, in _init_logger
[Fri Nov 26 11:05:22.303154 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]     fh = logging.FileHandler(self._log_file_path)
[Fri Nov 26 11:05:22.303159 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]   File "/usr/lib/python3.8/logging/__init__.py", line 1143, in __init__
[Fri Nov 26 11:05:22.303162 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]     StreamHandler.__init__(self, self._open())
[Fri Nov 26 11:05:22.303167 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]   File "/usr/lib/python3.8/logging/__init__.py", line 1172, in _open
[Fri Nov 26 11:05:22.303171 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]     return open(self.baseFilename, self.mode, encoding=self.encoding)
[Fri Nov 26 11:05:22.303186 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056] PermissionError: [Errno 13] Permission denied: '/imm_migration.log'

i have set the user and the owner to www-data to the whole working directory

This is my apache conf

<VirtualHost *:80>
       
        ServerAdmin webmaster@localhost
        DocumentRoot /home/admin/imm-migration

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
           <Directory /home/admin/imm-migration>
                # set permissions as per apache2.conf file
            Options FollowSymLinks
            AllowOverride None
            Require all granted
     </Directory>
        WSGIDaemonProcess migration python-path=/home/admin/imm-migration
        LoadModule wsgi_module "/usr/local/lib/python3.8/dist-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so"
        WSGIScriptAlias / /home/admin/imm-migration/easyucs_api.wsgi
         WSGIApplicationGroup %{GLOBAL}
        WSGIProcessGroup migration
        WSGIPassAuthorization On
</VirtualHost>

python version 3.8

@GrahamDumpleton
Copy link
Owner

GrahamDumpleton commented Nov 26, 2021 via email

@Tony-Frank-ML
Copy link
Author

Tony-Frank-ML commented Nov 26, 2021

Hi Graham, Thanks for the reply

I have tried providing absolute path for the log file too. Still facing the error

[Fri Nov 26 21:24:41.875712 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.105:57686] easyucs = Easyucs(logger_handle_log_level="debug", log_file_path="/home/admin/imm-migration/imm_migration.log")

Full error message

[Fri Nov 26 11:05:22.302892 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056] mod_wsgi (pid=1453): Failed to exec Python script file '/home/admin/imm-migration/easyucs_api.wsgi'.
[Fri Nov 26 11:05:22.302940 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056] mod_wsgi (pid=1453): Exception occurred processing WSGI script '/home/admin/imm-migration/easyucs_api.wsgi'.
[Fri Nov 26 11:05:22.303054 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056] Traceback (most recent call last):
[Fri Nov 26 11:05:22.303093 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]   File "/home/admin/imm-migration/easyucs_api.wsgi", line 8, in <module>
[Fri Nov 26 11:05:22.303099 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]     main()
[Fri Nov 26 11:05:22.303105 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]   File "/home/admin/imm-migration/easyucs_api.py", line 225, in main
[Fri Nov 26 11:05:22.303119 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]     easyucs = Easyucs(logger_handle_log_level="debug", log_file_path="/home/admin/imm-migration/imm_migration.log")
[Fri Nov 26 11:05:22.303127 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]   File "/home/admin/imm-migration/easyucs_api.py", line 38, in __init__
[Fri Nov 26 11:05:22.303144 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]     self._init_logger()
[Fri Nov 26 11:05:22.303149 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]   File "/home/admin/imm-migration/easyucs_api.py", line 102, in _init_logger
[Fri Nov 26 11:05:22.303154 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]     fh = logging.FileHandler(self._log_file_path)
[Fri Nov 26 11:05:22.303159 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]   File "/usr/lib/python3.8/logging/__init__.py", line 1143, in __init__
[Fri Nov 26 11:05:22.303162 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]     StreamHandler.__init__(self, self._open())
[Fri Nov 26 11:05:22.303167 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]   File "/usr/lib/python3.8/logging/__init__.py", line 1172, in _open
[Fri Nov 26 11:05:22.303171 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]     return open(self.baseFilename, self.mode, encoding=self.encoding)
[Fri Nov 26 11:05:22.303186 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056] PermissionError: [Errno 13] Permission denied: '/imm_migration.log'

@GrahamDumpleton
Copy link
Owner

What do you get for:

ls -lasd /home/admin/imm-migration

@Tony-Frank-ML
Copy link
Author

ls -lasd /home/admin/imm-migration
4 drwxrwxr-x 25 www-data www-data 4096 Nov 26 21:23 /home/admin/imm-migration

@GrahamDumpleton
Copy link
Owner

If the log still shows:

[Fri Nov 26 11:05:22.303171 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]     return open(self.baseFilename, self.mode, encoding=self.encoding)
[Fri Nov 26 11:05:22.303186 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056] PermissionError: [Errno 13] Permission denied: '/imm_migration.log'

then you can't be setting the path in the correct location as it still shows /imm_migration.log.

Ensure you do a full stop/start of Apache to ensure your modified code is being picked up.

BTW, your configuration has:

        LoadModule wsgi_module "/usr/local/lib/python3.8/dist-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so"

inside of the VirtualHost. Apache should error on that and not even start. So I question whether that config is even being used.

@Tony-Frank-ML
Copy link
Author

But earlier i managed to bring up the UI of the application..Is there any way to check if the config is being used

@GrahamDumpleton
Copy link
Owner

What is the full path name to that Apache config file? If it is in a sites-available directory if using debian/ubuntu, ensure that the same named file in sites-enabled directory is actually a symlink to it and not a copy. Maybe you managed to create a copy to sites-enabled instead and so Apache is using separate copy.

As to checking if in use, add something which is an obvious error in the file, such as:

ThisDirectiveDoesNotExist

Apache should fail to start if that file is being read.

@Tony-Frank-ML
Copy link
Author

The apache failed to start once i added something wrong in configration...seems the config is being used

If the log still shows:

[Fri Nov 26 11:05:22.303171 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056]     return open(self.baseFilename, self.mode, encoding=self.encoding)
[Fri Nov 26 11:05:22.303186 2021] [wsgi:error] [pid 1453:tid 140081484965632] [remote 10.81.0.121:57056] PermissionError: [Errno 13] Permission denied: '/imm_migration.log'

then you can't be setting the path in the correct location as it still shows /imm_migration.log.

Ensure you do a full stop/start of Apache to ensure your modified code is being picked up.

BTW, your configuration has:

        LoadModule wsgi_module "/usr/local/lib/python3.8/dist-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so"

inside of the VirtualHost. Apache should error on that and not even start. So I question whether that config is even being used.

So i rebooted the machine and the permission error no longer exist. For some reason my modified code is didnt picked up

@Tony-Frank-ML
Copy link
Author

I added something which is error im my configration and the apache threw error. Seems like the config is even used.

@GrahamDumpleton
Copy link
Owner

Okay, that's odd, never noticed that before.

https://httpd.apache.org/docs/2.4/mod/mod_so.html#loadmodule

This says that LoadModule can be used inside of a VirtualHost definition. Not sure why they would want to allow that since it isn't going to be isolated to the VirtualHost.

Anyway, it is more conventional to have it outside of VirtualHost and on debian/ubuntu is usually loaded from a file in a mods-available directory.

Anyway, sounds like the path issue is fixed at least.

@Tony-Frank-ML
Copy link
Author

Yes Graham the path issue is fixed..appreciate your help so much.Thank you

@Praveen240
Copy link

PermissionError
PermissionError: [WinError 5] Access is denied

Traceback (most recent call last)
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\flask\app.py", line 2551, in call
return self.wsgi_app(environ, start_response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\flask\app.py", line 2531, in wsgi_app
response = self.handle_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\flask\app.py", line 2528, in wsgi_app
response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\flask\app.py", line 1825, in full_dispatch_request
rv = self.handle_user_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\flask\app.py", line 1823, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\flask\app.py", line 1799, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\USER\OneDrive\Desktop\EE project\EEpy.py", line 26, in home
text = tess.image_to_string(Image.open(filename))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pytesseract\pytesseract.py", line 423, in image_to_string
return {
^^^^^^^^^
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pytesseract\pytesseract.py", line 426, in
Output.STRING: lambda: run_and_get_output(*args),
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pytesseract\pytesseract.py", line 288, in run_and_get_output
run_tesseract(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pytesseract\pytesseract.py", line 255, in run_tesseract
proc = subprocess.Popen(cmd_args, **subprocess_args())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.752.0_x64__qbz5n2kfra8p0\Lib\subprocess.py", line 1024, in init
self._execute_child(args, executable, preexec_fn, close_fds,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.752.0_x64__qbz5n2kfra8p0\Lib\subprocess.py", line 1493, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
^^^^^^^^^^^^^^^^^^
File "c:\Users\USER.vscode\extensions\ms-python.python-2023.6.0\pythonFiles\lib\python\debugpy_vendored\pydevd_pydev_bundle\pydev_monkey.py", line 901, in new_CreateProcess
return getattr(_subprocess, original_name)(app_name, cmd_line, *args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [WinError 5] Access is denied
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants