Apache

From LinuxIntro
Jump to: navigation, search

Apache is a popular http daemon. In other words it is software for web servers.

Contents

Setting up https

See set up Apache with https.

Apache configuration

Apache's configuration is stored in .conf files that can be found under /etc/apache2 for SUSE Linux. Now my way of keeping old configuration files is that I add a version behind old config files like this:

myconfig.conf
myconfig-1.conf
myconfig-2.conf

This can be a problem, e.g. in SUSE Linux' default configuration you will find:

# grep -i "vhosts.d"  *
[...]
httpd.conf:Include /etc/apache2/vhosts.d/*.conf
[...]

This means all .conf files in the folder /etc/apache2/vhosts.d will be used as config files. So if you remove a line and keep the old config in a file named *.conf as a backup, this setting will remain active. Better name your config backup files with a ".backup" suffix!

Apache needs too much memory - what to do

I have a web server at rackspace and every MB RAM costs me on a per-month base. Often, the server started swapping. Calling top and then typing M showed me the reason:

PID   USER   PR NI VIRT RES  SHR S %CPU %MEM  TIME+  COMMAND
31737 mysql  20 0 1108m 62m 6464 S    0 11.8 0:05.19 mysqld
32559 wwwrun 20 0 170m  31m 4260 S    0  6.0 0:08.69 httpd2-prefork
32562 wwwrun 20 0 165m  28m 4352 S    0  5.4 0:00.32 httpd2-prefork
 1432 wwwrun 20 0 165m  28m 3832 S    0  5.4 0:01.76 httpd2-prefork 

There are too many httpd2-prefork processes. This can be changed by editing /etc/apache2/server-tuning.conf. I set

StartServers 2
MinSpareServers 2
MaxSpareServers 6
ServerLimit 15
MaxClients 15

Then I restarted apache with the command

/etc/init.d/apache2 restart

and now Apache, mediawiki, dokuwiki, mysql and php fit into 512 MB.

MaxClients cannot be set higher than ServerLimIt. It determines the number of httpd2-prefork processes that are spawned by the first httpd2-prefork process. If MaxClients is 3 and the server has already been active you will see three spawned-out processes:

# ps faux
[...]
root     15110  0.0  1.8 152308  9916 ?        Ss   02:15   0:00 /usr/sbin/httpd2-prefork -f /etc/apache
wwwrun   15111  0.9  5.9 174712 32532 ?        S    02:15   0:07  \_ /usr/sbin/httpd2-prefork -f /etc/ap
wwwrun   15112  0.6  5.8 174188 31944 ?        S    02:15   0:05  \_ /usr/sbin/httpd2-prefork -f /etc/ap
wwwrun   15125  0.8  5.7 173748 31560 ?        S    02:15   0:07  \_ /usr/sbin/httpd2-prefork -f /etc/ap

WebDAV under Apache2

With Apache2, the DAV module is already included. You will need to activate the two needed modules dav_fs and dav using a2enmod. Afterwards, restart apache using

/etc/init.d/apache2 force-restart

In /etc/apache2/mods-available/dav_fs.conf you should find something like:

DAVLockDB /var/lock/apache2/DAVLock

The user-account Apache is running under (www or www-data) needs read-and write privileges in this folder.

Enter into /etc/apache2/apache2.conf:

Alias /daten /var/data
<Directory /var/data/>
DAV On
ForceType text/plain
AuthType Basic 
AuthName "WebDAV Daten" 
AuthUserFile /etc/apache2/htpasswd 
Require valid-user 
</Directory>

Create "/var/data" and issue

chown www-data:www-data /var/data

Create the FIRST user with:

htpasswd -c /etc/apache2/htpasswd benutzername

For further users, leave out the -c (create).

switch off Apache - Directory-Listing

Remove

Indexes 

from the options, then the content of the folders will no longer be shown.

Alias

An alias allows you to point a URL to a file like this:

Alias /skins /srv/www/htdocs/skins

Imagine we have a home page www.myhomepage.com and have a mediawiki running on it. The URL http://myhomepage.com/skins would now be processed by mediawiki. But we want it to contain pure files. So we make this alias.

Rewrite

Sometimes you want URLs to be rewritten, e.g. from a human-easy URL like http://www.linuxintro.org/wiki/apAche to a machine-easy URL like http://www.linuxintro.org/index.php?title=Apache. This can be done with apache's rewrite module mod_rewrite. Here is an example how to do it under SUSE Linux 12.1:

example

  • install apache2 with php:
yast -i apache2-mod_php5
  • add rewrite to APACHE_MODULES in /etc/sysconfig/apache.
  • set AllowOverride All in /etc/apache2/httpd.conf
  • (re)start apache2
/etc/init.d/apache2 restart
  • in /srv/www/htdocs create a file .htaccess with the following content:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^foo\.html$ /bar.html [R]

hints

  • FollowSymLinks must be allowed. Find out if it is allowed by
/srv/www/htdocs # ln -s bar.html foo.html
  • We are using .htaccess in this example. To test if .htaccess works, use .htaccess with the following content
asdf

if you do not get an error 500, .htaccess is not evaluated at all. In this case set AllowOverride to true

Proxy

For software like shell in a box or guacamole you want apache to fetch data from another port than 80.

Set mod_proxy

You want to store a user list with passwords in /etc/apache2/.htpasswd.

root@linuxintro:/etc/apache2/mods-enabled# cat proxy.conf 
<IfModule mod_proxy.c>
        #turning ProxyRequests on and allowing proxying from all may allow
        #spammers to use your proxy to send email.

        ProxyRequests Off

        <Proxy *>
                AddDefaultCharset off
                AuthUserFile /etc/apache2/.htpasswd
                AuthName Wuhuuu
                AuthType Basic
                require valid-user
                Order deny,allow
                Allow from all
                #Deny from all
                #Allow from .example.com
        </Proxy>

        # Enable/disable the handling of HTTP/1.1 "Via:" headers.
        # ("Full" adds the server version; "Block" removes all outgoing Via: headers)
        # Set to one of: Off | On | Full | Block

        ProxyVia On
</IfModule>

Use ProxyPass

Under Ubuntu 10.04 I go to /etc/apache2/sites-enabled and edit 000-default:

  <IfModule mod_proxy.c>
    <Location /shell>
      ProxyPass http://127.0.0.1:4200
    </Location>
  </IfModule>

Modules

Q: Which modules are in my apache?

A: Find out using the command

apache2ctl -t -D DUMP_MODULES

See Add mod_rewrite to apache.

See also