GrahamDumpleton / mod_wsgi

Source code for Apache/mod_wsgi.
Apache License 2.0
1.03k stars 268 forks source link

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

Closed Tony-Frank-ML closed 2 years ago

Tony-Frank-ML commented 2 years ago

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 commented 2 years ago

See:

https://modwsgi.readthedocs.io/en/master/user-guides/application-issues.html#application-working-directory https://modwsgi.readthedocs.io/en/master/user-guides/application-issues.html#application-working-directory

The issue is that you are using a relative path name rather than an absolute path name for the log file. As a consequence it is going to be written to the current working directory of the process, which will be root directory of the file system, which only root can write to. You need to calculate an absolute path name for the file to the derived location you want to write it.

Graham

On 27 Nov 2021, at 12:39 am, Tony-dot-Frank @.***> wrote:

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 [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 ***@***.***
    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

python version 3.8

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/GrahamDumpleton/mod_wsgi/issues/737, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAD355KS36NFNCMR5URWHDTUN6EZTANCNFSM5I2WCDNQ. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Tony-Frank-ML commented 2 years ago

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 commented 2 years ago

What do you get for:

ls -lasd /home/admin/imm-migration
Tony-Frank-ML commented 2 years ago

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

GrahamDumpleton commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

GrahamDumpleton commented 2 years ago

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 commented 2 years ago

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

Praveen240 commented 1 year ago

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_x64qbz5n2kfra8p0\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_x64qbz5n2kfra8p0\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