File found by Django server but not by Apache/mod_wsgi - linux

I've got a Django app which when run with "manage.py runserver 0.0.0.0:80" works fine, but when run by an Apache2 server tells me "No Such file or Directory"
I reckon it's something to do with telling Apache which path to look in, but I've done the obvious:
WSGIScriptAlias / /home/mywebsite/myproject/wsgi.py
WSGIPythonPath /home/mywebsite
<Directory /home/mywebsite/myapp>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
<Directory /home/mywebsite> //I thought this might give it access to the root folder
Order deny,allow
Require all granted
</Directory>
Note my file in the root of the project, next to manage.py. The app is able to display its front page, but the ajax fired by one of the buttons fails due to the above mentioned file error.
I must be missing something about how to specify the file path.
The code to access the file is simply this:
with open('demo.txt') as fp:
etc...
To reiterate, it works fine when I just run the Django server. Apache runs the main page just fine, it only falls over when looking for this file.
What do I need to change?

One way is to set up project path in the settings file: (If it's in the project root)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
from django.conf import settings
with open(os.path.join(settings.BASE_DIR, 'demo.txt')) as fp:
...
It will be consistent across all installations of your project and you can debug BASE_DIR locally.

Try using the full path of the file, or use the os module to get the relative path.

Related

Change the location of the public(www) Apache folder on debian 8

I am facing a peculiar problem with apache2 running on debian 8. I followed a couple of tutorials to install it and everything worked great. The problem is that i have partitioned the disk as 9gb for the system files, 1gb swap and 30gb for the home folder so I wanted to move the www folder from it's current location (/var/www) to home(/home/www).
I found more than a few guides on how to do that, some saying that i should change the lines in apache2.conf from this:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
to this:
<Directory /home/paul/www/>
Options Indexes FollowSymLinks
AllowOverride All
Order deny,allow
Allow from all
</Directory>
As well as the documentRoot
Others suggested using the 000-default.conf file located in the /etc/apache2/sites-available folder and change the default folder from /var/www to /home/paul/www.
I did both and the folder was seen but not accessible, so i used the quick solution and use chroot 777. After that apache redirected me back to /var/www.
All settings point to /home/paul/www and the www folder has full read write permission. In fact neither the apache.conf nor 000-sites-available has any reference of /var/www so why does apache keep pointing me there? Is there something i missed?
p.s i did updates and upgrades multiple times as well as restarted the apache service and the entire pc.
you need to change the ownership of files to allow apache there
please try
chown -R www-data /home/paul/www/
however if you set a particion for your paul www why not mount the partition in /var/www
or you can create a symlink
ln -s /home/paul/www /var/www/paulsite
then edit your apache conf accordingly.
Regarding the edited file I recomend you use the 000-default better. in fact i would use that as a skeletone. and create a new file for your site then enable that site.
Ok so with a little research i realized that what i was trieing to do was not the best practice at all! (Thanx for the heads up Sudakatux).
I found the solution in askubuntu for anyone interested.
Instead of trieing to change the mechanics of apache i enabled the userdir module and set up public_html in my home folder with 755 permissions. I use a redirection script from the initial location to the various locations on my home directory. Works just fine!

.htaccess not working on xampp Ubuntu 14.04

I have difficulties making my .htacces work on my Ubuntu 14.04 LTS. I know it is a hidden file and all; I've searched everywhere and people seem to have the same problem. I've made a virtual host on my machine and all. Can someone please help me ?
ErrorDocument 404 /pages/error.php?code=404
ErrorDocument 403 /pages/error.php?code=403
You need to make sure the following things are true:
You need to check what DocumentRoot is set to. When an url beginning with a slash is detected, it will try to load that file relative to the document root. (docs) The file <documentroot>/pages/error.php must thus exist.
You need to make sure that .htaccess files are allowed by the main Apache configuration. The easiest way to test this is by entering garbage into your .htaccess file, saving it and reloading a page. You'll get an internal server error if Apache reads the .htaccess file. Otherwise the page loads as expected. To enable .htaccess files, the AllowOverride directive must allow something (see next bullet point). Additionally, check in httpd.conf if AccessFileName is set to something different than .htaccess. Change it as necessary, then RESTART APACHE. (docs)
For ErrorDocument, AllowOverride must be at least set to FileInfo. See the docs. Go to your main config file (httpd.conf), probably apache/Apachex.y.z/conf/httpd.conf. Search for the <Directory ...> block that corresponds to your http root, and look around if it contains an AllowOverride directive. Add FileInfo as one of the arguments, save the file and RESTART APACHE.

Disabled PHP for Single Directory with HTACCESS (PHP-FPM)

I want to disable the parsing of PHP files in a specific directory, and found out how to do that here. The problem is that my server uses PHP-FPM, and I get Invalid command 'php_flag', perhaps misspelled or defined by a module not included in the server configuration when I try to use php_flag in my httpd.conf file.
How can I disable parsing of PHP files in a given web-accessible directory with an htaccess/httpd.conf file that is located above web root on a server using PHP-FPM?
Since Fedora 27 switched to php-fpm recently, I, too, ran into this problem. Unfortunately, the old ways of doing things with mod_php do not apply to php-fpm.
I did find another question here that definitely seemed more relevant:
Apache: Disable php in a directory
The gist of it was was to use a <directory> and <filesmatch> block in your config file, and use SetHandler ! for every directory you didn't want PHP code interpreted.
e.g.:
<Directory "/path/to/no/phpfiles">
<Files "*.*">
SetHandler !
</Files>
</Directory>
This is tested and working on Fedora 27, PHP-FPM 7.1.12.
Unlike using the fpm configs directly, this technique works recursively, so placing it at the top level of a tree of stuff you don't want PHP interpreting works as expected.
I disable .htaccess files in my configurations, but this technique should still work. However, <directory> is not valid inside a .htaccess file. Hopefully just removing it, and leaving:
<Files "*.*">
SetHandler !
</Files>
Should be sufficient.
How can I disable parsing of PHP files in a given web-accessible
directory with an htaccess/httpd.conf file that is located above web
root on a server using PHP-FPM?
You can't persay the way you're trying it without mod_php (which you don't want) which is why you're getting that error. PHP-FPM is not an Apache module. It runs independent of Apache. That's actually it's purpose to be used on heavily loaded sites and it can control all the PHP processes.
One way you might be able to achieve this is to specify the exact path you want to run PHP, in your virtualhost file with the Directory directive. Instead of just having the PHP handler stuff, enclose it with the Directory directive with the actual path you want it to run. Here is an example.
<VirtualHost *:80>
ServerAdmin webmaster#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html/
ErrorLog /var/www/example.com/error.log
CustomLog /var/www/example.com/access.log combined
<Directory /path/to/php/only/folder/>
#then you PHP handler stuff
<IfModule mod_fastcgi.c>
AddType application/x-httpd-fastphp5 .php
Action application/x-httpd-fastphp5 /php5-fcgi
Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi_example.com
FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi_example.com -socket /var/run/php5-fpm_example.com.sock -pass-header Authorization
</IfModule>
</Directory>
</VirtualHost>
Then restart Apache. This should limit it to that directory. You might have to remove the php handler info from the other config so it's not overwritten.
Note I have not tested this solution.

Laravel (on Linux, EC2) routes won't work

I have Laravel finally configured (for the most part) on my linux EC2 instance, but right now I can't get to any routes past my home page. All I get back is that *.net/people is not on the server. Anybody have any tips?
If you are using apache, check if your .htaccess is working. For example add 'deny from all' line at the beginning of file. If not - check your httpd.conf. Find <Directory /> block, if there is AllowOverride None line, change it to AllowOverride All and restart your apache.

Can anyone tell me why Apache will not work, for my Django?

I get this problem when I visit my domain.com. I have compared this with my other website with the SAME set-up (just different username!!!) For some reason that website works and this one doesn't.
Forbidden
You don't have permission to access / on this server.
Apache/2.2.3 (Red Hat) Server at www.mydomain.com Port 80
In terms of permission issues, I have added user "apache" to the group "dver" and "svn" that own the folders that everything is located on.
$ ls -l
total 4
drwxr-xr-x 4 dver svn 4096 Oct 13 19:49 tv
This is my django.wsgi:
import os
import sys
sys.path.append('/home/dver/tv')
os.environ['PYTHON_EGG_CACHE'] = '/home/dver'
os.environ['DJANGO_SETTINGS_MODULE'] = 'mtv.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
This is in my httpd.conf:
WSGIScriptAlias / /home/dver/tv/mtv/wsgi/django.wsgi
DocumentRoot "/home/dver/tv/mtv/"
Alias /media/ /home/dver/tv/mtv/media/
<Directory /home/dver/tv/mtv/media>
Order deny,allow
Allow from all
</Directory>
<Directory /home/dver/tv/mtv>
Order deny,allow
Allow from all
</Directory>
If anyone can help me I'll really appreciate it.
Thanks!
As the code will run as the Apache user, all directories from the root of the file system down to '/home/dver/tv/mtv/wsgi' must be readable/executable to others, or at least somehow the Apache user, not just your Django site directory.
Thus, if '/home/dver' has permissions 'rwxr-x---' then it will not work. This is the most common problem where people go and stick the WSGI script file under their own home account. Manipulating groups may work, but you don't indicate if that group ownership also applies to '/home/dver'.
Although it will not matter as refers to a parent directory, but more secure to have:
<Directory /home/dver/tv/mtv/wsgi>
Order deny,allow
Allow from all
</Directory>
That is, use '/home/dver/tv/mtv/wsgi' rather than '/home/dver/tv/mtv'.
One other possibility is that you have SELinux extensions enabled for operating systems. You should configure SELinux correctly if it is, or possibly disable it just to see if it then works.
There are three potential problems that I notice.
The first depends on the OS you're running — are you sure the user you should add to the groups is called apache and not www-data?
The second is that you have your wsgi script under the directory wsgi but you have no httpd.conf Directory definition for it. I would add that and try again.
The third is that your /home/dver egg directory might have the wrong permissions.

Resources