centos 7 Apache wsgi writing files permission denied - django

In my site2 Django application Apache wsgi don't have writing access for files in /var/www/site2, even if:
I have chown-ed to apache all files from /var/www/site2 directory
I give all rights (chmod -R 777).
and SELinux is disabled.
Below is a briefing state:
[root#nuc www]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built: Nov 19 2015 21:43:13
[root#nuc www]# ls -l
total 4
drwxr-xr-x. 2 root root 6 Nov 19 23:43 cgi-bin
drwxr-xr-x. 2 root root 6 Apr 2 15:02 html
drwxrwxrwx. 6 apache apache 4096 Apr 3 16:14 site2
[root#nuc site2]# ls -l
...
drwxrwxrwx. 5 apache apache 4096 Apr 2 15:23 blog
-rwxrwxrwx. 1 apache apache 95232 Apr 3 16:09 db.sqlite3
drwxrwxrwx. 5 apache apache 4096 Apr 2 16:54 home
-rwxrwxrwx. 1 apache apache 248 Mar 19 13:56 manage.py
drwxrwxrwx. 5 apache apache 4096 Apr 3 16:13 pock
drwxrwxrwx. 2 apache apache 4096 Apr 3 14:45 site2
[root#nuc www]# sestatus
SELinux status: disabled
httpd.conf ...
#-------------------------------------------------
WSGIScriptAlias / /var/www/site2/site2/wsgi.py
WSGIPythonPath /var/www/site2/
Alias /static/ /var/www/site2/blog/static/
Alias /static2/ /var/www/site2/home/static/
Alias /static3/ /var/www/site2/pock/static/
<Directory /var/www/site2/blog/static/>
Require all granted
</Directory>
<Directory /var/www/site2/home/static/>
Require all granted
</Directory>
<Directory /var/www/site2/pock/static/>
Require all granted
</Directory>
<Directory /var/www/site2/site2/>
<Files wsgi.py>
#Order deny,allow
Allow from all
Require all granted
</Files>
</Directory>
#-------------------------------------------------
And still in /var/log/httpd/error_log:
[Sun Apr 03 13:43:17.389404 2016] [:error] [pid 4753] /var/www/site2
[Sun Apr 03 13:43:17.390560 2016] [:error] [pid 4753] Internal Server Error: /pock/test/
[Sun Apr 03 13:43:17.390593 2016] [:error] [pid 4753] Traceback (most recent call last):
[Sun Apr 03 13:43:17.390602 2016] [:error] [pid 4753] File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
[Sun Apr 03 13:43:17.390609 2016] [:error] [pid 4753] response = self.process_exception_by_middleware(e, request)
[Sun Apr 03 13:43:17.390634 2016] [:error] [pid 4753] File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
[Sun Apr 03 13:43:17.390643 2016] [:error] [pid 4753] response = wrapped_callback(request, *callback_args, **callback_kwargs)
[Sun Apr 03 13:43:17.390650 2016] [:error] [pid 4753] File "/var/www/site2/pock/views.py", line 57, in test
[Sun Apr 03 13:43:17.390656 2016] [:error] [pid 4753] f = open('test.txt', 'w')
[Sun Apr 03 13:43:17.390662 2016] [:error] [pid 4753] IOError: [Errno 13] Permission denied: 'test.txt'
SOLVED
Thanks to suggestions of Volodymyr I did more digging and I find the way finnaly.
Moving project in /home directory was not working for me: getting "Forbidden" from Apache.
The file I was trying to write need absolute path:
f = open('/var/www/site2/test.txt', 'w')
Otherwise wsgi python would try to write it in "/" root file system, so the error above is explained
In the end I did chown -R 744 /var/www/site2 and it worked, writing to files and database.
Seems that I have done other error but, anyway, it works now.

Try to :
chown -R root:apache /Path/To/You/WebSitePackage
I had this problem when deploy Django with httpd on Centos 7. I chose other way : Create WEBSITE Package in home/user/ directory, then
chown -R apache:apache Path/To/WEBSITEPackage
chmod -R 770 Path/To/WEBSITEPackage
After You can try :
chown -R user:root Path/To/WEBSITEPackage
chmod -R 770 Path/To/WEBSITEPackage
chown -R apache:root Path/To/WEBSITEPackage
chmod -R 770 Path/To/WEBSITEPackage
Other words apache on centos 7 need root privelegies

Related

Error: (Internal server error) Django apache2 - mod_wsgi

I'm trying to deploy my django application on my remote server, but I'm getting an http 500 internal server error.
Here are the configurations I made:
Structure file
/home/ubuntu/project/env (virtualenv folder)
bin
include
lib
/home/ubuntu/project/
|- manage.py
| - env
| - my_project
/home/ubuntu/project/my_project
|__init__.py
|settings.py
|urls.py
|wsgi.py
Fichier 000-default.conf
<VirtualHost *:80>
WSGIDaemonProcess myproject python-home=/home/ubuntu/project/env python-
path=/home/ubuntu/project/my_project
WSGIProcessGroup myproject
WSGIScriptAlias / /home/ubuntu/project/my_project/wsgi.py
<Directory /home/ubuntu/project/my_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
Error log
[Fri Dec 30 11:28:18.988904 2022] [wsgi:error] [pid 31289:tid 140473618372288] [remote
41.82.143.41:50428] File "/home/ubuntu/project/my_project/wsgi.py", line 12, in <module>
[Fri Dec 30 11:28:18.988915 2022] [wsgi:error] [pid 31289:tid 140473618372288] [remote
41.82.143.41:50428] from django.core.wsgi import get_wsgi_application
[Fri Dec 30 11:28:18.988933 2022] [wsgi:error] [pid 31289:tid 140473618372288] [remote
41.82.143.41:50428] ModuleNotFoundError: No module named 'django'
[Fri Dec 30 11:28:19.164861 2022] [wsgi:error] [pid 31289:tid 140473651943104] [remote
41.82.143.41:50427] mod_wsgi (pid=31289): Failed to exec Python script file
'/home/ubuntu/project/my_project/wsgi.py'.
[Fri Dec 30 11:28:19.164958 2022] [wsgi:error] [pid 31289:tid 140473651943104] [remote
41.82.143.41:50427] mod_wsgi (pid=31289): Exception occurred processing WSGI script
'/home/ubuntu/project/my_project/wsgi.py'.
[Fri Dec 30 11:28:19.165130 2022] [wsgi:error] [pid 31289:tid 140473651943104] [remote
41.82.143.41:50427] Traceback (most recent call last):
[Fri Dec 30 11:28:19.165172 2022] [wsgi:error] [pid 31289:tid 140473651943104] [remote
41.82.143.41:50427] File "/home/ubuntu/project/my_project/wsgi.py", line 12, in <module>
[Fri Dec 30 11:28:19.165183 2022] [wsgi:error] [pid 31289:tid 140473651943104] [remote
41.82.143.41:50427] from django.core.wsgi import get_wsgi_application
[Fri Dec 30 11:28:19.165212 2022] [wsgi:error] [pid 31289:tid 140473651943104] [remote
41.82.143.41:50427] ModuleNotFoundError: No module named 'django'
I then used this process : make custom versions of Python and mod_wsgi ourselves.
apt install apache2 apache2-dev
wget https://codeload.github.com/GrahamDumpleton/mod_wsgi/tar.gz/4.9.4
tar xvfz 4.9.4
cd mod_wsgi-4.9.4
./configure --with-python=[your python path]
## For example: ./configure --with-python=/usr/bin/python3.10
sudo make
sudo make install
## Finally:
sudo systemctl reload apache2
I use python 3.10 and apache2
and the error still persists (500 internal server error)
I do not know what to do ???
I found the answer by adding to my wsgi.py file the path where django is installed

wsgi with apache is not able to read piped installed python packages

i get this eror :
sudo service apache2 restart :
[Thu Apr 18 13:11:02.687353 2019] [mpm_event:notice] [pid 2135:tid 140232732096384] AH00491: caught SIGTERM, shutting down
[Thu Apr 18 13:11:03.694739 2019] [ssl:warn] [pid 2300:tid 139990910658432] AH01909: ip-*.*.*.*.ec2.internal:443:0 server certificate does NOT include an ID which matches the server name
[Thu Apr 18 13:11:03.702921 2019] [ssl:warn] [pid 2301:tid 139990910658432] AH01909: ip-*.*.*.*.ec2.internal:443:0 server certificate does NOT include an ID which matches the server name
[Thu Apr 18 13:11:03.703014 2019] [wsgi:warn] [pid 2301:tid 139990910658432] mod_wsgi: Compiled for Python/2.7.11.
[Thu Apr 18 13:11:03.703018 2019] [wsgi:warn] [pid 2301:tid 139990910658432] mod_wsgi: Runtime using Python/2.7.12.
then i entered the domain we site and got in /var/log/apache2/error.log:
[Thu Apr 18 13:11:03.703598 2019] [mpm_event:notice] [pid 2301:tid 139990910658432] AH00489: Apache/2.4.18 (Ubuntu) OpenSSL/1.0.2g mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
[Thu Apr 18 13:11:03.703617 2019] [core:notice] [pid 2301:tid 139990910658432] AH00094: Command line: '/usr/sbin/apache2'
[Thu Apr 18 13:11:10.014862 2019] [wsgi:error] [pid 2304:tid 139990600201984] err : No module named boto
[Thu Apr 18 13:11:10.015214 2019] [wsgi:error] [pid 2304:tid 139990600201984] [client 81.218.184.134:30333] mod_wsgi (pid=2304): Target WSGI script '/home/ubuntu/path/to/my_project/src/my_project/wsgi.py' does not contain WSGI application 'application'.
i am working with ubuntu 16.0.4 apache2 and nod_wsgi on AWS EC2, start to get up project that is working (not a new project)
printing the Exception in my wsgi.py i get :
No module named boto from
try:
application = get_wsgi_application()
except Exception as e:
print "err : {}".format(e)
but i have boto install :
# pip freeze | grep boto
You are using pip version 8.1.1, however version 19.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
boto==2.49.0
boto3==1.9.112
botocore==1.12.16
here are other files that i configured according to https://docs.djangoproject.com/en/2.2/topics/install/ :
# cat /etc/apache2/mods-enabled/wsgi.load
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
# cat /etc/apache2/mods-enabled/wsgi.conf
<IfModule mod_wsgi.c>
WSGIPassAuthorization on
WSGIScriptAlias / /home/ubuntu/path/to/wsgi.py
WSGIPythonPath /home/ubuntu/path/to/my_project
<VirtualHost *:80>
<Directory "/home/ubuntu/path/to//my_project/">
Require all granted
</Directory>
</VirtualHost>
..
</IfModule>
It looks like you could be missing the site-packages directory in your WSGIPythonPath directive. I usually define the python path as part of the WSGIDaemonProcess directive:
WSGIDaemonProcess example \
display-name=example \
processes=2 threads=20 \
maximum-requests=10000 \
umask=0002 \
python-path=${SRV}/www:${SRV}/src:${SRV}/venv/prod_142/lib/python2.7/site-packages \
python-eggs=${SRV}/.python-eggs
WSGIScriptAlias / ${SRV}/www/example/wsgi.py \
process-group=example \
application-group=%{GLOBAL}
You can see a more full-featured virtualhost definition in the readme at https://github.com/datakortet/dkbuild-apacheconf (I'm the author).
the issue was that not all the python packages was installed , after installed all missing packages the website worked

Forbidden You don't have permission to access / on this server. on Django, Apache, Centos7

I'm using Centos 7. I create a django project under /home/WEFORUM/admshapersdev/stargate/ and create a wsgi file in /home/WEFORUM/admshapersdev/stargate/stargate
The Django.conf
Alias /static /home/WEFORUM/admshapersdev/stargate/landingpage/static
<Directory /home/WEFORUM/admshapersdev/stargate/landingpage/static>
Require all granted
</Directory>
<Directory /home/WEFORUM/admshapersdev/stargate/stargate>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess stargate python-path=/home/WEFORUM/admshapersdev/stargate:/home/WEFORUM/admshapersdev/stargate/stargateenv/lib/python3.6/site-packages
WSGIProcessGroup stargate
WSGIScriptAlias / /home/WEFORUM/admshapersdev/stargate/stargate/wsgi.py
The structure and rights of the project folder:
../stargate
drwxr-xr-x. 6 admshapersdev apache 4096 Aug 17 22:47 landingpage/
-rwxr-xr-x. 1 admshapersdev apache 806 Aug 17 21:42 manage.py*
-rw-r--r--. 1 admshapersdev apache 464 Aug 17 22:42 requirements.txt
drwxr-xr-x. 4 admshapersdev apache 4096 Aug 18 01:08 stargate/
drwxr-xr-x. 5 admshapersdev apache 77 Aug 17 21:41 stargateenv/
drwxr-xr-x. 2 admshapersdev apache 6 Aug 17 21:52 static/
../stargate/stargate
-rw-r--r--. 1 admshapersdev apache 0 Aug 17 21:42 __init__.py
drwxr-xr-x. 2 admshapersdev apache 4096 Aug 18 01:09 __pycache__/
-rw-r--r--. 1 admshapersdev domain users 3555 Aug 18 01:08 settings.py
-rw-r--r--. 1 admshapersdev apache 3676 Aug 18 01:05 settings.py_1
drwxr-xr-x. 9 admshapersdev apache 91 Aug 17 22:49 static/
-rw-r--r--. 1 admshapersdev apache 259 Aug 17 22:38 urls.py
-rwxr--r--. 1 admshapersdev apache 394 Aug 18 00:49 wsgi.py*
But each time I try to reach my page, I am having this error in apache log:
[core:error] [pid 22477] (13)Permission denied: [client 192.168.140.179:63696] AH00035: access to /favicon.ico denied (filesystem path '/home/WEFORUM/admshapersdev/stargate') because search permissions are missing on a component of the path, referer: http://stargate0.weforum.local/admin
Does anyone know how to handle this? thank you for suggestions.
I move the project to /var/www but now I am having this:
[Sat Aug 18 07:43:49.987481 2018] [core:notice] [pid 20202] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Sat Aug 18 07:58:19.927287 2018] [core:error] [pid 20205] (13)Permission denied: [client 192.168.140.179:49188] AH00035: access to /admin/ denied (filesystem path '/var/www/stargate') because search permissions are missing on a component of the path, referer: http://stargate0.weforum.local/admin/login/?next=/admin/
[Sat Aug 18 07:58:24.840377 2018] [core:error] [pid 20205] (13)Permission denied: [client 192.168.140.179:49188] AH00035: access to / denied (filesystem path '/var/www/stargate') because search permissions are missing on a component of the path
[Sat Aug 18 07:58:29.481225 2018] [core:error] [pid 20205] (13)Permission denied: [client 192.168.140.179:49188] AH00035: access to /admin/ denied (filesystem path '/var/www/stargate') because search permissions are missing on a component of the path, referer: http://stargate0.weforum.local/admin/login/?next=/admin/

Apache 2.4 with mod_wsgi: 403 Forbidden, don't have permission to access /calbase on this server

So I am trying to deploy my django project on a windows server, using apache 2.4 with mod_wsgi and pythong 3.4. Before I configure httpd.conf and just try start apache with mod-wsgi installed, it works and show me that "it works" page. then I did the following configuration in httpd.conf:
# Change Python path used by the server.
WSGIPythonPath “/EquipmentCalibration”
# Make calls to http://localhost/ refer to the Python/WSGI-script located at the specified location.
WSGIScriptAlias / /EquipmentCalibration/equipcal/wsgi.py
# Make calls to http://localhost/static refer to the specified folder.
Alias /static/ /EquipmentCalibration/static
Alias /media/ /EquipmentCalibration/media
<Directory /EquipmentCalibration/static>
Require all granted
</Directory>
<Directory /EquipmentCalibration/media>
Require all granted
</Directory>
<Directory /EquipmentCalibration/equipcale>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
And then try to go to localhost:8080 (I changed the port from 80 to 8080), I got this error saying:
Forbidden
You don't have permission to access / on this server.
And below is the relevant error.log.
[Thu Sep 29 15:05:25.171920 2016] [mpm_winnt:notice] [pid 7756:tid 528] AH00456: Apache Lounge VC10 Server built: Jul 9 2016 11:59:00
[Thu Sep 29 15:05:25.171920 2016] [core:notice] [pid 7756:tid 528] AH00094: Command line: 'C:\\Apache24\\bin\\httpd.exe -d C:/Apache24'
[Thu Sep 29 15:05:25.171920 2016] [mpm_winnt:notice] [pid 7756:tid 528] AH00418: Parent: Created child process 7524
[Thu Sep 29 15:05:25.500078 2016] [wsgi:warn] [pid 7524:tid 456] mod_wsgi: Compiled for Python/3.4.2.
[Thu Sep 29 15:05:25.500078 2016] [wsgi:warn] [pid 7524:tid 456] mod_wsgi: Runtime using Python/3.4.3.
[Thu Sep 29 15:05:26.171978 2016] [mpm_winnt:notice] [pid 7524:tid 456] AH00354: Child: Starting 64 worker threads.
[Thu Sep 29 15:05:27.174429 2016] [mpm_winnt:notice] [pid 7636:tid 456] AH00364: Child: All worker threads have exited.
[Thu Sep 29 15:05:29.923754 2016] [authz_core:error] [pid 7524:tid 1108] [client ::1:55483] AH01630: client denied by server configuration: C:/EquipmentCalibration/equipcal/wsgi.py
Could somebody please help? TIA.
The section:
<Directory /EquipmentCalibration/equipcale>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
has a directory name which does match what is used in the WSGIScriptAlias directive. One uses equipcal and the other equipcale. They need to match in that segment name.

Apache2 + VirtualEnv IOError: [Errno 13] Permission denied

I am having trouble getting apache to serve my site via virtualenv (and virtualenvwrapper). I tried following the walkthrough here. Of note, I am using Ubuntu Server v13.10 which I've read doesn't have native support for Apache v2.2 yet, thus in the walkthrough I was not able to install apache2.2-common. However, I don't think that is the issue as the apache error logs are saying there is a permissions problem.
In my setup the virtual environment lives in a non-root user's home directory, while the apache server runs as root (I think this is normal). I have tried to trace the permissions using namei and I've made sure to chmod +rw to the path as root. Any ideas?
[Thu Mar 06 14:16:37.639031 2014] [mpm_event:notice] [pid 8771:tid 140338386122624] AH00489: Apache/2.4.6 (Ubuntu) mod_wsgi/3.4 Python/2.7.5+ configured -- resuming normal operations
[Thu Mar 06 14:16:37.639144 2014] [core:notice] [pid 8771:tid 140338386122624] AH00094: Command line: '/usr/sbin/apache2'
[Thu Mar 06 14:16:53.456622 2014] [:error] [pid 8775:tid 140338291197696] [client 192.168.XXX.XXX:50742] mod_wsgi (pid=8775): Target WSGI script '/var/www/www.mysite.org/index.wsgi' cannot be loaded as Python module.
[Thu Mar 06 14:16:53.456735 2014] [:error] [pid 8775:tid 140338291197696] [client 192.168.XXX.XXX:50742] mod_wsgi (pid=8775): Exception occurred processing WSGI script '/var/www/www.mysite.org/index.wsgi'.
[Thu Mar 06 14:16:53.456782 2014] [:error] [pid 8775:tid 140338291197696] [client 192.168.XXX.XXX:50742] Traceback (most recent call last):
[Thu Mar 06 14:16:53.456830 2014] [:error] [pid 8775:tid 140338291197696] [client 192.168.XXX.XXX:50742] File "/var/www/www.mysite.org/index.wsgi", line 16, in <module>
[Thu Mar 06 14:16:53.456918 2014] [:error] [pid 8775:tid 140338291197696] [client 192.168.XXX.XXX:50742] execfile(activate_env, dict(__file__=activate_env))
[Thu Mar 06 14:16:53.456971 2014] [:error] [pid 8775:tid 140338291197696] [client 192.168.XXX.XXX:50742] IOError: [Errno 13] Permission denied: '/home/nrUser/Envs/mysite/bin/activate_this.py'
(mysite)root:~# namei -m /home/nrUser/Envs/mysite/bin/activate_this.py
f: /home/nrUser/Envs/mysite/bin/activate_this.py
drwxr-xr-x /
drwxr-xr-x home
drw-r--r-- nrUser
drwxrwxr-x Envs
drwxrwxr-x mysite
drwxrwxr-x bin
-rwxrwxr-x activate_this.py
To be able to access the files, the apache owner needs at least the x bits on all directories above your files, and the r bit to the files themselves. Your nrUser directory is missing the x bits; chmod 755 /home/nrUser should fix your problem.
If I remember correctly, Apache user/group needs permissions to read the directories. Try applying chown to your mysite directory, for example:
sudo chown -R <user>:www-data mysite <-- while in nrUser home directory
Restart apache using sudo service apache2 restart and see if you can access your website.