Reputation: 13
I have a django web application which I need to serve from an apache server on CentOS 6.8. In order to get this, I´m using mod_wsgi.
I have my project located in /path/to/myproject.
Right there I have the following directories and files:
- app1/
- db.sqlite3
- myproject/
- myprojectenv/
- manage.py
- static/
I have added to my setting.py this directive: STATIC_ROOT = os.path.join(BASE_DIR, "static/") and I´ve tried to run the django server and I can see my app view correctly.
So, after that I´ve tried to configure apache server in order to send a petition from my browser to my app, but apache rises a 403 Forbidden error.
The steps that I followed are:
- Add to httpd.conf the following information:
<VirtualHost *:80>
Alias /static /path/to/myproject/static
Alias /app1 /path/to/myproject/app1
<Directory /path/to/myproject/static>
Order allow,deny
Allow from all
</Directory>
<Directory /path/to/myproject/app1>
Order allow,deny
Allow from all
</Directory>
<Directory /path/to/myproject/myproject>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
WSGIDaemonProcess myproject user=myuser group=mygroup python-path=/path/to/myproject:/path/to/myproject/myprojectenv/lib/python2.7/site-packages
WSGIProcessGroup myproject
WSGIScriptAlias / /path/to/myproject/myproject/wsgi.py
</VirtualHost>
Then, I gave rights to apache user, in order to allow apache to have access to my django project:
- sudo usermod -a -G myuser apache
- chmod 710 /home/myuser
- chmod 664 ~/myproject/db.sqlite3
- sudo chown :apache ~/myproject/db.sqlite3
- sudo chown :apache ~/myproject
Finally, I restarted the htttpd service and tried to send a petition to my app, but I got a "403 Forbidden error".
After that, I tried to copy my django project to the apache directory /var/www/ and doing this, I finnaly could see my django files but just like files, apache couldn´t run my app and show my view correctly.
I think that the problem could be about the permissions of apache user but I don´t know what else I can do.
What am I doing wrong?
Upvotes: 1
Views: 235
Reputation: 58563
What is the actual error in the Apache error log, not the 403 error in the browser?
Most likely the problem is due to your home directory being 710
. This means an other user such as the Apache user cannot see into your home directory. It needs to be able to do that as the Apache user to map the URL to the WSGI application.
Either move your whole project outside of your home directory, or create a separate WSGI script file called myproject.wsgi
under /var/www/myproject
or similar, which has in it:
from myproject.wsgi import application
Also change:
WSGIScriptAlias / /path/to/myproject/myproject/wsgi.py
<Directory /path/to/myproject/myproject>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
to:
WSGIScriptAlias / /var/www/myproject/myproject.wsgi
<Directory /var/www/myproject>
<Files myproject.wsgi>
Order allow,deny
Allow from all
</Files>
</Directory>
The Apache user only needs to be able to see the WSGI script file when doing URL mapping. The application code will actually run in your case under daemon mode with user myuser
which presumably can read stuff from your home directory.
You didn't need to change ownership of your directory/files to Apache user, again because the code will run as myuser
.
Upvotes: 1