5

I am creating an RPM and in the pre stage, I want to determine apache's document root so I can copy the web application files there.

First, I check if apache is installed. If apache is installed, the script will look for the location of httpd.conf file since it contains the document root param. But I do not know how to handle the return of the command find / -name httpd.conf. I'm just new to shell scripts and RPM. Would like also to know if this is possible. The goal to have the RPM so it can be deployed at any system, without much manual intervention. Bellow is the code I was planning. It is not yet working

if [ "$(expr substr $(httpd -v) 17 6)" == "Apache"]; then
    echo "Apache is installed";
    find / -name httpd.conf
    //need to handle the return of find and store to a location var to be used on the next line
    grep -i 'DocumentRoot' $location/httpd.conf
    //need to store the return document root to a var
el
    echo "Apache is not installed";
fi
3
  • If you are creating an RPM package you definitely should be avoiding things like find / -name httpd.conf which will scan the entire filesystem during the installation of your package. You should also consult the packaging tools' database to see if Apache is installed, not probe it by calling it. Finally, most Apache configurations, at least those installed by package managers, use layouts like /etc/apache2/sites-{available,enabled} which easily enable multiple DocumentRoots none of which will normally be found in the main httpd.conf file. – Celada Mar 20 '15 at 8:50
  • thanks, i should probably just specify a static httpd location, and read from there the document root – Pelang Mar 20 '15 at 9:10
  • The usual approach on Debian is to install into a fixed known location. Then either add an Alias directive in somewhere like /etc/apache2/conf.d/, or simply leave that action documented in the README as a required step to enable the functionality. – roaima Mar 20 '15 at 11:04
4

Your task is harder than you think as there can be multiple .conf files which include others.

You may get some milage put of examining the output of

httpd -S or apachectl -S or apache2ctl -S (it's distro dependent)

This prints out the names of the vhosts that are currently defined along with information about where they are defined e.g.

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
_default_:443          a.example.com (/etc/httpd/conf.d/ssl.conf:74)
*:80                   is a NameVirtualHost
         default server b.example.com (/etc/httpd/conf/httpd.conf:1010)
         port 80 namevhost b.example.com (/etc/httpd/conf/httpd.conf:1010)

This tells us that vhosts are defined in /etc/httpd/conf.d/ssl.conf and /etc/httpd/conf/httpd.conf

1
  • i decided to better specify a static httpd location, and read from there the document root – Pelang Mar 20 '15 at 9:10
2

There is no such thing as the apache's document root, the configuration files can contain multiple roots for multiple sites.

Additionally you could break an existing setup by just copying in your data, particularly when the default apache2 has been modified, so you have to be very careful where and what you copy.

If you want to proceed with this scheme anyway, you can use a2query -s to see which sites are available, and based on that find some site that you are going to put your documents on. If a2query is not available, you probably have no apache2 installed.

If you copy things into one of the document roots, put everything under one specific directory name under the document root and check that this directory doesn't exist before writing anything there.


You might want to investigate other packages that provide HTML based documentation inserted in the existing apache setup. I don't know any that do, but you might.

If the default setup for apache2 on the systems your RPM is going to be installed on supports a mechanism for accessing "user pages", you might just want to create a new user for your package and put the documentation there. Of course the admin can have switched this mechanism off, but if not this doesn't interfere with the configuration. For that you also don't have to find which document root to write to (and if the user has nginx or any other server installed, you just might be lucky that it uses the same user pages mechanism).

1

I have used this on OpenSuse and Ubuntu with success.

find /etc/apache2 -name '*default*' -exec grep -sHin "documentroot.*www" {} \; | grep -v available
0
  1. Main DocumentRoot is just one location of web pages, many (probably most) of Apache admins use VirtualHosts for hosting.

See for example: https://httpd.apache.org/docs/2.2/vhosts/index.html

or: https://httpd.apache.org/docs/2.4/vhosts/index.html

  1. Searching for a file the whole system starting from root (/) is really bad idea, as it would take too much time, especially if server has some other resources mounted (worst scenario is network mount, on slow connection, with many files).

You probably need to target specific distributions for locations of httpd.conf (or apache2.conf but even then there is no guarantee that some admin isn't using some non-standard location for config.

1
  • thanks, i should probably just specify a static httpd location, and read from there the document root – Pelang Mar 20 '15 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.