django directory apache2 permissions - django

Hey I just got apache2 working with mod_wsgi on my django_project directory, which is pretty kool. However I can only make it work if I set the permissions on my django_project to
chmod -R 777 django_project not so nice I believe.
Can anyone tell me what chmod -R xxx django_project I should be running. Im not too tight on the whole linux-group-permissions. eg how do I tell what perm-group apache is in?
Thanks,
N

I believe only static directory needs to be writable by Apache in your case and not entire project directory.
You don't need 777 definitely, all you need is apache user (unless you have other user configured in WSGIDaemonProcess directive ) to own that directory.
Running ps -ef | grep httpd will show the user apache is running as. (Some OS may use apache instead of httpd but you get the idea).

Also you can see ls -l /var/www/ and it'll show you user-onwer and group-owner for the directory.
You should make permissions for django_project similar to /var/www/.
And writable permission is necessary for a socket if it's used (in fasctcgi-scheme)

Related

Nginx permissions

I deployed the server with Ubuntu 18, Django, Gunicorn, Nginx
And I ran into this problem:
everything works great but,
When I upload large pictures files in Django, Nginx gives 403 Error Forbidden.
I updated the permissions to the folder with static files on 755. It works!
But when I upload other files, the rights do not work.
I added the user root and user www-data to the folder owner’s group, but nothing has changed.
I understand that Nginx has no permissions, but how can I implement the inheritance permissions of new files from the parent folder
or will you suggest another solution?
You need to add FILE_UPLOAD_PERMISSIONS=0o644 variable to you settings.py file.
This is the numeric mode (i.e. 0o644) to newly uploaded files to.
For more information, please read this doc.
Try use this
chown -R www-data:www-data 'your project folder'

Apache2 WSGI Django - Directory access

I have an Django(10.5) WSGI application running on Ubuntu(16.04). The application works completely except for one piece of functionality.
The functionality is located in the views.py file and it tries to get a list of file names in a directory but is unable too.
apache2 is running under the user www-data and the directory is owned by another user. I have tried to chown and chmod the directory so that it is owned by www-data but this didn't make a difference.
I have also added the following to the sites-available file:
<Directory /home/other_user/backup>
Require all granted
</Directory>
NOTE: The functionality works if I run it in Django's dev server:
python3 ./manage.py runserver 0:80
I am wondering where you are trying to get the list of file names. Because Django project recognize that the directory where the manage.py located is the root directory. So The other directories outside of this root directory can't be recognized.(Due to BASE_DIR option in settings). It's nothing related with apache.
If you are trying to get the list of files in the directory which is inside root directory, the please check the permission.

Laravel vs. Django Running a Project

I have a Laravel project. I would like to know how can I run it and view the pages on my browser? Like for example, if I had a Django project, I would simply cd to it and type "python manage.py runserver" in the terminal to run the project. How can I do this with a Laravel Project?
Use the artisan serve command:
php artisan serve
I like the bare php command more, it provides some nice colors, green for OK, red for errors.
php -S localhost:8000 -t public/
Or even:
echo "php -S localhost:8000 -t public/" > start_server.sh
sh start_server.sh
Depends on your operating system (OSX, Windows, Linux) but I find that the software Laragon https://laragon.org is very good in providing everything you need, including apache, mysql, editing the host file, and sending (test) emails.

Can we read file from mount drives in django apache

I am writing one function to get file size in django application.
Django application we are running through apache server.
File is located in some other location. We are getting that file through mounted path.
Code is as follows,
import os
filePath = '/mnt/file.html'
size = flaot(os.stat(filePath))
If I run django server through runserver command then it is working fine.
If I run djnago server through apache then it will not work.
If the file is in the same location then it will work through both way(with apache and without apache)Ex. filePath = '/home' OR '/root' OR any location from same machine
What will the another way to get the file size from mounted path through django-apache
Please Help.
Thanks In Advance.
This has to do with directory/file permissions and/or privileges of the user that the process uses.
When you run the Django server, it runs under your username, and most probably your user has access to the places that hold your files; but when it runs behind Apache, it runs as Apache or www-data users, which doesn't have access to those places.
Solution: Either you change the directory permissions, add the Apache users to the group of users allowed to access this directory(s), or make the Apache user the owner of this directory(s), choose whichever that suits you!
Apache probably is running under a user that lacks permission to access /mnt.
For example, on Ubuntu apache runs under www-data. In order to confirm this, try to give read/execute permission on the /mnt folder to the user running apache or execute runserver under the same user running apache and see if you get the same error.
If you don't know which user is running apache, try:
ps aux | grep apache
If the apache server executable is not named apache or apache2 (for example, in some linux distros it is named httpd) then replace apache with the executable name in the above command.
Lets say the user running apache is www-data:
su - www-data -c "cat /mnt/file.html"
If the issue is related to permissions, you should see an error message telling you what is the problem.

Getting Django in a VirtualEnv to run through Upstart

I've been trying to trudge through the docs and examples to get my Django running through upstart so I can have it running all the time but am unable to so.
Here's my upstart configuration file located at /etc/init/myapp.conf:
start on startup
#expect daemon
#respawn
console output
script
chdir /app/env/bin
exec source activate
exec /app/env/bin/python /app/src/manage.py runserver 0.0.0.0:8000 > /dev/null 2>&1 &
end script
When I type sudo service myapp start, the console says that it has started but it doesn't seem to be running.
Is it possible to see some debugging output to see what's going wrong?
I need to run my Django application as another user — i.e. djangouser. How can I do so?
(I've been commenting out some lines to test where the service is going wrong). This is not for production use but my internal development use only.
Thanks.
Edit #1:
I have wrapped both my commands into a simple script at /app/run.sh
#!/bin/bash
cd /app/env/bin
source activate
cd /app/src
python manage.py runserver 0.0.0.0:8000 > /dev/null 2>&1 &
..and I've modified my /etc/init/myapp.conf to
start on startup
expect daemon
exec su - djangouser -c "bash /app/run.sh"
When executing sudo service myapp start — the application starts but the PID is wrong and I can't seem to kill it with sudo service myapp stop
Any ideas?
Change:
exec source activate
By just:
source activate
This will load the virtual environment. You should probably drop the other "exec". If that doesn't work, please post your upstart logs.
A couple of remarks:
logging the output to somewhere else than /dev/null might be useful :)
runserver is not ment to be stable, I see it crashing sometimes and in that case i guess you'll need to force upstart to reload, or put the runserver call in a while loop
you will not be able to use an interactive debugger like ipdb with this setup
How about using nginx and uwsgi with your virtualenv. this will give you a production like environment but will also start your django app at start up. if you are using ubuntu 10 you should take a look at uwsgi-python, otherwise just install the latest uwsgi. i usually start my virtualenv in uwsgi like so : sudo nano /etc/uwsgi-python/apps-available/app.xml
<uwsgi>
<socket>127.0.0.1:8889</socket>
<pythonpath>/home/user/code/</pythonpath>
<virtualenv>/home/user/code</virtualenv>
<pythonpath>/home/user/code/app</pythonpath>
<app mountpoint="/">
<script>uwsgiApp</script>
</app>
</uwsgi>
also setup yournginx files at /etc/nginx/apps-available/default (the file is a bit straight forward). this will help you have your django app at all times,
su is problematic becouse it forks the process. You can use sudo -u djangouser instead or simply add
setuid djangouser
in your conf file.
This should work on Ubuntu 14.04 and possibly other versions as well:
root#vagrant-ubuntu-trusty-64:/etc/init# service my_app start
my_app start/running, process 7799
root#vagrant-ubuntu-trusty-64:/etc/init# cat /var/log/upstart/my_app.log
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
June 30, 2015 - 06:54:18
Django version 1.8.2, using settings 'my_test.settings'
Starting development server at http://0.0.0.0:8080/
Quit the server with CONTROL-C.
root#vagrant-ubuntu-trusty-64:/etc/init# service my_app status
my_app start/running, process 7799
root#vagrant-ubuntu-trusty-64:/etc/init# service my_app stop
my_app stop/waiting
root#vagrant-ubuntu-trusty-64:/etc/init# service my_app status
my_app stop/waiting
Here is the config to make it work:
root#vagrant-ubuntu-trusty-64:/etc/init# cat my_app.conf
description "my_app upstart script"
start on runlevel [23]
respawn
script
su vagrant -c "source /home/vagrant/dj_app/bin/activate; /home/vagrant/dj_app/bin/python /home/vagrant/my_test/manage.py runserver 0.0.0.0:8080"
end script