Apache doesn't serve files in “javascript” dir

2020-02-10 04:15发布

问题:

All of the sudden I started getting 404s for files like http://example.localhost/javascript/jquery.min.js

Earlier everything was working just fine. I didn't change any configs, at least not manually.

But now if I'd try to access the /javascript directory itself I would get "Cannot serve directory /usr/share/javascript/: No matching DirectoryIndex (index.html,index.cgi,index.pl,index.php,index.xhtml,index.htm) found, and server-generated directory index forbidden by Options directive in the Apache error log.

回答1:

I was trying to find solution to this on Stack Overflow, but I couldn't. So I'm just leaving this here if anyone happens to encounter the same problem.

So why the hell would it look in the /usr/share/javascript instead of what I had configured in the VirtualHost. To figure that out I did something like the following:

$ cd /etc/apache2
$ grep -R Alias * | grep share

...
conf-enabled/javascript-common.conf:Alias /javascript /usr/share/javascript/
...

After googling for that configuration file name I found some explanation.

I don't know why, but I had the javascript-common package installed. It doesn't seem harmful to get rid of it, so doing $ sudo apt-get purge javascript-common solved the problem for me.



回答2:

The issue:

Web applications that use JavaScript need to distribute it through HTTP. Using a common path for every script avoids the need to enable this path in the HTTP server for every package.

This is the package that creates /usr/share/javascript alias and enables it in the Apache webserver.

Recommended fix

And that's it. You won't have any other problem with javascript directories. Another fix could be to rename /usr/share/javascript/ to /usr/share/javascript-common/, then adjust the Alias in javascript-common.conf to point to the renamed directory. I am still not sure if this will affect any future update.

Another Fix:

Go to /etc/apache2/conf-available/javascript-common.conf. You will find this:

Alias /javascript /usr/share/javascript/
<Directory "/usr/share/javascript/">
     Options FollowSymLinks MultiViews
</Directory>

So you just have to comment these lines (with the # char) (it is not recommended to edit the file directly in conf-enabled) to avoid the forbidden error. After that, do this:

a2disconf javascript-common
a2enconf javascript-common


回答3:

You don't need to edit the conf file or purge the package just disable it.

a2disconf javascript-common
service apache2 reload

If for some reason you want to use that conf:

a2enconf javascript-common
service apache2 reload


回答4:

I'm on a Debian machine, and there is no a2disconf command. I found the /etc/apache2/conf.d directory is a link to /etc/javascript-common/javascript-common.conf.

I went and edited that file (as root) and changed it to alias /javascript-common instead of /javascript by changing the top line to

Alias /javascript-common /usr/share/javascript/

and saving it and restarting Apache.



回答5:

I just had a similar problem on an Ubuntu system. Apparently the javascript-common package was hosed at some point in time and the configure script wouldn't run properly. Removing javascript-common and reinstalling with apt-get would not fix it. I had to:

dpkg --purge javascript-common

apt-get install javascript-common

And that seemed to fix the problem. This is an Ubuntu 16.04 LTS (Xenial Xerus) system that was upgraded from Ubuntu 14.04 LTS (Trusty Tahr).



回答6:

Uninstall apache2 and delete these folders:

  • rm -R /var/www/html/
  • rm -R /etc/apache2

Then reinstall javascript-common and apache2.