Apache hangs with Django Application and Matplotlib - mysql

Very stuck here and I don't have a whole lot to go on. I had a django application up and running and I started playing around with graphing using NetworkX and Matplotlib to visualize some of the data for a poster. I then tried to use the site again and got the error:
'/var/www' is not a writable dir; you must set /var/www/.matplotlib to be a writable dir. You can also set environment variable MPLCONFIGDIR to any writable directory where you want matplotlib data stored
Which I resolved by chmod'ding the directory. The site's homepage then loads fine but when I try to load any other page that django renders apache just hangs. I set the log level to debug and tail'ed it but it's not showing any new requests or errors or anything, it just sorta hangs until the browser gives up. I then thought it must just be an error with something that I just did so I reverted back to a working version in my repo and am having the same problem. Django's test server is still running the site fine which is leading me to believe that it's a problem with apache (of the whole thing apache is the part I have the least experience with).
my httpd.conf looks like:
ServerName >> my server <<
TraceEnable off
AcceptFilter http none
AcceptFilter https none
EnableMMAP off
EnableSendfile off
<VirtualHost *:80>
ServerAdmin >> my email <<
ServerName >> my server <<
DocumentRoot /Web/public/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
WSGIScriptAlias / /home/$USER/$APP/$APP/wsgi.py
<Directory /Web/public/static>
Options -Indexes
</Directory>
Alias /static /Web/public/static
Alias /media /Web/public/media
ErrorLog /var/log/apache2/error.log
LogLevel debug
CustomLog /var/log/apache2/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
Please let me know if you have any thoughts, or need any more info.
Thanks!

I had the same problem; Apache/Django would not work but manage.py runserver would work fine. I tried making the default directory readable, and I tried changing MPLCONFIGDIR with export MPLCONFIGDIR=/stuff/, but the only thing that worked was placing
import os
os.environ[ 'MPLCONFIGDIR' ] = '/tmp/'
BEFORE importing matplotlib. Only one page uses MPL in my case, so placing this 'everywhere' was acceptable to me.
Taken from original solution by steko.

Related

XAMPP: Accessing html pages in subdirectories of root

This is most likely a beginner's issue, but I can't seem to find the fix anywhere, and the few posts I found dealing with it are unanswered (e.g. xampp in window 7 cannot access files in subfolder inside C:/xampp/htdocs).
So far I have a working localhost using XAMPP (had to change the port to 8080), located in a custom document root. I can load the index.html, but when I click on a link towards a subdirectoy:
<li></li>
I get the following error:
Service unavailable!
The server is temporarily unable to service your request due to
maintenance downtime or capacity problems. Please try again later.
If you think this is a server error, please contact the webmaster.
Error 503
localhost Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/5.6.30
Even if I type in the address (http://localhost:8080/examples/test.html) directly in the browser, it also doesn't work.
Could someone please indicate if this should be working? Or if I should specify something in the apache config file?
I could also note that when simply viewing the html files in my browser (outside of the localhost), the pages work fine and load regardless of their position in the directories. Thanks for any help!
Edit:
Here is my modified conf file DocumentRoot section:
#DocumentRoot "F:/Apps/xampp/htdocs"
DocumentRoot "F:/me/GitWorkDir/myproject_io"
<Directory "F:/me/GitWorkDir/myproject_io">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
It turns out the issue was due to an unlucky coincidence preventing a subdirectory to be called, precisely, "examples"...
See this page which discusses the issue and proposes a fix.
In short, you can:
Either go to the file C:\xampp\apache\conf\extra\httpd-ajp.conf
and add a "#" to comment out the conflicting line:
ProxyPass /examples ajp://127.0.0.1:8009/examples smax=0 ttl=60 retry=5
Or simply rename the "examples" directory differently (e.g. examples2)
Mostly if you install XAMPP on windows it runs without any problem. Only thing which give issue on windows is permission. which you could resolve by right click on htdocs folder and go to security tab and give all rights to everyone.

What causes Apache to invoke an executable on the server?

I have an executable on the server that is suppose to be invoked by the browser but the browser downloads it instead as a binary file to the "Downloads" directory .
The index.html has:
<head>
<title></title>
<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=admin/launch?script=rhtemplate=login">
</head>
How does "launch" get started ?
ScriptAlias /admin/ "/opt/tms/lib/web/cgi-bin/"
<Directory "/opt/tms/lib/web/cgi-bin">
Options +ExecCGI
AllowOverride None
Options None
Order Allow,Deny
Allow from all
</Directory>
This use to work in httpd 2.0 ; we updated to 2.2
There are a couple of things that need to be set.
http://httpd.apache.org/docs/2.4/howto/cgi.html
you could put your executable in a ScriptAliased directory. default apache configs normally have /cgi-bin/ as a ScriptAlias. So if you move your script to there it should Just work.
ScriptAlias "/scripts/" "/usr/local/apache2/scripts/"
can be used to set up different directory, or to create one if your default doesn't include it.
Or if you want to allow execution outside of ScriptAlias directories, you can set Options +ExecCGI in the directory and then tell Apache what should be treated as a script.
Enable cgi execution in that directory.
<Directory "/usr/local/apache2/htdocs/admin">
Options +ExecCGI
</Directory>
Then you would need to tell Apache what extensions should be treated as cgi using a handler
AddHandler cgi-script .cgi .pl
so you would need to rename your script to something like launch.cgi
-- from new info --
<Directory "/opt/tms/lib/web/cgi-bin">
Options +ExecCGI
AllowOverride None
Options None
Order Allow,Deny
Allow from all
</Directory>
you have Options none and Options +ExecCGI in that stanza, remove the Options none, and see if that makes a difference..

Apache2 custom 404 Page won't load (.htaccess)

i want to create a custom errorpage for my small webserver which runs on apache2 on raspbian. But sadly the created page isn't loaded by a browser in case of an URL which can't be resolved ... the browser just prints the path to the created errorpage on the upper left-hand corner.
html of the custom page so far:
<!DOCTYPE html><html><head></head><body><p>Nooooot found ...</p></body></html>
html of the loaded page:
<html><head></head><body>sites/errorsites/404.html</bod></html>
My .htaccess-file, which is placed in /var/www/
Options -Indexes
ErrorDocument 404 sites/errorsites/404.html
Location of my errorsites-dir with custom errorsites: /var/www/sites/
(Of course I set "AllowOverride" in the default-file in /etc/apache2/sites-available to "all".)
Thanks for the help
Lukas
Edit:
default-file of apache:
ServerAdmin webmaster#localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
From the ErrorDocument docs:
URLs can begin with a slash (/) for local web-paths (relative to the
DocumentRoot), or be a full URL which the client can resolve.
Alternatively, a message can be provided to be displayed by the
browser.
sites/errorsites/404.html does not begin w/ a slash and so is treated as a message to be displayed. Changing it to /sites/errorsites/404.html should fix, if sites is relative to your DocumentRoot…

Treating a subdirectory as a root directory for Apache

I cannot find a way to do what I want. I want to upload a project to my server at http://example.com/MyNewProject
In each of the web pages I include a file which does all of the imports for stylesheets, JavaScript etc. I don't want to provide the full path every time I just want to be able to do /MyStylesheet.css
My main websites document root is /var/www/html/example and my new projects directory is stored within /var/www/html/example/MyNewProject.
What I want is in my import when I do /MyStylesheet.css instead of going to my servers main web directory it will go to /var/www/html/example/MyNewProject in order to get the CSS.
I've tried adding the following to my Apache config file:
Alias /NewProjectTemplate "/var/www/html/example/NewProjectTemplate"
Alias /NewPRojectTemplate/ "/var/www/html/example/NewProjectTemplate"
<Directory "/var/www/html/example/NewProjectTemplate">
Options Indexes FollowSymLinks Includes
AllowOverride All
XBitHack On
Order allow,deny
Allow from all
</Directory>
This unfortunately hasn't worked so I've also tried adding the following to my virtualhosts file
<VirtualHost *:80>
DocumentRoot /var/www/html/example/NewProjectTemplate
ServerName example.com/NewProjectTemplate
</VirtualHost>
I've looked around on Google but cannot find anything specific, the only thing I've found is something to do with using the Rewrite engine but this seems a little too complicated and OTT for my needs.
Update
I've got a little further, although not a lot, I'm sure its not supposed to be this complicated.
Instead of using the virtual hosts, I'm now using an alias and below is what I've added to my httpd.conf file
DocumentRoot "/var/www/html"
Alias /NewTemp/ "/var/www/html/example/NewProjectTemplate/"
Alias /NewTemp "/var/www/html/example/NewProjectTemplate"
<Directory "/var/www/html/example/NewProjectTemplate/">
Options FollowSymLinks Includes
AllowOverride all
</Directory>
<Directory "/var/www/html/example/NewProjectTemplate">
Options FollowSymLinks Includes
AllowOverride all
</Directory>
In my HTML code in my SSI I am doing the following:
<!--#include file="includes/imports.html"-->
As you can see this is a relative path and Includes is inside the root of NewProjectTemplate. However, this is a template file so I want it to always to go to the root to find the file so it is guaranteed to work no matter how deep into the site it. For example if I change the line to the below I then get an error error processing directive
<!--#include file="/includes/imports.html"-->
So even though it works without the / but doesn't with the slash, if I don't include the slash so this import then works, the imports within the imports.html file work even though they do contain the leading slash so it always go to the root. Below is my HTML imports file
<link rel="stylesheet" type="text/css" href="/StyleSheet.css">
<link rel="stylesheet" type="text/css" href="/navigation/top-nav.css">
<link rel="stylesheet" type="text/css" href="/navigation/side-nav.css">
<script type="text/javascript" src="/scripts/jquery.js"></script>
<script type="text/javascript" src="/scripts/menu.js"></script>
So just to clarify, if the #include file = includes/imports.html the SSI directive works and /StyleSheet.css is successfully imported even though it has the leading slash. If I add a / to the path of the SSI it then doesn't get imported.
But here is where it gets weirder.
If I then add another file to a subdirectory e.g. NewProjectTemplate/MySubDirectory and then add the SSI to be ../includes/imports.html it still doesn't work. Its like the #include file SSI directive expects the file to be in the same working directory.
So, have you tried like this in the virtual host?
<VirtualHost *:80>
ServerAdmin emailaddress#domain.com
DocumentRoot "/var/www/html/example/"
ServerName example.local
ServerAlias example.local
<Directory "/var/www/html/example/">
Options All Includes Indexes FollowSymLinks
Order allow,deny
Allow from all
AllowOverride All
</Directory>
Alias /NewProjectTemplate /var/www/html/example/NewProjectTemplate
<Directory "/var/www/html/example/NewProjectTemplate">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Hope it helps this time..
Just accidentally stumbled across this very old threat.
No need of host or alias if you just want a sub dir to be treated as root.
The way to do it is to amend the httpd.conf, there you will find
<Directory>
just update this line and add
<Directory "your/server/root/original_path/subpath">
# your options
</Directory>

Apache giving 403 forbidden errors

Ok, so i've previously set up two virtual hosts and they are working cool. they both house simple web projects and work fine with http://project1 and http://project2 in the browser.
Anyway, I've come to add another vhost. I edited the /etc/hosts file with 127.0.0.1 project3 and also updated the httpd-vhosts.conf file by copy and pasting the previous entries for project2 and editing the file path.
I've checked all the file and folder permissions (in fact I copied and pasted from project2) and simply put a "hello world" message in the index.php file.
I get a 403 forbidden permission denied message when accessing http://project3
Why is this, I just can figure out what step I've missed as everything seems to be set up correct.
Check that :
Apache can physically access the file (the user that run apache, probably www-data or apache, can access the file in the filesystem)
Apache can list the content of the folder (read permission)
Apache has a "Allow" directive for that folder. There should be one for /var/www/, you can check default vhost for example.
Additionally, you can look at the error.log file (usually located at /var/log/apache2/error.log) which will describe why you get the 403 error exactly.
Finally, you may want to restart apache, just to be sure all that configuration is applied.
This can be generally done with /etc/init.d/apache2 restart. On some system, the script will be called httpd. Just figure out.
I just fixed this issue after struggling for a few days. Here's what worked for me:
First, check your Apache error_log file and look at the most recent error message.
If it says something like:
access to /mySite denied (filesystem path
'/Users/myusername/Sites/mySite') because search permissions
are missing on a component of the path
then there is a problem with your file permissions. You can fix them by running these commands from the terminal:
$ cd /Users/myusername/Sites/mySite
$ find . -type f -exec chmod 644 {} \;
$ find . -type d -exec chmod 755 {} \;
Then, refresh the URL where your website should be (such as http://localhost/mySite).
If you're still getting a 403 error, and if your Apache error_log still says the same thing, then progressively move up your directory tree, adjusting the directory permissions as you go. You can do this from the terminal by:
$ cd ..
$ chmod 755 mySite
If necessary, continue with:
$ cd ..
$ chmod Sites
and, if necessary,
$ cd ..
$ chmod myusername
DO NOT go up farther than that. You could royally mess up your system.
If you still get the error that says search permissions are missing on a component of the path, I don't know what you should do. However, I encountered a different error (the one below) which I fixed as follows:
If your error_log says something like:
client denied by server configuration:
/Users/myusername/Sites/mySite
then your problem is not with your file permissions, but instead with your Apache configuration.
Notice that in your httpd.conf file, you will see a default configuration like this (Apache 2.4+):
<Directory />
AllowOverride none
Require all denied
</Directory>
or like this (Apache 2.2):
<Directory />
Order deny,allow
Deny from all
</Directory>
DO NOT change this! We will not override these permissions globally, but instead in your httpd-vhosts.conf file.
First, however, make sure that your vhost Include line in httpd.conf is uncommented. It should look like this. (Your exact path may be different.)
# Virtual hosts
Include etc/extra/httpd-vhosts.conf
Now, open the httpd-vhosts.conf file that you just Included. Add an entry for your webpage if you don't already have one. It should look something like this. The DocumentRoot and Directory paths should be identical, and should point to wherever your index.html or index.php file is located. For me, that's within the public subdirectory.
For Apache 2.2:
<VirtualHost *:80>
# ServerAdmin webmaster#dummy-host2.example.com
DocumentRoot "/Users/myusername/Sites/mySite/public"
ServerName mysite
# ErrorLog "logs/dummy-host2.example.com-error_log"
# CustomLog "logs/dummy-host2.example.com-access_log" common
<Directory "/Users/myusername/Sites/mySite/public">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
The lines saying
AllowOverride All
Require all granted
are critical for Apache 2.4+. Without these, you will not be overriding the default Apache settings specified in httpd.conf. Note that if you are using Apache 2.2, these lines should instead say
Order allow,deny
Allow from all
This change has been a major source of confusion for googlers of this problem, such as I, because copy-pasting these Apache 2.2 lines will not work in Apache 2.4+, and the Apache 2.2 lines are still commonly found on older help threads.
Once you have saved your changes, restart Apache. The command for this will depend on your OS and installation, so google that separately if you need help with it.
I hope this helps someone else!
PS: If you are having trouble finding these .conf files, try running the find command, such as:
$ find / -name httpd.conf
restorecon command works as below :
restorecon -v -R /var/www/html/
Notice that another issue that might be causing this is that, the "FollowSymLinks" option of a parent directory might have been mistakenly overwritten by the options of your project's directory. This was the case for me and made me pull my hair until I found out the cause!
Here's an example of such a mistake:
<Directory />
Options FollowSymLinks
AllowOverride all
Require all denied
</Directory>
<Directory /var/www/>
Options Indexes # <--- NOT OK! It's overwriting the above option of the "/" directory.
AllowOverride all
Require all granted
</Directory>
So now if you check the Apache's log message(tail -n 50 -f /var/www/html/{the_error_log_file_of_your_site}) you'll see such an error:
Options FollowSymLinks and SymLinksIfOwnerMatch are both off, so the RewriteRule directive
is also forbidden due to its similar ability to circumvent directory restrictions
That's because Indexes in the above rules for /var/www directory is overwriting the FolowSymLinks of the / directory. So now that you know the cause, in order to fix it, you can do many things depending on your need. For instance:
<Directory />
Options FollowSymLinks
AllowOverride all
Require all denied
</Directory>
<Directory /var/www/>
Options FollowSymLinks Indexes # <--- OK.
AllowOverride all
Require all granted
</Directory>
Or even this:
<Directory />
Options FollowSymLinks
AllowOverride all
Require all denied
</Directory>
<Directory /var/www/>
Options -Indexes # <--- OK as well! It will NOT cause an overwrite.
AllowOverride all
Require all granted
</Directory>
The example above will not cause the overwrite issue, because in Apache, if an option is "+" it will overwrite the "+"s only, and if it's a "-", it will overwrite the "-"s... (Don't ask me for a reference on that though, it's just my interpretation of an Apache's error message(checked through journalctl -xe) which says: Either all Options must start with + or -, or no Option may. when an option has a sign, but another one doesn't(E.g., FollowSymLinks -Indexes). So it's my personal conclusion -thus should be taken with a grain of salt- that if I've used -Indexes as the option, that will be considered as a whole distinct set of options by the Apache from the other option in the "/" which doesn't have any signs on it, and so no annoying rewrites will occur in the end, which I could successfully confirm by the above rules in a project directory of my own).
Hope that this will help you pull much less of your hair! :)
it doesn't, however, solve the problem, because on e.g. open SUSE Tumbleweed, custom source build is triggering the same 401 error on default web page, which is configured accordingly with Indexes and
Require all granted
The server may need read permission for your home directory and .htaccess therein
You can try disabling selinux and try once again using the following command
setenforce 0
In my case it was failing as the IP of my source server was not whitelisted in the target server.
For e.g. I was trying to access https://prodcat.ref.test.co.uk from application running on my source server.
On source server find IP by ifconfig
This IP should be whitelisted in the target Server's apache config file. If its not then get it whitelist.
Steps to add a IP for whitelisting (if you control the target server as well)
ssh to the apache server
sudo su -
cd /usr/local/apache/conf/extra (actual directories can be different based on your config)
Find the config file for the target application for e.g. prodcat-443.conf
RewriteCond %{REMOTE_ADDR} <YOUR Server's IP>
for e.g.
RewriteCond %{REMOTE_ADDR} !^192\.68\.2\.98
Hope this helps someone
Add
<Directory "/path/to/webroot">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow, deny
Allow from all
Require all granted
</Directory>
What this does is tell Apache2 to override any previous configs, and allow (200) from all before denying. (403) It also requires all requests to be granted. This code will have to go in every vhost file, but it does work. I have been using this for over a year.
to your config file (e.g. /etc/apache2/sites-enabled/000-default.conf)
Tested LAMP stack Debian 11