Treating a subdirectory as a root directory for Apache - html

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>

Related

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 cgi not parsing a css link correctly in an html page

I have the following problem:
My document root is /var/www/html, where I have an index.cgi script. The script prints in the browser an html page which it reads from the directory /var/www/pages. I also have a css file located in the dir /var/www/css. When the HTML page is printed to the user, it fails to include the css.
<link rel="stylesheet" type="text/css" href="../css/log.css">
Instead of going one directory UP, the html is searching for the css file in /var/www/html/css, which is invalid.
If I manualy open the HTML page, it finds the css, so the link is supposed to be correct?
If I move the css in the /var/www/html dir, i get another error, apache tries to execute the css file because of the apache settings, I guess. Here is my apache Virt config:
DocumentRoot /var/www/html
<Directory /var/www/cgi-bin >
SetHandler cgi-script
Options ExecCGI
</Directory>
<Directory /var/www/html>
SetHandler cgi-script
Options ExecCGI
</Directory>
How can I solve this, how to set the correct path for the css file?
After a lot of reading I managed to see the problem. In the config file I had to add the following:
Alias /css/ /var/www/css/
Same goes for scripts and others, linked in some way.

Apache hangs with Django Application and Matplotlib

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.

getting rid of .html using Apache on EC2

Is there a simple way to remove the .html from a URL namespace? For example, if I have www.mywebsite.com/special.html how can I make this www.mywebsite.com/special
Again, I'm using Apache on an Amazon EC2 instance. Thanks in advance!
Put this in your .htaccess file:
RewriteEngine On
RewriteRule ^(.+)\.html$ /$1
Make sure that mod_rewrite is installed and AllowOverride includes FileInfo.
Edit on how to get mod_rewrite working:
Usually, mod_rewrite already comes with apache, but sometimes needs to be enabled by the command
a2enmod rewrite
The AllowOverride directive sets permissions for .htaccess files. For mod_rewrite, FileInfo is required. To set this, change in your apache config file (usually /etc/httpd/conf/httpd.conf) the line
AllowOverride None
to
AllowOverride FileInfo
or
AllowOverride All
Make a directory named special and put an index.html inside of it with the markup from special.html.

Wordpress - Apache FollowSymlinks for themes, using Mac lion

I've installed wordpress locally on my Mac (lion)
After enabling vhosts, I've created an entry in my hosts file to point "wordpress" to 127.0.0.1
My vhosts.conf contains:
<VirtualHost *:80>
DocumentRoot "/Users/alex/Sites/wordpress"
ServerName wordpress
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
</VirtualHost>
This works, and I can access the default wordpress install, no problem.
Basically, inside my wp-content/themes folder, I've put a symlink to (for example)
/Users/Alex/Projects/SomeTheme/
This folder contains my theme files
However, it's just not detecting the theme (doesn't show up in WP admin)
If I copy the folder to wp-content/themes, then it works. Symlinks aren't being followed
In my /etc/apache2/users/alex.conf I have:
<Directory "/Users/alex/Sites/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
I'm obviously missing something somewhere....
Wordpress has issues using symlinks. This is due to how PHP handles the __FILE__ magic constant. In PHP, __FILE__ returns the absolute path for the file it is run within. Unfortunately it ignores symlinks in that process. (ie. while you might be accessing the file through /opt/wordpress/instance/wp-content/sym-themes/pretty and the file is actually in /opt/content/themes/pretty, when __FILE__ is called, instead of returning /opt/wordpress/instance/wp-content/sym-themes/pretty which is what wordpress expects, it returns /opt/content/themes/pretty.
Wordpress uses heavy use of __FILE__ in it's code and also the basename() function to compare the wordpress root directory against the theme directory to get the name of the theme directory for things like parsing files. Due to how PHP handles the __FILE__ magic constant, Wordpress tries to match the paths but since they are two different ones, it just appends one path to the other and you end up with a path to something that doesn't exist.
Unless you plan on doing a LOT of code moficiations, I highly recommend not using Symlinks anywhere in the wordpress structure.
As #Drahkar has pointed out, symlinks are difficult in WordPress. To change the theme directory, use a simple plugin:
add_filter( 'theme_root', 'sp8963532_theme_root' );
function sp8963532_theme_root()
{
return 'FULL_LOCAL_PATH_TO_YOUR_THEMES_DIRECTORY';
}
add_filter( 'theme_root_uri', 'sp8963532_theme_root_uri' );
function sp8963532_theme_root_uri()
{
return 'URI_TO_YOUR_THEMES_DIRECTORY';
}
Copy the code into a file, and put the file in wp-content/mu-plugins/.